Mastermind is the classic code-breaking game for two players: one hides a row of four coloured pegs, the other has to work it out from black and white clues. Play it free below, either breaking the computer’s code or watching the computer crack yours, then read the full rules, how the clues work, and the maths behind the best strategy.

Code:
Black key peg = right colour in the right hole. White = right colour in the wrong hole. 10 guesses. Tap a peg in your guess to remove it. Free, no sign-up.

Mastermind at a Glance

Mastermind
Players2: a codemaker and a codebreaker
EquipmentA decoding board with a shield and 10 or 12 rows (some sets have 8 or 6), code pegs in 6 colours, small black (or red) and white key pegs
The code4 pegs, colours can repeat: 1,296 possible codes (360 if repeats are not allowed)
GoalBreak the code in as few guesses as possible
CluesBlack = right colour in the right hole; white = right colour in the wrong hole
Best possibleEvery code can be broken in 5 guesses or fewer
Invented1970, by Mordecai Meirowitz

How to Play Mastermind

  1. Agree the rules. Decide whether colours can repeat in the code (the standard game allows it) and how many games to play. Players swap roles every game, so play an even number.
  2. Make the code. The codemaker secretly places 4 code pegs in the holes under the shield.
  3. Guess. The codebreaker places a row of 4 code pegs on the first row of the board.
  4. Give the clues. The codemaker puts one black key peg beside the row for every peg of the right colour in the right hole, and one white key peg for every peg of the right colour in the wrong hole. The positions of the key pegs say nothing about which code pegs they belong to.
  5. Guess again. The codebreaker uses the clues to make the next guess on the next row.
  6. End of the game. The game ends when the codebreaker gets 4 black pegs, or when every row is full. Then the shield is lifted.

How the Black and White Pegs Work

Black pegs are easy: count the holes where the guess matches the code exactly. White pegs need care when a colour appears more than once. The exact rule, as the computer scientist Donald Knuth put it in 1976: for each colour, take the smaller of the number of times it appears in the code and in the guess, and add these up. That total is black plus white pegs, so subtract the blacks to get the whites. A peg in the guess can never earn more than one key peg.

In the examples below, the numbers 1 to 6 stand for the six colours, as in the game above.

CodeGuessKey pegsWhy
253235231 black, 2 whiteThe 5 is in the right hole. The 2 and one of the two 3s are in the code but in the wrong holes; the code has only one 3.
112211123 black, 0 whiteThree pegs match exactly. The guess has a third 1 but the code has only two, so it scores nothing.
123443210 black, 4 whiteAll four colours are right, none in the right hole.
112311112 black, 0 whiteOnly the two 1s at the front score. The extra 1s in the guess earn nothing, because the code holds just two.
112333110 black, 3 whiteThe 3 and both 1s are in the code, all in the wrong holes.
556666550 black, 4 whiteSwapping two pairs gives four white pegs.

Mastermind Rules Questions

  • Can colours repeat? In the standard game, yes: the code can even be four pegs of one colour. For an easier game, agree that every peg must be a different colour.
  • Can the code have blanks? Only if the players agree beforehand. Blanks then work like an extra colour.
  • Does the order of the key pegs matter? No. The codemaker can place them in any order, so they never show which code peg is right.
  • Can you get 3 black and 1 white? No. If three pegs are exactly right, the fourth is either right too or the wrong colour, so that answer never happens.
  • How many guesses do you get? One per row: 10 or 12 on most boards, 6 on the travel version. The game above gives 10.

Scoring

Only the codemaker scores. They get 1 point for every guess the codebreaker makes, plus 1 extra point if the codebreaker fails to break the code on the last row. After the agreed number of games, the player with the most points wins. Some players score by the number of key pegs instead.

Mastermind Odds and Maths

With 4 holes and 6 colours there are 6 × 6 × 6 × 6 = 1,296 possible codes, so a lucky first guess wins 1 time in 1,296. Without repeated colours there are 360. Most random codes contain a repeated colour:

Type of codeExampleCodesShare
Four different colours123436028%
One pair112372056%
Two pairs1122907%
Three of a kind11121209%
Four of a kind111160.5%

The best first guess

A good first guess splits the 1,296 codes into many small groups, one for each possible answer. The table shows how many codes can still be the answer after each kind of first guess.

First guessDifferent answers possibleCodes left at worstCodes left on average
11115625512
111211317236
112213256205
112314276185
123414312188
Mastermind first guess comparison: 1111 leaves up to 625 codes, 1112 up to 317, 1122 up to 256, 1123 up to 276 and 1234 up to 312
Codes still possible after the first guess: two pairs has the smallest worst case.

Two pairs (1122) gives the smallest worst case, 256 codes, which is why Knuth chose it. A pair and two singles (1123) leaves slightly fewer codes on average, and four of one colour is by far the weakest start.

Knuth’s five-guess method

In 1976 Donald Knuth proved that the codebreaker can always break the code in 5 guesses or fewer. His method: start with 1122; after each answer, cross out every code that no longer fits; then choose the guess whose worst possible answer leaves the fewest codes, preferring a code that could still be right. The Hint button in the game above uses exactly this method, and so does the computer when it breaks your code.

Guesses neededCodesShare
110.1%
260.5%
3625%
453341%
569454%
Average4.48

Sometimes the best guess is one that cannot be the answer. In Knuth’s own example, after 1122, 1344 and 3526 seven codes are left, and only 1462, which is not one of them, tells all seven apart.

Mastermind guesses needed: Knuth's method solves every code in 5 guesses or fewer, average 4.48; guessing any code that still fits averages 4.63; the lowest code that still fits averages 5.02
Guesses needed with three strategies, all starting with 1122.
  • Simpler play is nearly as good. Starting with 1122 and then guessing any code that still fits, picked at random, took 4.63 guesses on average in 20,000 games; 99% were solved within 6 and none needed more than 8.
  • Taking the codes in order is slower. Always guessing the lowest code that still fits needs 5.02 guesses on average and up to 8 from 1122, or 5.76 and up to 9 starting from 1111.
  • The best possible average is 5,625 / 1,296 ≈ 4.34 guesses, found by Kenji Koyama and Tony Lai in 1993 with an exhaustive computer search. It needs up to 6 guesses for some codes.
  • Without repeated colours the same method breaks all 360 codes within 5 guesses, 4.14 on average, starting with 1234.

Mastermind Strategy Tips

  • Open with two pairs, such as 1122, then bring in the colours you have not tried yet.
  • Play a code that could be right. A guess that fits every clue so far can win straight away, and it is almost as good as the computer’s choice.
  • Use dead colours as fillers. Once a colour scores nothing, put it in holes you want to test, so the clues tell you about the other pegs only.
  • Move one thing at a time. Swapping two pegs, or changing one colour, makes the change in the clue easy to read.
  • Count the colours first. Black plus white tells you how many of the guessed colours are in the code; once the total reaches 4, you only need the order.
  • Do not assume four different colours. 72% of all codes have a repeated colour.

Mastermind Variations

VersionColoursHolesCodes
Mastermind (original)641,296
Super (Deluxe) Mastermind8532,768
Parker Mastermind (1993)844,096
Mastermind for Kids63216
Mini or Travel Mastermind (6 rows)641,296
  • Number Mastermind uses digits instead of colours; the codemaker may also give the sum of the digits as an extra clue.
  • Word Mastermind uses letters: the code and every guess must be a real word.
  • Bulls and Cows is the older pen-and-paper game it grew from: a secret number with all different digits, with “bulls” for right digits in the right place and “cows” for right digits in the wrong place.

History of Mastermind

Mastermind was invented in 1970 by Mordecai Meirowitz, an Israeli postmaster and telecommunications expert. After showing it at the Nuremberg International Toy Fair he sold it to Invicta Plastics, a company near Leicester in England, which has held the rights since 1971 and always called it Master Mind. Hasbro now makes it in most countries, with Pressman in the US. Knuth’s 1976 paper showing the five-guess method made it a favourite puzzle for computer scientists.

More Logic and Strategy Games

For another game of deduction, try Clue. Word players can turn to Scrabble, and for hidden pieces and bluffing, see Game of the Generals. On your own, try a hand of Solitaire.

More hidden information: in Stratego every enemy piece is a secret until you attack it.

More deduction: in Battleship every hit and miss narrows down where the enemy fleet can be.

Mastermind: frequently asked questions

How do you play Mastermind?

One player secretly sets a code of 4 coloured pegs. The other guesses a row of 4 pegs and gets a black key peg for every peg of the right colour in the right hole and a white key peg for every right colour in the wrong hole. Using the clues, the codebreaker keeps guessing until the code is broken or the board is full.

What do the black and white pegs mean in Mastermind?

A black (or red) key peg means one of your pegs is the right colour in the right hole. A white key peg means one is the right colour but in the wrong hole. The order of the key pegs does not tell you which pegs they refer to.

How many possible codes are there in Mastermind?

1,296 with 4 holes and 6 colours when colours can repeat (6 x 6 x 6 x 6), or 360 when every colour must be different.

What is the best first guess in Mastermind?

Two pairs, such as red-red-orange-orange (1122). Whatever the answer, it leaves at most 256 of the 1,296 codes, the smallest worst case of any first guess, which is why Donald Knuth chose it.

Can Mastermind always be solved in five guesses?

Yes. Donald Knuth proved in 1976 that every code can be broken in 5 guesses or fewer. His method needs 4.48 guesses on average; the best possible average is about 4.34.

Can colours repeat in Mastermind?

Yes, in the standard game the code can repeat colours, even four of the same. Players can agree to ban repeats for an easier game.

How many guesses do you get in Mastermind?

One per row of the board: usually 10 or 12, and 6 on the travel version. The free game on this page gives 10.

Who invented Mastermind?

Mordecai Meirowitz, an Israeli postmaster and telecommunications expert, in 1970. Invicta Plastics of England bought the rights and launched it in the early 1970s.

Fadi Hayek
Author & Casino Expert

After going through several successful experiences with several industry pioneers, Fadi Hayek has managed to excel in the world of content for online gaming by providing unique content for several projects that allowed him to obtain the necessary experience to launch his career. Last but not least, Fadi also holds several certificates, including but not limited to content marketing and iGaming.

Latest News

Artificial Intelligence in Gambling – How It Is Changing the Future of the Industry

As online casinos adopt machine learning models and artificial intelligence in gambling becomes the next disruption, you don’t want to be left behind. Read on to learn about the impact AI will have on the future of the gambling industry.

Arabian Football Betting Trends – Stay Current on Betting Preferences in the Middle East!

Keep abreast of the latest Arabian football betting trends with the Arabian sports betting insights below. This guide covers the most popular bet types with Arab bettors and the top bookmakers for Arabian and global football competitions.

Al-Nassr vs PSG | Cristiano Ronaldo vs Lionel Messi

The biggest event in world football is scheduled to take place later this month as Al Nassr vs PSG is on the cards.

Yggdrasil Collaborate with Reflex to Release Dublin Up DoubleMax

Yggdrasil and Reflex are set to launch Dublin Up DoubleMax, an Irish-style video, as one of their latest releases. The slot game is designed by incorporating Yggdrasil’s proprietary Game Engagement Mechanic (GEM) DoubleMax coupled with unique features like free spins bonus and fairy magic.