3tej home
← All Games

What is Tic-Tac-Toe?

A Tic-Tac-Toe computes tic-tac-toe from the inputs you provide. It applies the standard formula to the values you enter and returns the result instantly, without sending any data to a server. Or play 2-player local. The tool runs entirely in.

Tic-Tac-Toe

Play vs unbeatable AI or local 2-player.

🎮 How to Play

  1. You play X. Click any empty square to place your mark.
  2. AI plays O - uses minimax (cannot lose, only tie).
  3. Get 3 in a row (horizontal, vertical, diagonal) to win.
  4. Switch to 2 Player mode for local multiplayer.

About this tool

Classic 3-in-a-row game. The AI uses minimax algorithm - it cannot lose. Best you can do is draw. Solved game: with perfect play from both sides, every game ends in a draw.

About this tic-tac-toe game

This is a browser-based tic-tac-toe that pairs you against a minimax AI that cannot lose, plus a 2-player local mode for sharing the keyboard. The board is a standard 3-by-3 grid with eight winning lines: three rows, three columns, two diagonals. You play X and move first. The AI plays O and responds in roughly 200 milliseconds because the full game tree has only 26,830 possible games when symmetry is collapsed, so an exhaustive search runs in microseconds.

Tic-tac-toe is the canonical introductory example for game-theory teaching and for the minimax algorithm in computer-science courses. Mathematicians proved the game is a draw with perfect play in the 1960s, and that fact is what makes the AI here unbeatable rather than merely strong: there exists no opening, fork, or sequence that beats correct minimax response.

How the AI works

function minimax(board, isMax):
  result = checkWinner(board)
  if result == "O" return +1     // AI wins
  if result == "X" return -1     // human wins
  if result == "tie" return  0   // draw
  if isMax:
    best = -Infinity
    for each empty square i:
      board[i] = "O"
      best = max(best, minimax(board, false))
      board[i] = ""
    return best
  else:
    best = +Infinity
    for each empty square i:
      board[i] = "X"
      best = min(best, minimax(board, true))
      board[i] = ""
    return best

The algorithm recursively simulates every legal continuation, alternating between the AI maximising its score and the human minimising. At terminal positions it assigns plus one for an AI win, minus one for an AI loss, zero for a draw, then propagates the value back up. The AI plays the move with the best leaf value. Because the search is exhaustive and the rules are deterministic, the AI never gets surprised, never blunders, and never loses to a fork it should have seen.

Opening strength table

If you play optimally as O against an optimal X, here is the count of games still leading to a draw vs the count where an opening blunder hands X a forced win.

X opens atStrengthBest O replyOutcome with perfect play
CentreStrongest (4 winning lines)CornerDraw
CornerStrong (3 winning lines)CentreDraw (O must play centre)
EdgeWeakest (2 winning lines)CentreDraw, easiest for O to hold
Heuristic: always grab the centre if available, take a corner if not, and never take an edge as your opening or your first reply. Edges only become useful for completing a forced two-in-a-row late in the game.

Common pitfalls

  • Ignoring a fork. A fork is a single move that creates two simultaneous three-in-a-row threats. You can only block one, the opponent wins on the next move. Always look two moves ahead for a fork before placing a non-blocking mark.
  • Replying centre with centre. If you open X at centre, O cannot play centre. The minimax AI never makes that mistake; human players sometimes do.
  • Playing edges early. An edge belongs to only two winning lines, half of what centre belongs to. Early edge plays let the opponent build forks easily.
  • Trying to win against the AI. The AI is unbeatable. Treat ties as wins and you will enjoy the game more.
  • Not blocking immediate threats. Always scan for opponent two-in-a-rows before pursuing your own plan. A missed block costs the game.

Frequently asked questions

Can the tic-tac-toe AI be beaten?

No. The AI uses the minimax algorithm, which exhaustively searches every possible game from the current position and picks the move that maximises its worst-case outcome. Tic-tac-toe with perfect play from both sides is a draw, so the best result a human can achieve against minimax is a tie. If you win, it is a bug, not a strategy.

Why does tic-tac-toe always end in a draw with perfect play?

Tic-tac-toe is a solved game. Mathematician E. R. Berlekamp and others showed in the 1960s that the game tree of 26,830 possible games (255,168 if order matters) contains no forced win for either side. As long as both players block immediate three-in-a-row threats and avoid forks, every game terminates in a tie. The first move offers a tiny edge: centre is the strongest opening, corners next, edges weakest.

What is the best opening move in tic-tac-toe?

Centre. Centre is part of four winning lines (one row, one column, two diagonals), so it threatens the maximum number of three-in-a-row possibilities and limits the opponent's safe responses to just two: corner or another threat. A corner opening is the second strongest because it belongs to three winning lines. An edge opening is the weakest because it belongs to only two lines.

How does the minimax algorithm work?

Minimax recursively explores every legal move from a position, alternating between the AI (maximising) and the opponent (minimising). At terminal positions it assigns plus one for an AI win, minus one for an AI loss, zero for a draw. It propagates the values back up the tree: at AI nodes it picks the max child value, at opponent nodes it picks the min child value. Tic-tac-toe's game tree is small enough (under 100,000 nodes) that minimax runs instantly without alpha-beta pruning.

Can a child realistically draw against the AI?

Yes, with practice. The two rules that get you to a draw every time are: open with centre, and after your opponent plays, always check if there is a two-in-a-row to block or a two-in-a-row of your own to complete. Six and seven year olds learn these heuristics in about ten games.

Last updated 2026-05-28.

CT
3Tej Editorial
Free, browser-based tools -.