Home/Blog/How to Win Mastermind | Strategy to Crack Any Code

How to Win Mastermind | Strategy to Crack Any Code

By PlayBrain Teamยทยท7 min read

Mastermind is one of the greatest logic games ever created. A secret code of 4 colored pegs is hidden, and you have 10 guesses to crack it. After each guess, you receive feedback: black pegs for correct colors in the correct position, white pegs for correct colors in the wrong position.

Sounds simple? The code has 1,296 possible combinations (6 colors, 4 positions). Cracking it consistently in 5 guesses or fewer requires real strategy.

Play Mastermind free online at PlayBrain and put these strategies to the test.

Understanding the Feedback

Before diving into strategy, make sure you fully understand what the feedback pegs mean:

  • Black peg: Right color, right position. This peg is exactly where it belongs.
  • White peg: Right color, wrong position. This color exists in the code, but you've placed it in the wrong slot.
  • No peg: This color (in this position) tells you nothing useful โ€” or the color isn't in the code at all.

Critical rule: Each color in the code can only produce ONE feedback peg. If the code has one red and you guess two reds, only one of your reds will generate feedback.

Opening Move Strategy

Your first guess is the most important because it gathers maximum information from a blank slate.

The Classic Opening: AABB

Start with two pairs of colors, like Red-Red-Blue-Blue. This is widely considered the strongest opening because:

  • If you get 0 feedback pegs, you've eliminated 2 colors completely (huge information gain)
  • If you get 4 black pegs, you've won on turn 1 (odds: 1 in 1,296)
  • Any feedback between 0 and 4 narrows the possibilities dramatically

Alternative Opening: ABCD

Start with four different colors, like Red-Blue-Green-Yellow. This tells you which of these 4 colors appear in the code, but gives less positional information than AABB.

Why Not AAAA?

Guessing all one color (Red-Red-Red-Red) only tells you how many times red appears in the code (0-4). You waste a guess learning about just one color when you could learn about two or four.

Mid-Game Strategy: Process of Elimination

After your opening, shift to systematic elimination.

Track What You Know

After each guess, maintain three lists:

  1. Confirmed colors: Colors you know are in the code (white or black peg)
  2. Eliminated colors: Colors you know are NOT in the code (from zero-peg feedback)
  3. Confirmed positions: Positions where you know the exact color (from black peg deduction)

The Swap Technique

If you got 1 black and 1 white from your first guess, try swapping positions of two colors in your next guess while keeping the other two the same. The change in feedback tells you exactly which colors generated which pegs.

One Change at a Time

When you're narrowing down positions, change only one thing between guesses. If you change multiple colors/positions and the feedback changes, you won't know which change caused the difference.

Advanced Strategy: Knuth's Algorithm

In 1977, computer scientist Donald Knuth proved that Mastermind can always be solved in 5 guesses or fewer using an optimal algorithm:

  1. Start with an initial guess (Knuth used AABB)
  2. After receiving feedback, eliminate all codes from the remaining possibilities that wouldn't give the same feedback
  3. For your next guess, choose the code that minimizes the maximum number of remaining possibilities (minimax strategy)
  4. Repeat until solved

This algorithm guarantees a solution in at most 5 guesses, with an average of 4.34 guesses. You don't need to compute it by hand โ€” but understanding the principle helps: each guess should eliminate as many possibilities as possible.

Common Mistakes to Avoid

  1. Random guessing after turn 3. Every guess should use ALL the information from previous guesses. Never ignore feedback.
  1. Forgetting about repeated colors. The code can have the same color in multiple positions. Red-Red-Blue-Green is a valid code. Don't assume all 4 colors must be different.
  1. Confusing white and black pegs. A white peg means the color IS in the code but NOT in that position. Many beginners treat white pegs as partial credit rather than specific information.
  1. Not tracking eliminated colors. If a guess with Red in it gives 0 pegs, and no other guess with Red has given feedback either, Red is not in the code. Stop guessing Red.
  1. Changing too many things at once. If you change 3 colors between guesses, you can't determine which change affected the feedback. Change 1-2 things at a time for clean information.

Difficulty Levels

The classic Mastermind uses 6 colors and 4 positions (1,296 combinations). Variants include:

  • Easy: 4 colors, 4 positions (256 combinations) โ€” great for beginners
  • Standard: 6 colors, 4 positions (1,296 combinations) โ€” the classic game
  • Hard: 8 colors, 5 positions (32,768 combinations) โ€” for experts

PlayBrain's Mastermind lets you practice at multiple difficulty levels. Start on easy to internalize the strategy, then graduate to standard.

The Psychology of Mastermind

Mastermind isn't just a logic game โ€” it teaches a thinking style:

  • Hypothesis testing: Each guess is a hypothesis about the code. Feedback confirms or denies your theory.
  • Information theory: The best guess maximizes information gain, just like the scientific method.
  • Patience: Rushing leads to wasted guesses. Careful analysis pays off.
  • Working memory: Tracking multiple constraints simultaneously exercises your brain.

These skills transfer directly to programming (debugging is essentially Mastermind), scientific reasoning, and everyday problem solving.

Practice Makes Perfect

The average beginner solves Mastermind in 7-8 guesses. With practice and strategy, you can consistently solve it in 4-5. Here's a practice routine:

  1. Play 5 games. Track how many guesses each one takes.
  2. Review your worst game. Where did you waste a guess? What information did you miss?
  3. Focus on your opening. Always use AABB or a similar structured opening.
  4. Play daily. Your pattern recognition improves with repetition.

Play Mastermind now and see how many guesses it takes you. Can you beat 5?

If you enjoy code-breaking puzzles, also try Code Breaker โ€” a Mastermind-style logic puzzle with a different color system and unique twist on the feedback mechanics. Both are free on PlayBrain.

Related reading:

Get weekly game picks in your inbox

New games, tips, and challenges every week. No spam.

Frequently Asked Questions about How to Win Mastermind | Strategy to Crack Any Code

What is the best strategy for solving Mastermind?
Knuth's algorithm solves any standard 4-peg, 6-color Mastermind in 5 moves guaranteed. Start with 1122 as your first guess regardless of the answer โ€” this eliminates the most possibilities statistically. After each response, use minimax elimination to select the next guess that minimizes worst-case remaining codes.
How do you read the black and white pegs in Mastermind?
A black peg means a correct color in the correct position. A white peg means a correct color but in the wrong position. Only the counts matter โ€” not which peg corresponds to which slot in your guess. Use both counts together to systematically eliminate possible codes.
How many possible codes are there in Mastermind?
With 4 positions and 6 colors with repetition allowed, there are 6^4 = 1,296 possible codes. Without repetition there are 360 possible codes. Knuth's minimax algorithm guarantees cracking any code in at most 5 guesses. You can practice it free on PlayBrain's Mastermind game.
P

PlayBrain Team

Our editorial team reviews and tests every game and guide we publish. Have a question or correction? Get in touch.