S ScienceFairProjects.info
Medium⏱ time2 hr$ cost0

Tic-Tac-Toe AI

Program an unbeatable opponent that plans ahead using the minimax algorithm.

Start building ↓
fig — working model Result first

The build

Step 01

Code the board

Represent the 3×3 game state.

Step 02

List moves

Generate all legal moves.

Step 03

Score with minimax

Recursively rate each move for both players.

Step 04

Play

The AI picks the best-scoring move.

Working Principle

Minimax looks ahead at every possible future board, assuming both players play their best, and chooses the move with the best worst-case result.

The science behind it

A closer look

It's adversarial search: the AI maximises its outcome while expecting the opponent to minimise it — the root of game AI.

Take it further

Variables to test

  • 1 Add alpha-beta pruning — how much faster?
  • 2 Try a 4×4 board — why does it slow down?

More AI