奥赛罗的Alpha Beta修剪问题

时间:2012-02-27 07:45:33

标签: java algorithm search minimax

我正在创建一个简单的引擎,播放奥赛罗,使用minimax和alpha beta剪辑。 它打得很好,但有时我得到一个奇怪的索引超出范围异常(靠近 结局,总是)。

这是'我的算法

private float minimax(OthelloBoard board, OthelloMove best, float alpha, float beta, int depth)
{
    calls++;

    float bestResult = -Float.MAX_VALUE;
    OthelloMove garbage = new OthelloMove();

    int state = board.getState();
    int currentPlayer = board.getCurrentPlayer();

    if (state == OthelloBoard.STATE_DRAW)
        return 0.0f;
    if ((state == OthelloBoard.STATE_BLACK_WINS) && (currentPlayer == OthelloBoard.BLACK))
        return Float.MAX_VALUE;
    if ((state == OthelloBoard.STATE_WHITE_WINS) && (currentPlayer == OthelloBoard.WHITE))
        return Float.MAX_VALUE;
    if ((state == OthelloBoard.STATE_BLACK_WINS) && (currentPlayer == OthelloBoard.WHITE))
        return -Float.MAX_VALUE;
    if ((state == OthelloBoard.STATE_WHITE_WINS) && (currentPlayer == OthelloBoard.BLACK))
        return -Float.MAX_VALUE;

    if (depth == maxDepth)
        return OthelloHeuristics.eval(currentPlayer, board);

    ArrayList<OthelloMove> moves = board.getAllMoves(currentPlayer);

    for (OthelloMove mv : moves)
    {            
        board.makeMove(mv);
        alpha = - minimax(board, garbage, -beta, -alpha, depth + 1);
        board.undoMove(mv);

        if (beta <= alpha)
            return alpha;
        if (alpha > bestResult)
        {                
            best.setFlipSquares(mv.getFlipSquares());
            best.setIdx(mv.getIdx());        
            best.setPlayer(mv.getPlayer());
            bestResult = alpha;
        }
    }     
     return bestResult;
}

在makeMove和undoMove里面我更新游戏状态(黑赢,白赢,平局)。 我也在这些方法中切换玩家。当一名球员没有动作时,我会做一个假人 移动而不改变棋盘,并切换球员。

有更多的代码,但我认为当算法遇到问题时就会出现问题 比赛结束。当我将引擎设置为随机移动时,不会发生此问题,因此问题应该是alpha beta算法。

这是getAllMoves,这个调用getFlips:

   public ArrayList<OthelloMove> getAllMoves(int player)
   {
    ArrayList<OthelloMove> moves = new ArrayList<OthelloMove>();

    for (int i = 10; i < 90; i++) 
    {
        int col = i % 10;

        if (col != 0 && col != 9)            
        {
            if (cells[i] == EMPTY)
            {
                ArrayList<Integer> flips = getFlips(i, player);                    
                if (flips.size() > 0)
                {
                    OthelloMove mv = new OthelloMove();
                    mv.setFlipSquares(flips);
                    mv.setIdx(i);                        
                    mv.setPlayer(player);
                    moves.add(mv);
                }
            }
        }

    }                                     

    return moves;
}

这是getFlips。

    public ArrayList<Integer> getFlips(int idx, int player)
    {        
    int opponent = getOpponent(player);
    ArrayList<Integer> flips = new ArrayList<Integer>();

    if (cells[idx] != EMPTY)
        return flips;

    for (Integer dir : DIRECTIONS)
    {
        int distance = 1;
        int tempIdx = idx;

        while (cells[tempIdx += dir] == opponent)
            distance++;

        if ((cells[tempIdx] == player) && (distance > 1)) 
        {

            while (distance-- > 1)
            {                    
                tempIdx -= dir;
                flips.add(tempIdx);
            }                            
        }            
    }
    return flips;
}

这是updateState:

    public void updateState()
    {                   
    int opponent = getOpponent(currentPlayer);
    int playerMoves = getAllMoves(currentPlayer).size();
    int opponentMoves = getAllMoves(opponent).size();

    if ( ((playerMoves == 0) && (opponentMoves == 0)) ||  (emptyCells == 0))
    {                        
        int blackDiscs = countDiscs(BLACK);
        int whiteDiscs = countDiscs(WHITE);

        if (blackDiscs > whiteDiscs)
            state = STATE_BLACK_WINS;
        else if (blackDiscs < whiteDiscs)
            state = STATE_WHITE_WINS;
        else 
            state = STATE_DRAW;

    }                       

}

谢谢!

2 个答案:

答案 0 :(得分:2)

我并不熟悉这款游戏,但我认为这与该系列中的事实有关:

while (cells[tempIdx += dir] == opponent)

你也应该检查你是不是没有受到约束,否则 - 如果董事会的最后还有对手,你会继续增加dir

尝试将此行更改为:

while (tempIdx + dir >= 0 && tempIdx + dir < cells.length && cells[tempIdx += dir] == opponent)

根据经验,通常在数组访问中是一种很好的做法,特别是在循环中,通过明确地检查长度来防止超出范围。

答案 1 :(得分:0)

发现问题,无论如何,谢谢。

该错误是玩家无法移动的情况,必须通过转弯。 棘手的是发挥'鬼动'(即不改变棋盘的动作),以及 切换球员转身,以便Minimax甚至没有注意到这种情况。

我这样做,但是在错误的地方!代码如下:

   public void makeMove (OthelloMove move)
   {                    
    int player = move.getPlayer();        
    ArrayList<Integer> flips = move.getFlipSquares();

    if (flips != null)
    {                                   
        int idx = move.getIdx();                    
        cells[idx] = player;
        for (Integer flip : flips)
            cells[flip] = player;        

        emptyCells--;    
        this.updatePhase();           
    }
    this.toogleCurrentPlayer();                    
}

public void undoMove (OthelloMove move)
{                    
    int player = move.getPlayer();        
    ArrayList<Integer> flips = move.getFlipSquares();
    int opponent = getOpponent(player);

    if (flips != null)
    {
        int idx = move.getIdx();

        cells[idx] = EMPTY;
        for (Integer flip : flips)
            cells[flip] = opponent;

        emptyCells++;                                         
        this.updatePhase();
    }
    this.toogleCurrentPlayer();         
 }