What algorithms are available to solve Tic Tac Toe? Especially with board size 4 * 4 or bigger instead of 3 * 3? I tried 4 * 4 with Minimax & alpha-beta pruning but the pc seems to hang and throws exceptions on stack overflow. I saw these source code written in javascript but I don't know which algorithm it uses, could anyone clarify it for me? http://js-x.com/page/javascripts__example.html?view=153
Asked
Active
Viewed 803 times
3
-
I have paste it here for better display : http://pastebin.com/ewsQSDWX with syntax highlighting – Duc Tran May 22 '12 at 17:29
1 Answers
1
try to do cut-offs in certain depth... i don't think you need more than 4 or 5 of depth to make a perfect move.
(java for 3*3 one-dim board with depth):
int minimax(int turn, int depth) { // turn(side to move) is 1 or -1
int val = -turn; // worst value for that turn
int result = NULVAL;
if (wins()) return turn; // evaluate board (board is a field)
if (depth == 0) return DRAW;
for (int i = 0; i < 9; i++) {
if (board[i] == EMPTY) {
board[i] = turn; // make move
result = minimax(-turn, depth - 1);
board[i] = EMPTY; // delete move
if (result == turn)
return turn; // sees win
if (result == DRAW)
val = result;
}
}
// if result keeps NULVAL: couldn't make a move (board is full)
if (result == NULVAL) return DRAW;
return val;
}

matias
- 105
- 1
- 10