【发布时间】:2012-02-27 07:45:32
【问题描述】:
我正在创建一个播放黑白棋的简单引擎,使用带有 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;
}
}
谢谢!
【问题讨论】:
-
哇,伙计们在这里很快,谢谢。线程“Thread-3”中的异常 java.lang.ArrayIndexOutOfBoundsException: 100 at OthelloBoard.getFlips(OthelloBoard.java:119) at OthelloBoard.getAllMoves(OthelloBoard.java:85) at MinimaxOthello.minimax(MinimaxOthello.java:53) at MinimaxOthello .doMove(MinimaxOthello.java:23) 在 OthelloPanel.doLogic(OthelloPanel.java:118) 在 OthelloPanel.run(OthelloPanel.java:162) 在 java.lang.Thread.run(Thread.java:680)
-
如您所见,它位于
getFlips()。请添加此方法的相关代码。getAllMoves()可能也需要。 -
问题出在while (cells[tempIdx += dir] ==对手)
-
cells 是一个 int[100],OthelloBoard 类的实例变量。我将它映射到一个 8x8 数组,只是为了澄清。谢谢!
-
这意味着在执行 cells[tempIdx += dir] 之前,您必须检查 tempIdx + dir 是否小于 cells.length
标签: java algorithm search minimax