我试图在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;
}每次移动后,活动玩家将被更改。在onTouchEvent的GameView方法中,玩家先移动,然后将玩家变成白色的,即AI,然后移动。它应该在他的树中寻找最好的移动,然后执行一个移动,而不是他做了几个奇怪的动作。我不知道为什么,对于每个分支,我创建一个新的副本板,所以我不知道为什么主游戏板会被修改。
有什么想法吗?
发布于 2013-12-06 18:15:42
如果更改对象的副本会影响原始对象。那就是“浅薄的复制品”。这意味着数据结构中的对象是共享的。你想要一份“深版”。
向我们展示new GameBoard(gb)的代码
一些optinos:您可以为您的游戏板和它包含的所有对象(以及它们包含的对象)实现克隆。或者,在你的棋盘中实现一个撤销()函数。只要你撤销你的棋盘中的每一个动作,你就可以在它上执行移动。这将节省计算期间进行测试移动时的内存和对象创建开销。
https://stackoverflow.com/questions/20430194
复制相似问题