Mastermind solver

Enter your guesses and their black and white scores, and the Mastermind solver lists every secret code that still fits and picks the guess that narrows them down fastest. Or think of a code and let it crack it.

Pick a color, then tap a peg to paint it

Codes left

How to use the Mastermind solver

Set up the game first: the classic board has 4 pegs and 6 colors with repeats allowed, but the Mastermind solver also handles 3 to 6 pegs, 4 to 10 colors, and games where colors cannot repeat. In Help me guess, add one row per guess you have played: pick a color, tap the pegs to paint them, and set the score you got. A black peg is a right color in the right place; a white peg is a right color in the wrong place. The right panel shows how many codes still fit, lists them when there are few enough, and suggests the next guess. Use this guess copies it into a new row for you to score.

In Guess my code, think of a code and write it down. The solver makes a guess, you score it, and it keeps going until it has your code. If your scores contradict each other, it tells you that no code fits.

How scoring works

Black pegs are counted first, position by position. For white pegs, count each color the guess and the code have in common, using the smaller of the two counts, and subtract the blacks. For code red, red, blue, green and guess red, blue, red, yellow: one black (red in the first place), and the remaining reds and blue match by color, giving two whites. Some scores can never happen: with 4 pegs, 3 black and 1 white is impossible, because if three pegs are right the fourth cannot be the right color in the wrong place.

How the solver picks a guess

Every possible code is tested against every guess so far; the ones that would have given exactly the same scores are still possible. To choose the next guess, the solver imagines playing each candidate guess and groups the remaining codes by the score they would produce. For the classic 4 pegs and 6 colors it uses Donald Knuth's 1977 rule: pick the guess whose biggest group is smallest, so the worst case is as good as possible, preferring a guess that could itself be the answer. That rule opens with two pegs of one color and two of another (1122) and always finds the code in at most 5 guesses. We checked this by letting the solver play all 1,296 possible codes: none took more than 5 guesses, 4.48 on average.

For other setups it picks the guess that leaves the fewest codes on average. In very large games (up to a million codes with 6 pegs and 10 colors) it scores the guesses on a random sample of the remaining codes, and the page says when it does.

Worked example

Press Example. The first guess, red red yellow yellow, scored one black and one white; the second, red green blue blue, scored two whites. The solver lists the codes that fit both scores and suggests a guess that splits them into small groups, so the next score tells you as much as possible.

The same habit, keeping only the possibilities that fit everything you have seen, is how the minesweeper solver finds safe cells. For clues written as sentences, try the logic grid puzzle solver.

Solver tested . Found a problem? Tell us.

Questions

What is the best first guess in Mastermind?

For 4 pegs and 6 colors, Knuth's minimax rule opens with two pegs of one color and two of another, such as 1122. Whatever the score, at most 256 of the 1,296 codes are left.

How many guesses does it take to solve Mastermind?

With 4 pegs and 6 colors, Knuth's strategy always solves it in 5 guesses or fewer. This solver uses that strategy and finds every one of the 1,296 codes within 5 guesses.

What do black and white pegs mean?

A black peg means one of your pegs is the right color in the right place. A white peg means a right color in the wrong place. The pegs do not tell you which of your pegs they refer to.

Why does the solver say no code fits?

The scores you entered contradict each other, so at least one guess or score was copied wrong. Check each row, especially the difference between black and white.

Can it solve games with more pegs or colors?

Yes: 3 to 6 pegs and 4 to 10 colors, with or without repeated colors. Larger games take a moment longer to compare all the guesses.