Note that there are some explanatory texts on larger screens.

plurals
  1. POAlpha Beta pruning problems in Othello
    primarykey
    data
    text
    <p>I'm creating a simple engine that plays Othello, using minimax with alpha beta cuts. It's playing well, but sometimes i get a weird index out of bounds exception (near the endgame, always).</p> <p>Here' my algorithm</p> <pre><code>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) &amp;&amp; (currentPlayer == OthelloBoard.BLACK)) return Float.MAX_VALUE; if ((state == OthelloBoard.STATE_WHITE_WINS) &amp;&amp; (currentPlayer == OthelloBoard.WHITE)) return Float.MAX_VALUE; if ((state == OthelloBoard.STATE_BLACK_WINS) &amp;&amp; (currentPlayer == OthelloBoard.WHITE)) return -Float.MAX_VALUE; if ((state == OthelloBoard.STATE_WHITE_WINS) &amp;&amp; (currentPlayer == OthelloBoard.BLACK)) return -Float.MAX_VALUE; if (depth == maxDepth) return OthelloHeuristics.eval(currentPlayer, board); ArrayList&lt;OthelloMove&gt; moves = board.getAllMoves(currentPlayer); for (OthelloMove mv : moves) { board.makeMove(mv); alpha = - minimax(board, garbage, -beta, -alpha, depth + 1); board.undoMove(mv); if (beta &lt;= alpha) return alpha; if (alpha &gt; bestResult) { best.setFlipSquares(mv.getFlipSquares()); best.setIdx(mv.getIdx()); best.setPlayer(mv.getPlayer()); bestResult = alpha; } } return bestResult; } </code></pre> <p>Inside makeMove and undoMove i update the game state(black wins, white wins, draw). I also toggle the players inside these methods. When a player has no moves i make a dummy move without changing the board, and toggle the players.</p> <p>There's a lot more of code, but i think the problem happens when the algorithm hits the game over position. This problem doesn't happen when i set the engine to play random moves, so the problem should be the alpha beta algorithm.</p> <p>Here is getAllMoves, this call getFlips:</p> <pre><code> public ArrayList&lt;OthelloMove&gt; getAllMoves(int player) { ArrayList&lt;OthelloMove&gt; moves = new ArrayList&lt;OthelloMove&gt;(); for (int i = 10; i &lt; 90; i++) { int col = i % 10; if (col != 0 &amp;&amp; col != 9) { if (cells[i] == EMPTY) { ArrayList&lt;Integer&gt; flips = getFlips(i, player); if (flips.size() &gt; 0) { OthelloMove mv = new OthelloMove(); mv.setFlipSquares(flips); mv.setIdx(i); mv.setPlayer(player); moves.add(mv); } } } } return moves; } </code></pre> <p>Here is getFlips.</p> <pre><code> public ArrayList&lt;Integer&gt; getFlips(int idx, int player) { int opponent = getOpponent(player); ArrayList&lt;Integer&gt; flips = new ArrayList&lt;Integer&gt;(); 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) &amp;&amp; (distance &gt; 1)) { while (distance-- &gt; 1) { tempIdx -= dir; flips.add(tempIdx); } } } return flips; } </code></pre> <p>Here is updateState:</p> <pre><code> public void updateState() { int opponent = getOpponent(currentPlayer); int playerMoves = getAllMoves(currentPlayer).size(); int opponentMoves = getAllMoves(opponent).size(); if ( ((playerMoves == 0) &amp;&amp; (opponentMoves == 0)) || (emptyCells == 0)) { int blackDiscs = countDiscs(BLACK); int whiteDiscs = countDiscs(WHITE); if (blackDiscs &gt; whiteDiscs) state = STATE_BLACK_WINS; else if (blackDiscs &lt; whiteDiscs) state = STATE_WHITE_WINS; else state = STATE_DRAW; } } </code></pre> <p>Thanks!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload