Play Code Cracking Puzzle Online
Crack the secret colour code using logic and deduction. Each guess gives you feedback: black pegs for correct colour in the correct position, and white pegs for correct colour in the wrong position. A classic Mastermind-style challenge.
Created by Brian Hamilton
Pick colours and submit your guess
How to play Code Cracking
Work out the hidden colour code from the feedback on each guess.
- The code is 4, 5 or 6 pegs drawn from 6, 8 or 10 colours.
- A black peg means a colour is correct and in the right position.
- A white peg means the colour is in the code but in the wrong position.
- Each peg in the code counts once. Guess two reds against a code holding one red and you get one peg back, not two.
- The reply says how many of each, never which pegs earned them.
- You get ten guesses. Every setting is winnable well inside that — the hardest averages under seven.
Controls: tap a colour to fill the active slot, then submit. Use the duplicates toggle to allow or forbid repeated colours in the code.
Each peg in the code can only be matched once
A guess comes back as two counts: black pegs for colours in the right position, white pegs for colours that are in the code but somewhere else. The rule that catches people out is that the two counts are drawn from the same pool. A colour in the code can account for one peg of feedback, not two.
That only matters when a colour repeats — which is exactly what the duplicates toggle turns on. Guess two reds against a code with one red and you get one peg back, not two.
Eighteen settings, and none of them is a trap
Three code lengths, three colour counts and a duplicates toggle give eighteen combinations, ranging from 360 possible codes to a round million. Every one of them is capped at ten guesses, which raises a fair question: is ten always enough?
It is. Playing each setting against forty randomly chosen codes with a strategy that picks the guess leaving the fewest possibilities on average, the worst case anywhere was nine guesses — and that was on the million-code setting. Most settings average between four and six. No combination the game offers can hand you a code you cannot crack in the guesses you are given.
| Setting | Possible codes | Answers | Average guesses | Worst seen |
|---|---|---|---|---|
| 4 pegs, 6 colours | 360 | 11 | 4.00 | 5 |
| 4 pegs, 10 colours | 5,040 | 14 | 5.35 | 7 |
| 4 pegs, 10 colours, duplicates | 10,000 | 14 | 5.83 | 8 |
| 6 pegs, 6 colours | 720 | 6 | 5.70 | 7 |
| 6 pegs, 6 colours, duplicates | 46,656 | 27 | 5.50 | 7 |
| 6 pegs, 10 colours, duplicates | 1,000,000 | 26 | 6.90 | 9 |
More possibilities can mean an easier puzzle
Look at the two six-peg, six-colour rows. Turning duplicates on multiplies the number of codes by sixty-five — from 720 to 46,656 — and yet the average number of guesses falls, from 5.70 to 5.50. That is not noise, and it is worth understanding, because it is the clearest illustration of what actually makes this puzzle hard.
With six pegs, six colours and no duplicates, every code is a rearrangement of all six colours. So is every sensible guess — which means black plus white always totals six, and the only thing that varies is the black count. There are just six distinct answers a guess can return. Allow duplicates and that jumps to twenty-seven.
The difficulty of a deduction puzzle is not the size of the search space. It is the size of the space measured against how finely each answer can divide it. Six answers splitting 720 codes is a worse position to be in than twenty-seven answers splitting 46,656.
The same arithmetic sets a floor on every setting. With twenty-seven possible answers and 46,656 codes, no strategy whatsoever can average below log27(46,656) ≈ 3.3 guesses; the no-duplicates version needs at least log6(720) ≈ 3.7. Good play lands around 5.5 either way, so there is real room between what is conceivable and what is reachable — the answers are never evenly sized.
How to choose a guess
Open with structure, not a stab. The first guess cannot be wrong, but it can be wasteful. A guess using several different colours divides the field more evenly than one repeating a single colour, so it teaches you more whatever comes back.
Never guess something already ruled out. The commonest waste is a guess that contradicts an earlier answer. It cannot be the code, so the only thing it can teach you is what you already knew. Before committing, check the guess against every reply so far.
Change several pegs at once. Testing one peg per guess is safe and slow. With ten guesses available and an average requirement of five to six, caution is the expensive option — a guess that could resolve three unknowns is worth it.
Read the total before the split. Black plus white tells you how many of your colours belong in the code at all, regardless of position. Settle that first; positions are a separate and later problem.
The five-guess result, reproduced
Standard Mastermind — four pegs, six colours, duplicates allowed, 1,296 codes — is the version Donald Knuth analysed in 1977, showing it can always be cracked in at most five guesses by choosing, at each step, the guess whose worst possible answer leaves the smallest number of candidates.
That setting is small enough to check rather than cite, so it was: building the full strategy tree over all 1,296 candidate guesses reaches a worst case of exactly five, with an average of 4.476 against Knuth's published 4.478. Choosing instead to minimise the average rather than the worst case gets that down to 4.395 — but pushes the worst case out to six. The two goals genuinely pull apart.
What was verified on this page
The feedback function was checked against an independently written one on 2,846,016 guess-and-code pairs, covering every setting small enough to enumerate both sides exhaustively. They agree everywhere, including the repeated-colour cases where the naive counting method goes wrong: guessing two reds against a code holding one red correctly returns a single peg, not two.
The code generator was checked across all eighteen settings over 40,000 draws each: no colour is ever out of range, no duplicate is ever produced when duplicates are switched off, every small setting is fully covered, and no position favours any colour by more than 4.5%. The difficulty labels are honest too — more colours genuinely means more guesses at every code length.
Where the game came from
Mastermind was published as a board game in 1970, but the idea is much older: it is a coloured version of a pencil-and-paper game played with numbers for decades before that. The commercial version's specific shape — four pegs, six colours, ten rows — is what made it a target for analysis, and it became one of the standard examples of a deduction problem small enough to solve completely and rich enough to be worth solving.
More puzzles like this
If you like narrowing things down by elimination, try these: