What algorithms are available to solve Tic Tac Toe?

What algorithms are available to solve Tic Tac Toe? Especially with a board size of 4 * 4 or more, and not 3 * 3? I tried 4 * 4 with Minimax and alpha beta pruning, but the PC seems to freeze and throw exceptions when the stack overflows. I saw this source code written in javascript, but I don’t know which algorithm it uses, can anyone clarify it? http://js-x.com/page/javascripts__example.html?view=153

+3
source share
1 answer

try to make cutoffs with a certain depth ... I don't think you need more than 4 or 5 depths to make the perfect move.

(java for 3 * 3 one-dimensional boards 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;
}
+1
source

All Articles