Skip to content

Instantly share code, notes, and snippets.

@jaapz
Created March 8, 2012 09:16
Show Gist options
  • Save jaapz/1999848 to your computer and use it in GitHub Desktop.
Save jaapz/1999848 to your computer and use it in GitHub Desktop.
// Find optimal move
private Best chooseMove( int side, int[][] board )
{
int opp; // The other side
int win;
Best reply; // Opponent's best reply
int simpleEval; // Result of an immediate evaluation
int bestRow = 0;
int bestColumn = 0;
int bestReply = 0;
int value = UNCLEAR;
if (board == null)
board = cloneBoard();
// kijk wie de opponent is
if (side == COMPUTER) {
opp = HUMAN;
win = COMPUTER_WIN;
} else {
opp = COMPUTER;
win = HUMAN_WIN;
}
// als game over, return dat in een Best
if((simpleEval = positionValue()) != UNCLEAR )
return new Best( simpleEval );
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[i].length; j++) {
int move = (i * board[i].length) + j;
if (moveOk(move)) {
// kijk wat er gebeurt als de move gespeeld wordt,
// en wat de opponent doet
playMove(move, board);
// als huidige positie wint, gelijk best returnen
value = positionValue(board);
if (value == win)
return new Best(win, i, j);
reply = chooseMove(opp, board);
int antwoord = -reply.getVal();
if (antwoord < bestReply) {
bestReply = antwoord;
bestColumn = j;
bestRow = i;
}
}
}
}
return new Best(value, bestRow, bestColumn);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment