How to Win Mastermind in 5 Guesses or Fewer (Knuth's Strategy)

Mastermind
Mastermind - classic 4-peg code-breaking puzzle. Free, no signup.

With 6 colours and 4 pegs, standard Mastermind has 1,296 possible codes. Guessing randomly feels hopeless, yet you never need more than five guesses if you pick probes that split the remaining possibilities cleanly.

Why AABB beats ABCD as a first guess

Most players start with four different colours (ABCD). That feels informative, but it leaves a large worst-case cluster when you get only one or two pegs back. Starting with AABB shrinks the worst-case surviving set from 312 codes down to 256 immediately.

A practical human method (usually 5 to 6 guesses)

  1. Guess 1: play 1-1-2-2 (AABB).
  2. Guess 2: if you got 0 pegs, those two colours are dead โ€” play 3-3-4-4. If you got 1 or 2 pegs, test one kept colour alongside two new ones (such as 1-3-4-4).
  3. Once you know which 4 colours exist (total black + white pegs = 4), stop introducing new colours and focus strictly on swapping positions.
  4. Never waste a guess testing a colour that cannot increase your information; sometimes guessing an impossible code splits the remaining 6 legal codes in one shot.

Common questions

What is the best first guess in Mastermind?

1-1-2-2 (AABB) minimises the worst-case number of remaining codes to 256, guaranteeing a 5-guess win with optimal play.

What is the average number of guesses with optimal play?

About 4.34 guesses on average (4.48 under Knuth's minimax rule), with 5 as the strict worst case.

Can colours repeat in standard Mastermind?

Yes. Standard Mastermind allows duplicate colours (6^4 = 1,296 codes); disabling duplicates reduces the space to 360 codes.