What this game is and how to use it
Tic-Tac-Toe is a two-player game on a 3x3 grid, and here you play it against the computer. You are X, the computer is O, and whoever first places three of their own marks in a straight line wins. The game is small enough that it has been completely solved, which makes it a good place to see how a game-playing program actually thinks. Use the page for a quick break, for teaching a child the idea of "looking one move ahead", or for learning the handful of patterns that let you avoid losing.
How to play
- You are X and the computer is O. You start by default; the "Who starts" menu lets the computer open instead, and changing it restarts the game.
- There are eight winning lines: three rows, three columns and two diagonals. Three identical marks on any one of them wins immediately.
- If all nine squares are full and nobody has a line, the game is a draw.
- Click or tap a square, or Tab to one and press Enter or Space. While the computer is "thinking" (about 0.3 seconds) clicks are ignored, so a double tap cannot place two marks. Pressing New Game cancels a computer move that is still pending.
- The record at the top (wins, draws, losses) lives only in the open page. Reloading or closing the tab resets it, and nothing is sent to a server or synced between devices.
The three difficulty levels
- Easy: the computer picks any empty square with equal probability. It uses the browser's Math.random, which is fine for a game but not meant for security purposes.
- Medium: on every turn it flips a fair coin. Heads, it plays a best move; tails, it plays a random square. It will usually look sensible and then make an odd mistake.
- Hard: it runs the minimax algorithm, which follows every possible continuation to the end of the game. It scores a win as 10 minus the number of marks on the board and a loss as the number of marks minus 10, so it prefers a quick win and, when it is losing, the slowest possible defeat. When several squares share the best score it picks one at random, so the same position can get different replies in different games.
How many games are there? (counted when this page is built)
Filling nine squares in turn gives 9! = 362,880 orderings, but that overcounts because a game stops the moment someone completes a line. Enumerating every legal game with X moving first gives 255,168 distinct complete games: 131,184 won by X, 77,904 won by O and 46,080 drawn. These counts are produced by the same rules as the game itself and are recomputed each time the site is built, not typed in by hand. The first player wins far more of these sequences, but that only describes two players clicking squares at random.
| Game ends after | Number of games |
|---|---|
| 5 marks placed | 1,440 |
| 6 marks placed | 5,328 |
| 7 marks placed | 47,952 |
| 8 marks placed | 72,576 |
| 9 marks placed | 127,872 |
| Total | 255,168 |
No game can end before the fifth mark, because X needs three marks in a row and X's third mark is the fifth one on the board. Most games run long: the largest group, 127,872 of them, fills all nine squares. Counting board positions instead of games, 5,478 different boards can appear, including the empty board and finished boards. If you treat a board and its rotations or mirror images as the same, only 765 remain. That small number is why the computer can examine everything and still answer instantly.
What happens against a player who clicks at random?
The table below assumes X picks every empty square with equal probability, and adds up the probability of every possible game for each computer level. It is an exact calculation, but it is a baseline for "clicking without thinking", not a prediction of how a real person will do. On Easy, both sides are random.
| Computer level | X (you) wins | Draw | O (computer) wins |
|---|---|---|---|
| Easy | 58.5% | 12.7% | 28.8% |
| Medium | 28.0% | 18.5% | 53.5% |
| Hard | 0.0% | 22.3% | 77.7% |
Two things stand out. The first player has a clear edge when both sides are random, and a random player is beaten most of the time by Hard, because Hard never misses a chance to finish a line. Against Hard you cannot win at all; the realistic goal is to avoid losing.
Where to open, and how the reply changes things
With perfect play from both sides the result is a draw; the computed value of the empty board is a draw (0). Number the squares 1 to 9 from the top left, reading across. Squares belong to different numbers of winning lines: a corner is on 3 lines, an edge square on 2, and the center on 4. After the first player's opening, we counted how many second-player replies still hold the draw:
| First player opens | Lines through it | Possible replies | Replies that keep the draw |
|---|---|---|---|
| Corner (e.g. square 1) | 3 | 8 | 1 |
| Edge (e.g. square 2) | 2 | 8 | 4 |
| Center (square 5) | 4 | 8 | 4 |
If the first player opens in a corner, exactly one of the eight replies saves the second player: the center. Every other reply loses against perfect play. That makes the corner the most practical opening, since it gives the opponent the most ways to go wrong. After an opening on an edge square such as square 2, the safe replies are squares 1, 3, 5 and 8; after a center opening they are the four corners. These figures describe perfect opponents and are not winning percentages for people.
Winning with a fork
A fork is a move that creates two threats at once, so the opponent can block only one. Here is a sequence you can replay. X takes square 1 (top-left corner) and O answers in the opposite corner, square 9. X takes square 3, which threatens 1-2-3, and O must block at 2. X now plays 7, which threatens both 1-4-7 and 3-5-7. O can block only one of them, say 4, and X completes the diagonal with 5. The mistake was O's first reply: a corner answer to a corner opening allows the fork, while the center does not.
How to avoid losing
- As the second player, answer a corner opening with the center. Answer a center opening with a corner; any of the four corners holds the draw.
- Each turn, check in this order: can I complete a line right now? If not, does the opponent have two in a line with the third square free? Block it. Only then look for ideas of your own.
- Watch for forks one move ahead. Before the opponent moves, ask which empty square would give them two threats at once, and either take that square yourself or make a threat of your own that forces them to respond elsewhere.
- Play several games in a row as both first and second player. On Hard, the computer's choice among equal moves is random, so you will see different lines each time.
Common misunderstandings and limits
- There is no secret winning strategy against a perfect opponent. Tic-Tac-Toe is a solved game, and a draw is the best either side can force.
- "The first player has the advantage" is true when mistakes are possible, as in the random-play table. It is not true between perfect players.
- The percentages on this page are exact for the stated random-opponent assumption. They are not win rates collected from visitors, and this page collects no play statistics.
- The computer is strong only because the 3x3 board is tiny. Larger variants, such as five-in-a-row on a big board, cannot be searched to the end this way in a browser.
Frequently Asked Questions
Can I beat the Hard level?
No, not if the computer plays as designed. With perfect play from both sides the game is always a draw, and Hard only chooses moves with the best minimax score. Your target on Hard is a draw; wins are possible on Easy and Medium.
Is going first really an advantage?
Against opponents who can slip, yes: with random play, X wins about 58.5% of games and O about 28.8%. Between perfect players both sides draw. Going first mainly gives the opponent more chances to err, especially after a corner opening.
Is it harder when the computer starts?
A little, because you must reply correctly right away. If the computer opens in a corner you need the center. On Hard, every opening square has the same value on an empty board, so the computer picks one at random and you may see any opening.
How many possible games are there?
With X moving first and play stopping when a line is completed, there are 255,168 distinct games and 5,478 distinct boards (765 if rotations and reflections count as one). Both numbers are computed when the site is built.
How does Medium decide its moves?
Each turn it chooses between a best move and a random move with equal probability. That is why the same position can get a strong reply in one game and a weak one in the next.
If you like rule-based games, try Gomoku (Omok), Number Baseball, Minesweeper or Rock Paper Scissors.