MinMax algorithm not working properly

I am working on a chess engine and now trying to implement Minimax algorithm. At the moment I have put together a mimimax code but it's not really working properly. I beat the engine within minutes given that I'm not a good chess player.

I would like someone to kindly look into my minimax code and tell me what I've written correctly.

Thanks in advance.

Here is my code:

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;
}

You took a fairly complicated task :) MiniMax implementation itself is almost ok, take a look at WIKI page:

Minimax Algorithm

I think minimizing player should work with best move = +infinity (in your case Integer.MAX_VALUE)

However, since you state that your program plays fairly bad, I'll provide another observation. I think that the algorithm will work only if you have a really good evaluation function (EvaluatePieceScore() method in your case). This is where the "art" hides. Are you sure that your method implementation is good enough? I'm saying that because usually people spend the main effort on implementing this function and not an algorithm itself.

Hope this helps

链接地址: http://www.djcxy.com/p/56378.html

上一篇: 使用MinMax与Alpha一起寻找最佳举措

下一篇: MinMax算法不能正常工作