我正在創建一個簡單的引擎來播放奧賽羅,使用帶alpha測試版的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;
}
}
謝謝!
什麼是您的堆棧跟蹤? – amit 2012-02-27 07:47:33
我們這些傢伙在這裏很快,謝謝。例外在線程 「螺紋3」 java.lang.ArrayIndexOutOfBoundsException:100 \t在OthelloBoard.getFlips(OthelloBoard.java:119) \t在OthelloBoard.getAllMoves(OthelloBoard.java:85) \t在MinimaxOthello.minimax(MinimaxOthello。 java:53) \t at MinimaxOthello.doMove(MinimaxOthello。Java的:23) \t在OthelloPanel.doLogic(OthelloPanel.java:118) \t在OthelloPanel.run(OthelloPanel.java:162) \t在java.lang.Thread.run(Thread.java:680) – Fernando 2012-02-27 07:49:45
正如你可以看到,它在'getFlips()'中。請添加此方法的相關代碼。 'getAllMoves()'也可能需要。 – amit 2012-02-27 07:51:56