Free

Tic-Tac-Toe with AI

An opponent that plays perfectly, using minimax with alpha-beta pruning.

  • JavaScript
  • Minimax
  • HTML

About this project

Tic-tac-toe against an opponent that cannot be beaten. Not by rules like "take the centre" — it plays out every remaining game and picks the move with the best guaranteed outcome. From an empty board that is 255,168 possible games, which a browser explores in a couple of milliseconds.

Most versions of this project use heuristics, which are fiddly and usually beatable. This one is a genuine minimax implementation with alpha-beta pruning, and the README explains both well enough to defend in a viva. Deleting the two pruning lines leaves the AI playing identically but slower, which is the clearest way to feel what pruning actually does.

What you will learn

  • Implement minimax: choose the move that maximises your score, assuming the opponent minimises it
  • Understand why a depth term makes an AI win promptly and lose slowly
  • Add alpha-beta pruning and measure how many positions it removes
  • Search with make-and-undo on one board instead of copying at every node
  • Model difficulty as how often a perfect player makes a mistake, rather than as a worse evaluation

Before you start

  • JavaScript basics — arrays, functions, and what recursion is
  • A browser and a text editor. Nothing to install.

New to JavaScript? The free JavaScript course covers everything this project assumes.

Academic integrity notice

These projects are provided for learning, experimentation and reference. Understand the code, modify it for your own requirements, and acknowledge external work according to your institution’s academic policies. Priodemy does not supply work to be submitted as your own.