Skip to content
اردو

Strategy

How to win Mastermind: strategy and Knuth's five-guess algorithm explained

Mastermind looks like luck, but the classic game can be won in five guesses or fewer every time. This guide explains the strategy that gets you there: a good opening, consistent guesses, and Knuth's minimax method, step by step and in plain words.

5 min readUpdated By PlayNest team

The numbers behind Mastermind

The classic Mastermind code has 4 holes and 6 colours, and a colour may repeat. That gives 6 x 6 x 6 x 6 = 1,296 possible codes. You have ten guesses (our game also offers bigger variants), and after each guess you see black pegs for the right colour in the right place and white pegs for the right colour in the wrong place. If you want to try everything below as you read, you can play Mastermind online free.

A guess can get 14 different answers (black and white counts that make sense together), and 1,296 divided by 14 is under a hundred. That is the key idea: if each guess cuts the remaining codes into much smaller groups, a handful of guesses is enough to reach a single code.

How the pegs are counted

Count blacks first. Then, among the pegs that were not black, count how many colours the guess and the code share, taking each colour only as many times as both of them have it. Those are the whites. With code red red blue green and guess red red red red, you get two blacks and no whites, because the code has no more reds.

This counting rule matters for strategy: a guess with a repeated colour is a clean way to learn how many of that colour the code has.

The best first guess

Open with two colours in pairs, for example red red blue blue (written 1122). The thirteen possible answers split the 1,296 codes into groups: 256 codes get no pegs at all, 256 get one white, 256 get one black, 208 get one black and one white, 114 get two blacks, and so on down to a single code for four blacks. The biggest group has 256 codes, and that is the smallest biggest-group any first guess can achieve. A four-different-colours opening is worse: its biggest group is 312.

Tip

Open with a pair-and-pair guess like 1122. It is the opening Donald Knuth used and the one our computer plays.

Consistent guesses: the strategy anyone can use

After the first answer, always guess a code that could still be the secret given every clue so far. Check it against your earlier guesses: if the code were the secret, would each earlier guess have scored exactly what it scored? If yes, the guess is consistent.

Consistent guessing sounds simple, and it is surprisingly strong. It wins most games within five or six guesses and each guess has a chance of being right. The Hint button in the game picks a consistent guess for you; the trick is to understand why it fits.

  • Write down each guess and its black and white count.
  • For a new guess, check it against all earlier guesses.
  • If it would not have produced the same scores, it cannot be the code. Change it.

Knuth's five-guess algorithm in four steps

In 1977 Donald Knuth published a method that guarantees a win in five guesses or fewer on the classic game. It is a computer method, but the idea fits in four lines.

  • Guess 1122.
  • Keep only the codes that would have produced the same answer.
  • For each possible next guess, count how many codes would remain for every possible answer and note the biggest group. That is the worst case.
  • Play the guess whose worst case is smallest. If several tie, prefer one that could be the code. Repeat from step 2.

Why it takes at most five guesses

Guess 1122 leaves at most 256 codes. The second guess, chosen to minimise the worst case, leaves at most a few dozen. The third leaves a handful, the fourth leaves one or two, and the fifth names the code. Run the algorithm on all 1,296 codes and the totals are: 1 code found on guess 1, 6 on guess 2, 62 on guess 3, 533 on guess 4 and 694 on guess 5. The average is about 4.48 and the maximum is 5. We run this check on our own code, so the number is not a claim but a test that passes.

You can watch it live: in the game choose Computer cracks yours, make a code, and press Next guess. A panel under the board shows how many codes still fit and how small the worst case becomes.

Bigger games and Bulls and Cows

With 5 pegs and 8 colours there are 32,768 codes. Checking every possible guess against every remaining code gets slow, so the computer looks at a sample of the remaining codes instead. It is still strong, but there is no guarantee of five. Turning repeats off or using blanks changes the numbers: without repeats there are only 360 codes, with blanks 2,401.

Bulls and Cows is the number version: a code of four different digits has 10 x 9 x 8 x 7 = 5,040 possibilities. A bull is a right digit in the right place and a cow is a right digit in the wrong place. With perfect play it can always be solved in seven guesses or fewer, and the average is just over five. The same ideas work: open with four different digits such as 1234, keep a list of what is still possible, and play consistent guesses. Start with Mastermind or Bulls and Cows and compare how your counts change.

Mastermind and other logic puzzles

Mastermind rewards the same habit as nonograms: read each clue for what it rules out, not just what it says. It also shares a feel with tic tac toe strategy, where thinking about the opponent's best reply beats hoping for luck. The minimax idea at the heart of Knuth's algorithm, minimising your worst case, is the same one used in game-playing programs for chess and many other games.

Questions people ask

What is the best first guess in Mastermind?
Two colours in pairs, such as red red blue blue. It leaves at most 256 of the 1,296 codes, the best any opening can do in the worst case.
Is it true Mastermind can always be solved in five guesses?
Yes for the classic 4 peg, 6 colour game with repeats allowed. Knuth's algorithm cracks every one of the 1,296 codes in five guesses or fewer.
What is a consistent guess?
A guess that could still be the secret code given all the clues so far. If it were the secret, every earlier guess would have scored exactly what it scored.
How is Bulls and Cows different?
It uses digits instead of colours, with four different digits in the code. A bull is a right digit in the right place and a cow is a right digit in the wrong place.
All guides