I'm taking an Artificial Intelligence class where we need to create an AI that plays Tic-Tac-Toe.
The instructor specified to use alpha-beta pruning for the AI's decision making process when placing it's next move. The issue I'm running into at this point is the amount of time it takes for the AI to create the decision tree and make its move. Normal 3x3 is fine, and 3x4 and 4x3 take a little time, but 4x4 takes multiple minutes for the first move, and I haven't gotten results out of game boards larger than that.
Source code I used:
/** Get next best move for computer. Return int[2] of {row, >col} */
@Override
int[] move() {
int[] result = minimax(2, mySeed, Integer.MIN_VALUE, >Integer.MAX_VALUE);
// depth, max-turn, alpha, beta
return new int[] {result[1], result[2]}; // row, col
}
/** Minimax (recursive) at level of depth for maximizing or >minimizing player
with alpha-beta cut-off. Return int[3] of {score, row, col} >*/
private int[] minimax(int depth, Seed player, int alpha, int >beta) {
// Generate possible next moves in a list of int[2] of {row, >col}.
List<int[]> nextMoves = generateMoves();
// mySeed is maximizing; while oppSeed is minimizing
int score;
int bestRow = -1;
int bestCol = -1;
if (nextMoves.isEmpty() || depth == 0) {
// Gameover or depth reached, evaluate score
score = evaluate();
return new int[] {score, bestRow, bestCol};
} else {
for (int[] move : nextMoves) {
// try this move for the current "player"
cells[move[0]][move[1]].content = player;
if (player == mySeed) { // mySeed (computer) is >maximizing player
score = minimax(depth - 1, oppSeed, alpha, beta)[0];
if (score > alpha) {
alpha = score;
bestRow = move[0];
bestCol = move[1];
}
} else { // oppSeed is minimizing player
score = minimax(depth - 1, mySeed, alpha, beta)[0];
if (score < beta) {
beta = score;
bestRow = move[0];
bestCol = move[1];
}
}
// undo move
cells[move[0]][move[1]].content = Seed.EMPTY;
// cut-off
if (alpha >= beta) break;
}
return new int[] {(player == mySeed) ? alpha : beta, >bestRow, bestCol};
}
}
Source link if it is needed
The instructor also suggested using iterative deepening search, but I am a dummy who doesn't know how.