2

我正在创建一个播放奥赛罗的简单引擎,使用带有 alpha beta 切割的 minimax。它玩得很好,但有时我会得到一个奇怪的索引超出范围异常(总是接近尾声)。

这是我的算法

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

这是更新状态:

    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;

    }                       

}

谢谢!

4

2 回答 2

2

我对游戏并不特别熟悉,但我相信这与该行中的事实帽子有关:

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

您还应该检查您是否没有出界,否则 - 如果棋盘末端仍有对手,您将继续增加dir

尝试将此行更改为:

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

根据经验,通常在数组访问中,尤其是在循环中,通过显式检查长度来防止越界是一种很好的做法。

于 2012-02-27T08:10:56.227 回答
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();         
 }
于 2012-02-27T20:27:20.847 回答