← MAIN
TIC-TAC-TOE // MINIMAX · ALPHA-BETA PRUNING

TIC-TAC-TOE

Three in a row against an agent that searches the game tree. The shallow levels can be beaten; the full search cannot.
STATUS READY LEVEL MEDIUM W-D-L 0-0-0

HOW TO PLAY

Get three of your marks in a row, across, down or diagonally, before the agent does. X moves first. Click an empty square to play it.

YOU PLAY picks your mark; when you play O, the agent opens. LEVEL sets how far ahead the agent searches: EASY looks 2 moves ahead, MEDIUM 4, and PERFECT to the end of the game. Changing either starts a new game, and NEW GAME restarts with the same settings.

EASY and MEDIUM can be beaten from either side. Look for a fork: a move that makes two threats at once, so one block can not stop both. PERFECT never loses, so a draw is the best you can do.

Pick a square.
WHAT THE AGENT IS THINKING
Each column is a square the agent considered, with its minimax value from the agent's side (+1 it wins, 0 draw, -1 it loses). Below it are your replies. A dashed box with ≤ or ≥ is only a bound: the search stopped as soon as it proved the move was no better. Struck replies were pruned and never searched. At EASY and MEDIUM, a 0 can also mean "past the horizon": the agent did not look that far and assumed a draw.
positions searched 0 pruned 0
MATCH
YOU PLAY
LEVEL

HOW IT WORKS

The agent is minimax with alpha-beta pruning. It assumes you always make your best reply, picks the move whose worst case is best, and skips every branch it can prove will not change that choice. The levels cut the search off at a fixed depth.

Minimax

A board is nine squares, stored as a nine-character string row by row. Minimax scores a finished game +1 if X has won, −1 if O has won, and 0 for a draw. A position still in play takes the best of its children for the side to move: X maximises, O minimises.

V(s)=\begin{cases}u(s) & s\ \text{finished}\\ \max_{m} V(s\cdot m) & \text{X to move}\\ \min_{m} V(s\cdot m) & \text{O to move}\end{cases}

Here s · m is the board after move m, and u(s) is the result of a finished game. Tic-tac-toe has 5,478 distinct reachable boards and 549,946 move sequences, and its value from the empty board is 0: with perfect play on both sides every game is a draw1.

Alpha-beta pruning

Alpha-beta carries a window (α, β): α is the best value X is already sure of elsewhere, and β the best value O is sure of. Once a node's value leaves the window, the side choosing above it will never let the game reach it, so its remaining moves are pruned. The pruned moves are the struck boxes in the tree2.

From the empty board the full search visits 18,297 positions and prunes 6,930 moves, against 549,946 nodes for the whole game tree. Squares are tried in order 1 to 9, and the first square with the best value wins ties, so the agent always plays the same way from the same board.

The horizon: what the levels change

A level is a depth limit: the number of moves the search looks ahead. A position at the limit that is still in play scores 0, because the agent has not looked past it and assumes a draw3. EASY searches 2 moves: it sees its own wins and your next winning move, so it takes the first and blocks the second, but a fork two moves away looks like a draw. MEDIUM searches 4 moves and sees a fork one move out, but not one set up earlier.

The depths were chosen by a check that plays every human strategy against each level4. A human can force a win against depths 2 and 4 from either side. Depth 5 can only be beaten by a human playing X, and from depth 6 the agent never loses. From the empty board, EASY visits 26 positions and MEDIUM 206.

5
function ALPHA-BETA(board, turn, α, β, ply, depth):
    if board has a winner: return +1 if X won else −1
    if board is full: return 0
    if ply ≥ depth: return 0                # the horizon: unseen counts as a draw
    for each empty square m, in order 1..9:
        v ← ALPHA-BETA(board + m, other turn, α, β, ply + 1, depth)
        if turn is X: α ← max(α, v)         # X raises its guarantee
        else:         β ← min(β, v)         # O lowers its guarantee
        if α ≥ β: break                      # prune the rest
    return the best v for turn