Skip to content
Toolcroft

Games & Puzzles

Tic-Tac-Toe vs AI - Play Unbeatable Noughts & Crosses

Play Tic-Tac-Toe against an unbeatable AI powered by the Minimax algorithm, or challenge a friend in two-player mode. Tracks wins, losses, and draws.

X: 0Draw: 0O: 0

Click New Game to start.

Rules

Tic-tac-toe is a two-player game played on a 3×3 grid. Players alternate marking squares: one player places X, the other places O. The first to mark three squares in a horizontal, vertical, or diagonal line wins. If all nine squares are filled with no winner, the game is a draw (also called a “cat’s game”).

Controls: click any empty square to place your mark.

Perfect play

Tic-tac-toe is a solved game: with optimal play from both sides, every game ends in a draw. The AI opponent uses the minimax algorithm, which evaluates every possible future game state and always picks the move that leads to the best outcome. You cannot beat a minimax player - the best result is a draw.

Optimal strategy

  • Go first in the center: the center square participates in 4 winning lines (all 3 rows/columns, and both diagonals). It is the strongest opening move.
  • If you go second: if the opponent takes the center, play a corner. If the opponent takes a corner, play the center.
  • Create forks: a fork is a position where you have two ways to win on your next move. The opponent can only block one, so you win. Forks are the only way to beat a non-optimal opponent.
  • Block forks first: if your opponent is building a fork, disrupt it by creating your own two-in-a-row that forces them to defend instead.

Fun fact

Tic-tac-toe is one of the simplest examples of a game tree - a branching structure representing all possible moves. The full game tree has about 255,168 possible games (before accounting for symmetry). It was among the first games to be fully analyzed by computers and remains a classic teaching example in artificial intelligence for minimax and alpha-beta pruning.

AI algorithm: minimax with alpha-beta pruning

The computer opponent uses the minimax algorithm: it recursively evaluates every possible future game state, assuming both players play optimally. The AI maximizes its own score and minimizes the human's. At each position, it assigns a score: +1 for an AI win, −1 for a human win, 0 for a draw.

Alpha-beta pruning dramatically reduces the number of nodes evaluated. It maintains two bounds (alpha: best the maximizer is guaranteed; beta: best the minimizer is guaranteed) and abandons any branch where the outcome is already worse than a known alternative. For tic-tac-toe, the complete game tree has 255,168 terminal nodes - alpha-beta typically reduces this to a few hundred evaluations per move.

The result: the AI is provably unbeatable. Every game against a perfect opponent ends in a draw if the human plays correctly, or in an AI win if the human makes any mistake.

Variants

  • 4×4 tic-tac-toe: requires 4 in a row; more complex with many more possible games.
  • 3D tic-tac-toe (4×4×4): played on a 4×4×4 grid; first player wins with optimal play.
  • Ultimate tic-tac-toe: a 3×3 grid of 3×3 boards; each small win claims a large board square; significantly more complex and strategic.