我这段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