Mastermind Game Rules, and Nibbler Near-Death Experience

And here’s why all your circuits should have some kind of reverse-polarity or over-voltage protection: I nearly killed Nibbler yesterday. I brought the hardware to a friend’s house for a nerdfest party, but when I demoed it, I accidentally plugged in the 9V AC power supply from Mozart’s Credit Card instead of the normal 5V DC supply. I’ve gotten lazy with my circuit designs, preferring to use an external regulated 5V supply instead of including power regulation on the board. Nibbler has no voltage regulator, rectifier, reverse-polarity protection, or fuse, so the 9V AC went straight to the power pins on all the chips. Baaaaaad!
Fortunately my mistake didn’t release the magic smoke from the chips. The speaker buzzed and the LCD showed some strange lines, and at first I thought a wire must have come loose during transport. It only took me about 10 seconds to recognize my error, and luckily Nibbler was fine after switching to the right power supply. If I’d left the 9V AC supply plugged in for longer, I think the board would have been toast.
Square Pegs in a Round Hole
Once Nibbler was up and running, we tried the Mastermind program that I wrote a few months back. It was a hilarious example of how six very smart guys can take fifteen minutes and a lot of arguing to solve a single Mastermind puzzle, but it also exposed some subtleties in the rules of Mastermind that I’d never considered before.
Most readers are probably familiar with the game: a codemaker chooses a four-element secret color code , and a codebreaker tries to guess it. After each guess, the codemaker gives feedback in the form of black or white pegs: a black peg means some element is the right color and in the right position, while a white peg means some element is the right color but in the wrong position. But what does that mean exactly, and what elements does it refer to?
Imagine you’re the codebreaker, and your guess is yellow-red-blue-green:

The codemaker comes back with three black pegs and one white peg:
![]()
Depending on your interpretation of the rules, you may think this is an invalid result, and the codemaker has made a mistake. After all, if three of the guesses are the right color in the right position, then there’s only one incorrect position remaining. The color guess at that position must either be correct (which would result in a black peg) or incorrect (which would result in no peg at all). Right? Wrong!
The critical question is whether the black and white pegs reflect the codebreaker’s guess with respect to the secret code, or the secret code with respect to the codebreaker’s guess. It might seem that it would be the same either way, but it’s not. Translated into pseudo-code, the two possible scoring algorithms are:
Secret-centric algorithm:
for (i in the range 1 to 4) {
if (secretCode[i] == guess[i]) {
emit black peg;
}
else (for j in the range 1 to 4) {
if (secretCode[i] == guess[j]) {
emit white peg;
break;
}
}
}
Guess-centric algorithm:
for (i in the range 1 to 4) {
if (guess[i] == secretCode[i]) {
emit black peg;
}
else (for j in the range 1 to 4) {
if (guess[i] == secretCode[j]) {
emit white peg;
break;
}
}
}
If we assume the secret code is yellow-green-blue-green:

and with the yellow-red-blue-green guess shown earlier, then the secret-centric scoring algorithm will result in black-black-black-white, but the guess-centric algorithm will result in black-black-black.
I think most people play the game using the guess-centric scoring algorithm, without realizing it. It seems more natural, as the score pegs are a reflection of how closely the guess matches the secret code, not how closely the secret code matches the guess. But surprisingly, I can’t find any definitive answer to which algorithm is correct. The online descriptions of Mastermind rules that I’ve found are vague on this point, or contradictory. The Wikipedia description of the rules implies both of these scoring algorithms are wrong, but isn’t specific enough to define an alternate algorithm. This page sounds like the guess-centric algorithm in its text, but the provided example uses the secret-centric algorithm. Unfortunately the official Hasbro game rules for Mastermind to Go only gives one example, which is ambiguous as to which algorithm should be used. The Parker Brother game rules are also ambiguous, and don’t give any examples. But the published rules for Pressman Ultimate Mastermind (which uses five colors instead of four) uses the secret-centric algorithm.
So how can we know which algorithm is the right one? I’m not sure we can. Mastermind is just a modern version of an old pencil-and-paper game called Bulls and Cows, which may date back more than a century. I couldn’t find anything definitive about the origins of that game or the correct rules. Ultimately it doesn’t really matter which scoring algorithm is used, as long as both players agree on it. For Nibbler I chose the secret-centric algorithm. Even though it’s less intuitive to me, it seems to be the most common choice among those people who pay attention to this quirk in the game rules. What do you think?
Read 3 comments and join the conversation
3 Comments so far
Leave a reply. For customer support issues, please use the Customer Support link instead of writing comments.


I haven’t played Mastermind for a long time, but my recollection of my interpretation of the rules was that I would give a black for each match, then exclude those positions and give a white for each remaining guess which matches any of the remaining secrets.
Hmm, I like that way of looking at it. I think you’re probably right.
After some thought, he’s an example that will separate the three possible scoring algorithms.
secret: yellow-red-yellow-orange
guess: yellow-red-red-red
secret-centric scores 2 blacks and 1 white
guess-centric scores 2 blacks and 2 whites
Tom Davies scores 2 blacks
As a person who also programmed 2 Mastermind programs on programmable calculators back in 1997 and 2002 if I remember,
the first question I had to ask myself when writing the comparison loop was whether the program was going to be secret code centric,
or guess centric. You’d get two different key peg combinations if at least one color repeated based on which perspective was used, secret code centric or guess centric.
There were no online manuals to answer this question.
There are two ways to solve this ambiguity.
1. Restrict the rules to either secret code centric or guess centric: The original poster said himself that there is no general establishment of which.
2. Have rules where this ambiguity doesn’t matter, because both secret code centric and guess centric comparisons produce the same key peg combination: This how my programs played, and most other programs play.
1st point:
From secret code centric logic standpoint, A key peg represents a one to one connection between one code element, and one guess position. It is (not they are) in the right position or wrong position, not both.
The statement is the same if it’s reworded for guess centric logic.
When there are repeat colors in the code or guess, you are going to have ambiguity if you are comparing a code element or a guess position again,
after you have already established one connection before, involving at least one of them with a black or white key peg.
So once you’ve established a key peg for a relationship, you would disregard any comparisons to that code element and that guess position again,
and consider only the remaining elements and positions leftover in the code, and the guess.
Rule (1): Once you’ve established a key peg for the relationship between a code element and a guess position, you disregard any further comparisons to that code element and that guess position,
and consider only the remaining elements and positions leftover in the code, and the guess.
2nd point:
There is no general rule that states whether to consider the code and guess from left to right, right to left,
from third element or position to first element or position, etc.. That’s also ambiguous.
When a black or white peg matches colors that repeat in the secret code or guess, either a black or white key peg must take priority,
since the peg establishes only one match. The peg cannot represent both black and white. The white peg cannot take priority because depending on secret code centric or guess centric,
you could arguably never have 4 black pegs. This is because it’s ambiguous what to compare first. So you could start with the repeated color, and then the white peg would always take priority.
Rule (2): When colors repeat in either the code or guess, a black key peg must take priority over a white peg. So you must look for any black pegs first.
So with the yellow-green-blue-green secret code, and the yellow-red-blue-green guess; code centric or guess centric doesn’t matter, if you follow these two general rules.
You will always have 3 black pegs and no white pegs. Matter of fact, code centric or guess centric,
the combinations of key pegs will always match with these 2 rules.