当我递归备份时,我的木板就全部搞砸了。
如何在不始终复制的情况下返回上一个电路板(我的minimax必须具有快速的性能)
我的代码:
for all possible moves:
board.apply(move);
int currentScore = minimax(board, depth - 1, false); // recursive call
int newScore = Math.max(value, currentScore);
// Undo move on the board or create a temp board for recursive calls, as the recursive calls will mess up the current board
board.undo(move);
我想到了Board类中的撤消方法,它需要采取措施并将其撤消,但是这会使我回到当前的董事会吗?
答案 0 :(得分:1)
是的,只要您撤消以相反顺序应用的所有举动,您都将获得原始棋盘。 move
本身的对象通常不会携带足够的信息来撤消自身,因此实现看起来更像:
for all possible moves:
// apply the move and remember how to undo it
undomove = board.apply(move);
// minimax function promises to return the board unmodified
int currentScore = minimax(board, depth - 1, false); // recursive call
int newScore = Math.max(value, currentScore);
// Undo move to recover the original board
board.apply(undomove);