Back to the blog

Games

Can you beat an unbeatable tic-tac-toe computer?

An unbeatable tic-tac-toe computer looks at every game that can follow, so it never loses. Why a draw is the best anyone can do, and how to get one.

By 4 min read

A game of tic-tac-toe: X in the top left corner, O in the centre, and the free squares each marked Draws, with the score beside the board

No. A computer that plays tic-tac-toe perfectly can never be beaten, and the best you can do is a draw. That is not because computers are clever but because the game is small enough to be worked out completely.

Tic-tac-toe is a solved game.

There are 255,168 ways a game of tic-tac-toe can be played from the empty board to its end, and 5,478 different boards along the way. A computer can look at all of them in a moment.

It works out each board from the end backwards: a board where someone has won is worth that win; any other board is worth the best move for whoever is to play, assuming the other player then does their best too. This is called minimax. From the empty board, the answer is a draw: neither player can force a win.

How KeepSimp's unbeatable computer plays.

It knows what every move leads to. Of the moves that keep its best result, it picks the one that leaves you the most replies that lose, so it plays for traps without ever risking a loss: it opens in a corner, where only the centre holds. When several moves are equally good, it picks one at random, so games vary.

Its tests play every game that can be played against it, as X and as O, trying every move it might choose, and check that it never loses and never lets a won position go.

How to hold it to a draw.

When it opens in a corner, take the centre. If it then takes the opposite corner, answer on an edge, never a corner. After that, block every line of two, and the game ends in a draw.

When you go first, a corner is the trickiest start, but against perfect play it still only draws. Turn on hints if you are unsure: every free square shows whether it wins, draws or loses.

Wins are for the other levels.

To practise winning, play Hard, which plays perfectly most of the time and slips now and then, or Medium, which blocks every line but does not see a fork coming. The review after each game shows the move that decided it.

Further reading

Topics: impossible tic tac toe · unbeatable tic tac toe · can you beat tic tac toe · tic tac toe minimax · tic tac toe always a draw · tic tac toe ai

A little less theory. A little more doing.

Try it on your own terms, with a free browser tool.

Try the tool