Mastering Mastermind: Worst-Case Guessing Strategy

Two young adults in a library playing Scrabble, promoting learning and fun.

Mastermind is a code-breaking puzzle invented in 1970 by Mordecai Meirowitz. The codemaker chooses a secret of four colored pegs from six colors; the codebreaker guesses, gets feedback, and refines. Casual players often need several more guesses than strictly necessary. Donald Knuth proved in 1976 that the worst-case minimum is five guesses with optimal play. Mastering Mastermind means learning the deduction process that hits that bound, or at least gets you within one of it every game.

Key takeaways

  • The standard game uses 4 pegs from 6 colors with repetition allowed: 1,296 possible codes.
  • Knuth’s 1976 algorithm guarantees solving any code in five guesses or fewer using a minimax approach.
  • Knuth’s method opens with 1122 (two colors, two of each).
  • Each guess gives black pegs (correct color in correct position) and white pegs (correct color, wrong position).
  • After each guess, eliminate every code from your possibilities list that would not have produced the same feedback.

The game and the math

Standard Mastermind: codemaker picks a 4-peg code from 6 colors, repetition allowed. That gives 6^4 = 1,296 possible codes. The codebreaker guesses; the codemaker responds with the number of black pegs (right color in right spot) and white pegs (right color, wrong spot).

The codebreaker keeps guessing until the code is found. Casual goal: under 8 guesses. Mastery goal: 5 guesses worst case (Knuth’s bound), with an average around 4.5.

Knuth’s five-guess algorithm

Donald Knuth published “The Computer as Master Mind” in the Journal of Recreational Mathematics in 1976. The algorithm is:

  1. Start with the guess 1122 (two of color 1, two of color 2 — using the digits 1-6 for the six colors).
  2. Receive the feedback (black and white pegs).
  3. From the set of all possible codes (1,296 at start), eliminate any code that would not have produced the same feedback against your guess.
  4. Choose the next guess by minimax: for each candidate guess, compute the worst-case (largest) remaining set size after the feedback. Pick the guess that minimizes this worst case.
  5. Repeat from step 2 until the code is found.

The minimax property is what guarantees the five-guess worst case. Each guess is chosen so that, no matter what feedback the codemaker gives, the remaining candidate set is as small as possible.

Why 1122 as the opener

The opening guess 1122 is special because it covers two colors and tests for both repetition and position simultaneously. The feedback partitions the 1,296 codes into outcome classes (0 black 0 white, 0 black 1 white, etc.), and the largest class contains 256 codes.

A guess like 1234 (all different colors) tests four colors but partitions the codes less evenly; the worst-case remaining set is larger. A guess like 1111 (all the same color) leaves a very large remaining set when the feedback is 0 black 0 white.

Up to relabeling colors and positions, there are only five kinds of first guess: 1111, 1112, 1122, 1123 and 1234. Knuth found that 1122 leads to a five-guess guarantee, while the other openings do not win in five tries on every code with his method.

Minimax in practice

Most humans cannot mentally compute the minimax guess. Two approximations work:

  1. Eliminate aggressively. After each guess, mentally cross off codes that would not have produced the same feedback. Keep a list of remaining candidates.
  2. Guess from the remaining candidates. If you guess only codes that are still possible, you may take an extra turn or two but you simplify the bookkeeping.

The “guess only from candidates” approach (known as the “consistent” strategy) is usually somewhat worse than minimax, especially in the worst case, but it is far easier to run in your head. For human play it is a strong heuristic.

Reading the feedback

Black and white pegs encode different information:

  • Total pegs (black + white): tells you how many colors in your guess are in the code.
  • Black pegs: tell you how many colors are in the right position.
  • White pegs: tell you how many colors are present but misplaced.

If you guess 1122 and get 0 black 0 white, neither color 1 nor color 2 is in the code. Your remaining candidates are codes using only colors 3-6: 4^4 = 256 codes. A big elimination.

If you get 4 black 0 white, the code is 1122. Done. (This is a 1-in-1,296 event from the starting opener.)

Most feedback is between these extremes and requires more guesses to narrow.

The deduction process

Mastermind mastery is mostly bookkeeping discipline. After every guess:

  1. Look at the feedback.
  2. Mentally (or on paper) eliminate codes that could not have produced this feedback.
  3. If the remaining set is small (under 10 codes), pick a guess that partitions the set in half.
  4. If the remaining set is large, pick a guess that introduces new color and position information.

The classic trap: guessing a code that matches the feedback but does not partition the remaining set well. A guess that gives the same feedback for 50 remaining codes leaves you no closer to a unique solution.

Variants

The standard game allows duplicate colors. Some variants forbid duplicates, which reduces the code space dramatically: 6 × 5 × 4 × 3 = 360 possible codes. A smaller code space generally means fewer guesses are needed.

Other variants change the peg count (5 pegs, 6 pegs) or the color count (8 colors, 10 colors). The minimax bounds scale with code space: more colors and longer codes need more guesses. The optimal algorithms remain the same — minimax over the remaining candidates.

Codemaker strategy

If you are setting the code, the strongest play is uniform random selection. Any pattern preference the codemaker has (avoiding repeats, avoiding extreme colors, picking favorites) the codebreaker can exploit over multiple games.

Against an analytical opponent, uniform random is the only choice that cannot be statistically punished. Hand-picking a code because it “looks random” is still a pattern a regular opponent can learn.

Common mistakes

  • Guessing without elimination. Treating each guess as fresh information without tracking the remaining candidate set.
  • Repeating colors that the feedback says are not in the code. If a guess containing color 5 scored zero pegs in total, color 5 is not in the code and should not appear in a guess meant to be the answer.
  • Not using duplicates. If 1122 returns 1 black 0 white, the code has either one 1 in position 1 or 2, or one 2 in position 3 or 4. Eliminating duplicates removes valid possibilities.
  • Guessing only “consistent” codes from the candidate set. Sometimes the minimax guess is a code that cannot possibly be the answer — it is just useful for partitioning the set.
  • Forgetting the count. The total black + white pegs is a precise count of the colors in common between your guess and the code, not an approximation.

How to practice

Our Mastermind game sets the code for you and can allow or forbid duplicate colors. To practice the algorithm, keep a written list of remaining candidates after each guess, so you can verify that your guess is actually shrinking the candidate set efficiently.

For the original algorithm, see Knuth’s 1976 paper “The Computer as Master Mind.” For the broader history of the game and its analysis, the Wikipedia article on Mastermind is a solid reference. For the basic rules walkthrough, see our how to play Mastermind guide.

Frequently asked questions

What is the optimal first guess in Mastermind?

For the standard 4-peg, 6-color game, the optimal first guess under Knuth’s minimax algorithm is 1122 — two of the first color and two of the second color. Its worst-case outcome leaves 256 of the 1,296 codes, and it is the opening that lets his method guarantee a win in five guesses.

What is the worst-case number of guesses?

Five, under Knuth’s minimax algorithm. Knuth proved that any code can be solved in five guesses or fewer using the right sequence of guesses. His method averages around 4.5 guesses; later work found strategies with a lower average (about 4.34) at the cost of a six-guess worst case.

How does Mastermind feedback work?

After each guess, the codemaker returns the number of black pegs (correct color in correct position) and white pegs (correct color, wrong position). The total (black + white) tells you how many colors in your guess are also in the code, regardless of position.

Can humans achieve Knuth’s bound?

Not without a chart or program. The minimax calculation per guess is computationally heavy. Humans using consistent-strategy elimination (guessing only codes that are still possible) can get close, but occasionally need an extra guess or two.

How many possible codes are there?

For the standard game (4 pegs, 6 colors, repetition allowed), there are 6^4 = 1,296 possible codes. For the no-repetition variant, there are 6 × 5 × 4 × 3 = 360 codes.

The bottom line

Mastering Mastermind is a deduction discipline. Open with 1122, eliminate consistent codes after each guess, and pick guesses that partition the remaining set evenly. Five guesses worst case is achievable; six is reasonable for casual study. When you need a break from logic puzzles, the Chrome Dino game is the no-deduction-required tab.

🔌 Connect any AI assistant to dinogame.gg — we run an MCP server: https://dinogame.gg/mcp