How to use the tic tac toe solver
Tap the squares in the order the game was played: the tic tac toe solver places X and O in turn, starting with X (switch to O first if your game started with O). Tap a mark again to remove it. Every empty square then gets a label: Win in 2 means the player to move wins with their second move from now if they play that square and keep playing well, Draw means best play from both sides ends in a draw, and Loses means the opponent can force a win. The best squares are outlined, and the list explains each move in plain words: a block, a fork, a threat, or why it loses.
Choose Play against it to play a game, then pick a level. Perfect never loses; when several moves are equally good it picks one at random, so games vary. Easy makes mistakes on purpose: about half of its moves go on a random empty square, so you can beat it. In 2,000 test games against random moves, easy won 1,357, lost 391 and drew 252; perfect won 1,724 and drew the rest.
How the solver works
Tic tac toe is small enough to solve completely. There are 5,478 different positions that can arise from an empty board, and the solver works out the value of each one by minimax: a square is a win if it leads to a position where every reply by the opponent still loses, a loss if the opponent has a winning reply, and a draw otherwise. Among winning moves it prefers the fastest win; among losing moves, the one that holds out longest. The results are remembered so each position is solved only once, which takes a few milliseconds.
We test it against a plain search with no shortcuts on every one of the 5,478 positions (16,167 move results, all identical), and we play it against every possible sequence of opponent moves, as X and as O: it never loses.
What perfect play teaches
- The game is a draw. If neither player makes a mistake, every game ends in a draw. All nine first moves draw with best play.
- Win when you can, block when you must. If you have two in a row with the third square empty, take it. If your opponent does, block it.
- Forks win. A fork is a move that makes two threats at once; the opponent can block only one. Most wins between beginners come from a fork.
- Answer a corner with the center. If X starts in a corner, O must take the center: every other reply loses, as the solver shows on the board. If X starts in the center, O must take a corner.
- Answer the edge carefully. If X starts on an edge, O draws with the center, either corner next to X, or the edge opposite X.
Worked example
Tap the top left square for X. The solver marks the center as a draw for O and all seven other squares as losses. Tap the bottom right for O to see why: X takes another corner, makes a fork, and wins in 4 moves in total. Undo, put O in the center instead, and every square is a draw again.
The same win, draw or loss marking on a bigger board is what the Connect 4 solver does, and nine men's morris adds moving pieces to the three-in-a-row idea.
Solver tested . Found a problem? Tell us.
Questions
Can you always win at tic tac toe?
No. With perfect play from both sides tic tac toe is always a draw. You can only win if your opponent makes a mistake, usually by allowing a fork (two threats at once).
What is the best first move in tic tac toe?
Every first move draws with perfect play. A corner gives the opponent the most chances to go wrong: only the center reply saves O. The center is the safest start; O must answer in a corner.
How many possible tic tac toe positions are there?
5,478 different positions can be reached from the empty board, counting the empty board and finished games, with X moving first. The solver checks all of them.
What does "Win in 3" on a square mean?
If the player to move takes that square and keeps playing perfectly, they complete three in a row by their third move from now (counting this one), whatever the opponent does.
Why does the solver say my board cannot happen?
Players take turns, so the player who started has the same number of marks as the other or one more, and the game stops at the first three in a row. Boards that break these rules cannot come from a real game.