Monkey黑白棋 所使用的经典算法(源代码)
2007-04-18 14:58
267 查看
/**//// <summary>
/// AlphaBeta剪枝法
/// </summary>
/// <returns></returns>
private int AlphaBetaSearch(int curDepth, int alpha, int beta)
...{
lock (this)
...{
if (curDepth >= Depth || times >= MaxTimes)
...{
times++;
//开局或中局对棋盘评分,boardState指定是开局还是中局
MonkeyOthello.BoardFX(boardState);
SetNums(0);
SetNums(1);
//返回估值
return SetScore(boardState);
}
else
...{
//if (times < MaxTimes)
//{
// StaticObject.ThinkingProgressBar.Value = times;
//}
if (!MonkeyOthello.CanDown())
...{
//如果没棋可下,交换对手
MonkeyOthello.CurrentColor = 1 - MonkeyOthello.CurrentColor;
if (!MonkeyOthello.CanDown())
...{
//双方都没棋可下,则不用再往下搜索
MonkeyOthello.CurrentColor = 1 - MonkeyOthello.CurrentColor;
//中局就已经结束游戏,获取中局的结果
GameResult result = MonkeyOthello.GetMidGameResult((int)MonkeyOthello.CurrentColor);
//中局就已经结束游戏,返回游戏结束的估值
return EvalGameOver(result, beta);
}
else
...{
//其中一方无棋可下,继续由对方下
return -AlphaBetaSearch(curDepth + 1, -beta, -alpha);
//alpha = -beta; beta = -alpha; curDepth++;
}
}
//进栈(当前的所有棋步)
AllStepStack.Push(new Stack<Step>(MonkeyOthello.Mobility));
//当前估值
int score;
//棋步数量
int Num = MonkeyOthello.MobilityNum[(int)MonkeyOthello.CurrentColor];
for (int i = 0; i < Num; i++)
...{
//获取没有访问过的棋步
Step step = GetNotVisitedStep();
//模拟下一步棋
MonkeyOthello.DownStones(step);
// 搜索下一步(递归)
score= -AlphaBetaSearch(curDepth + 1, -beta, -alpha);
//退一步
MonkeyOthello.Reback();
//搜索到比上次高的估值,替换
if (score > alpha)
...{
alpha = score;
//如果认为已经赢定,则可以终止搜索
if (score >= beta)
break;
}
}
//退栈(当前的所有棋步)
AllStepStack.Pop();
return alpha;
}
}
}
我把它用在开局和中局的思考,当然开局跟中局的评估函数有区别
而当离游戏还有10-12步时,用它来搜索最大赢子数.
/// AlphaBeta剪枝法
/// </summary>
/// <returns></returns>
private int AlphaBetaSearch(int curDepth, int alpha, int beta)
...{
lock (this)
...{
if (curDepth >= Depth || times >= MaxTimes)
...{
times++;
//开局或中局对棋盘评分,boardState指定是开局还是中局
MonkeyOthello.BoardFX(boardState);
SetNums(0);
SetNums(1);
//返回估值
return SetScore(boardState);
}
else
...{
//if (times < MaxTimes)
//{
// StaticObject.ThinkingProgressBar.Value = times;
//}
if (!MonkeyOthello.CanDown())
...{
//如果没棋可下,交换对手
MonkeyOthello.CurrentColor = 1 - MonkeyOthello.CurrentColor;
if (!MonkeyOthello.CanDown())
...{
//双方都没棋可下,则不用再往下搜索
MonkeyOthello.CurrentColor = 1 - MonkeyOthello.CurrentColor;
//中局就已经结束游戏,获取中局的结果
GameResult result = MonkeyOthello.GetMidGameResult((int)MonkeyOthello.CurrentColor);
//中局就已经结束游戏,返回游戏结束的估值
return EvalGameOver(result, beta);
}
else
...{
//其中一方无棋可下,继续由对方下
return -AlphaBetaSearch(curDepth + 1, -beta, -alpha);
//alpha = -beta; beta = -alpha; curDepth++;
}
}
//进栈(当前的所有棋步)
AllStepStack.Push(new Stack<Step>(MonkeyOthello.Mobility));
//当前估值
int score;
//棋步数量
int Num = MonkeyOthello.MobilityNum[(int)MonkeyOthello.CurrentColor];
for (int i = 0; i < Num; i++)
...{
//获取没有访问过的棋步
Step step = GetNotVisitedStep();
//模拟下一步棋
MonkeyOthello.DownStones(step);
// 搜索下一步(递归)
score= -AlphaBetaSearch(curDepth + 1, -beta, -alpha);
//退一步
MonkeyOthello.Reback();
//搜索到比上次高的估值,替换
if (score > alpha)
...{
alpha = score;
//如果认为已经赢定,则可以终止搜索
if (score >= beta)
break;
}
}
//退栈(当前的所有棋步)
AllStepStack.Pop();
return alpha;
}
}
}
我把它用在开局和中局的思考,当然开局跟中局的评估函数有区别
而当离游戏还有10-12步时,用它来搜索最大赢子数.
相关文章推荐
- Monkey黑白棋 所使用的经典算法(源代码)
- 【面试经典题之字符串】实现一个算法,确定一个字符串的所有字符是否全都不同。假设不允许使用额外的数据结构
- 韩顺平_PHP程序员玩转算法公开课(第一季)05_使用单链表解决约瑟夫问题_学习笔记_源代码图解_PPT文档整理
- 【算法笔记】使用栈实现汉诺塔(Hanoi)经典算法
- 经典算法—快速排序(Quicksort)使用详解
- STL(六):变动性算法源代码分析与使用示例(copy_backward、 transform、 replace_copy_if 等)
- 韩顺平_PHP程序员玩转算法公开课(第一季)07_使用数组实现堆栈_学习笔记_源代码图解_PPT文档整理
- 数据挖掘十大经典算法之apriori算法&源代码
- C++之STL(六):变动性算法源代码分析与使用示例(copy_backward、 transform、 replace_copy_if 等)
- [徒步天下原创]使用EXCEL VBA完成校验码生成算法(源代码)
- 经典算法问题:“this is a test” 逆序输出为“test a is this”.使用Python实现
- 基于GPU实现的经典光照模型算法:漫反射模型(使用cg语言实现)
- 使用KNN算法的分类实验源代码
- 韩顺平_PHP程序员玩转算法公开课(第一季)09_使用栈完成高级计算器(1)_学习笔记_源代码图解_PPT文档整理
- 经典算法 | 使用DP求解数组中最长子序列的个数
- 数据挖掘十大经典算法之apriori算法&源代码
- 从零开始学C++之STL(六):变动性算法源代码分析与使用示例(copy_backward、 transform、 replace_copy_if 等)
- (转) 白话经典算法系列之三 希尔排序的实现(附源代码实现)
- 韩顺平_php从入门到精通_视频教程_第13讲_选择器使用细节_块元素和行内元素_盒子模型_盒子模型经典应用①_学习笔记_源代码图解_PPT文档整理
- 八大排序经典算法(图解+参考源代码)