Alpha beta pruning for minimax implementation

I'm trying to create an AI player for a game using the minimax algorithm with alpha-beta pruning. I'm having some trouble trying to implement it properly. I have 2 functions to work with, one to evaluate the current state of my board for a given player (that returns some score) getBoardScore, and another to return all the possible board states created by every possible move (from a given board state for a given player) getPossibleBoards.

My AI makes a move by initially calling alphaBeta, passing it the current board state. It then sets a new board state from a variable 'bestBoard' which the alphaBeta function had recursively modified. Here is my code for my alphaBeta function:

 static int MAX = -1;
 static int MIN = 1;
 Board node;
 Board bestBoard;   

public int alphaBeta(Board node, int depth, int alpha, int beta, int player) {

    if (depth == 0 || node.gameFinished()) {
        return node.getBoardScore(player);
    }

    ArrayList<Board> childNodes = node.getPossibleBoards(player); //All valid moves from current the board state
    if (player == MAX) {
        for (Board currentBoard: childNodes) {
            int result = alphaBeta(currentBoard, depth-1, alpha, beta, -player);      
            if (alpha < result) {
                alpha = result;
                    bestBoard = currentBoard;
            }
            if (beta <= alpha) {
                break; //alpha cut-off                            
            }
        }                    
    return alpha;
    }
    else { 
        for (Board currentBoard: childNodes) {
            int result = alphaBeta(currentBoard, depth-1, alpha, beta, -player);
            if (beta > result) {
                beta = result;
                    bestBoard = currentBoard;
            }
            if (beta <= alpha) {
                    break; //alpha cut-off
            }                            
        }
        return beta;
    }
}

My issue is that it's just setting my bestBoard variable to the last board state looked at (and not the optimal one). I can't seem to figure out where I should be setting my bestBoard variable (or if I should have some condition before I set it). Could anyone point me in the right direction? Thanks


我认为问题在于,只有当您处于搜索的第一层时,才需要保存bestBoard

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

上一篇: beta修剪minimax算法

下一篇: 阿尔法贝塔修剪minimax实施