Code Breaker
The board above has hidden four pegs drawn from six colours, and repeats are allowed, so 3-3-3-3 is as likely as anything else. Every guess you submit comes back as two numbers: the filled dot counts pegs of the right colour in the right slot, and the hollow dot counts pegs of the right colour sitting somewhere else in the code. Ten rows, 1296 possible codes, and nothing is downloaded or fetched while you play - the code is chosen in your browser and never leaves it.
Every page in this section
- Bulls and CowsBulls and Cows, playable in the page
- Number Guessing GameNumber Guessing Game, playable in the page
What the two counts actually count
Every peg in the hidden code answers for exactly one peg in your guess, and the pegs that are already in the right place come off both sides before the second count is worked out. That sentence is the whole of the scoring, and it is the part a code breaker game most often gets wrong. Say the code is 1-2-2-2 and you play 1-1-2-2. Slot one matches, and so do slots three and four, which is three filled dots. What is left over is a single 2 in the code against a single 1 in your guess, and they are not the same colour, so the hollow count is zero. The answer is three and nothing – your spare 1 has nothing left to pair with, because the code’s only 1 was spoken for by the exact match in slot one.
Count that spare peg a second time and the board hands back three and one instead. It looks harmless. It is not: a player who trusts it reasons carefully towards a code that cannot exist, runs out of rows, and is entirely right to blame the board. Repeats are what make this delicate, which is why this page is the one with repeats switched on.
The answer this board can never give you
Three filled dots and one hollow dot is impossible on a four-peg board, and not because of anything the code happens to be. If three pegs are exactly right, the only unmatched slot left is the fourth, and any copy of your fourth colour elsewhere in the code is sitting in a slot that has already been matched and taken off the table. So of the fifteen ways two counts could add up to four or less, only fourteen ever appear.
That is worth more than a curiosity. Three filled dots always means the same thing: one of your four pegs is the wrong colour, and no rearrangement of what you have played can fix it. It does not tell you which slot is wrong, so the field is four slots multiplied by the five colours the odd peg could become – exactly twenty codes, whatever you had played. Twenty is nearer the end than the middle, though not as near as it feels: played out from there, the board’s own solver still needs two more rows for thirteen of the twenty and three more for another four of them.
Ten rows, and why five is enough
Six colours in four slots is 6 x 6 x 6 x 6, or 1296 codes. The star button plays Donald Knuth’s minimax rule over that list: of every code it could play next, it picks the one whose worst possible answer leaves the fewest codes standing. On an empty board the sweep settles on two pegs of one colour and two of another – 1-1-2-2 in the digits printed on the pegs – and the worst that can happen to it is 256 survivors out of 1296. Three different answers tie at that number, and no other opening does better; all 1296 were swept to find out.
Run that solver against every one of the 1296 codes in turn and it never needs a sixth row. One code falls in a single row, six in two, sixty-two in three, 533 in four and 694 in five, for an average of just under four and a half. Ten rows is deliberate slack, not a difficulty setting: it is roughly twice what the machine needs, which is about the right allowance for a person who is also holding a cup of tea.
The button that rules a peg out has never seen the code
The third button under the board strikes one peg in a row you have already played, dimming it and drawing a cross over it. What matters is what that button is not given. The function behind it is handed your rows and their two counts and nothing else – the same information printed on your screen – and it only ever reports something that holds for every single code still consistent with them. It cannot quietly steer you towards the answer, because it does not have the answer.
Two of its deductions need no list at all. A row that scored nothing whatsoever names every colour in it as absent from the code, so those pegs are wrong in every slot they appear in, on that row and on any other. And in any row with no filled dots, every peg is in the wrong slot by definition. Past those it lists the codes still standing and looks for a slot that not one survivor agrees with. When nothing at all can be deduced it says “Nothing can be ruled out from here.” rather than inventing something plausible.
Every control under the board
The palette holds six pegs, and each one is a colour and a shape and a digit at the same time, so nothing here depends on telling red from green. Pick a peg and it drops into the highlighted slot, after which the highlight moves to the next empty slot and wraps round to the start if it has to. The tick submits the row. The backspace symbol empties the highlighted slot, or steps back one slot if that one is already empty. The star fills the row with the machine’s next guess – it shows “Working it out…” while it sweeps – and then prints how many codes are still consistent with your rows, a line that reads “1296 codes still fit.” on an empty board. It only fills the row; you still submit it yourself. New game deals a fresh code.
By keyboard: 1 to 6 place pegs, the arrow keys move the highlight, backspace empties a slot and Enter submits. Those keys answer only while the board itself has focus, so pressing a digit while you are reading this far down the page cannot disturb a game. A row with a slot still empty is refused rather than accepted – the row is outlined and an exclamation mark appears in the score column – and a refused row does not cost you one of the ten. Win or lose, the last row on the board is the code itself.
Nearly three codes in four repeat a colour
Of the 1296 codes this board can hide, only 360 use four different colours – six choices, then five, then four, then three. The other 936 repeat at least one peg, which is 72 per cent of the field, and it is worth holding on to because the instinct runs hard the other way. A player who quietly assumes the four pegs are all different has discarded nearly three quarters of the possibilities before playing a single row, and will spend the rest of the game reasoning carefully towards codes that were never candidates.
It is also the reason the scoring above has to be exact rather than approximately right. On a board where repeats are forbidden, adding the two counts together tells you cleanly how many of your colours appear in the code at all, because each of your pegs is either in it or not. Here that sum means much less, since two of your pegs can be competing for one peg in the code – and the board with repeats switched off is where the cleaner arithmetic lives, if that is the codebreaker game online you actually came for.