Tic-Tac-Toe (vs Computer)

Fill a row, column, or diagonal first to win. You're X, the computer is O.

Record 0W 0D 0L

Click a square, or Tab to it and press Enter or Space. The record resets when you close or reload this page.

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

The three difficulty levels

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 afterNumber of games
5 marks placed1,440
6 marks placed5,328
7 marks placed47,952
8 marks placed72,576
9 marks placed127,872
Total255,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 levelX (you) winsDrawO (computer) wins
Easy58.5%12.7%28.8%
Medium28.0%18.5%53.5%
Hard0.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 opensLines through itPossible repliesReplies that keep the draw
Corner (e.g. square 1)381
Edge (e.g. square 2)284
Center (square 5)484

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

Common misunderstandings and limits

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.