skip to content
Peter Chiang

Mastermind in 5 guesses or less

/ 2 min read

Table of Contents

The Game

Mastermind is a code-breaking game from 1970. There exists a perfect strategy that solves it in at most 5 guesses. This page shows you how — by playing it first, then taking the algorithm apart.

The Rules

One player sets a secret code — a row of 4 pegs, each chosen from 6 colors, with repetition allowed.

That gives 64 = 1,296 possible codes.

The guesser submits a code and gets feedback as colored pegs: a black peg for every position that is the right color and the right place; a white peg for every peg that is the right color but in the wrong place. The counts are given but not which position matched.

Try it — click any peg to change it and see the feedback update live.

Your Guess

Secret Code

1 black peg  ·  1 white peg

Black peg = right color, right position.

White peg = right color, wrong position.

Click any peg above to change it.

Play: You’re the Code Breaker

The computer has chosen a secret code. You have 10 guesses to find it. Click a peg to select it, then pick a color from the palette that appears. Hit Submit when all four are filled.

1

Click a peg to pick its color.

10 guesses remaining

What Makes a Good Guess?

You might have felt that some guesses were better than others. The key is how much information a guess gives you, regardless of what the response turns out to be.

Imagine it's your first guess and all 1,296 codes are still possible. Which of these two guesses would you choose?

Choose one to see the analysis.

Play: You’re the Code Maker

Now you set the secret. The AI will use an efficient strategy to crack it, one guess at a time. Watch the possible codes shrink with each step — it never needs more than 5.

Set a secret code. The AI will try to crack it using the minimax strategy.

The Search Space

Before the first guess, every one of these 1,296 codes is a valid candidate. The algorithm’s only job is to eliminate them as fast as possible.

Each dot below represents one possible secret code. There are exactly 6 × 6 × 6 × 6 = 1,296 of them — one for each way to arrange 4 pegs with 6 colors (repetition allowed). The color of each dot shows the first peg of that code.

Every single one of these is a valid guess — and a valid secret. The AI has to narrow them down to exactly 1 to win.

A Guess Splits the Possibilities

Each guess divides the remaining codes into groups — one group per possible response. More groups, and smaller ones, means the guess is more informative.

Pick any guess below. Every one of the 1,296 codes will be placed into a group based on what feedback that guess would produce against it. More, smaller groups = better guess.

Guess:
1B0W
256 codes
0B1W
256 codes
0B0W
256 codes
1B1W
208 codes
2B0W
114 codes
0B2W
96 codes
1B2W
36 codes
2B1W
32 codes
3B0W
20 codes
0B3W
16 codes
2B2W
4 codes
4B0W
1 codes
0B4W
1 codes

13 distinct responses for this guess. The largest group has 256 codes.

The Worst Case

We don’t control which response we get. So what matters is the biggest group — the number of codes we could still be left with even if we’re unlucky. A good guess keeps that small.

For any guess, the worst case is the response that leaves the most codes still possible — the biggest group. We want to minimise that number.

Custom:
1B0W
256 ← worst case
0B1W
256
0B0W
256
1B1W
208
2B0W
114
0B2W
96
1B2W
36
2B1W
32
3B0W
20
0B3W
16
2B2W
4
4B0W
1
0B4W
1
Worst case for this guess: 256 codes remain

Minimax: Always Pick the Best Worst Case

Minimax is the complete algorithm: at every turn, choose the guess whose largest remaining group is as small as possible. Applied consistently, this guarantees a solution in 5 guesses or fewer.

The minimax algorithm picks the guess with the smallest worst-case partition. Here are a selection of first guesses and their worst-case sizes. The best ones are highlighted.

GuessWorst-case group size
1122
256
✓ optimal
1123
276
1134
276
1223
276
1234
312
1235
312
1236
312
1245
312
1112
317
1111
625

Several first guesses tie at a worst-case of 256 codes. 1122 is the conventional choice — it was the one Donald Knuth used in his 1977 paper, and it's lexicographically small among the optimal openers.

Applying this principle at every step — always guessing to minimise the worst-case remaining codes — guarantees solving any Mastermind game in 5 guesses or fewer.

This is the core of Knuth's algorithm. The full decision tree has 1,296 leaves and never goes deeper than 5 levels.