Introduction
Mastermind is a code-breaking board game where one player sets a secret code of four colored pegs and the other player tries to crack it using feedback after each guess. Mordecai Meirowitz, an Israeli postmaster and telecommunications expert, invented the game in 1970. Invicta Plastics published it in 1971 and it went on to sell over 50 million copies worldwide. Enter your guesses and the black and white peg feedback above. The solver narrows down the remaining candidates and suggests the next guess using a minimax strategy. All computation runs in your browser.
What this tool does
- Filters candidate codes against your entered guesses and feedback. The solver starts with all 1,296 possible codes (6 colors, 4 positions) and removes any code inconsistent with the feedback you received.
- Suggests the next guess using a minimax heuristic inspired by Donald Knuth's 1976 algorithm. The solver picks the guess that minimizes the worst-case remaining candidate count.
- Shows remaining candidates so you can see how many codes are still possible after each round of feedback.
- Parses a simple text format: enter each guess as four color letters followed by black and white peg counts, like "RGBY 1 2".
- Uses six standard colors: R (Red), B (Blue), G (Green), Y (Yellow), O (Orange), P (Purple). Colors may repeat within a code.
- All solving runs client-side. No data is sent to any server.
How this tool works
The solver generates all 1,296 possible codes at startup (6 colors to the power of 4 positions). When you enter a guess with its feedback, the solver evaluates that guess against every remaining candidate. It keeps only candidates where the evaluation produces the same black and white peg counts you entered. A black peg means a peg is the correct color in the correct position. A white peg means a peg is the correct color but in the wrong position. Each peg is counted at most once. After filtering, the solver suggests the next guess. For the first move, it returns a known strong starting guess (RRGG) to avoid expensive computation. For subsequent moves, it runs a minimax search over all possible guesses: for each guess, it partitions the remaining candidates by their feedback pattern, then picks the guess whose largest partition is smallest. Ties prefer guesses that are themselves candidates. This approach guarantees the code can be solved in five or fewer guesses from the first move, as Knuth proved.
How the Mastermind solver algorithm works
Mordecai Meirowitz invented Mastermind) in 1970. According to the Wikipedia article on Mastermind), Meirowitz was a postmaster and telecommunications expert in Israel who based the game on earlier pencil-and-paper games like Bulls and Cows. Invicta Plastics manufactured the game in the UK starting in 1971, and it became a commercial hit, selling over 50 million copies.
The game uses six colors and four positions. The codemaker secretly arranges four colored pegs behind a shield. The codebreaker has up to 10 attempts to guess the code. After each guess, the codemaker places black key pegs for each correct color in the correct position and white key pegs for each correct color in the wrong position. The codebreaker must deduce both the colors and their positions from this feedback alone.
Donald Knuth, the computer scientist known for The Art of Computer Programming, published a paper in 1976 titled "The Computer as Master Mind" (PDF). In it, Knuth proved that the code can always be solved in five or fewer guesses using a minimax strategy. His algorithm works as follows: start with all 1,296 possible codes. After each guess and its feedback, eliminate codes that are inconsistent. Then choose the next guess that minimizes the maximum number of remaining candidates across all possible feedback outcomes. Knuth showed that this strategy never requires more than five guesses, with an average of 4.478 guesses. The Wikipedia article on the Mastermind board game) describes this result in the "Five-guess algorithm" section.
This solver implements a version of Knuth's minimax approach. For performance, it uses a hardcoded starting guess (RRGG, equivalent to Knuth's AABB) on the first move. After that, it partitions candidates by feedback pattern and selects the guess with the smallest maximum partition size.
How to use this tool
- Enter your guess and feedback in the format: RGBY 1 2. The four letters are your color guess (R, B, G, Y, O, P). The first number is black pegs (correct position). The second number is white pegs (wrong position).
- Add one line per guess. The solver processes each line and filters the candidate set incrementally.
- The output shows the number of remaining candidates and a suggested next guess using the minimax heuristic.
- If the candidate count drops to 20 or fewer, the solver lists all remaining possible codes.
- If no candidates remain, check your feedback entries for errors. A single wrong peg count can eliminate all possibilities.
- Use the suggested guess in your actual game, then enter the new feedback to continue narrowing down.
Real-world examples
First guess with no feedback yet
A player starts a new game. They enter no guesses yet. The solver displays the instructions and the color reference. The suggested first guess is RRGG (Red, Red, Green, Green). This is a known strong starting guess from Knuth's algorithm. The player makes this guess in their physical game and receives feedback: 1 black, 1 white.
Narrowing down after two guesses
The player enters two lines: RRGG 1 1 BBYY 0 2 The solver filters all 1,296 codes against both guesses. The first guess eliminates codes that do not produce 1 black and 1 white when compared. The second guess further narrows the set. The output shows the remaining candidate count (typically around 10-20 codes after two guesses) and suggests the next guess. If 20 or fewer candidates remain, the solver lists them all so the player can cross-reference.
Detecting a feedback error
A player enters three guesses but makes an error on the second line, recording 2 black pegs when the game actually showed 1. After the third guess, the solver reports "No consistent codes remain." This means no four-color combination produces all three feedback patterns simultaneously. The player reviews their entries, corrects the second line, and the solver produces a valid candidate list again. This is a common use case: catching transcription errors during a long game.
Comparison with similar methods
| Method | Complexity | Typical use |
|---|---|---|
| Knuth's minimax (this solver) | O(n^2) per guess | Guaranteed solve in 5 or fewer guesses |
| Random guessing | O(1) per guess | Baseline, average ~7-8 guesses |
| Brute force enumeration | O(n) per guess | Lists all consistent codes, no optimal suggestion |
| Entropy-based selection | O(n^2) per guess | Research variant, slightly better average than minimax |
Limitations or considerations
This solver implements a heuristic version of Knuth's minimax algorithm. It uses a fixed starting guess (RRGG) rather than computing the optimal first move, which would require evaluating all 1,296 guesses against all 1,296 codes. The minimax search in subsequent moves evaluates all possible guesses but may not always match Knuth's exact tie-breaking rules. The solver assumes standard Mastermind rules: 6 colors, 4 positions, colors may repeat. Variants with different color counts or code lengths are not supported. The solver cannot help if you enter incorrect feedback. Always verify your black and white peg counts before submitting. For other puzzle-solving tools, try the Fallout terminal hacking tool, the Codenames analyzer, the frequency analysis tool, or the Caesar cipher.
Frequently asked questions
Can the solver always crack the code in 5 guesses?
Knuth proved in "The Computer as Master Mind" (1976) that a minimax strategy guarantees solving standard Mastermind (6 colors, 4 positions) in 5 or fewer guesses. This solver implements a version of that strategy. It uses a fixed starting guess and heuristic tie-breaking, so it may occasionally take a sixth guess in edge cases. The guarantee holds for the exact algorithm Knuth described.
What do black and white pegs mean?
A black peg means your guessed peg is the correct color in the correct position. A white peg means the color is correct but the position is wrong. Each peg in the secret code is matched at most once. The total of black plus white pegs never exceeds 4. If you guess RRGG and the secret is RBRG, you get 2 black (the two R matches in positions 1 and 3) and 0 white, because the remaining G and B do not align.
Why does the solver suggest RRGG as the first guess?
Knuth's original paper used AABB as the starting guess. This solver uses RRGG, which is equivalent (two of one color, two of another). Starting with a two-and-two split provides good information because it tests four positions with only two colors, maximizing the feedback signal. The first-guess computation is skipped for performance since the optimal move is known.
What if no candidates remain after I enter my feedback?
This means at least one of your feedback entries is incorrect. No four-color code produces all the black and white peg counts you entered. Review each line and verify the peg counts against your actual game board. A single wrong number can eliminate all possibilities. Correct the error and the solver will recalculate.
Can I use this for Mastermind variants with different rules?
No. This solver is configured for standard Mastermind: 6 colors (R, B, G, Y, O, P), 4 positions, and colors may repeat. Variants with 5 or 8 colors, different code lengths, or color restrictions are not supported. You would need to modify the color list and code generation logic in the underlying library to handle those variants.
Conclusion
The Mastermind solver narrows down the 1,296 possible codes using your guess feedback and suggests the next move with a minimax heuristic inspired by Knuth's 1976 proof. Enter your guesses, check the remaining candidates, and use the suggested move. For more puzzle tools, try the Fallout terminal hacking tool, the Codenames analyzer, the frequency analysis tool, or the Caesar cipher.