我正在尝试在 Reversi/Othello 游戏中实现 MiniMax 算法,但我非常卡住,因为我编写的函数看起来非常正常,但我得到了一些奇怪的动作,并且在几次之后崩溃。这是寻找最佳移动的函数:
public Field findBestMove(GameBoard gb, int depth, int player)
{
if(depth >= max_depth) return null;
ArrayList <Field> moves = findAllPossibleMoves(gb, player);
Field best_move = null;
/** iterating over all possible moves, to find the best one */
for (int i=0; i<moves.size(); i++)
{
/* board to simulate moves */
GameBoard temp_board = new GameBoard(gb);
Field move = moves.get(i);
game.move(move, temp_board, player);
int opposite_player = player == GameBoard.WHITE ? GameBoard.BLACK : GameBoard.WHITE;
Field best_deep_move = findBestMove (temp_board, depth + 1, opposite_player);
/** if the maximum depth is reached, we have a null, so we evaluate */
if (best_deep_move == null)
{
/** we rate it according to the evaluation table */
move.setRating(evaluate (temp_board, player));
}
else
{
move.setRating(best_deep_move.getRating());
}
if(best_move == null)
{
best_move = move;
}
else
{
if (depth%2==0)
{
/** for us, we look for the maximum */
if (best_move.getRating() < move.getRating()) best_move = move;
}
else
{
/** for the opponent, we look for the minimum */
if (best_move.getRating() > move.getRating()) best_move = move;
}
}
}
return best_move;
}
每次移动后,活跃的玩家都会改变。在 GameView 的 onTouchEvent 方法中,先是玩家出手,然后玩家变成了 WHITE 的,也就是 AI,它出手了。它应该在他的树中搜索最好的动作,然后执行一个动作,而不是他做了几个奇怪的动作。我不知道为什么,对于每个分支,我都会创建一个新的棋盘副本,所以我不知道为什么主游戏棋盘会被修改。
有任何想法吗?