Tic-tac-toe is a solved game: when neither player makes a mistake, every game ends in a draw. Play the machine on Impossible and you will see it for yourself, because it never loses. Below the board you can read how its algorithm thinks, and the strategy that lets you always draw, or win as soon as your opponent slips.
Keyboard: 1–9 or the arrows to play, H hint, U undo, N new game.
How the machine plays
Every tic-tac-toe position is a node in a tree, and every legal move is a branch to a new position. The tree is small enough to explore completely: it holds exactly 255,168 possible games (X wins 131,184 of them, O wins 77,904 and 46,080 end in a draw), and they pass through only 5,478 distinct positions, or 765 if rotations and reflections of the board count as the same position.
Minimax
On Impossible the machine runs minimax over that whole tree. A finished game is worth \(+1\) if the machine won, \(-1\) if it lost and \(0\) if it is a draw. Those values then travel up the tree: on its own turn the machine takes the move with the highest value, and on yours it assumes you will answer with the move that is worst for it, the one with the lowest value.
\[ v(s) = \begin{cases} u(s) & \text{if } s \text{ is a finished game} \\ \max_{a}\, v(s_a) & \text{if the machine moves} \\ \min_{a}\, v(s_a) & \text{if you move} \end{cases} \]
Here \(u(s)\) is the result of a finished game and \(s_a\) is the position reached by playing move \(a\) in \(s\).
The value of the empty board comes out as \(0\): with perfect play from both sides, tic-tac-toe is a draw. That is also why you cannot beat the machine. To win you would need a line of play it did not foresee, but minimax has already examined every reply you could make. If any of your moves led to your victory, that branch would have value \(-1\) and the machine would never have chosen the move that allowed it.
Four refinements
- Fast wins, slow losses. A win counts as \(10 - n\), where \(n\) is the number of moves until the game ends, and a loss counts as \(n - 10\). Among winning moves the machine takes the shortest route instead of toying with you.
- Memory. The same position can be reached through different move orders, so each evaluated position is stored in a table (memoization). The tree is solved once, in a few milliseconds, and every later move is instant.
- Setting traps. Several moves are often equally good, all of them a draw. Instead of choosing at random, the machine prefers the one that leaves you the most replies that lose. That is why it always opens in a corner when it starts: 7 of your 8 possible answers lose.
- Analysis. The Show analysis option uses the same function: it labels each empty square as a win, a draw or a loss for the player to move, assuming perfect play afterwards. Hint plays the best of those squares.
Difficulty levels
Easy. Plays at random, although it usually completes a three in a row when it has one ready.
Medium. Looks one move ahead: it completes its own three in a row, blocks yours, and otherwise prefers the center and then the corners. It has a blind spot: it cannot see forks, so a double threat beats it.
Impossible. The full minimax described above, with the trap-setting tie-break. It never loses: the best you can get is a draw.
How to always draw, or win
Since perfect play ends in a draw, the goal is twofold: never lose, and punish your opponent the moment they make a mistake. The squares are numbered from 1 to 9 like this:
The key idea: the fork
A fork is a move that creates two threats of three in a row at once. Your opponent can block only one of them, so you win with the other. Almost every decided game of tic-tac-toe is won with a fork, so good strategy is all about creating forks and preventing them.
Priority rules
- Win: if you have two in a row, complete the third.
- Block: if your opponent has two in a row, take the third square.
- Fork: make a move that creates two threats at once.
- Block the fork: if your opponent can fork on their next move, prevent it, ideally by creating a threat of your own that forces them to answer somewhere harmless.
- Center: take it if it is free.
- Opposite corner: if your opponent is in a corner, take the opposite one.
- Empty corner: take any free corner.
- Empty edge: take any free edge.
At every turn, apply the first rule that fits. This list comes from Newell and Simon (1972), and following it you will never lose. The subtle one is rule 4: blocking a fork the wrong way still loses, as the trap further down shows.
If you play first (X)
- Open in a corner. It is the opening that leaves your opponent the fewest ways to survive: of their 8 replies, only the center draws and the other 7 lose.
- If they answer on an edge or in a corner, you can force a win. Example: you play 1 and they answer 2. Take 5, threatening 9; they must block at 9. Now play 7: you threaten 4 and 3 at once.
- If they take the center, play the opposite corner, 9. If they now take a corner you have a fork; if they take an edge, block what you must and the game is a draw.
- Other openings. The center is also solid: the 4 edge replies lose and the 4 corner replies draw. An edge is the weakest opening: 4 replies draw (the center, the two adjacent corners and the opposite edge).
If you play second (O)
- Against a corner, take the center. It is the only move that does not lose.
- Against the center, take a corner. Any edge loses.
- Against an edge, take the center, one of the two adjacent corners or the opposite edge.
The classic trap. X plays 1, you take the center and X answers with the opposite corner, 9. The tempting move is another corner, and it loses: at 3 or 7 you make a threat, X blocks it, and that very block is a fork. The right answer is an edge (2, 4, 6 or 8): your threat forces X to block on a square that does not help them.
How to win
Against perfect play nobody wins, so winning means setting problems your opponent might fail to solve. As X, open in a corner and watch for any reply other than the center. As O, hold the draw and pounce on the first mistake with a fork. Practice with Show analysis turned on: teal squares win, grey squares draw and red squares lose. Then switch it off and try to see them on your own.