Logic & deduction · How to play

How to Play Mastermind and Crack the Code Logically

Mastermind board for worked game 1: five guess rows with black and white key pegs, ending with B Y O R scoring 4 black; the codes left fall from 256 to 9, 2 and 1

One player hides a four-peg color code and the other tries to copy it exactly: that's how to play Mastermind in a sentence, and Goliath's current box challenges the codebreaker to do it in 10 moves or fewer (Goliath). After every guess, the codemaker answers with key pegs: a black peg for each peg of the right color in the right hole, and a white peg for each right color sitting in the wrong hole (Knuth). Use those answers to rule codes out until only one is left.

With six colors, four holes and repeats allowed, there are 6 × 6 × 6 × 6 = 1,296 possible codes (Knuth). Goliath's box is for two players aged 8 and up and comes with 108 code pegs in six colors, 30 red and white key pegs, and a shield to hide the code (Goliath). Knuth's paper calls the two kinds of feedback black hits and white hits, and this guide uses his names. If your set uses other colors, check its rule sheet for which one marks an exact match.

Score one guess slowly before you play

Most Mastermind arguments start with feedback, so practice on a single guess. The secret code is blue, yellow, orange, red (B Y O R). The guess is blue, blue, orange, red (B B O R).

  1. Count blacks hole by hole. Hole 1: blue under blue, black. Hole 2: yellow under blue, nothing. Hole 3: orange under orange, black. Hole 4: red under red, black. That's 3 black.
  2. Count every color match, ignoring position. For each color, take the smaller of its count in the code and its count in the guess, then add them up; this is Knuth's counting rule (Knuth). Blue: 1 in the code, 2 in the guess, so 1. Orange: 1. Red: 1. Yellow: 1 in the code, 0 in the guess, so 0. Total: 3.
  3. Whites are the difference. 3 matches minus 3 black leaves 0 white.

The answer is 3 black, 0 white. The guess's second blue earns nothing, because the code's only blue was already paid for with a black peg.

Scoring the guess B B O R against the code B Y O R: 3 black by hole, a colour count totalling 3 matches, so 3 minus 3 leaves 0 white
Scoring one guess: 3 black, 0 white
What your guess hasKey peg
Right color in the right holeBlack
Right color in the wrong hole, matching a code peg that hasn't scored yetWhite
A color the code doesn't useNothing
More copies of a color than the code hasNothing for the extras
3 black and 1 whiteImpossible, so recount

Key pegs report counts, not positions (Knuth), and that missing information is the whole puzzle. The last row comes from Knuth's paper too, and the reason is simple: with three holes already exact, the only code peg left sits in the same hole as your fourth peg, so any match there would be black, never white.

How to play Mastermind: setup and turns

  1. Pick roles. You'll swap after every code, so it doesn't matter who starts.
  2. Hide the code. The codemaker sets four code pegs behind the shield. Colors may repeat, and Goliath's box also allows empty holes (Goliath); agree before the first round whether blanks are in play and whether a blank can earn a key peg.
  3. Guess. The codebreaker fills the next row with four pegs.
  4. Answer. The codemaker sets key pegs beside that row, counted as above, in any order.
  5. Repeat until the codebreaker earns 4 black or runs out of rows.
  6. Swap roles. To keep score without fuss, play an even number of codes and total the guesses each of you needed. Fewer wins.

Worked game 1: B Y O R in five guesses

Both games below were generated by a short program that follows Knuth's rule: always play the guess whose worst possible answer leaves the fewest codes standing (Knuth). When several guesses tie, the program can pick a different one from the guess printed in Knuth's paper, so treat these as Knuth-style games rather than copies of his table. Colors: R red, G green, B blue, Y yellow, O orange, W white.

GuessPegsAnswerCodes still possible
1R R G G0 black, 1 white256
2B B O R3 black, 0 white9
3B G B W1 black, 0 white2
4B O O R3 black, 0 white1
5B Y O R4 blackSolved

Guess 1, R R G G. Two pairs, the same shape as Knuth's recommended opener, 1122 (Knuth). One white and no black means the code holds exactly one peg that's red or green, and it isn't in a hole where you guessed that color. Of the 1,296 codes, 256 survive.

Guess 2, B B O R. Three blacks: three pegs are exact and one is wrong. Combine that with guess 1 and only nine codes fit:

Y B O R · O B O R · W B O R · B Y O R · B O O R · B W O R · B B B R · B B Y R · B B W R

All nine end in red. Guess 1 promised one red or green somewhere, and the only place it can go without spoiling guess 2's three blacks is hole 4, as the red. That also rules green out completely.

Guess 3, B G B W. This is the counterintuitive move: B G B W can't be the code, because green is already gone. It's still the best guess on the table, because of how it splits the nine survivors:

If the answer isCodes left
0 black, 1 whiteY B O R, O B O R
0 black, 2 whiteW B O R
1 black, 0 whiteB Y O R, B O O R
1 black, 1 whiteB W O R, B B Y R
1 black, 2 whiteB B W R
2 black, 0 whiteB B B R
Six possible answers to guess B G B W and the codes each leaves among the nine survivors, never more than two codes
How guess 3 splits the nine surviving codes

No answer leaves more than two codes. Try each of the nine possible codes as guess 3 instead and every one of them has some answer that leaves three. Green works here as a spacer: it can't score, so the other three pegs' feedback reads cleanly. Knuth points out a similar can't-win guess in his own example and calls it "a brilliant stroke" (Knuth).

Guesses 4 and 5. The answer, 1 black and 0 white, leaves B Y O R or B O O R. Guess one of them: B O O R scores 3 black, so the code must be the other, and B Y O R gets four blacks on guess 5.

Worked game 2: W G Y B in four guesses

GuessPegsAnswerCodes still possible
1R R G G0 black, 1 white256
2B B O R0 black, 1 white44
3G W B Y0 black, 4 white2
4W G Y B4 blackSolved

The third answer, four whites, does most of the work. It says the code is made of exactly green, white, blue and yellow, none of them in the hole guessed. Layer the earlier answers on top:

  • Green. Guess 1 put green in holes 3 and 4 without scoring a black, and guess 3 put it in hole 1. So green sits in hole 2.
  • The other three. White, blue and yellow fill holes 1, 3 and 4, with blue not in hole 3 and yellow not in hole 4 (guess 3 again). Three arrangements fit: W G Y B, B G Y W and Y G W B.
  • Blue. Guess 2 started with blue and scored no black, so the code doesn't start with blue. B G Y W is out.

Two codes remain, and the program's guess of W G Y B happens to be right. With two codes left, guessing either one is a coin flip to win now and a sure win on the next turn.

Why five guesses is always enough

Donald Knuth's paper "The Computer as Master Mind," in the Journal of Recreational Mathematics, vol. 9, no. 1 (1976–77), proves that with six colors and four holes "the codebreaker can always succeed in five moves or less" (Knuth). He built the strategy with a computer, opening with 1122 and choosing at every stage the guess that minimizes the maximum number of codes left. Two details from the paper are worth taking to the table:

  • The opener matters. Start with 1123 and there's one line of play that can't be finished in five; start with 1234 and it "appears to be impossible" to guarantee five (Knuth).
  • Worst case isn't average case. Knuth notes his strategy isn't the best possible for the expected number of moves, though he calls it "probably very close" (Knuth).

The same paper describes Master Mind as produced by Invicta Plastics, Ltd., and a hit in the 1975 Christmas season (Knuth). The Victoria and Albert Museum's catalogue credits Mordecai Meirowitz with inventing the game in 1970–71 and says Invicta bought the rights and released it in 1971–72 (V&A).

A strategy you can run in your head

You won't replay a computer's tree at the table, but its logic turns into four habits:

  1. Open with two pairs, like R R G G, the shape of Knuth's 1122.
  2. Test each guess against every earlier answer. A guess that contradicts feedback you already have can't be the code. Play one only on purpose, the way guess 3 did above.
  3. Spend a dead color as a spacer. Once a color is ruled out, it can't earn a key peg, which makes the rest of the row easier to read.
  4. List the survivors when they get few. Once you're down to a handful of codes, write them out and pick the guess that splits them into the smallest groups.

Feedback mistakes that wreck a round

  • Paying twice for one code peg. Each code peg earns at most one key peg, so extra copies in a guess score nothing.
  • Giving a white to a peg that already earned black. Count blacks first, then work out whites from the total.
  • Lining key pegs up with the holes. Feedback is a count, not a map; matching positions gives away information the rules don't.
  • Skipping the blank-hole conversation. If you allow empty holes, settle how they're scored before the first code.
  • Arguing an impossible answer. If you ever see 3 black and 1 white, the codemaker miscounted. Recount together.

Mastermind questions people ask

Is there a Mastermind for kids? Goliath's Mastermind for Kids is for two players aged 6 and up, with 72 animal pegs in six colors, red and white scoring pegs, and three levels of play (Goliath). The standard game is listed for ages 8 and up (Goliath).

Can you play Mastermind on paper? Yes. Knuth wrote codes as the digits 1 to 6 (Knuth), and that's all you need: write the secret on a folded slip, write each guess as four digits, and answer with a tally of B and W. Allow a blank as a seventh symbol and the count rises from 1,296 to 7 × 7 × 7 × 7 = 2,401 codes, which squares with Goliath's "more than 2,000 possible codes" (Goliath).

What games are like Mastermind? Anything where each answer shrinks a list of possibilities. Logic grid puzzles run the same elimination on paper with no opponent, Clue's notes sheet applies it to hidden cards, and our roundup of deduction board games has more. If what you like is the head-to-head part, browse our 2-player board game picks.

By the Golden Lab Games team. Every feedback score in both worked games can be rechecked with the counting rule in the first section.

Sources