Tic-tac-toe — noughts and crosses outside North America — is the smallest game most people ever learn, and the first one most programmers solve. Two players alternate marking squares on a 3×3 grid; the first to complete a row, column or diagonal wins. If the grid fills with no line, the game is a draw.
| Level | How it decides | Can you beat it? |
|---|---|---|
| Easy | Uniformly random from the empty squares. | Almost always. |
| Medium | One ply: takes an immediate win, blocks an immediate threat, otherwise prefers centre, then corners, then edges. | Yes — build a fork, a move that creates two threats at once, and it can only block one. |
| Perfect | Minimax over the entire game tree, no depth limit, no heuristic. Wins are scored higher the sooner they arrive, so it never dawdles in a won position. | No. The best result available to you is a draw. |
Tic-tac-toe is solved: the value of the starting position is known, and it is a draw. There are 255,168 legal games and only 26,830 reachable positions, small enough that a browser can search every one of them in a few milliseconds. That is exactly what the Perfect setting does — it is not estimating, it is reading the answer off the bottom of the tree.
The practical consequence is that against a perfect opponent your goal changes. You are not trying to win; you are trying not to lose, which means recognising forks before they are built rather than after.
A fork is a move that creates two winning threats simultaneously. Your opponent can block one, and you take the other. Every win in tic-tac-toe between competent players comes from a fork, so the whole game reduces to creating one or preventing one.
| Square | Winning lines through it | Notes |
|---|---|---|
| Centre | 4 | Both diagonals, the middle row and the middle column. The single strongest square. |
| Corner | 3 | A row, a column and one diagonal. Best opening against a weak opponent — it sets more fork traps than the centre. |
| Edge | 2 | A row and a column only. Rarely the right move outside a forced block. |
If you open in a corner and your opponent replies anywhere other than the centre, you almost always have a forced win. If you open in the centre, your opponent must reply in a corner; an edge reply loses.
No. The game is solved and perfect play by both sides is a draw. You can only win when your opponent errs — which is why the Perfect opponent here is genuinely unbeatable, not just hard.
The centre is the safest: it lies on four of the eight winning lines, versus three for a corner and two for an edge. Against imperfect opponents a corner opening actually wins more often, because it creates more fork opportunities that a one-ply opponent cannot see coming.
Minimax. It plays every legal continuation to the end, scores each terminal position as a win, draw or loss, and assumes you will always choose your best reply. Because the tree is tiny it needs no evaluation function and no depth cutoff — it simply knows.
255,168 distinct games, reaching 26,830 distinct positions. Of the terminal positions, 91 are X wins, 44 are O wins and the rest are draws — the asymmetry is entirely the first-move advantage.
It stops being solvable by brute force. Try Ultimate Tic-Tac-Toe, which nests nine of these grids inside a tenth and adds one rule: your move dictates which board your opponent must play in. The state space explodes and the game becomes genuinely strategic.
Sign in with GitHub to share strategies, ask questions, or report a bug.