Skip to content
Logic Puzzle

Mastermind

Break a hidden color code using feedback on your guesses. A classic code-breaking puzzle. Pick 4 colors and click Guess. A black peg means a color is correct and in the right position; a white peg means a color is correct but in the wrong position. Crack the code in 10 guesses or fewer.

0/10Guesses
Pick a color, then click a slot. Repeat for all 4 slots.

The rules of Mastermind

Mastermind is a code-breaking puzzle invented by Israeli telecommunication expert Mordecai Meirowitz in 1970 and commercialized by Invicta Plastics the same year. The codemaker (here, the computer) picks a secret sequence of four colors, drawn from six possibilities, with repetition allowed. The codebreaker (you) makes successive guesses and receives feedback after each:

  • Black peg: one of your colors is correct and in the correct position.
  • White peg: one of your colors is in the secret code but in a different position.

You win if you crack the code within 10 guesses. The order of the feedback pegs does not correspond to the positions of the colors — only the counts matter. This ambiguity is what makes the puzzle interesting: a feedback of "2 black, 1 white" tells you that two of your colors are right and well-placed, and a third is right but misplaced, but not which ones.

Worked example

Suppose the secret code is (Green, Green, Yellow, Blue). You guess (Green, Red, Yellow, Bronze). Feedback: 2 black, 0 white. The first green and the third yellow are correct and well-placed. The red and bronze are not in the code at all (or you'd have gotten a white peg).

Strategy

Donald Knuth published a famous algorithm in 1976 that guarantees solving any Mastermind code in five or fewer guesses, using a minimax strategy. As a human player, you won't compute that exactly, but the underlying principle is useful:

  • Use early guesses to gather information. Your first guess should test four different colors (not a repeat) to maximize what you learn.
  • Track what you've eliminated. If a color produces zero pegs (not even white), it's not in the code — drop it.
  • Test position specifically. If you know a color is in the code but you don't know where, swap it into different positions across successive guesses while keeping other slots stable.
  • Account for repetition. The code may contain the same color twice. If you got "1 black, 1 white" on a guess with two of the same color, one of them is well-placed and the other is misplaced — both are in the code.

Difficulty

The classic configuration is 4 positions from 6 colors with repetition allowed — which gives 6^4 = 1296 possible codes. That's the version implemented here. Variants with more colors or more positions exist and are correspondingly harder. With optimal play (Knuth's algorithm), 5 guesses always suffice; casual human play usually solves a code in 6-8 guesses.

Related If you enjoy deduction under uncertainty, also try our Sudoku 9×9 — a different style of constraint-based puzzle.