Mastermind Solver - Free Online Mastermind & Bulls and Cows Solver
What Is a Mastermind Solver?
Mastermind is the classic code-breaking board game: a hidden code of four coloured pegs is hidden, and after each of your guesses you learn how many pegs are the right colour in the right place (black pegs) and how many are the right colour in the wrong place (white pegs). Its number cousin Bulls and Cows plays the same way with four distinct digits. A solver takes your guesses and their feedback and answers the real question: which codes are still possible, and what should I guess next?
This Mastermind solver supports both variants. It filters the full code space - 1,296 codes for Mastermind, 5,040 for Bulls and Cows - against every guess you have entered, then runs the Knuth minimax algorithm to recommend the guess that mathematically minimises the worst case. That is the same strategy Donald Knuth published in 1977, which proves any Mastermind code can be cracked in five guesses or fewer.
Mastermind and Bulls & Cows Rules
Mastermind: a code of four pegs from six colours (labelled A-F here), repeats allowed - 1,296 possible codes. Feedback: black pegs for exact positions, white pegs for right colour in the wrong position.
Bulls and Cows: a code of four distinct digits 0-9 - 5,040 possible codes. Feedback: bulls for exact digit-position matches, cows for correct digits in the wrong position.
Feedback is exact. The counts are computed against the hidden code, including careful handling of repeated colours in Mastermind.
How to Use the Mastermind Solver
Pick your variant. Toggle between Mastermind (colours A-F) and Bulls & Cows (digits 0-9) at the top.
Enter each guess you have already played - four characters per row.
Enter the feedback. Put the black/bull count in the ● box and the white/cow count in the ○ box, exactly as the game told you.
Press Filter. The solver lists how many codes remain and suggests the next guess with the smallest worst case, plus every surviving code when there are 24 or fewer.
Reset or demo. Reset clears the rows; Demo loads a real two-guess example so you can watch the candidate list collapse.
What the Solver Tells You
Below, the opening guess AABB with feedback (1 black, 1 white) cuts the 1,296-code space to 208 candidates; adding BCDE with (1, 1) leaves 39 - and the solver recommends the guess that keeps the worst case at just 5:

If your feedback numbers cannot be true for any code - a mistyped count, usually - the solver reports that the feedback is inconsistent instead of guessing:

How the Mastermind Solver Works
Two algorithms cooperate here. First, consistency filtering: a code survives only if playing every one of your guesses against it reproduces the exact feedback you recorded. This uses the correct duplicate-colour semantics - whites are counted as "colour matches minus blacks" per colour, so codes with repeated pegs are judged fairly.
Second, Knuth's minimax: for every code in the entire space, the solver simulates playing it against your surviving candidates and buckets them by the feedback they would produce. The guess whose largest bucket is smallest is the one that guarantees the most progress no matter what the hidden code is - and when several guesses tie, the solver prefers one that is itself a possible answer. On the full Mastermind space that is 1,296 × 1,296 comparisons, done in about half a second entirely in your browser.
Worked Example: AABB → BCDE → Home
Guess AABB against the hidden code. Feedback comes back (1 black, 1 white). The solver filters 1,296 codes down to 208 and recommends AACD by name.
Guess BCDE (the solver's next suggestion after AACF fails you, say) and record (1, 1). The pool drops to 39, and the new recommendation leaves a worst case of just 5 candidates.
One or two more disciplined guesses and the code is forced out - five guesses is the Knuth guarantee for the whole game.
The key insight the solver makes visible: early guesses should test new colours rather than chase blacks - a guess that sounds "bad" because it scores 0 can still be the information-maximising play, exactly like the SPOIL move in our Wordle example.
Tips for Winning Mastermind
Open with doubled colours. Knuth's 1122 (or AABB) is provably optimal: repeats split the code space fastest because they test the repeat rules immediately.
Test four new colours in guess two. Whatever the first guess returned, a second guess of four fresh colours maximises coverage - resist the urge to chase early blacks.
Watch the white pegs. Whites are positional information in disguise: every white peg means "this colour, but slide it" - track which positions each colour has been banned from.
Handle duplicates carefully. In Mastermind a grey result on a repeated colour does not remove it entirely - only the copies beyond those already scored. This solver implements the rule exactly, so trust its filtering.
Minimax beats intuition at the endgame. With two candidates left, guessing one at random is a coin flip; a guess that separates them guarantees success.
Is Mastermind Good for Your Brain?
Mastermind is applied deductive logic: each guess is an experiment, each feedback a constraint, and the solution emerges from intersection after intersection. It trains hypothesis testing, information evaluation (which experiment teaches me the most?) and disciplined bookkeeping of what has been eliminated - the same skills behind scientific thinking and debugging. The Bulls and Cows variant adds mental arithmetic on digit positions, making the pair a complete deduction workout.
Frequently Asked Questions
How does this Mastermind solver work?
It filters the complete code space against your guesses and feedback using exact duplicate-aware semantics, then applies Knuth's minimax to suggest the next guess with the smallest worst-case candidate pool.
What is the Knuth minimax strategy?
Published by Donald Knuth in 1977: always play the guess that minimises the maximum number of candidates that could remain after any feedback. For standard Mastermind it guarantees a solve in five guesses or fewer.
What is the best first guess in Mastermind?
Any guess with the worst case of 256 candidates - AABB, 1122 and their rotations all achieve it. The solver's first recommendation will always be one of these optimal openers.
Can the solver prove my feedback is wrong?
Yes - when no code in the entire space reproduces your feedback history, the solver says so. That usually means a black/white count was swapped or mistyped.
Does it work for Bulls and Cows?
Yes - switch to the digits mode: four distinct digits 0-9, feedback bulls and cows. The code space grows to 5,040 codes and the minimax search adapts automatically.
Why does filtering matter more than the suggestion?
Because the candidate list is the real state of the game. Once only a handful codes remain, any human can finish - the hard part is keeping the list correct, which is what the solver guarantees.
Is this Mastermind solver free?
Completely free with no sign-up. Filtering and the minimax search run entirely in your browser after the page loads, so it even works offline.
Play Code-Breaking Games
Ready to crack codes yourself? Play our free Bulls and Cows (Mastermind) game, the number-logic Crack the Code, or explore more tools like the 24 Game Solver.
