Mastermind Game
Play the classic code-breaking game free, and learn the rules, the clues and the best strategy.
Play the classic code-breaking game free, and learn the rules, the clues and the best strategy.
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.
| Mastermind | |
|---|---|
| Players | 2: a codemaker and a codebreaker |
| Equipment | A 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 code | 4 pegs, colours can repeat: 1,296 possible codes (360 if repeats are not allowed) |
| Goal | Break the code in as few guesses as possible |
| Clues | Black = right colour in the right hole; white = right colour in the wrong hole |
| Best possible | Every code can be broken in 5 guesses or fewer |
| Invented | 1970, by Mordecai Meirowitz |
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.
| Code | Guess | Key pegs | Why |
|---|---|---|---|
| 2532 | 3523 | 1 black, 2 white | The 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. |
| 1122 | 1112 | 3 black, 0 white | Three pegs match exactly. The guess has a third 1 but the code has only two, so it scores nothing. |
| 1234 | 4321 | 0 black, 4 white | All four colours are right, none in the right hole. |
| 1123 | 1111 | 2 black, 0 white | Only the two 1s at the front score. The extra 1s in the guess earn nothing, because the code holds just two. |
| 1123 | 3311 | 0 black, 3 white | The 3 and both 1s are in the code, all in the wrong holes. |
| 5566 | 6655 | 0 black, 4 white | Swapping two pairs gives four white pegs. |
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.
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 code | Example | Codes | Share |
|---|---|---|---|
| Four different colours | 1234 | 360 | 28% |
| One pair | 1123 | 720 | 56% |
| Two pairs | 1122 | 90 | 7% |
| Three of a kind | 1112 | 120 | 9% |
| Four of a kind | 1111 | 6 | 0.5% |
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 guess | Different answers possible | Codes left at worst | Codes left on average |
|---|---|---|---|
| 1111 | 5 | 625 | 512 |
| 1112 | 11 | 317 | 236 |
| 1122 | 13 | 256 | 205 |
| 1123 | 14 | 276 | 185 |
| 1234 | 14 | 312 | 188 |

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.
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 needed | Codes | Share |
|---|---|---|
| 1 | 1 | 0.1% |
| 2 | 6 | 0.5% |
| 3 | 62 | 5% |
| 4 | 533 | 41% |
| 5 | 694 | 54% |
| Average | 4.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.

| Version | Colours | Holes | Codes |
|---|---|---|---|
| Mastermind (original) | 6 | 4 | 1,296 |
| Super (Deluxe) Mastermind | 8 | 5 | 32,768 |
| Parker Mastermind (1993) | 8 | 4 | 4,096 |
| Mastermind for Kids | 6 | 3 | 216 |
| Mini or Travel Mastermind (6 rows) | 6 | 4 | 1,296 |
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.
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.
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.