Mastermind is a 1970 board game where one player hides a code and the other cracks it. Donald Knuth proved it can always be solved in 5 guesses. Here is the math.
Mastermind has sold over 50 million copies since 1970. It is a two-player deduction game: one player sets a secret code of four colored pegs, the other tries to crack it in ten attempts. After each guess, the codemaker gives feedback in the form of black and white pegs. Black means right color, right position. White means right color, wrong position.
The game looks like a toy. It is actually a constraint satisfaction problem with a well-defined mathematical structure. Donald Knuth, the Stanford computer scientist who wrote The Art of Computer Programming, published a paper in 1976 proving that the code can always be cracked in five or fewer guesses using a minimax strategy. That result is the reason Mastermind shows up in computer science curricula.
The game was invented by Mordecai Meirowitz, an Israeli postmaster and telecommunications expert, and manufactured by Invicta Plastics in England. It became a global hit after a licensing deal and a BBC television segment in 1973. You can practice the deduction with our Mastermind Solver, which implements the minimax algorithm and shows you the optimal next guess for any game state.
Mordecai Meirowitz was an Israeli postmaster and telecommunications engineer who designed Mastermind in 1970. He had been working on telephone exchange systems, and the game's structure, guessing a hidden configuration from limited feedback, reflects that background. The Wikipedia article on Mordecai Meirowitz documents his life and the game's creation.
Meirowitz pitched the game to several manufacturers before Invicta Plastics, a small English company based in Oadby, Leicestershire, agreed to produce it. Invicta was primarily a plastics manufacturer that had made components for other toys. Mastermind was their first original product, and it saved the company. The Wikipedia article on Mastermind) covers the manufacturing history and the game's commercial trajectory.
The game's visual design contributed to its success. The board is a flat plastic tray with rows of holes for the code pegs and smaller holes for the feedback pegs. The codemaker sits behind a shield that hides the secret code. The design is clean and tactile. The pegs click into the holes with a satisfying physical precision that digital versions cannot replicate.
Invicta marketed the game aggressively. They secured a segment on the BBC television show "Whirlybird" in 1973, which demonstrated the game to a British audience. Sales exploded. The game won the Game of the Year award from the British Toy and Hobby Association in 1973. By 1975, it was selling in the United States through a licensing deal with Pressman Toys. Over 50 million copies have been sold worldwide across various editions and licensed versions.
The name "Mastermind" and the cover art, which featured a seated businessman in a suit facing a woman in traditional dress, were marketing decisions by Invicta. The cover art has been criticized in retrospect for its gendered and orientalist imagery, but it was part of what made the game recognizable on store shelves in the 1970s. Modern editions use different artwork.
The standard game uses six colors and four positions. The codemaker selects a secret code: four pegs, each one of six colors. Colors can repeat. The codebreaker has ten attempts to guess the code.
After each guess, the codemaker provides feedback using small pegs. A black peg means one peg in the guess is the right color in the right position. A white peg means one peg in the guess is the right color but in the wrong position. The feedback pegs do not indicate which specific pegs are correct. If the guess has two black pegs and one white peg, the codebreaker knows that two pegs are exactly right and one peg is the right color in the wrong position, but not which ones.
The ambiguity of the feedback is what makes the game hard. If the feedback identified which pegs were correct, the game would be trivial. The codebreaker would simply test each position independently. Because the feedback is aggregate, the codebreaker must reason about the entire pattern simultaneously.
The total number of possible codes is 6 to the 4th power, which is 1,296. With six colors and four positions, and allowing repeats, that is the full keyspace. The codebreaker's job is to identify the one correct code out of 1,296 possibilities using at most ten guesses, each of which returns one of 14 possible feedback responses (ranging from 0 black, 0 white to 4 black, 0 white).
The game can be made harder or easier by changing the parameters. Some versions use eight colors. Some use five positions. The mathematics change with each variant, but the core structure remains: guess, receive aggregate feedback, narrow the candidate set, repeat until the code is identified or attempts are exhausted.
You can compare this to the Fallout Terminal Hacking puzzle, which is a Mastermind variant using words instead of colored pegs. The feedback structure is the same: aggregate positional matching without identifying which elements are correct.
Donald Knuth is a computer scientist at Stanford University and the author of The Art of Computer Programming, a multi-volume work that has been in progress since 1968 and is considered the foundational reference for algorithm analysis. The Wikipedia article on Donald Knuth covers his career and contributions.
In 1976, Knuth published a paper titled "The Computer as Master Mind" in the Journal of Recreational Mathematics, volume 9, issue 1. The paper is short, nine pages, and it proves that the standard Mastermind game (six colors, four positions, repeats allowed) can always be solved in five or fewer guesses. The paper is available online through various academic archives.
Knuth's strategy is a minimax algorithm. The idea is to choose each guess so as to minimize the maximum possible remaining candidate set size after the feedback. In other words, for each possible guess, consider every possible feedback response. For each feedback response, count how many candidates would remain. Take the worst case (the largest remaining set). Choose the guess that has the smallest worst case.
The first guess in Knuth's strategy is "AABB" (two pegs of the first color, two of the second). This guess partitions the 1,296 candidates more evenly than a guess like "ABCD" (four different colors), because it accounts for the possibility of repeated colors in the secret code. After the feedback, the candidate set is reduced to a subset. Knuth then applies the same minimax principle to choose the second guess, and so on.
The proof shows that no matter what the secret code is, the minimax strategy will identify it in at most five guesses. In practice, the average is about 4.48 guesses, meaning most games are solved in four or five attempts. The worst case of five is tight: there exist codes that require exactly five guesses under the minimax strategy, so the bound cannot be improved without a different algorithm.
Knuth's result is notable because it is a complete worst-case guarantee. The codebreaker does not need luck. The strategy works against any code the codemaker chooses, including adversarial choices designed to maximize the number of guesses needed. The Mastermind Solver on this site implements this approach and will show you the optimal guess at each step.
The reason five guesses suffice is a combination of the keyspace size and the information content of each feedback response. There are 1,296 possible codes. Each guess produces one of 14 possible feedback responses (the combinations of 0-4 black pegs and 0-4 white pegs, subject to the constraint that the total does not exceed 4). In information-theoretic terms, each guess can provide up to log2(14) bits of information, which is approximately 3.81 bits.
The total information needed to identify one code out of 1,296 is log2(1296) bits, which is approximately 10.34 bits. Dividing 10.34 by 3.81 gives a theoretical lower bound of about 2.72 guesses, which means three guesses is the absolute minimum in the best case. But this is an average-case bound, not a worst-case bound. The worst case requires more guesses because no single guess can partition the candidate set perfectly evenly for every possible code.
Knuth's minimax strategy achieves five in the worst case by ensuring that each guess minimizes the maximum remaining set size. The first guess, "AABB," produces partitions that are as balanced as possible. After the first guess, the largest possible remaining candidate set is 256. After the second guess, it drops to at most 14. After the third, at most 4. The fourth guess typically identifies the code, and the fifth is needed only in the worst cases where the third guess leaves a small but ambiguous set.
The math connects to the broader field of constraint satisfaction problems, which are studied extensively in computer science. A constraint satisfaction problem consists of a set of variables, a set of possible values for each variable, and a set of constraints that the values must satisfy. Mastermind maps directly onto this framework. The variables are the four positions. The possible values are the six colors. The constraints are the feedback from each guess. The Codenames Analyzer on this site solves a different but related constraint problem, using semantic similarity rather than positional matching to narrow word candidates.
The worst-case bound of five is specific to the standard six-color, four-position game. If you increase the number of colors or positions, the bound changes. An eight-color, five-position game has a keyspace of 32,768 and requires more guesses. The minimax approach still works, but the computation becomes more expensive because the candidate set is larger at each step.
Mastermind is a clean example of a constraint satisfaction problem, and it is used in computer science education for exactly that reason. Each guess adds a constraint. The codebreaker's task is to find the code that satisfies all constraints simultaneously.
The process is as follows. Start with the full set of 1,296 candidates. After each guess and feedback response, eliminate every candidate that is inconsistent with the feedback. A candidate is inconsistent if, when compared to the guess, it would not produce the same feedback that was received. For example, if you guess "AABB" and receive "1 black, 1 white," then any candidate that would produce a different feedback when compared to "AABB" is eliminated.
This elimination process is the same logic used in cryptanalysis. When you apply frequency analysis to a substitution cipher, you start with all possible mappings and eliminate those that are inconsistent with the observed letter frequencies. Each observation constrains the remaining possibilities. The structure is identical: guess, observe, eliminate, repeat.
The difference is that in cryptanalysis, the "guesses" are the ciphertext observations, and the "feedback" is the statistical properties of the plaintext language. In Mastermind, the guesses are active choices, and the feedback is provided by the codemaker. But the constraint propagation logic is the same.
This is why Mastermind teaches cryptanalysis. The game trains the player to think in terms of constraint elimination rather than random guessing. A novice player guesses colors and hopes. An experienced player chooses guesses that maximize information, even if the guess itself is unlikely to be the correct code. The shift from "guess what I think the answer is" to "guess what will tell me the most" is the core skill that Mastermind develops, and it is the same skill that a cryptanalyst uses when choosing which ciphertext properties to analyze first.
Mastermind has been used in computer science and mathematics education since the 1970s. The game demonstrates several concepts that are otherwise abstract and hard to teach through lectures alone.
First, it demonstrates the concept of information gain. Each guess provides a specific amount of information, measured by how much it reduces the candidate set. A guess that eliminates most of the candidates is more valuable than a guess that eliminates few, even if the latter is more likely to be the correct code. This is the same principle that governs optimal decision tree construction in machine learning, where each split is chosen to maximize information gain.
Second, it demonstrates worst-case analysis. Knuth's minimax strategy is a worst-case algorithm. It does not optimize for the average game. It optimizes for the hardest possible game. This is a fundamental concept in algorithm design, where worst-case bounds (Big-O notation) are the standard way to characterize algorithm performance. Mastermind makes the concept tangible because the player can feel the difference between a guess that leaves 14 candidates and a guess that leaves 256.
Third, it demonstrates the power of constraint propagation. The game is solvable not because any single guess reveals the code, but because the accumulation of constraints from multiple guesses narrows the possibilities until only one remains. This is the same mechanism that makes Sudoku puzzles solvable, that makes SAT solvers work, and that makes cryptanalysis possible. The Fallout Terminal Hacking puzzle on this site is another example of the same principle in a different wrapper.
The game's educational value is why it has stayed in print for over 50 years. It is not the most exciting board game. It is not the most complex. But it teaches a way of thinking that is transferable to any field that involves deduction under uncertainty, which is most of computer science and all of cryptanalysis. If you want to practice, the Mastermind Solver on this site will play the game with you and show you the optimal move at each step, so you can learn the strategy by watching it work.
Mordecai Meirowitz, an Israeli postmaster and telecommunications engineer, designed the game in 1970. It was manufactured by Invicta Plastics in England and became a global hit after a BBC television segment in 1973. Over 50 million copies have been sold worldwide.
Yes. Donald Knuth proved in his 1976 paper 'The Computer as Master Mind' that the standard six-color, four-position game can always be solved in five or fewer guesses using a minimax strategy. The average under this strategy is about 4.48 guesses.
At each step, choose the guess that minimizes the maximum possible remaining candidate set size after the feedback. In other words, for each possible guess, consider the worst-case feedback, and pick the guess with the smallest worst case. The first guess in Knuth's strategy is 'AABB.'
In the standard game with six colors and four positions, allowing repeated colors, there are 6 to the 4th power possible codes, which is 1,296. The codebreaker must identify the one correct code from this set using at most ten guesses.
Mastermind is a constraint satisfaction problem. Each guess adds a constraint, and the codebreaker eliminates candidates that are inconsistent with the feedback. This is the same logic used in cryptanalysis, where each observation about the ciphertext constrains the possible keys or plaintexts until only one remains.
Mastermind Solver
Solve Mastermind board game codes by entering guesses and black or white peg feedback, with candidate filtering and next-guess suggestions based on Knuth's minimax strategy.
Fallout Terminal Hacking
Play the Fallout terminal hacking minigame: guess the password from a list of same-length words with X/Y correct feedback, across five difficulty levels.
Codenames Analyzer
Analyze Codenames board game clues by ranking grid words against a spymaster's clue word, using semantic association heuristics for the 5x5 word grid.
Codenames Is Secretly a Lesson in Cryptographic Thinking
Codenames by Vlaada Chvatil is a party game built on semantic clustering, ambiguity, and channel capacity. Here is why it teaches real cryptographic thinking.
How Fallout's Terminal Hacking Puzzle Works (and How to Solve It)
Fallout's terminal hacking minigame is a word-deduction puzzle based on Shannon entropy. Here is how it works, the math behind it, and a guaranteed strategy that solves it every time.