Solver · Live
Mastermind Solver,
crack the code in the fewest guesses.
Use this free Mastermind solver to crack a hidden code of colored pegs or digits. Enter each guess and the black and white pegs you got back, and it lists every code that still fits and suggests the best next guess. It works as a Code Breaker and Bulls and Cows solver too.
Game setup
Mastermind Solver
Quick setup
Picking a quick setup clears the guesses entered so far.
Colors
Pegs in code
Show pegs as
Colored pegs are numbered from 1. Feedback is counted as black pegs (right color, right place) and white pegs (right color, wrong place).
0 possible codes in this game. Everything runs in your browser.
Working out the first guess…
Your guesses so far
Nothing yet. Play any guess in your game, or start with the suggestion below, and add it here with the feedback you got.
Add a guess
Tap a color to fill the highlighted peg. You can also type the number keys, and use the arrow keys and Backspace.
Black pegs
right color, right place
White pegs
right color, wrong place
Next guess
CalculatingCalculating the best first guess…
Enter every guess in the order you played it. The solver only uses the guesses and replies you type.
Field guide
How the Mastermind Solver works.
Mastermind is a code-breaking game for two. One player hides a row of colored pegs, usually four pegs in six colors. The other player guesses, and after every guess the code-maker answers with small pegs: a black peg for each peg with the right color in the right place, and a white peg for each peg with a right color in the wrong place. Bulls and Cows is the same game played with digits, where bulls are black pegs and cows are white pegs. A good guess uses what the earlier replies ruled out.
Using the solver
Pick the number of colors (2 to 10), the number of pegs (3 to 6) and whether the code may repeat a color, or tap a quick setup for the classic game or Bulls and Cows. Tap colors to fill in a guess you have played, set the black and white pegs you were given, and press Add this guess. The tool shows how many codes are left after each guess, lists the remaining codes, and suggests the next guess. Use this guess copies the suggestion into the entry row so you only need to add the reply.
How the next guess is chosen
The solver first removes every code that would have given a different reply to any of your guesses. What is left are the codes that could still be the secret. To judge a possible guess, it sorts the remaining codes into groups by the reply that guess would receive. A group is what you would be left with after seeing that reply, so the biggest group is your worst case:
In the 1970s Donald Knuth showed that picking the guess with the smallest worst case cracks any four-peg, six-color code in five guesses or fewer. This tool applies that rule to every possible guess, not only to codes that could be the answer, and prefers a guess that could be the code when two guesses tie. When a game is too large to test every guess against every code, it scores a sample and labels the suggestion as an estimate.
Worked example
In the classic game there are 6 × 6 × 6 × 6 = 1,296 codes. The tool opens with 1 1 2 2, two pegs of the first color and two of the second. If the reply is no black and no white pegs, the code uses only colors 3 to 6, so 4 × 4 × 4 × 4 = 256 codes remain, which is also the biggest group this opening can leave. The next suggestion is 3 3 4 5, after which at most 46 codes can remain. A reply of one black and one white to the opening leaves 208 codes instead. Enter your own replies and the numbers will follow.
Bulls and Cows and other sizes
Bulls and Cows uses four different digits from 0 to 9, so there are 10 × 9 × 8 × 7 = 5,040 codes. The opening guess 0123 leaves at most 1,440 of them, whatever the reply. Turn off repeats and the same engine handles any size from three to six pegs. A game with six pegs and ten colors has a million codes. They are all counted exactly, but the suggestion then comes from a sample.
Tips
- Enter every reply carefully. One wrong black or white peg can empty the list. The per-guess counts help you spot where it happened.
- Repeats can be worth it. The opening 1 1 2 2 leaves at most 256 codes, while four different colors, 1 2 3 4, can leave up to 312. Do not avoid a guess just because it repeats a color.
- Use the alternatives. Several guesses can be nearly as good as the top one, so pick a code that could win when the numbers are close.
Disclaimer: This tool is an independent fan creation and is not affiliated with, authorized, or endorsed by Invicta Plastics, Pressman Toy Corporation, or Hasbro, Inc. “Mastermind” is a trademark of its respective owner and is used here only to describe the type of puzzle. Provided for practice and entertainment only.