有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java MinMax算法工作不正常

我正在研究一个国际象棋引擎,现在正试图实现极大极小算法。目前,我已经编写了一个mimimax代码,但它并没有真正正常工作。考虑到我不是一个好的棋手,我在几分钟内就击败了引擎

我想有人看看我的minimax代码,告诉我我写对了什么

提前谢谢

这是我的密码:

private int MiniMax(Game game, int depth){

    return Max(depth);
}
private int Max(int depth){
    if (depth <= 0
            || this.chessgame.getGameState() == Game.GAME_STATE_END_YELLOW_WON
            || this.chessgame.getGameState() == Game.GAME_STATE_END_BROWN_WON){ 
        return EvaluatePieceScore();
        }
    int max = -Integer.MIN_VALUE;
    List<Move> moves = generateMoves(false);

     for(Move allMove : moves){
           executeMove(allMove);
           int score = -Mini(depth - 1);
           undoMove(allMove);

            if( score > max){
                max = score;
            }
        }
    return max;
}

private int Mini(int depth) {
    if (depth <= 0
            || this.chessgame.getGameState() == Game.GAME_STATE_END_YELLOW_WON
            || this.chessgame.getGameState() == Game.GAME_STATE_END_BROWN_WON){ 
        return EvaluatePieceScore();
        }
    int min = Integer.MIN_VALUE;
    List<Move> moves = generateMoves(false);

     for(Move allMove : moves){
           executeMove(allMove);
           int score = -Max(depth - 1);
           undoMove(allMove);

            if( score > min){
                min = score;
            }
        }
    return min;
}

共 (1) 个答案

  1. # 1 楼答案

    您完成了一项相当复杂的任务:)MiniMax实现本身几乎没有问题,请查看WIKI页面:

    Minimax Algorithm

    我认为最小化玩家应该使用最佳移动=+无限(在你的例子中是Integer.MAX_值)

    然而,由于您声明您的程序运行得相当糟糕,我将提供另一个观察结果。我认为只有当您有一个非常好的求值函数(在您的例子中是EvaluatePieceScore()方法)时,该算法才会起作用。这就是“艺术”隐藏的地方。您确定您的方法实现足够好吗?我这么说是因为人们通常把主要精力花在实现这个函数上,而不是算法本身

    希望这有帮助