私はアルファベータ版でminimaxを使って、オセロを演じるシンプルなエンジンを作っています。 これはうまくいっていますが、時々私は奇妙なインデックスを外に出します( の終わり近く)。ここでOthelloのアルファベータプルーニング問題
」私のアルゴリズム
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私は(黒勝ち、白の勝利は、ドロー)ゲームの状態を更新します。 また、これらのメソッド内のプレーヤーを切り替えることもできます。プレイヤーが移動していないときは、ボードを変更せずにダミーの を動かし、プレイヤーを切り替えます。
さらに多くのコードがありますが、アルゴリズムが ゲームオーバーになると問題が発生すると思います。ランダムな動きをするようにエンジンを設定すると、この問題は発生しないので、問題はアルファベットアルゴリズムでなければなりません。ここで
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である:ここでは
はgetAllMoves、このコールgetFlipsです。
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;
}
}
ありがとう:ここ
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です!
あなたのスタックトレースは何ですか? – amit
みんな速いですね、ありがとう。スレッド "スレッド3" java.lang.ArrayIndexOutOfBoundsExceptionの例外:MinimaxOthello.minimaxでOthelloBoard.getAllMoves(OthelloBoard.java:85) \tでOthelloBoard.getFlips(OthelloBoard.java:119) \tで100 \t(MinimaxOthello。 java:53) \t at MinimaxOthello.doMove(MinimaxOthello。Javaの:あなたとして23)OthelloPanel.doLogic(OthelloPanel.java:118)OthelloPanel.runで \t(OthelloPanel.java:162)で \t java.lang.Thread.run(Thread.java:680で \t) – Fernando
それは 'getFlips()'にあります。このメソッドの関連コードを追加してください。 'getAllMoves()'も必要かもしれません。 – amit