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

Allow Duplicates
Guesses
0
Best
—
Wins
0

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.

How black and white pegs are counted when a colour repeats A four-peg code of red, red, blue, amber against a guess of red, blue, red, green. The first peg matches exactly, giving one black. Of what is left, the code holds a red and a blue and the guess holds a blue and a red, giving two whites. The total is one black and two whites, not three whites, because each peg in the code can be matched only once.
The counting rule that repeated colours make visible.
Average guesses needed for each of the eighteen settings A grouped bar chart of the average number of guesses good play needs, for four, five and six pegs at six, eight and ten colours, with duplicates off and on. Every setting averages between four and seven guesses, well below the ten the game allows. At five and six pegs with six colours the duplicates-allowed bar is lower than the no-duplicates bar, even though allowing duplicates multiplies the number of possible codes.
Every setting the game offers, and how many guesses good play actually needs.

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.

Forty randomly chosen codes per setting, played with a strategy that minimises the expected number of remaining possibilities. “Answers” counts the distinct black/white replies a guess can come back with.
Setting Possible codes Answers Average guesses Worst seen
4 pegs, 6 colours360 114.005
4 pegs, 10 colours5,040 145.357
4 pegs, 10 colours, duplicates10,000 145.838
6 pegs, 6 colours720 65.707
6 pegs, 6 colours, duplicates46,656 275.507
6 pegs, 10 colours, duplicates1,000,000 266.909

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:

🎉 Code Cracked!

You solved it in 5 guesses.