Skip to main content

Mastermind Solver

Crack a Mastermind code from the guesses and peg feedback you already have.

In Mastermind, a hidden code is made of colored pegs. After each guess you are told how many pegs are the right color in the right spot (black pegs) and how many are the right color in the wrong spot (white pegs). Enter the guesses you have made and the feedback you got for each, and this tool lists every code that still fits and suggests a strong next guess.

Click a peg to change its color. Set the black and white peg counts you were told for each guess.

How it works

The solver builds the full set of possible codes for your chosen number of positions and colors, then keeps only the ones that would have produced the exact black and white peg counts you entered for every guess. Black pegs count right color in the right spot; white pegs count right color in the wrong spot, never double counting a peg.

For the next guess it uses Knuth's worst case strategy: it scores each candidate guess by the largest group of codes that any single feedback result would leave behind, and picks the guess that makes that worst case as small as possible. That guarantees fast progress no matter which code is hidden. When only one code remains, that is the answer.