Mastermind Strategy: How to Crack Number-Guessing Puzzles
How to solve Mastermind, Bulls and Cows and other code-breaking puzzles in fewer guesses, from your first guess to reading "right spot, wrong spot" clues.
The best Mastermind strategy is to treat every guess as an experiment: use your early guesses to find out which colours or digits are in the code, then work out their positions, and from then on only make guesses that fit every clue you've had so far. Done carefully, this solves the classic four-peg, six-colour game in five or six guesses, and Donald Knuth showed in 1976 that a computer can always do it in five or fewer. The same approach works for Bulls and Cows and other number-guessing puzzles. Here's how to read the clues and what to guess.
I built two code-breaking games into Habz, the daily Numdler and the Combination Lock practice game, so this is a topic I've spent a lot of happy hours on.
How Mastermind and Bulls and Cows work
Mastermind was invented in 1970 by Mordecai Meirowitz and released commercially in the early 1970s. One player sets a secret code of four pegs chosen from six colours (colours can repeat). The other player guesses, and after each guess gets feedback:
- A coloured (black or red) key peg for each peg that's the right colour in the right spot.
- A white key peg for each peg that's the right colour in the wrong spot.
The key detail is that the feedback doesn't tell you which pegs it refers to. One black and one white means "something is right and something else is the right colour", and it's your job to work out what.
Bulls and Cows is the older pencil-and-paper version, usually played with a four-digit number in which all the digits are different. A "bull" is a right digit in the right place, a "cow" is a right digit in the wrong place. It's the same idea with numbers, and it was one of the earliest computer games: a version called MOO ran at the Cambridge Computer Laboratory before the summer of 1970.
Wordle, which you may know better, is a close cousin. The big difference is that Wordle tells you exactly which letters are green or yellow, which makes it much more forgiving.
How many guesses does Mastermind need?
With four pegs and six colours there are 6 × 6 × 6 × 6 = 1,296 possible codes. That sounds like a lot, but each guess can rule out a big chunk of them.
- Knuth's five-guess result. In 1976, the computer scientist Donald Knuth showed that the codebreaker can always solve the classic game in five moves or fewer. His method starts with the guess 1122 (two pegs of one colour, two of another), and after each answer picks the guess that leaves the fewest possible codes in the worst case.
- The average. Later work by Koyama and Lai found that the best possible method averages about 4.34 guesses for a random code.
- Bulls and Cows with four different digits has 5,040 possible numbers, and any of them can be found in at most seven guesses, with an average of about 5.21 for perfect play.
Those are computer-perfect numbers. A good human player will usually take a guess or two more, and that's fine.
A practical Mastermind strategy for humans
Knuth's method involves checking every possible code after every guess, which isn't much fun with a pencil. This simpler approach gets you most of the way there.
- Find the colours first. Start with something like 1122, then 3344, then 5566 (using numbers for colours). Add up the black and white pegs from each guess and you know how many of each pair is in the code. Often you won't need the third guess, because the first two already account for all four pegs.
- Then find the positions. Once you know which colours you're working with, rearrange them and watch how the black pegs change. If moving one colour turns a white into a black, you've learned where it goes.
- Only guess codes that could be right. This is the single most useful rule. Before each guess, check it against every clue so far. If a guess contradicts an earlier answer, it can't be the code, so it's wasting a turn.
- Change one or two things at a time when you're close. If you swap two pegs and the score changes, you know the change was caused by those two.
Reading "right spot / wrong spot" clues
Getting good at code-breaking is mostly about squeezing every drop out of each clue. A few patterns come up all the time:
- Zero pegs is great news. If a guess gets nothing back, every colour or digit in it is out. That's often the most informative answer you can get.
- Total pegs tells you "what", the split tells you "where". Add black and white together to know how many of your colours are in the code. Then the number of blacks tells you how many are already in place.
- All white means every position is wrong. If you get four whites, you have the right colours and none is in the right spot. That rules out a lot of arrangements at once.
- Compare guesses that differ by one peg. If 1234 scores one black and 1235 scores two blacks, the 5 is right in the last spot (and the 4 isn't).
- Write it down. Even a quick table of "colour: in or out, positions ruled out" stops you from forgetting a clue three guesses later.
Bulls and Cows strategy
Because the digits in Bulls and Cows are all different, there's a neat opening. Guess 1234, then 5678. Add up the bulls and cows from each. Whatever is missing from four must come from 9 and 0. For example, if 1234 gives two hits and 5678 gives one, then exactly one of 9 and 0 is in the number.
After that, it's the same process as Mastermind: narrow down which digits from each group are in, then shuffle them into position, and never make a guess that contradicts an earlier answer. Swapping a single digit between guesses is slow but clear, because any change in the score must come from that digit.
Number-guessing puzzles in Habz: Numdler and Combination Lock
Habz has two code-breaking games, and they use different kinds of clues, which makes them good practice for different skills.
Numdler (daily)
Numdler is a daily five-digit number with six tries, the same for everyone. It uses Wordle-style colours on each digit: green for the right digit in the right place, yellow for a digit that's in the number somewhere else, dark for a digit that isn't there. Digits can repeat. Because the feedback is per digit, it's more forgiving than Mastermind, but six tries for five digits is still tight.
A strategy I like: open with 12345, then 67890. Between them, those two guesses test all ten digits, so after two tries you know exactly which digits are in the number. If fewer than five digits light up across both guesses, one or more of them must repeat. Then spend the remaining four tries placing them, using the yellows to rule out positions. The number comes from a public randomness beacon, so there's no pattern to spot, only logic. I've explained how the daily puzzles work in more detail.
Combination Lock (practice game)
Combination Lock unlocks at level 14 and gets harder as you go, which makes it a sort of course in code-breaking:
- Early levels give you higher/lower arrows on each wheel, so it's like a quick binary search.
- Then green and yellow lights, like Wordle or Numdler.
- Later levels give you only "right spot / wrong spot" counts, Mastermind-style, plus written clues to reason with.
Locks range from two to five wheels, and you're against the clock, so the "only guess codes that could be right" rule really earns its keep. The other games are listed on the games page, and I've written about what each Habz game trains.
How it compares to Wordle strategy
The thinking is the same: spend early guesses on information, then switch to trying to win. The difference is that Wordle's letters aren't equally likely (E and S turn up far more than Q), so a good opener uses common letters, while in Mastermind and Bulls and Cows every colour or digit is equally likely, so coverage is what matters. If you play both, my Wordle strategy and best starting words guide covers the word side, and there are more puzzles like these in logic puzzle games without ads. If you like the numbers side in particular, have a look at mental maths games.
Common mistakes
- Guessing the same colour in a spot that's already been ruled out. Track your "not here" positions.
- Changing too much at once late in the game, so you can't tell which change mattered.
- Forgetting about repeats in Mastermind and Numdler. Two of the same colour or digit is allowed, and it's often the answer when the totals don't add up.
- Going for the win too early. A guess that can't be right but splits the possibilities nicely is sometimes worth more than a hopeful stab.
Code-breaking puzzles reward patience more than cleverness. Find the pieces, then find their places, and never ignore a clue you've already been given. Try it on a pencil-and-paper game of Bulls and Cows with a friend, or on today's Numdler, and you'll notice your guesses getting more deliberate within a week.
Frequently asked questions
What is the best first guess in Mastermind?
For the classic four-peg, six-colour game, Donald Knuth's method starts with 1122: two pegs of one colour and two of another. For a human player, guesses like 1122 then 3344 are a simple way to find out which colours are in the code.
Can Mastermind always be solved in five guesses?
Yes, for the classic four pegs and six colours. Donald Knuth showed in 1976 that a codebreaker can always find the code in five guesses or fewer with the right strategy.
What is the difference between bulls and cows?
A bull is a correct digit in the correct position, and a cow is a correct digit in the wrong position. In the usual game the secret number has four different digits.
How many guesses does it take to solve Bulls and Cows?
With four different digits, any number can be found in at most seven guesses with perfect play, and the average is about 5.21. Most people take a few more.
Is there a free Mastermind game for my phone?
Habz is free with no ads and has two code-breaking games: Numdler, a daily five-digit number puzzle, and Combination Lock, which works up to Mastermind-style right spot and wrong spot clues.