Learn & Understand

How to Always Win Mastermind: Information Theory and Knuth's Algorithm

In a hurry? Skip straight to the numbers.

Open the Mastermind Combinations Calculator →

The companion calculator counts Mastermind's possible codes, and for the standard game the answer is 1,296. That number is large enough to make blind guessing hopeless but small enough to reason about, which is precisely what makes Mastermind a beautiful playground for information theory, and the setting for a famous algorithm that guarantees a win in at most five guesses.

Too Many to Brute-Force, Few Enough to Outsmart

With six colors in four positions and repeats allowed, there are 1,296 possible secret codes. You typically get only ten to twelve guesses, so trying codes one by one is a losing strategy, you would exhaust your guesses long before working through the possibilities. The game cannot be brute-forced; it must be deduced. That gap between the number of possibilities and the number of guesses is what forces clever play.

Each Guess Is a Question That Should Reveal the Most

The key insight is that a guess is not really an attempt to hit the code, it is a question, and the feedback pegs are the answer. A good question is one whose answer eliminates as many possibilities as possible no matter what the answer turns out to be. This is the heart of information theory: the value of a guess is measured by how much it shrinks your uncertainty, how much information it yields.

Thinking of guesses as information
A guess that...Is...
Splits the remaining codes into many small groups by feedbackExcellent, it slashes uncertainty whatever the reply
Leaves most codes in one big groupWeak, it barely narrows things down

The best strategy keeps only the codes still consistent with all feedback so far, then chooses the next guess that most reduces the worst-case number of survivors, guaranteeing steady progress toward the answer.

Knuth's Five-Guess Algorithm

In 1977, computer scientist Donald Knuth proved you can do remarkably well. He published an algorithm that solves any standard Mastermind code in at most five guesses, by always choosing the guess that minimizes the largest possible number of remaining candidates, a minimax approach. The famous recommended opening is a specific two-color pattern, and from there each guess is chosen to worst-case-minimize what is left. Knuth's result is a landmark: a simple board game, provably solved with a guaranteed upper bound on guesses, using the same maximize-information logic that runs through code-breaking and machine learning.

The Same Idea Behind Modern Word Games

If this feels familiar, it should. The optimal-guessing strategy behind popular word-guessing games is the very same principle: pick the guess that, across all possible answers, is expected to eliminate the most candidates and shrink the field fastest. Mastermind is the classic ancestor of that whole family of deduction puzzles, and the solvers people build for modern word games are running a version of the reasoning Knuth formalized decades ago.

Playing It Better Yourself

You cannot run a minimax search in your head, but you can borrow the spirit. Make each guess do double duty: rather than fixating on the one code you hope is right, choose guesses that would split the possibilities usefully whatever the feedback, and rigorously discard every code inconsistent with the pegs you have seen. Deduction, not luck, is the intended path, and the 1,296 figure is exactly why.

Using the Count Well

Take the calculator's combination count as the size of the search space, the thing that makes brute force hopeless and deduction essential. Then play like an information theorist: treat guesses as questions, keep only codes consistent with the feedback, and aim to shrink the field fastest. It is the principle Knuth proved can crack the game in five, and the same one powering today's word-game solvers.

Ready to Put This Into Practice?

Now that you understand how it works, plug in your own numbers and get an instant, accurate result.

Use the Mastermind Combinations Calculator Now →