Mastering Mastermind: Worst-Case Guessing Strategy

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 solve it in 6-8 guesses by intuition. 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.
- The optimal first guess is 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:
- Start with the guess 1122 (two of color 1, two of color 2 — using the digits 1-6 for the six colors).
- Receive the feedback (black and white pegs).
- 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.
- 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.
- 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 14 distinct outcome classes (0 black 0 white, 0 black 1 white, etc.), with the largest class containing 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.
Knuth checked the 1,296 possible first guesses by computer (more accessible in 1976 than the problem implies) and found 1122 to be a minimax optimum. Several other openings (1123, 1234, etc.) are slightly worse — they do not guarantee 5-guess solutions, but they get close.
Minimax in practice
Most humans cannot mentally compute the minimax guess. Two approximations work:
- Eliminate aggressively. After each guess, mentally cross off codes that would not have produced the same feedback. Keep a list of remaining candidates.
- 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 one or two guesses worse than minimax on average but still finishes in 6-7 guesses worst case. 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:
- Look at the feedback.
- Mentally (or on paper) eliminate codes that could not have produced this feedback.
- If the remaining set is small (under 10 codes), pick a guess that partitions the set in half.
- 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. The minimax bound for no-duplicates Mastermind is 4 guesses with optimal play.
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.
For one-shot games against an analytical opponent, uniform random is the only choice that cannot be statistically punished. Choosing 6655 because it “looks random” actually concentrates in the unusual-codes part of the distribution and gives the codebreaker an edge if they suspect it.
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 your last guess included color 5 and you got 0 white for it, color 5 should not appear in your next guess.
- 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
Online Mastermind games against an AI codemaker are widely available. To practice the algorithm, use a Mastermind site that lets you see the remaining-candidate count after each guess. You can verify that your guess is actually shrinking the candidate set efficiently.
For the original algorithm, Knuth’s 1976 paper “The Computer as Master Mind” is publicly available through the Stanford archive. 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. This partitions the 1,296 codes into the most balanced set of outcome classes.
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. The average is around 4.5 guesses.
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) average 5-6 guesses on the standard game, close to but not at the optimal bound.
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.








