Skip to main content
gamesalgorithmsjavascript

Play Tic-Tac-Toe Against an Unbeatable Minimax AI

Free browser tic-tac-toe with a perfect minimax AI opponent. Try to win — the best you can do is force a draw.

WebdevToolbox Team

Tic-tac-toe is a “solved” game — with perfect play from both sides, every game ends in a draw. Our tic-tac-toe tool plays against you using the minimax algorithm, meaning the computer never makes a mistake. The best outcome you can achieve against it is a draw; any slip on your part and it wins.

What minimax actually does

Minimax works by recursively exploring every possible sequence of remaining moves to the end of the game, scoring terminal states (win/loss/draw) and propagating those scores back up the tree. At each of its turns, the computer (maximizing player) picks the move that leads to the best guaranteed outcome, assuming you (the minimizing player) also play your best possible move at every step.

function minimax(board, isMaximizing):
    if board has winner: return score
    if board is full: return 0 (draw)
    if isMaximizing:
        return max over all empty cells of minimax(board-with-move, false)
    else:
        return min over all empty cells of minimax(board-with-move, true)

Because tic-tac-toe’s game tree is small (at most 9! = 362,880 terminal states, far fewer once you prune completed games), a browser can run full minimax instantly with no performance concerns — no need for the alpha-beta pruning or move-ordering optimizations that larger games like chess require.

Why you can’t beat it

Since the computer evaluates every possible continuation before choosing a move, it never walks into a losing line if a non-losing one exists. The only way to guarantee not losing yourself is to also play optimally — center first, then respond to corners with corners, and always block two-in-a-rows immediately.

Try it

Play a few rounds. If you play carefully, you should be able to force a draw every time. If you play carelessly (ignoring a two-in-a-row threat, for example), the AI will win instantly and without hesitation.

Tagged with

gamesalgorithmsjavascript

Related posts