Mastermind
Crack the hidden code from black and white pegs. The scoring is a two-pass count, and the obvious one-pass version is wrong.
Your guesses
The scoring is what everyone gets wrong
Black pegs are right colour in the right place. White pegs are right colour in the wrong place — but each peg in your guess and each peg in the code may only be counted once, so duplicates do not multiply. The correct method is two passes: mark the exact matches and remove them, then count colour overlaps among what is left. Counting overlaps first double-counts every exact match and produces scores that look plausible and are wrong.
Guess red-red-red-red against a code containing exactly one red and you get one black and no whites — not one black and three whites. One consequence falls straight out of the rules and is worth knowing at the table: a response of three blacks and one white is impossible on a four-peg code. If every peg but one is exactly right, the last one has nowhere else to be.
How to use
- Pick colours to build a guess, then submit it.
- Black means right colour in the right place.
- White means right colour in the wrong place.
- Each peg counts once, so repeats do not multiply.
Frequently asked questions
How is a Mastermind guess scored?
In two passes. First mark the pegs that are the right colour in the right place — those are the blacks — and remove them from consideration entirely. Then, among what is left, count how many of your colours appear somewhere in the remaining code. Those are the whites. Doing it in one pass double-counts the exact matches.
What is the difference between black and white pegs?
Black means right colour and right position. White means right colour but wrong position. Crucially neither tells you which peg it refers to, which is the entire game — you learn how many are right without learning which, and have to separate the two by choosing your next guess carefully.
How do repeated colours score?
Each peg is used at most once on both sides. If you guess four reds and the code contains exactly one red, you score one black or one white, not four. The two-pass method handles this automatically because exact matches are removed before overlaps are counted, and a naive count gets it badly wrong.
Can I get three blacks and one white on a four-peg code?
No, and it is a useful thing to know at the table. If three pegs are exactly right, the fourth is the only one left over on both sides — so it is either right, giving four blacks, or wrong, giving three blacks and nothing. There is nowhere else for it to go.
How many possible codes are there?
Colours raised to the power of the length, since repeats are allowed and position matters. The classic four pegs from six colours gives 1,296 codes. Five pegs from eight colours gives 32,768, which is why the longer settings are considerably harder rather than just slower.
How many guesses should it take?
The classic four-peg six-colour game can always be solved in five guesses with optimal play, and a good human player usually manages six or seven. Knuth published the five-guess algorithm in 1977, which works by always guessing whichever code minimises the worst-case number of remaining possibilities.
What is a good opening guess?
Something with a repeat rather than four different colours, counter-intuitively. Two pairs is a strong opener in the classic game because it splits the space of possible codes more evenly than four distinct colours does, which is what actually matters — you want the answer to be informative rather than merely pleasant.
How should I use the information from a guess?
Treat every response as a constraint that eliminates codes, and try to choose next guesses that would divide the survivors roughly evenly whatever the answer. A guess that can only tell you one thing is wasted, even if it feels like progress, and it is often worth playing a code you know is wrong in order to learn more.
Is Mastermind the same as Bulls and Cows?
Almost. Bulls and cows is the older pencil-and-paper game, usually played with digits and usually without repeats. Mastermind is the 1970 board game version with coloured pegs, and it allows repeated colours — which is exactly what makes the scoring subtle rather than obvious.
Does the game know my guesses in advance?
No. The code is generated randomly when the game starts and never changes, so the responses are honest throughout. Some implementations cheat by choosing the code adversarially as you play, which makes the game harder in a way the rules do not describe.
Are longer codes always harder?
Not in proportion. Adding a peg multiplies the search space by the number of colours, but it also makes each response more informative, so the number of guesses needed grows much more slowly than the number of codes. Adding colours generally hurts more than adding length.
Does the game save my progress?
No. Everything runs in your browser, nothing is stored or uploaded, and closing the tab loses the current code and history. Each new game generates a fresh random code.
🔒 This tool runs entirely in your browser. Nothing you enter is uploaded, logged, or stored.