How Many Guesses Does Code Breaker Need? Knuth, the Best Opening and Our Test
Short answer: With good play, Code Breaker takes about 4 guesses on Easy, 4.5 on Medium and 5.6 on Hard, far inside the 10 or 12 the game allows. We checked every possible secret code by computer. On Easy, simply guessing any code that could still be right is as good as Donald Knuth's famous method (4.13 guesses on average for both). On Medium, Knuth's method mainly protects you from bad luck: it never needs more than 5 guesses, while simple logic needs 6 or more in about one game in seven. The best opening is 1-2-3-4 on Easy and 1-1-2-2 on Medium.
- 2048: what the corner strategy is really worth
- Pairs: how many tries does a perfect memory need?
- Code Breaker: how many guesses does it take? (you are here)
Code Breaker is our version of Mastermind: the computer hides a code of coloured pegs, and after each guess you learn how many pegs have the right colour in the right place (black dots) and how many have a right colour in the wrong place (white dots). Our Code Breaker guide covers the rules. This article answers the next questions: how many guesses should a game take, which first guess is best, and how much does a clever method help compared with plain logic?
How many codes are there?

Our three levels use different codes:
| Level | Pegs | Colours | Repeats in the code | Possible codes | Guesses allowed |
|---|---|---|---|---|---|
| Easy | 4 | 6 | No | 360 | 10 |
| Medium | 4 | 6 | Yes | 1,296 | 10 |
| Hard | 5 | 8 | Yes | 32,768 | 12 |
Medium is the classic Mastermind setting. Each answer cuts the list of codes that are still possible, and the game is about making that list shrink as fast as possible. A guess can get at most 14 different answers, so two guesses can sort the 1,296 codes into at most 14 × 14 = 196 groups. That is too few to tell every code apart, so no method can be sure of winning by the third guess. Knuth showed that five guesses are always enough.
Two computer players
We wrote a program that loads the answer rule straight from our game and plays every possible secret code of Easy and Medium, and 3,000 random codes of Hard. It uses two players:
- The logical player follows the advice from our guide: every guess must be a code that could still be the secret, given all the answers so far. Among those, it picks one at random.
- The Knuth-style player uses the method Donald Knuth published in 1977. For every code it could guess, it works out how the possible answers would split the remaining codes, and it picks the guess whose worst answer leaves the fewest. It may even guess a code it knows is wrong, if that guess splits the rest better. On Hard, to keep the computing time reasonable, it chooses only among codes that could still be right, so there it is a restricted version of Knuth's method.
The results
| Level | Logical player: average | Logical player: worst | Knuth-style: average | Knuth-style: worst |
|---|---|---|---|---|
| Easy | 4.13 | 6 | 4.13 | 5 |
| Medium | 4.64 | 8 | 4.48 | 5 |
| Hard | 5.87 | 9 | 5.62 | 8 |
Easy: all 360 codes (the logical player 20 times each, because it chooses at random). Medium: all 1,296 codes (also 20 times each). Hard: the same 3,000 random codes for both.
Our Knuth player's Medium result, 4.476 guesses on average and never more than 5, is exactly the figure Knuth published, which is a good check that the program is right.
Three things stand out:
- On Easy, logic is all you need. When colours do not repeat, the logical player matches Knuth's average (4.13 guesses). It needed 6 guesses in 2.7% of games, where Knuth's never needed more than 5, but if you only ever guess codes that fit every earlier answer, you match the computer's average.
- On Medium, a clever method protects the worst case more than the average. Knuth's method gains only 0.16 guesses on average. But the logical player needed 6 guesses in 13% of games (about 1 in 8), 7 in about 1 game in 90, and 8 in 2 of 25,920 games. Knuth's player never needed more than 5.
- There is plenty of room. In our tests, a player who only guessed codes that could be right never needed more than 9 guesses on any level, and the game allows 10 or 12. Running out almost always means a guess broke an earlier answer.
The best first guess
The first guess is the only one you can plan in advance, because you know nothing yet. Here is how the most common openings split the possible codes. "Worst case" is the number of codes left after the least helpful answer; "on average" is how many are left after a typical answer.
| Opening | Easy: worst case | Easy: on average | Medium: worst case | Medium: on average |
|---|---|---|---|---|
| 1-1-1-1 | 240 | 200 | 625 | 512 |
| 1-1-1-2 | 96 | 74 | 317 | 236 |
| 1-1-2-2 | 96 | 72 | 256 | 205 |
| 1-1-2-3 | 102 | 64 | 276 | 185 |
| 1-2-3-4 | 88 | 65 | 312 | 188 |
The numbers explain the classic advice. On Medium, 1-1-2-2 is Knuth's opening because it has the best worst case: whatever the answer, at most 256 of the 1,296 codes remain. 1-1-2-3 leaves fewer codes on average, so it is the better choice if you care about the typical game rather than the unlucky one. 1-1-1-1 is the worst opening on every level: it can only tell you how many 1s there are.
On Easy, where the secret never repeats a colour, 1-2-3-4 is the best opening for the worst case (at most 88 codes left). 1-1-2-3 is very slightly better on average (64 against 65), so a guess with a repeated colour is not simply wasted, even though the code itself never repeats one. On Hard, of the patterns we tested, 1-1-2-3-4 was best on both measures: at most 7,051 of the 32,768 codes remain, and about 4,300 on average. Five different colours (1-2-3-4-5) is a close second for the worst case.
Why Knuth's idea works

The key idea is that a guess is not just an attempt to hit the code. It is a question, and a good question is one whose answers split the remaining possibilities into many small groups. That is why Knuth's player will sometimes deliberately play a code it knows is wrong: a wrong code that tests three uncertain colours at once can teach more than a right-looking code that tests only one.
Later researchers went further. In 1993 Kenji Koyama and Tony Lai used a full computer search to find the strategy with the lowest possible average for classic Mastermind: 5,625 guesses over all 1,296 codes, or 4.340 per code, compared with Knuth's 4.476. Their strategy needs six guesses for a single code. The best strategy that never needs more than five uses 5,626 guesses in total: the price of a guaranteed five is exactly one guess across all 1,296 codes. In 2006 Jeff Stuckman and Guo-Qiang Zhang showed that, for codes of any length, just deciding whether there is any code that fits all the answers so far is NP-complete, the same family of hard problems as large Sudoku grids (see Sudoku by the numbers).
What this means when you play

- Open with the right guess for the level: 1-2-3-4 on Easy, 1-1-2-2 (or 1-1-2-3) on Medium, 1-1-2-3-4 on Hard.
- Check every guess against every earlier row before you press Check. This one habit is what the logical player does, and on Easy it matches the computer's average. If the guess would not have produced the same dots as each earlier row, it cannot be the code.
- When a few codes are left, ask a question instead of guessing. Say only 1-2-3-4, 1-2-3-5 and 1-2-3-6 are possible. Guessing 1-2-3-4 might be right, but if it is not, the answer is three black for both of the others, and you may need two more guesses. Guess 1-2-5-4 instead, which cannot be the code: 1-2-3-4 answers three black, 1-2-3-5 two black and one white, 1-2-3-6 two black. All three answers differ, so your next guess always wins. The exact guess matters: 4-5-1-1, for example, gives the same answer for two of the three codes.
- Aim for four or five guesses. Your score is (guesses allowed − guesses used + 1) × 20, multiplied by 1.5 on Medium and 2.5 on Hard. Cracking an Easy code in 4 guesses gives 140 points; a Medium code in 5 gives 180; a Hard code in 6 gives 350.
Did you know?
- Mastermind was invented in 1970 by Mordecai Meirowitz, an Israeli postmaster and telecommunications expert. Several toy companies turned it down before the British company Invicta Plastics took it on.
- There are 15 combinations of black and white dots that add up to four or fewer, but no guess can ever get three black and one white: if three pegs are right and all four colours are in the code, the fourth peg must be right too. 1-1-2-2 also never gets one black and three white, because any rearrangement of 1, 1, 2, 2 puts an even number of pegs in the right place. That leaves it 13 possible answers.
This is the last part of our series. Start with Part 1: what the corner strategy in 2048 is really worth, or read how we tested how many Solitaire deals can be won.
