| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 836 人关注过本帖
标题:我这段Alpha-Beta剪枝的代码哪里出问题了?
取消只看楼主 加入收藏
tcyfish
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2008-6-21
收藏
 问题点数:0 回复次数:0 
我这段Alpha-Beta剪枝的代码哪里出问题了?
public synchronized boolean thinkAndMove()
{
//AI开局先手,首先占天元
if (engine.checkPoint(engine.getTotalCol() / 2, engine.getTotalRow() / 2))
bestMove = RenjuChessman.getInstance(chessmanColor, engine.getTotalCol() / 2, engine.getTotalRow() / 2);
//人类玩家先手,AI在天元周围8个位置随机应子
else if (engine.getStep() == 1)
{
boolean canAdd = false;
int a = -engine.getTotalCol();
int b = -engine.getTotalRow();
do
{
Random r = new Random();
a = r.nextInt(3) - 1;
b = r.nextInt(3) - 1;
canAdd = engine.checkPoint(engine.getTotalCol() / 2 + a, engine.getTotalRow() / 2 + b);
}
while (!canAdd);
bestMove = RenjuChessman.getInstance(chessmanColor, engine.getTotalCol() / 2 + a, engine.getTotalRow() / 2 + b);
}
//Alpha-Beta剪枝搜索最好的下子点
else
{
//生成走法集合
ArrayList<IChessman> moveCanGo = NearMovesGenerator.generateMove(engine);
//由于ArrayList非线程安全,先转换为数组
IChessman[] chessman = new IChessman[moveCanGo.size()];
chessman = moveCanGo.toArray(chessman);
int bestValue = Integer.MIN_VALUE + 1;
//对走法集合中的每一个走法
for (int i = 0; i < chessman.length; i++)
{
//在棋盘上虚拟下一子
engine.virtualAddChessman(chessmanColor, chessman[i].getX(), chessman[i].getY());
//递归Alpha-Beta剪枝搜索
int value = alpha_beta(depth, new Integer(Integer.MIN_VALUE + 1), new Integer(Integer.MAX_VALUE - 1), true);
//恢复棋盘
engine.recoverVirtualAdded(chessman[i].getX(), chessman[i].getY());
//若返回值大于之前的bestValue,bestValue = value,并标记此走法为当前最好的走法
if (value > bestValue)
{
bestValue = value;
bestMove = chessman[i];
}
}
}
//在棋盘上下子
return putAChessman(chessmanColor, bestMove.getX(), bestMove.getY());
}

/**
* Alpha-Beta Search
* @param depth 搜索深度
* @param alpha
* @param beta
* @param isMaxLayer 当前是否极大层
* @return
*/
private int alpha_beta(int depth, Integer alpha, Integer beta, boolean isMaxLayer)
{
int value = Integer.MIN_VALUE + 1, bestvalue = Integer.MIN_VALUE + 1;
//获取上一个在棋盘上的虚拟下子
IChessman latedAdd = engine.getLatestVirtualAdded();
if (latedAdd != null)
{
//判断上一个虚拟下子造成的结果
int winner = engine.getWinner(latedAdd.getChessmanColor(), latedAdd.getX(), latedAdd.getY());
if (winner != IEngine.NO_WINNER)
//上一个虚拟下子造成结果为AI玩家获胜
if (winner == chessmanColor)
return SituationInfo.SureWinValue;
//上一个虚拟下子造成的结果为人类玩家获胜
else
return -SituationInfo.SureWinValue;
}
//达到搜索深度
if (depth <= 0)
{
//评估上一个虚拟下子的价值(对自己的价值和对对手的价值)
int vS = SituationInfo.analyze(chessmanColor, latedAdd.getX(), latedAdd.getY(), engine);
int vO = SituationInfo.analyze(engine.getOpponentChessmanColor(), latedAdd.getX(), latedAdd.getY(), engine);

if (isMaxLayer)
return vS + vO;
else
return -(vS + vO);
}

//确定下一个将在棋盘上虚拟下子的棋子颜色
int cColor = isMaxLayer ? engine.getOpponentChessmanColor() : chessmanColor;
//生成走法集合
ArrayList<IChessman> moveCanGo = NearMovesGenerator.generateMove(engine);
IChessman[] chessman = new IChessman[moveCanGo.size()];
chessman = moveCanGo.toArray(chessman);
//对走法集合中的每一个走法
for (int i = 0; i < chessman.length; i++)
{
//在棋盘上虚拟下一子
engine.virtualAddChessman(cColor, chessman[i].getX(), chessman[i].getY());
//递归Alpha-Beta剪枝搜索
value = alpha_beta(depth - 1, new Integer(Integer.MIN_VALUE + 1), new Integer(Integer.MAX_VALUE - 1), !isMaxLayer);
//恢复棋盘
engine.recoverVirtualAdded(chessman[i].getX(), chessman[i].getY());
if (isMaxLayer)
{
//取最大值
if (value > alpha)
{
alpha = value;
bestvalue = alpha;
//剪枝
if (alpha >= beta)
return beta;
}
}
//取最小值
else if (value < beta)
{
beta = value;
bestvalue = beta;
//剪枝
if (alpha >= beta)
return alpha;
}
}
return bestvalue;
}
}


结果这样的AI傻乎乎的,就连我之前写的SimpleAI都比不上。
完整的源代码http://files. ,源文件编码使用UTF-8
搜索更多相关主题的帖子: 剪枝 代码 
2008-06-21 20:20
快速回复:我这段Alpha-Beta剪枝的代码哪里出问题了?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.021472 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved