Code breaker game strategy: crack the code in fewer guesses
The simplest strong strategy for a code breaker game is to guess only codes that agree with every clue so far. In BrainFinch’s Code Breaker that strategy cannot run out of guesses. By our count it needs between 3.0 and 4.1 guesses on average, depending on the level, and never more than 7.
The game above is Code Breaker. Every number on this page about it comes from a program that played out all of its possible codes, and the code breaker game strategy itself takes five steps.
How do the feedback marks work?
After each guess you get one mark for every shape that is in the code. A filled dot means a shape is in the code and in the right place. A ring means a shape is in the code but in a different place. A shape that is not in the code gets no mark.
The marks are not lined up with the shapes. Filled dots are always listed first, so the clue tells you how many shapes are right and never which ones.
Take the code Green, Blue, Yellow and the guess Blue, Red, Green. Blue and Green are both in the code and both in the wrong place, and Red is not in the code. The clue is two rings.
What are the exact rules of BrainFinch’s Code Breaker?
The code is a row of three or four colored shapes with no shape used twice. You tap shapes to fill a row, and the row is checked as soon as it is full.
| Stage | Reached at step | Code length | Colors to choose from | Guess limit |
|---|---|---|---|---|
| 1 | 0 to 1 | 3 | 4 | 6 |
| 2 | 2 to 3 | 3 | 5 | 6 |
| 3 | 4 to 6 | 4 | 5 | 7 |
| 4 | 7 and up | 4 | 6 | 7 |
The step starts at the level saved for you and rises by one after every two codes. A round lasts 60 seconds.
Scoring depends on how many guesses you used. A three shape code scores 3 points within three guesses, 2 points on the fourth or fifth and 1 point on the sixth. A four shape code allows one more guess at each tier. Running out of guesses or skipping scores nothing and shows the code.
What is the consistent-guess strategy?
Before each guess, ask one thing of it: if this were the code, would all my earlier guesses have received the clues they did. If yes, the guess is consistent and you may play it. If no, it cannot be the answer.
In steps:
- Play any first guess. With no repeated shapes allowed, every first guess is equally good.
- Read the clue.
- Think of a candidate for the next guess.
- Score each earlier guess against the candidate as if the candidate were the code. If any clue comes out different from the one you saw, drop the candidate and try another.
- Play the first candidate that passes, and go back to step 2.
Here is a game from our program at stage 1, with the code Green, Blue, Yellow. There are 24 possible codes at the start.
| Guess | Clue | Codes still possible |
|---|---|---|
| Blue, Red, Green | 2 rings | 9 |
| Red, Green, Yellow | 1 filled dot, 1 ring | 3 |
| Green, Blue, Yellow | 3 filled dots | solved |
After the second guess, the three codes left were Red, Yellow, Blue and Green, Blue, Yellow and Yellow, Green, Blue. The player picked one of them and it happened to be right. Had it been wrong, the clue would have settled which of the other two it was.
How many guesses does the consistent strategy need?
It needs about three guesses for the short codes and about four for the long ones, and it always finishes inside the limit.
“Pick any consistent code” leaves the choice open, so we measured it two ways. For the average, we assumed the player picks at random among the consistent codes, and worked out the exact expected number of guesses over every possible code and every possible sequence of picks. For the worst case, we took the unluckiest picks at every turn. Nothing here is sampled.
| Code | Possible codes | Average guesses | Worst case | Guess limit | Chance of the full 3 points |
|---|---|---|---|---|---|
| 3 shapes from 4 colors | 24 | 3.03 | 4 | 6 | 68.1 percent |
| 3 shapes from 5 colors | 60 | 3.30 | 5 | 6 | 56.1 percent |
| 4 shapes from 5 colors | 120 | 3.95 | 6 | 7 | 71.6 percent |
| 4 shapes from 6 colors | 360 | 4.13 | 7 | 7 | 66.4 percent |
At the top stage the worst case equals the limit. It is rare: with random consistent picks, a game runs to seven guesses about once in 10,000.
The spread at the top stage looks like this.
| Guesses used | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
|---|---|---|---|---|---|---|---|
| Share of games | 0.3 percent | 2.8 percent | 17.1 percent | 46.3 percent | 30.8 percent | 2.8 percent | 0.01 percent |
The game’s own code includes a plainer player that always takes the first consistent code in a fixed order. It averages 3.08, 3.33, 3.98 and 4.15 guesses across the four stages, with worst cases of 4, 5, 6 and 6. That player is how the guess limits were sized.
Can you do better than picking any consistent code?
A little. If you choose among consistent codes with care, the worst case at stage 2 drops from 5 guesses to 4, and at stage 4 from 7 to 6. At stages 1 and 3 it stays at 4 and 6.
Choosing with care means preferring the guess whose possible clues split the remaining codes into small groups. A guess that could come back with many different clues tells you more than one that will almost always come back the same.
In a 60 second round this is rarely worth the time. The consistent strategy already earns full points in most games, and the seconds you spend comparing candidates are seconds not spent on the next code.
What does the first clue tell you?
At stage 1 the first clue leaves at most 9 of the 24 codes. Every first guess gives the same breakdown, which we counted:
| First clue | Codes left |
|---|---|
| 2 rings | 9 |
| 1 filled dot, 1 ring | 6 |
| 1 filled dot, 2 rings | 3 |
| 2 filled dots | 3 |
| 3 rings | 2 |
| 3 filled dots | 1, the guess itself |
With three shapes drawn from four colors, your guess leaves out one color, so at least two of your shapes are in the code. That is why there is no clue with fewer than two marks.
Three marks mean the code uses exactly the three shapes you tried, in another order. Two marks mean the missing color is in the code and one of yours is out.
What did Knuth prove about the classic game?
Donald Knuth showed that the classic board game can always be won in five guesses or fewer. The game is Mastermind, invented in 1970 by Mordecai Meirowitz, and the classic version has four pegs and six colors with repeats allowed, which gives 1,296 possible codes.
Knuth’s paper, “The Computer as Master Mind”, appeared in volume 9 of the Journal of Recreational Mathematics, dated 1976 to 1977. His method starts with a guess of two pegs each of two colors. For every later guess it looks at each possible clue, counts the codes that would remain, and plays the guess whose worst clue leaves the fewest.
It differs from the consistent strategy in two ways. Knuth’s method may play a guess that is not among the remaining codes when that guess splits them better. And it takes far more counting than a person can do in a timed round.
Our game does not allow repeats, so its numbers are smaller. The top stage has 360 codes against 1,296.
Where can you practice?
Play Code Breaker with the five steps above and count how often you finish inside the full-points window. People get quicker at this with practice, and the score reflects only that.
For more deduction, Mini Sudoku uses the same elimination habit on a grid and has a rules page for the 4x4 version. Logic Gate asks you to follow on and off signals through a circuit, explained in our beginner’s guide to logic gates. Balance Scale has you rank three shapes by weight from two clues.
Common questions
What is the best simple strategy for a code breaker game?
Only guess codes that agree with every clue you have received so far. Each guess then has a real chance of being the answer, and every clue removes more of the remaining codes.
How many guesses does it take to crack the code in BrainFinch's Code Breaker?
With the consistent-guess strategy, a three shape code takes 3.0 to 3.3 guesses on average and a four shape code takes 3.9 to 4.1. The worst case is 7 guesses, which is within the limit.
What did Knuth prove about the classic four peg game?
Donald Knuth showed that the classic game with four pegs and six colors can always be solved in five guesses or fewer. His method picks each guess to keep the largest group of remaining codes as small as possible.
Can a shape repeat in the Code Breaker code?
No. Every shape in the code is different, and the game will not let you enter the same shape twice in one guess.