perplexd

Strategy guide

How to crack a 4-digit Mastermind code in four guesses

Mastermind-family puzzles — board-game pegs, "cows and bulls", or LockCode — all obey the same logic. This is the general method: four moves that turn count feedback into certainty, and the arithmetic of why four guesses is usually enough.

What the feedback actually tells you

Each guess returns two counts: digits that are right and in the right position, and digits that are right but misplaced. Two things follow that beginners miss:

The four moves

  1. Eliminate. Use or find a zero (or low-scoring) set. If your opening information includes a "nothing" clue, start from the six surviving digits.
  2. Settle membership before position. Compare overlapping guesses. If 4567 has two members and 4568 has three, then 8 is in and 7 is out — the digit you swapped is the digit the totals talk about. Chain such comparisons until you know the four members.
  3. Probe. With the members known, only arrangements remain — at most 24, usually fewer. Choose a guess whose feedback differs across the remaining arrangements, even if that guess cannot itself be the code. One good probe usually forces the answer; a hopeful guess merely tests it.
  4. Place. A member that scored amber in one guess and green in another has told you its column. Two placements fix the rest by elimination.

Worked example

Opening clues: 0123 → nothing · 4567 → ■1 ●1 · 4589 → ■2 · 6789 → ●1.

  1. The zero removes 0, 1, 2, 3. Six digits remain: 4, 5, 6, 7, 8, 9.
  2. Membership sums: 4567 has two members; 4589 has two; 6789 has one. Since 6789 contributes only one member and 4567 has two, at least one of 4, 5 is in. But 4589 also has exactly two members and shares 4, 5 with 4567 — if both 8 and 9 were in, 6789 would score at least two. So 8, 9 supply at most one member, forcing at least one of 4, 5 into both guesses… run the same comparison once more and the only consistent assignment is: 4 and 5 in, exactly one of 6 or 7 in, 8 and 9 out.
  3. Positions: 4589 → ■2 with 8, 9 dead means 4 and 5 are both correctly placed there: 4 first, 5 second. 4567 → ■1 ●1 then says of 6 and 7, whichever is in sits in the wrong one of columns three/four.
  4. Probe with 4576: if it returns ■4 you're done; ■3 ●0 means the member was 6 placed fourth… each remaining candidate gives distinct feedback, so guess five is a formality. Most days you'll be in by guess three or four.

Why four guesses is usually enough

Knuth showed 1970s Mastermind (6 colours, 4 pegs, repeats allowed) is always solvable in five guesses with minimax play. Digit codes without repeats are gentler: 5,040 starting candidates, and each guess's feedback splits the space into up to 14 classes. Three well-chosen guesses can therefore distinguish ~14³ ≈ 2,700 outcomes — and once opening clues (as in LockCode) have cut the field to single digits of candidates, two probes are almost always decisive. If you're guessing on feel after guess two, you've skipped move 2: go back and settle membership.

Practise on today's puzzle

Play today's LockCode

Every past puzzle has a full deduction walkthrough in the archive.