TIC-TAC-TOE

Noughts and crosses — a solved game, and a good place to see why
Opponent
You are
X to move
You0
Draws0
AI0

Tic-Tac-Toe

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.

How to play

The three opponents

LevelHow it decidesCan you beat it?
EasyUniformly random from the empty squares.Almost always.
MediumOne 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.
PerfectMinimax 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.

Why perfect play is always 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.

The strategy that actually matters: forks

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.

SquareWinning lines through itNotes
Centre4Both diagonals, the middle row and the middle column. The single strongest square.
Corner3A row, a column and one diagonal. Best opening against a weak opponent — it sets more fork traps than the centre.
Edge2A 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.

Frequently asked questions

Can you always win at tic-tac-toe?

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.

What is the best first move?

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.

How does the unbeatable AI work?

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.

How many possible tic-tac-toe games are there?

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.

Why is the game so much harder on a bigger board?

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.

Related games

Discussion

Sign in with GitHub to share strategies, ask questions, or report a bug.