我用 minmax AI 用 Java 创建了简单的 Checkers 游戏,但我不工作。我使用 minmax 的递归版本,但它一定有问题,因为它返回的移动不是最好的,但可能是第一次生成的。
public int minmax(int [][] board, int depth, int curPlayer){
ArrayList<Move> moves = findMoves(curPlayer, board);
if (depth == 0 || moves.size() == 0){
return heurValue(curPlayer, board);
}
int bestVal = 0;
if (curPlayer == GameCore.BLACK){
bestVal = Integer.MIN_VALUE;
curPlayer = GameCore.RED;
}else{
bestVal = Integer.MAX_VALUE;
curPlayer = GameCore.BLACK;
}
for(int i = 0; i<moves.size(); i++){
Move m = moves.get(i);
int [][] boardNew = makeMove(m, board);
int value = minmax(boardNew, depth-1, curPlayer);
board = undoMove(m, boardNew);
// computer plays as black
if (curPlayer == GameCore.BLACK){
if (value < bestVal){
bestMove = m;
bestVal = value;
}
}else{
if (value >= bestVal){
bestMove = m;
bestVal = value;
}
}
}
return bestVal;
}
如果我用 depth = 1 调用 minmax 它应该“返回 7 个值(有 7 个可能的移动),但如果我从 2,4 移动到 3,3,它只返回 1 ...但是当我尝试调试它时,ArrayList move 有正确的大小。所以我不知道它有什么问题。:(
编辑: “返回”我错误地表示第一个条件(当深度为 0 或移动为空时)只发生一次,但如果它是正确的,它应该发生 7 次。对不起,我的英语不好。
你知道一些网站,哪里有正确的 minmax 递归伪代码(最好使用 alpha/beta,因为我需要扩展它)或者你能帮我解决这个问题吗?应该只是小事。谢谢!