Code Breaker Game: Rules, How to Read the Pegs and How to Win

By Dawid Siedlarz · · Updated · 3 min read

Play Code Breaker free →

Short answer: In Code Breaker, the computer hides a secret code of coloured pegs and you try to guess it. After each guess you get a black dot for every peg with the right colour in the right place and a white dot for every right colour in the wrong place. You use these dots to work out the code in as few guesses as you can. It plays like the classic board game Mastermind, and you can play Code Breaker online for free in your browser.

Try it right here, or open Code Breaker full screen.

Code-breaking games are older than the board game: Bulls and Cows was played with pencil and paper long before Mastermind came out in 1970. The idea is the same: every guess gets an answer, and the answers together point to only one code.

The rules

  • The secret code has 4 pegs (5 on Hard), each one of 6 colours (8 on Hard).
  • You build a guess by picking colours, then press Check.
  • You get one black dot for each peg that has the right colour in the right place.
  • You get one white dot for each peg that has a right colour in the wrong place.
  • The dots are not in peg order, so you have to work out which pegs they refer to.
  • You win when you get all black dots. You have 10 guesses (12 on Hard).

A worked example

Say the secret code is 1-2-3-4 and you guess 1-3-5-6. Peg 1 is right in the right place: one black dot. The 3 is in the code but in the wrong place: one white dot. 5 and 6 are not in the code: no dots. Your answer is one black and one white.

Repeated colours are counted once per matching peg. If the code is 1-1-2-2 and you guess 1-2-2-2, you get three black dots (pegs 1, 3 and 4) and no white dot, because the code has only two 2s and both are already matched.

A simple strategy for beginners

1. Find out which colours are used

On Easy, where colours do not repeat, guess 1-2-3-4 and then 5-6-1-2. Adding up the dots tells you how many of each group are in the code.

2. Move one thing at a time

Swap two pegs or change a single colour. If the number of black dots changes, you know exactly why.

3. Only play guesses that could be right

Before you check a guess, compare it with every earlier row: would the secret code you have in mind give the same dots? If not, it cannot be the code.

4. On Medium and Hard, test repeats

Because colours can repeat, a guess like 1-1-2-2 is a good opener: it tells you how many 1s and 2s there are.

Can you always win?

Yes, with 4 pegs and 6 colours. In 1977, the computer scientist Donald Knuth showed that every one of the 1,296 possible codes can be found in at most five guesses, by choosing each guess so that the worst possible answer leaves as few codes as possible. You do not need his method to enjoy the game, but it shows that every guess is really a question.

A much simpler rule gets close. We let a computer play every Easy and Medium code, and 3,000 random Hard codes, with the game's own scoring code: a fixed first guess, then always the first code that fits every answer so far (tools/code-breaker-strategy.js).

LevelCodes playedAverage guessesMost guessesGuesses allowed
Easy: 4 pegs, 6 colours, no repeatsall 3604.15610
Medium: 4 pegs, 6 colours, repeatsall 1,2965.02810
Hard: 5 pegs, 8 colours, repeats3,000 random6.611112

The simple rule never ran out of guesses. The gap to Knuth's five is the price of not planning ahead: on Medium it needs about five guesses on average too, but up to eight. For a person the lesson is the same as for the computer: a guess that contradicts an earlier answer is a wasted row.

More logic games

Play Code Breaker free in your browser, or see all free brain games.

Sources

  • Knuth, D. E. (1977). The Computer as Master Mind. Journal of Recreational Mathematics, 9(1), 1–6.