Most Mastermind players guess randomly, adjust based on feel, and hope to crack the code before running out of attempts. There is a far better way. In 1977, mathematician Donald Knuth proved that any Mastermind code (4 pegs, 6 colors) can be solved in five guesses or fewer using a specific strategy. In this guide, we explain that strategy and how you can apply it yourself.

The Basics: How Mastermind Works

The codemaker picks a secret sequence of colored pegs — typically 4 pegs chosen from 6 colors, with repetition allowed. That gives 6⁴ = 1,296 possible codes. The codebreaker tries to identify the secret code by making guesses and receiving feedback:

Four black pegs means you've cracked it. The challenge is to get there in as few guesses as possible.

Quick stats: With 4 pegs and 6 colors there are 1,296 possible codes. A random guesser might need 6–8 attempts on average. An optimal strategy guarantees success in 5 or fewer — and averages just 4.48 guesses.

Why Random Guessing Fails

The problem with random guessing is that it wastes information. Every guess and its feedback tells you something about the secret code — but only if your next guess is chosen to maximally exploit that information. A random guess might be consistent with the feedback you've received, but it may not narrow the remaining possibilities as efficiently as a better-chosen guess would.

Consider this: after receiving "2 blacks, 1 white" from your first guess, there might be 80 codes still consistent with that feedback. A random consistent guess might reduce that to anywhere from 30 to 65 codes depending on luck. An optimal guess is guaranteed to reduce it to at most 20.

The Minimax Strategy

Knuth's approach is called minimax: for each possible guess, calculate the worst-case number of codes that would remain after any possible feedback response. Then pick the guess that minimizes that worst case.

In plain English: choose the guess that, even in the worst possible scenario, leaves you with the fewest remaining possibilities to work through.

Step 1: Start with 1-1-2-2

Knuth proved that the optimal first guess is 1-1-2-2 (two of one color, two of another). This guess guarantees that no matter what feedback you receive, you'll be left with at most 256 possible codes — out of 1,296 — after just one guess.

Other strong opening guesses include 1-1-2-3 and 1-2-3-4, but 1-1-2-2 is mathematically optimal for the classic game.

Step 2: Filter consistently

After each guess and its feedback, eliminate every code that is inconsistent with what you've learned. A code is inconsistent if it would have produced different feedback than what you actually received.

Example game

Guess 1
1 black, 0 white
Guess 2
2 blacks, 0 white
Guess 3
4 blacks — solved!

Step 3: Choose the next guess optimally

From the remaining consistent codes, apply minimax: for each candidate guess, work out how many codes would remain after each possible feedback response, and find the maximum. Pick the candidate guess with the lowest maximum — that's your next guess.

In practice, doing this by hand is time-consuming. That's exactly what our Mastermind Solver does for you — it computes the minimax-optimal next guess after each piece of feedback, so you can focus on the game rather than the calculation.

Practical Tips for Playing by Hand

If you're playing without a solver, here are the most useful heuristics that approximate the minimax strategy:

Always use your feedback fully

Before guessing again, make sure your next guess is consistent with everything you've learned. Never guess a code that you've already proven impossible. This sounds obvious but is the most common mistake among casual players.

Prefer guesses that could be the answer

When two guesses are equally good at reducing the search space, prefer the one that is itself a possible answer. This way, if you're lucky, you solve it immediately rather than gaining information for next time.

Test new colors early

Your first two guesses should ideally cover all six colors — so you know which colors appear in the code and which don't. Knowing that a color is absent eliminates a huge chunk of possibilities immediately.

Watch out for position traps

A white peg means right color, wrong position. Players often forget exactly which positions are now excluded for that color — keeping a mental note (or written note) of excluded positions prevents repeated mistakes.

Variations: More Pegs, More Colors

The classic game uses 4 pegs and 6 colors, but Mastermind comes in many variants. With 5 pegs and 6 colors there are 7,776 possible codes; with 5 pegs and 8 colors, 32,768. The minimax principle still applies — it just takes more guesses to guarantee a solution.

Our Mastermind Solver supports any combination of 2–5 pegs and 2–8 colors, computing the optimal guess each time regardless of the variant you're playing.

Want to play optimally every time?

Our Mastermind Solver computes the minimax-optimal next guess after each piece of feedback — for any peg and color combination.

🎯 Try the Solver

Summary

The key to cracking Mastermind efficiently is not luck — it's information management. Start with a proven opening (1-1-2-2), filter consistently after every guess, and always choose your next guess to minimize the worst-case remaining possibilities. Do that, and you'll solve any classic Mastermind code in five guesses or fewer, every single time.