Puzzle guide

How to Play Mastermind: Rules, Strategy and Solver Logic

To play Mastermind, one player hides a secret code of four colored pegs and the other tries to guess it. After every guess, black pegs show colors in the right spot and white pegs show right colors in the wrong spot, and you use those clues to crack the code in as few guesses as possible.

This guide explains the rules, works through a real game guess by guess and shows the deduction method computer solvers use, so you can beat the code every time.

Mastermind code breaking board with rows of colored pegs and black and white feedback pegs Mastermind game previewPlay Mastermind freeClimb 10 solo stages or race a friend to crack a code. Free in your browser, no download.Play now

Quick summary

  • One player hides a secret code of colored pegs. The other guesses, and after each guess gets feedback pegs.
  • A black peg means right color in the right spot. A white peg means right color in the wrong spot. Pegs never say which position they belong to.
  • Best method: only play guesses that fit every clue so far, and prefer guesses that split the remaining codes most evenly.
  • Donald Knuth proved the classic game (6 colors, 4 holes, repeats allowed) can always be solved in 5 guesses or fewer.

What is Mastermind?

Mastermind is a code breaking game for two players, first sold as a board game in the early 1970s. One player is the codemaker and the other is the codebreaker. It is a pure logic game: there is no luck in the feedback, only in your guesses, so a careful player wins far more often than a lucky one. It belongs to the same family as pencil and paper games like Bulls and Cows.

Mastermind game rules

Classic board game rules

  1. The codemaker secretly picks a code of four colored pegs from six colors. In the classic game a color may appear more than once, which gives 6 ร— 6 ร— 6 ร— 6 = 1,296 possible codes.
  2. The codebreaker places a guess of four pegs in the next row of the board.
  3. The codemaker answers with up to four small key pegs: one black for each peg that is the right color in the right hole, and one white for each peg that is the right color in the wrong hole.
  4. The codebreaker keeps guessing until the answer is four black pegs or the rows run out. Boards usually have 10 or 12 rows.

Players usually swap roles and play an even number of rounds. The codemaker scores a point for every guess the codebreaker needs.

How our online version works

In Mastermind on OnlineGames2Play the computer is the codemaker. The code is four different colors chosen from red, orange, yellow, green, blue and pink, and your guesses also use four different colors. Order matters, so there are 6 ร— 5 ร— 4 ร— 3 = 360 possible codes. Every peg also carries a small symbol, so color blind players can tell the colors apart.

FeatureClassic board gameOnlineGames2Play
Colors and holes6 colors, 4 holes6 colors, 4 holes
Repeated colorsAllowedNot allowed
Possible codes1,296360
Guesses allowedUsually 10 or 1212 down to 6 in solo stages, 8, 10 or 12 in races

How to read the black and white pegs

The feedback is a count, not a map. If you get one black and two white pegs, you know three of your colors are in the code and exactly one of them is in its correct hole, but not which one. That ambiguity is the whole puzzle.

Two quick rules help a lot:

A worked example, guess by guess

Here is a real game from our no repeat version. The secret code is Blue, Red, Pink, Yellow.

Guess 1: Red, Orange, Yellow, Green. Feedback: 0 black, 2 white. Only two of these four colors are in the code, so the two colors we left out, Blue and Pink, must both be in it. Zero black also tells us Red is not first and Yellow is not third. That one guess cuts 360 codes down to 84.

Guess 2: Yellow, Red, Blue, Pink. We include Blue and Pink and test Yellow and Red. Feedback: 1 black, 3 white. Four pegs in total means the colors are exactly Yellow, Red, Blue and Pink. Only five orders still fit both clues:

Guess 3: Pink, Yellow, Blue, Red. We choose a guess that splits those five codes into small groups. Feedback: 0 black, 4 white. Only two of the five codes give that exact answer: Yellow, Pink, Red, Blue and Blue, Red, Pink, Yellow.

Guess 4: Blue, Red, Pink, Yellow. Four black pegs. Cracked in four guesses. Had it been the other code, guess five would have finished the job, which is still a perfect game.

Mastermind strategy for humans

1. Learn the colors first

In our version, your first guess always tests four of the six colors, so the total number of pegs tells you right away how many of them are in the code. Four pegs means you already know every color. Two pegs means the two colors you left out are both in the code. Three pegs means exactly one of the two left out colors is in.

2. Only play guesses that could be the answer

Before you press Check guess, compare your new row with every earlier row and ask whether it would have produced the same pegs. If not, it cannot be the code, so do not waste a row on it. This one habit is the biggest difference between beginners and strong players.

3. Change one thing at a time when unsure

If you cannot see which peg earned a black, swap just two pegs. If a black turns white, one of those two was in its right place.

4. Use zero black results

A row with no black pegs is gold. Every color in it is banned from that exact hole, which cuts the possible orders sharply.

How a Mastermind solver works

Computer solvers use exactly the idea from tip 2, done perfectly:

  1. Start with a list of every possible code.
  2. Make a guess and read the pegs.
  3. Delete every code that would not have given the same pegs for that guess.
  4. For each possible next guess, work out how it would split the remaining codes into groups by feedback. Pick the guess whose biggest group is smallest.
  5. Repeat until one code is left.

This is the minimax method published by computer scientist Donald Knuth in 1977. For the classic game with 1,296 codes, he showed it always wins in five guesses or fewer, starting with two pairs of colors such as red, red, orange, orange, and averages about 4.48 guesses. Later research found strategies with a slightly lower average of about 4.34, but none can guarantee four guesses for every code.

We ran the same method on our no repeat version. It cracked all 360 codes in five guesses or fewer, averaging about 4.14. A simpler strategy that always plays the first code still possible averaged about 4.15 but sometimes needed six. So if you follow tip 2 you are already playing almost like a computer.

Play Mastermind online: solo and with a friend

Solo stages

Solo play is a ladder of 10 stages with 12, 11, 10, 10, 9, 9, 8, 7, 7 and 6 guesses. Each unused guess is worth 1,000 points, and cracking a code on your very last guess still earns 500. Run out of guesses and the code is revealed and you start again at stage 1. Once per stage you can use a hint that reveals one peg in its correct hole, at a cost of 2,000 points. Your best stage and best run score are saved on your device.

Race a friend

Choose Race a friend, create a room and share the six letter code or invite link. The host picks 8, 10 or 12 guesses. You both play at the same time, but each of you cracks a different code made by the other player's device and locked with a cryptographic fingerprint, so nobody can change it halfway. Fewer guesses wins, and if you need the same number, the faster player wins.

Enjoy logic games? Try Sudoku or SET next, or browse all our free puzzle games.

Frequently asked questions

What do the black and white pegs mean in Mastermind?

A black peg means one of your colors is correct and in the correct position. A white peg means one of your colors is in the code but in a different position. A color that is not in the code gets no peg. The pegs never tell you which position they refer to.

Can colors repeat in Mastermind?

In the classic board game, yes, a code can use the same color more than once. In the OnlineGames2Play version the code always has four different colors from six, and every guess must use four different colors too.

What is the best first guess in Mastermind?

For the classic game with repeats, Knuth's solver opens with two pairs of colors, such as red red orange orange. In our no repeat version any four colors work equally well, because the result always tells you how many of those four are in the code.

How many guesses does it take to solve Mastermind?

With perfect play the classic game never needs more than five guesses, and the average is about four and a half. Our no repeat version has 360 possible codes, and a good strategy also cracks every one of them in five guesses or fewer.

How does a Mastermind solver work?

It keeps a list of every code that is still possible. After each guess it removes every code that would not have produced the same pegs. It then picks the next guess that leaves the fewest possible codes in the worst case, which is the method Donald Knuth published in 1977.

How do I play Mastermind online with a friend?

Open Mastermind on OnlineGames2Play, choose Race a friend and create a room. Share the six letter room code or invite link. The host picks 8, 10 or 12 guesses, then you both crack a secret code at the same time. Fewer guesses wins, and equal guesses go to the faster player.

Looking for more? Browse all our puzzle games, read more guides on the OnlineGames2Play blog or head back to the free online games homepage.

Mastermind game previewReady? Play Mastermind free nowClimb 10 solo stages or race a friend to crack a code. Free in your browser, no download.Play now

More guides from the blog

Games you might also like