Tic-Tac-Toe

The classic, against an opponent that has already played every possible game.

X to move
Minimax AI ยท plays perfectly

Tip: press 1โ€“9 to place a mark.

Under the hood

How the Tic-Tac-Toe AI thinks

This one is unbeatable by design: it plays out every possible game to the end before it moves, so it never makes a mistake it could have seen coming.

search full game treemax depth 9 pliespruning alpha-betaworst case a draw
01

It looks all the way to the end

A game has at most 9 moves, so the AI doesn't need a shortcut: minimax explores every line, assuming you'll always pick the reply that's best for you. Alpha-beta pruning skips branches that can't change the answer.

02

It wants to win fast

A win is worth 10 minus the number of moves it takes, and a loss costs 10 minus the number of moves you needed. So it takes the quickest win on offer, and when it's lost it makes you work for it.

03

Why it opens in a corner

Every opening draws against perfect play, so it falls back on a preference: corners, then the centre, then edges. A corner is the opening with the most ways for you to go wrong, and it picks randomly among the four.

04

Why going first is an unfair fight

After a corner opening, one reply holds the draw and the other seven lose. Unless you find it, the AI builds a fork (two threats at once) and you can only block one.

Your move

You're O, and the AI is X, going first. Take the centre square. It's your only move that doesn't lose, and the best you can do is a draw. Open anywhere else and you lose every time. Swap the players to see how the AI defends when you go first.