【问题标题】:Minimax recurse back up the treeMinimax 递归备份树
【发布时间】:2019-11-17 18:40:23
【问题描述】:

当我递归备份时,我的板子全乱了。

如何在不总是复制的情况下返回上一个棋盘(我的 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 类中的 undo 方法,它可以移动并撤消它,但这会让我回到我当前的董事会吗?

【问题讨论】:

    标签: algorithm minimax


    【解决方案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);
    

    【讨论】:

    • 这里的招式是一个字符串,所以如果我刚刚应用的招式杀死了一个棋子,我很难回到以前的状态,我怎么能撤消刚刚被杀死的棋子?
    • board.apply(move)返回的值需要指明如何撤消移动。这意味着它必须表明应该更换死棋。你如何做到这一点并不重要,但必须这样做。如果您愿意,您可以使用不同的方法来应用撤消移动,因为它们有更多的可能性。不过,没有理由必须将移动存储为字符串。
    • 例如,一个移动(包括一个撤消移动)可以打包到一个 17 位的 int 中。 6 位用于源位置,6 位用于目标位置,3 位用于替换件(通常在源位置),2 位表示撤消左侧或右侧的途中捕获
    • 谢谢!!!这行得通吗: int[][] grid = board.apply(move);哪个网格是移动前的板,然后代替 board.apply(undomove) 我使用 board.setGrid(grid) 你怎么看?
    • 格子是13乘13,会贵多少?顺便说一句,感谢您的帮助,非常感谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多