Mastermind Strategy: How to Crack Any Code in Fewer Guesses

Mastermind is a code-breaking game: one player hides a code, the other guesses, and every guess earns feedback on how close it was. The board game with coloured pegs was invented by Mordecai Meirowitz and published in 1970, but the pencil-and-paper version with digits, Bulls and Cows, is much older. The good news is that Mastermind is pure logic. With a plan for your first two guesses and a habit of checking each new guess against all the feedback so far, you will crack codes in far fewer moves. Practise every tip below in our free Codebreaker, which uses digits instead of pegs.

The rules and the feedback

A secret code is chosen: a row of digits or colours.

You make a guess of the same length.

Feedback tells you how many are correct and in the right place (black pegs in the board game, "bulls" in the paper game).

It also tells you how many are correct but in the wrong place (white pegs, or "cows").

The feedback never says which positions are right. Working that out is the game.

A common beginner mistake is to count a digit twice. If the code is 1 2 3 4 and you guess 1 1 5 6, you get one exact match, not one exact and one misplaced, because the code only has one 1.

How many possible codes are there?

Every guess is a way of splitting the remaining possibilities. Knowing how many there are tells you how much each guess must achieve.

In 1977 Donald Knuth proved that every one of the 1,296 classic Mastermind codes can be solved in five guesses or fewer, starting with a guess shaped like 1 1 2 2. Later computer searches found that the best possible average is about 4.3 guesses. You do not need a computer to get close: you need good openings and consistent guesses.

ModeCodeRepeats allowedNo repeats
Codebreaker Easy3 digits from 1 to 6216120
Classic Mastermind4 pegs from 6 colours1,296360
Codebreaker Medium4 digits from 1 to 84,0961,680
Codebreaker Hard5 digits from 1 to 959,04915,120
Codebreaker Extreme6 digits from 1 to 9531,44160,480

Strong opening guesses

Classic 4-from-6 with repeats: open with a pair-pair guess such as 1 1 2 2, as Knuth's strategy does. It tests two symbols and their positions at once.

Codebreaker Medium (4 digits from 1 to 8): open with 1 2 3 4, then 5 6 7 8. Between them, these two guesses account for every digit, so you know exactly how many of each half are in the code.

No-repeats modes: use guesses with all-different digits. In Medium, if 1 2 3 4 scores 3 in total, then exactly one of 5 6 7 8 is in the code.

Long codes (Hard and Extreme): spend the first two or three guesses sweeping the digits in blocks. Counting first and placing later is faster than trying to place too early.

The one rule that matters most: stay consistent

After the openings, only make guesses that could still be the answer. Before you enter a guess, check it against every earlier row: if this guess were the secret, would each earlier guess have scored exactly what it did? If not, the guess cannot be right, and playing it wastes a move.

This single habit, sometimes called playing a consistent guess, takes most players from eight or nine guesses down to five or six. It is slow at first and becomes quick with practice.

A worked example

Codebreaker Medium, no repeats, secret code 6 2 8 1 (hidden from you).

Notice guess 3. It moves every digit to a new position, so its score tells you about identity and position at once. Moving digits to fresh positions is the most useful idea in the whole game. Guess 4 then follows the consistency rule, and that is what cuts 22 possibilities down to 2.

GuessExactMisplacedWhat you learn
1 2 3 411Two of the digits 1 to 4 are in the code, so the other two must come from 5 to 8.
5 6 7 802Confirmed: two of 5 to 8 are in the code, and none of them sits where it was tried.
2 1 6 503Every digit moved to a fresh position. Three of 2, 1, 6 and 5 are in the code, none where tried. Only 22 codes now fit all three rows.
8 2 5 121A guess that fits every earlier row. Just two codes remain possible: 6 2 8 1 and 8 2 1 6.
6 2 8 140Solved, on what had become a 50:50 between the last two codes.

Common mistakes

Repeating a digit in a position where an earlier guess already ruled it out.

Changing one digit at a time. It is safe but slow; change two or three and read the difference.

Forgetting that a total of 0 is great news: every digit in that guess is eliminated everywhere.

Guessing too early with lots of possibilities left. Use one more information guess instead.

Games that use the same logic

Codebreaker: Mastermind with digits, four difficulty levels, with or without repeats.

Safe Cracker: feedback tells you how close each digit is, a gentler version of the same deduction.

Number Guess: higher-or-lower feedback, the simplest form of splitting possibilities in half.

Logic Grid: pure deduction from clues, no guessing at all.

For more deduction games, read our roundup of pattern and codebreaker games and the logic puzzle tips guide.

Sources

Knuth, D. E. (1976–77). The computer as Master Mind. Journal of Recreational Mathematics, 9(1).

Koyama, K., & Lai, T. W. (1993). An optimal Mastermind strategy. Journal of Recreational Mathematics, 25(4).

Frequently asked questions

What is the best first guess in Mastermind?

For classic Mastermind (4 pegs, 6 colours) a pair-pair guess such as 1 1 2 2 is the well-known strong opening from Knuth's strategy. For digit versions without repeats, sweep the digits in blocks, such as 1 2 3 4 then 5 6 7 8.

Can every Mastermind code be solved in five guesses?

Yes, for the classic 4-peg, 6-colour game. Donald Knuth proved in 1977 that a careful strategy solves every code in five guesses or fewer.

What is the difference between Mastermind and Bulls and Cows?

They use the same feedback. Bulls and Cows is the older pencil-and-paper game with digits; Mastermind is the 1970 board game with coloured pegs.

What do black and white pegs mean?

A black peg means a correct colour in the correct position. A white peg means a correct colour in the wrong position. The pegs never tell you which positions they refer to.

How do I get better at Mastermind?

Use strong opening guesses, then only play guesses that fit all the feedback so far, and move digits to new positions so each guess tests identity and position at once.

Open with guesses that cover the most digits, then only play codes that fit every row of feedback, and move digits to fresh positions each time. That is all it takes to start cracking codes in five or six guesses. Put it into practice in Codebreaker, starting on Medium.