Kolton Ndoj

Minimax: how a chess computer thinks ahead

When you play chess against an app, or tic-tac-toe against a computer that never loses

What problem does it solve?

How should a computer pick a move when its opponent will always fight back?

The big idea

Imagine the tree of all futures: my moves, then your replies, then mine. At the bottom, score each position. Then reason backward: on my turn I pick the highest score, on yours I assume you pick the lowest. The value at the top is the best I can guarantee against perfect play. Alpha-beta pruning adds the key insight: once you've found that a move lets the opponent do something terrible to you, you don't need to know how terrible, so whole branches can be skipped.

How it works, step by step

  1. 1. Generate the moves available, then the replies, a few moves deep.
  2. 2. Score the positions at the bottom with an evaluation (material, safety…).
  3. 3. Pass scores upward: maximise on your turns, minimise on the opponent's.
  4. 4. Skip any branch that can't change the decision (alpha-beta pruning).
  5. 5. Play the move with the best backed-up score.

The key formula

V(s)={max⁡mV(sm)my turnmin⁡mV(sm)opponent’s turnV(s)=\begin{cases}\max_{m} V(s_m) & \text{my turn}\\ \min_{m} V(s_m) & \text{opponent's turn}\end{cases}
V(s)V(s)
the value of position s if both sides play perfectly from here
sms_m
the position after making move m

How fast is it?

Exponential: with about 35 moves per chess position, looking d moves deep means about 35^d positions. With good move ordering, alpha-beta roughly lets you search twice as deep for the same effort. Tic-tac-toe can be searched completely; chess can't.

Try it yourself

Fun fact

IBM's Deep Blue beat world champion Garry Kasparov in 1997 using alpha-beta search on custom chips that examined around 200 million positions per second. Today the top engine Stockfish still uses alpha-beta, but with a small neural network (NNUE, since 2020) to score positions, while DeepMind's AlphaZero (2017) used a different search, Monte Carlo tree search, guided by a neural network.

Related

Open it in the Math Lab →

Games · Intermediate