Unbeatable Tic-Tac-Toe

Before every move the computer plays out every game that could still happen, then picks a square that cannot lose. The search panel shows that work in full: how many positions it looked at, what it thought each square was worth, and the line it expects you to play.

minimax + alpha-beta 549,946 positions in the full tree plain JS, nothing loaded the best you can get is a draw

The game

move 1
you computer

 

 

your wins
0
draws
0
its wins
0
games
0

Squares are named like a map: a1 top-left, b2 the middle, c3 bottom-right. Click a square, or tab to the board and use the arrow keys.

▸ how the search actually works

Minimax. The computer walks the whole game forward, one square at a time, until every line ends in a win, a loss or a full board. On its own turns it assumes it will pick the best square available; on your turns it assumes you will pick the worst square for it. That is the whole idea: take the maximum of your minimums.

The scores. A finished line is worth +1 if the computer won, -1 if you won, 0 if the board filled up. To stop it from dawdling, the score shrinks the longer a win takes: a win on the very next move scores +9, a win four moves out scores +6, and a loss it cannot avoid is pushed as far away as possible. That is why it blocks you immediately instead of wandering — and why the numbers in the score map differ even when several squares all end in a draw.

Alpha-beta pruning. While searching, it keeps track of the best score it has already locked in. The moment a branch is proven worse than that, it stops reading the rest of that branch — nothing there can change the answer. It reaches exactly the same move having looked at a fraction of the positions, which is the second number in the stats strip.

Ties. When several squares score the same, the computer picks one of them at random, so games do not repeat. Every tied square is equally good, so this costs it nothing.

What is drawn. The tree panel shows the first three half-moves of the real search. Deeper plies are searched exactly the same way, there are simply far too many boards to draw — from an empty board the full tree is 549,946 positions.

▸ why you cannot win

Tic-tac-toe is solved: with both sides playing perfectly, every game is a draw. The computer searches to the end of the game every single move, so it never plays a second-best square. It cannot be tricked, and it has no opening book to catch it out — it re-derives everything from the position in front of it.

So a draw is a win for you. Try the pre-played opening: after you take a corner, the score map shows that exactly one of the computer's eight replies holds the draw. Every other square hands you the game. Take the middle yourself and watch the same thing happen from the other side.

Where people slip. Answer a corner opening with an edge instead of the middle, or chase your own line while the computer builds two at once, and the score map turns red one move before you feel it.

What it worked out

 
+ computer wins 0 draw − you win bigger number = happens sooner

The tree it searched

 
 

Click any board in the tree to follow that branch. Faded, dashed boards are the ones alpha-beta pruning threw away without looking — once one reply is known to be good enough, the rest of that branch cannot change the answer.