Chess AI MiniMax algorithm not working

I have implemented the minimax algorithm here in my chess AI and I know it is not working properly because it just moves 1 piece back and forth over and over again.

getPieces(true) returns all white pieces in the current board state

getPieces(false) returns all black pieces in the current board state

ChessPiece.getAllPotentialMoves() pretty self explanatory, gets all possible moves for a particular piece

PieceMovements.move() moves a piece(p) to position(m -> represents a move)

PieceMovements.undo() undoes the previous movement

searchDepth is a variable that is first passed as the depth value when miniMax is first called, in other words, it is how far you want to search down. The reason for...

if(depth == searchDepth) {
    piece = p;
    move = m;
}

... is to record the piece and move to be made. I record this value at the top level of the search tree. piece and move represent the actual piece and move the algorithm thinks is the most efficient.

when miniMax is called, it looks like this: miniMax(searchDepth, false). False because the AI, black, is the minimiser.

Here is my method

public int miniMax(int depth, boolean maxi) {

    if(maxi) {
        if(depth == 0) return evaluateBoard();
        int max = -9999; //negative infinity
        for(ChessPiece p : getPieces(maxi)) for(Vector2 m : p.getAllPotentialMoves()) {
                PieceMovements.move(board, p, (int)m.x, (int)m.y);
                max = Math.max(max, miniMax(depth-1, !maxi));
                PieceMovements.undo();
                if(depth == searchDepth) {
                    piece = p;
                    move = m;
                }
            }
        return max;
    } else {
        if(depth == 0) return -evaluateBoard();
        int min = 9999; //positive infinity
        for(ChessPiece p : getPieces(maxi)) for(Vector2 m : p.getAllPotentialMoves()) {
                PieceMovements.move(board, p, (int)m.x, (int)m.y);
                min = Math.min(min, miniMax(depth-1, !maxi));
                PieceMovements.undo();
                if(depth == searchDepth) {
                    piece = p;
                    move = m;
                }
            }
        return min;
    }
}

and my evaluation function, which at the moment just takes the relative piece values of each piece and adds them up:

public int evaluateBoard() {
    int total = 0;
    for(ChessPiece[] row : board)
        for(ChessPiece piece : row)
            if(piece != null)
                switch(piece.getPiece()) {
                    case WPAWN:
                    case BPAWN:
                        total += RelativePieceValues.PAWN;
                        //a pawn about to be promoted takes on more value
                        if(piece.getPosition().y == 1 || piece.getPosition().y == 6)
                            total += 50; //50 + 10 = 60
                        break;
                    case WKNIGHT:
                    case BKNIGHT:
                        total += RelativePieceValues.KNIGHT;
                        break;
                    case WBISHOP:
                    case BBISHOP:
                        total += RelativePieceValues.BISHOP;
                        break;
                    case WROOK:
                    case BROOK:
                        total += RelativePieceValues.ROOK;
                        break;
                    case WQUEEN:
                    case BQUEEN:
                        total += RelativePieceValues.QUEEN;
                        break;
                    case WKING:
                    case BKING:
                        total += RelativePieceValues.KING;
                        break;
                }

    return total;
}

and the RelativePieceValues class:

public class RelativePieceValues{

//piece value constants
public static final int PAWN = 10;
public static final int KNIGHT = 30;
public static final int BISHOP = 30;
public static final int ROOK = 50;
public static final int QUEEN = 90;
public static final int KING = 900;
}

If you have any questions, please ask. Thankyou to any responses, I have been stuck on this for a while. I was wondering if there is actually something wrong with my mini max algorithm or my evaluation function or is there probably something else going wrong in my program that you cant see. Thanks.

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

上一篇: alpha beta搜索迭代加深反驳表

下一篇: 棋AI MiniMax算法不起作用