← All animations

🎮 Minimax & Game Trees

How does a computer play a game perfectly? It imagines every possible future, scores the endings, and assumes both sides play their best. One player (MAX) tries to maximise the score; the other (MIN) tries to minimise it. That single idea — minimax — makes tic-tac-toe unbeatable, and underlies how chess and Go engines think. Play below, then watch the AI's reasoning bubble up its game tree.

1. Play an unbeatable AI — then see how it thinks

You're X; the computer is O and uses minimax, so it explores the whole game tree before every move. Try your hardest — the best you can ever get is a draw. After the AI moves, hit Show tree to watch it score each ending (+1 AI win, 0 draw, −1 AI loss) and propagate values upward to justify its choice.

Your move — you are X
MAX node (AI's turn — picks the highest) MIN node (your turn — picks the lowest)

2. Alpha–beta pruning — the same answer, far less work

Minimax is correct but wasteful: it visits every leaf. Alpha–beta pruning keeps the same result while skipping whole branches it can prove won't change the outcome — once a move is found that the other player would never allow, its remaining siblings are abandoned (greyed out). Press Run to watch values bubble up and pruned subtrees vanish, and compare the node counts. Increase depth or branching to feel the game-tree explosion that makes pruning essential.

Full tree: — leaves
MAX MIN pruned (never examined)

3. Why this works — and why chess needs more

Minimax assumes a zero-sum, perfect-information game: what's good for one player is exactly bad for the other, and nothing is hidden (no dice, no secret cards). Tic-tac-toe, chess, checkers, and Go all qualify.

  • MAX (here, the AI) wants the largest score; MIN (you) wants the smallest. Each backs up the best value for itself from the level below.
  • Search to the end of the game, label each ending +1 / 0 / −1, and propagate up. The value at the root is the game's outcome under perfect play — and the move that achieves it is the AI's choice.
  • Tic-tac-toe is solved: its full tree (≈ 26,830 reachable positions) backs up to a draw. With perfect play neither side can win — exactly what you discovered above.
  • Alpha–beta returns the identical value while visiting far fewer nodes; with good move-ordering it roughly square-roots the work, letting an engine search twice as deep in the same time.

So why can't we just solve chess? Its game tree has on the order of 10120 paths — astronomically more than there are atoms in the observable universe. Engines can't reach the leaves, so they stop early and replace the true win/draw/loss with a heuristic (an evaluation of material, king safety, space…). Chess and Go programs are minimax with alpha-beta plus a clever guess at the bottom — and, for Go, neural-network evaluation guiding the search. The core idea you watched is exactly the same.