Puzzle

Black-peg Mastermind solver and simulator

Solves any black-peg-only Mastermind game, where each guess only tells you how many symbols sit in exactly the right place. The black-peg Mastermind simulator lets you play one yourself or compare strategies over many games, all in your browser.

Game size

Code length 10
Symbols 12

12¹⁰ = 61,917,364,224 possible codes Symbols A to L

Your next guess

Type this into your game

Then tap the score your game showed

I typed something different

Type the guess you entered, then tap its score above.

Your guesses

    No guesses yet.

    Where you are

    Codes still possible 61,917,364,224
    Guesses so far 0

    Loading the solver…

    Where each symbol can be

    Chance of each symbol at each position.

    What is black-peg Mastermind?

    A secret code has N positions, each holding one of K symbols. Repeats are allowed. You guess any code of the same length, and the only answer is the number of positions where your guess and the secret have the same symbol, from 0 to N. You never learn which positions matched. The game ends when the answer is N/N; that final guess counts.

    Classic Mastermind answers with two kinds of pegs: black for the right colour in the right place and white for the right colour in the wrong place. Black-peg Mastermind, also called black-peg only Mastermind, gives just the black pegs. One count can be explained in many different ways, which is what makes it hard.

    Example

    Secret GGHHKGAGHF, guess HHHHHHHHHH. The H in positions 3, 4 and 9 matches, so the answer is 3. Nothing says which three.

    How the solver works

    The solver keeps track of every code that still fits all answers so far, without ever listing them. For each code, the number of matches with each past guess forms a tally. Going position by position, a dynamic program counts how many codes reach every possible tally, and only the codes whose tally equals the answers exactly are kept. That gives the exact number of remaining codes, even when it is in the billions.

    The same counts let it draw remaining codes uniformly at random and give the chance of each symbol at each position. To choose a guess, it looks for the one whose answer is the least predictable over those codes, i.e. carries the most information (entropy). It starts from several random and frequency-weighted guesses and improves each by hill climbing: repeatedly applying the single-symbol change that raises the information most.

    The best guess is often not a possible answer. Placing symbols already known to be wrong in some positions makes the count speak only about the others. Near the end, when few codes remain, the solver prefers guesses that could also win.

    How many guesses does it take?

    There are KN possible codes, so finding one takes log2(KN) bits of information. An answer from 0 to N can carry at most log2(N + 1) bits, so no strategy can always succeed in fewer than log2(KN) / log2(N + 1) answered guesses.

    For 10 positions and 12 symbols that is 35.85 bits, with at most 3.46 bits per answer: about 10.4 guesses. Real answers are far from evenly spread. Against a fresh code each position matches with probability 1/12, so the first answer carries only about 1.70 bits, whatever the first guess is. Measured averages at 10 × 12:

    StrategyAverage guesses
    Max information≈ 17.9
    Any possible code≈ 18.5
    Count, then halve≈ 30.0
    Count, then one by one≈ 32.8

    For other sizes, run Compare strategies above; every strategy plays the same secrets.

    The strategies

    Max information recommended
    Each turn, picks the guess whose answer splits the remaining codes as evenly as possible across all possible answers, found by the hill climb described above.
    Any possible code
    Each turn, guesses a random code that still fits every answer. Simple and surprisingly strong, but it never uses the more informative "impossible" probes.
    Count, then halve
    First counts each symbol with guesses like AAAA, BBBB, and picks an absent symbol as a blank filler. Then finds each symbol's positions by binary search: put it in half of the open positions, blanks elsewhere, and split again. The best method that is easy to run by hand.
    Count, then one by one
    Counts each symbol the same way, then fills the positions in turn, trying the most frequent remaining candidate with blanks everywhere else until the answer confirms it.

    Both count methods need a symbol that is certainly absent, which is only guaranteed when there are more symbols than positions. With K ≤ N they are unavailable.

    Known results

    For classic 4 × 6 Mastermind with white pegs, Knuth (1976) showed every code can be found in at most five guesses. For black-peg Mastermind the research is about how the number of guesses grows with very large codes:

    None of these gives the optimal strategy for a specific size like 10 × 12, which is far beyond exhaustive search. A greedy one-step method like Max information is the practical approach.

    FAQ

    Can I use it for a game that only tells me how many are in the right place?

    Yes. That is exactly black-peg Mastermind. Enter each guess and the number of positions that matched, and the solver shows how many codes still fit and which guess to try next.

    Does it work with repeated symbols?

    Yes. Codes may repeat symbols, and guesses may too. A code like AABA is as valid as ABCD.

    What sizes are supported?

    Codes of 2 to 12 positions, with 2 to 16 symbols (the letters A to P). The presets are classic 4 × 6, 5 × 8 and 10 × 12.

    Can I simulate games, not just solve them?

    Yes. The page is also a black-peg Mastermind simulator. Play lets you crack a random secret yourself, and Compare strategies plays many games with every strategy on the same secret codes, then reports the average, median and worst number of guesses. You can also watch a single game step by step.

    Is anything sent to a server?

    No. Everything runs in your browser, in background workers. Your guesses, answers and secret codes never leave the page.

    Why does the solver suggest a code that can't be the answer?

    Because it picks the guess whose answer tells it the most, and that is often a code already ruled out. Such a guess can put known-wrong symbols in some positions to isolate the others. When only a few codes remain, it prefers ones that could win right away.

    What if my game also gives "right colour, wrong place"?

    Then it is classic Mastermind, and this solver ignores that extra information. You can still use it with only the right-place count, but a classic Mastermind solver will need fewer guesses.