Game AI

Tic Tac Toe vs Computer: How Game AI Chooses Its Moves

A beginner-friendly look inside a real tic tac toe opponent: what Easy, Medium and Hard actually do differently, and why one of them can never be beaten.

Three tic tac toe boards showing the same position answered differently by Easy, Medium and Hard difficulty opponents.
One position, three opponents — Easy misses the block that Medium and Hard both take.

“The computer” in a tic tac toe game is not one thing. Behind three difficulty buttons there are usually three genuinely different pieces of logic, and the gap between them is much bigger than the labels suggest. This article opens up the opponent in Tic Tac Toe and explains what each level actually does.

Everything below describes the real implementation, not a generic textbook example.

The short answer

Tic Tac Toe’s opponent uses a different algorithm at each difficulty. Easy picks a random empty square with no lookahead at all. Medium works down a fixed checklist — take a win, otherwise block one, otherwise prefer the centre and then a corner. Hard runs a complete minimax search of every possible continuation and plays perfectly, which makes it impossible to beat.

One position, three answers

The clearest way to see the difference is to give all three the same problem.

Three tic tac toe boards showing the same starting position where X threatens to complete the top row. Easy plays elsewhere and leaves the threat open; Medium and Hard both take the blocking square.

X has two marks on the top row and the third square is empty. Unless O takes that square, X wins next turn.

  • Easy plays somewhere else entirely. It is not being clever or sandbagging — it genuinely did not look.
  • Medium takes the blocking square. It checks for exactly this every turn.
  • Hard also takes it, but for a different reason: it had already worked out that every alternative loses.

Medium and Hard produce the same move here. They diverge two or three moves later, which is exactly where most games are decided.

Easy: random

Easy is the simplest possible opponent. It collects the list of empty squares and picks one at random, with equal probability. There is no evaluation, no lookahead and no preference.

That means it will:

  • walk straight past its own winning move
  • leave your two-in-a-row unblocked
  • occasionally play a strong move purely by accident

It is not a weakened version of the harder opponents — it is separate logic that does not consider the board’s contents at all beyond which squares are free. That makes it genuinely suitable for a young child or a warm-up, and genuinely unsuitable as practice.

Medium: one move of foresight

Medium runs the same short checklist every turn, in a strict order, and stops at the first rule that applies:

  1. Can I win right now? Try each empty square in turn. If placing a mark there completes a line, play it.
  2. Can my opponent win next turn? Run the same test with your mark. If any square would complete a line for you, take that square to block it.
  3. Is the centre free? Take it.
  4. Is a corner free? Take one at random.
  5. Otherwise, take any free square.

If that looks familiar, it should — it is the same three-check routine any decent human player uses, which is why Medium feels like playing a person rather than a machine.

Its limitation is precise and worth understanding: it only ever looks one move ahead. Rules 1 and 2 both ask “does this single move complete a line?” Nothing in the list asks “does this move let my opponent set up two threats at once?”

That blind spot is exactly the size of a fork. Medium will block the fork’s first threat and lose to its second, every time. If you want to practise the fork from the strategy guide, Medium is the perfect target.

Hard: searching the whole game

Hard uses minimax, and it is worth being precise about what that means, because the name sounds more intimidating than the idea.

The computer builds a tree. From the current board, every legal move is a branch. From each of those, every one of your possible replies is another branch, and so on, until every path reaches a finished game — a win, a loss or a draw.

Then it scores the endings and works backwards.

A diagram showing a current position branching into three candidate moves, each labelled with an outcome and a score: one leads to a loss scoring minus eight, one to a draw scoring zero, and one to a forced win scoring plus nine. The winning branch is highlighted.

The scoring has one refinement that makes a visible difference to how the opponent feels:

  • A computer win scores 10 minus the number of moves it took
  • A player win scores the number of moves minus 10
  • A draw scores 0

So winning in three moves scores 7, while winning in five scores 5. Both are wins, but the faster one scores higher, and the computer prefers it. The same rule applied to losses means that when Hard is losing, it picks the longest path — which is why a beaten Hard opponent keeps playing on instead of collapsing, and gives you every chance to misstep on the way.

Working back up the tree, the computer takes the highest score on its own turns and the lowest on yours, because that is what a competent opponent would do. By the time the values reach the top, every candidate move is labelled with the outcome it guarantees against best play. Hard plays the best of those.

Why that makes it unbeatable

Tic tac toe is a solved game. Every possible sequence has been enumerated, and the result is that perfect play from both sides always ends in a draw. Neither X nor O has a forced win.

An opponent that evaluates every continuation is, by definition, playing perfectly. So it cannot lose — not because it has been given extra information or a handicap, but because there is no mistake in its play for you to exploit. The best result available to you is a draw, and getting one means you played the entire game without an error.

That is a much more interesting goal than winning, and you can try it right now.

Is searching everything not slow?

For chess, hopelessly. For tic tac toe, no.

The absolute upper bound is 9 factorial — 362,880 orderings — and the real number explored is far smaller, because every branch stops as soon as somebody completes a line. Most games end well before the board fills.

The first reply is the expensive one; every move after that removes a square and shrinks the tree sharply. The whole search finishes in a few milliseconds on a phone, with no optimisation, no pruning and no precomputed table.

This creates an odd design problem: the answer arrives too fast. Tic Tac Toe deliberately waits 400 milliseconds before playing the computer’s reply. Without that pause the mark appears the instant you lift your finger, which reads as a glitch rather than as intelligence.

Try it against the same code

The opponent described here is not a simplified web version. The rules engine and all three difficulty algorithms are written in plain TypeScript with no dependency on the mobile framework around them, so the browser game on this site imports the same files that ship inside the Android app.

The practical upshot: the Hard opponent you draw against in your browser is the same Hard opponent on your phone, move for move.

A few things worth testing yourself:

  • Set Easy and count how many moves it takes to miss an obvious block.
  • Set Medium and run the corner fork from the strategy guide. It should lose to it every time.
  • Set Hard and try the same fork. Watch it play an edge square several moves early, defusing a threat that does not exist yet.

That last one is the moment the difference between “checks the next move” and “searched everything” becomes obvious.

Play against all three levels, or read how to play tic tac toe if you want the rules first.

Frequently asked questions

How does a computer decide its tic tac toe move?

It depends entirely on the difficulty. A simple opponent picks a random empty square. A medium one works down a short checklist: win if possible, block if necessary, otherwise prefer the centre. A hard one searches every possible continuation of the game and picks the move with the best guaranteed outcome.

Can a computer be beaten at tic tac toe?

An easy or medium computer can. A perfect one cannot. Tic tac toe is a solved game, so an opponent that evaluates every continuation will never make a mistake you can exploit — the best result available against it is a draw.

What is minimax?

Minimax is a decision rule for two-player games. The computer maximises its own outcome while assuming you will always reply with the move that minimises it. It plays every branch out to the end of the game, scores the results, and chooses the move whose worst realistic outcome is best.

Why does the computer pause before moving?

Because the calculation finishes far too quickly to feel natural. A full search of a tic tac toe position takes single-digit milliseconds, so Tic Tac Toe waits 400 milliseconds before playing its reply. Without that pause the mark would appear the instant you lifted your finger, which reads as broken rather than fast.

  • game ai
  • minimax
  • tic tac toe vs computer
  • difficulty levels

← All articles