Mastermind

Crack the secret colour code in 10 guesses or fewer.

Open tool

Overview

Mastermind is the classic code-breaking game. A secret four-peg colour code is chosen at random from six colours, and you have ten guesses to crack it. After each guess, feedback pegs report how many of your colours are in the right position (black peg) and how many are correct colours but in the wrong position (white peg).

This version supports unique-colour and repeats-allowed modes, an in-game guess history, and adjustable code length and palette size if you want to scale the difficulty up or down. Mastermind is a great practical exercise in information theory because each guess carves the remaining possible-code set into smaller and smaller subsets.

How it works

The secret code is generated when the game starts, either by sampling four distinct colours or by sampling with replacement, depending on the mode. Each of your guesses is scored against the secret: first, every peg matching by colour and position counts as a black peg; then, among the remaining pegs, each guess colour that appears in the unmatched secret pegs counts as a white peg. Colours are paired off so neither side double-counts.

A perfect strategy for the classic 4-peg, 6-colour game is Donald Knuth's five-guess algorithm. It picks the guess that minimises the maximum remaining possible-code set after the worst-case feedback, and it provably solves any code in five guesses or fewer. Random play averages about seven guesses; pattern-based play (varying colours and inferring constraints) typically lands in five or six.

Examples

  • Guess Red Red Green Blue, secret Red Green Blue Yellow. One black (Red in position 1), two white (Green and Blue elsewhere).
  • Guess Red Red Red Red, secret with two reds: two black, zero white — even repeated colours count properly.
  • A common opening in 6-colour mode is Red Red Green Green because it tests four colours with maximum information.
  • After two informative guesses, the remaining possible-code set is usually under 30 of the original 1296 (6^4).

FAQ

Can the secret have repeated colours?
Yes if repeats are enabled; the default mode allows them, which gives 1296 possible codes for a 4-peg, 6-colour game.

What does a black peg mean?
A peg that's both the right colour and in the right position.

Can a colour count as both black and white?
No — each guess peg is matched at most once. Black matches take priority over white.

Is there always a guaranteed-five-guess solution?
Yes, by Knuth's algorithm, for the classic 4-peg, 6-colour game. Larger games may need more guesses.

Why don't I see any feedback pegs?
A feedback row with no pegs means every guess colour is missing from the secret entirely.

Try Mastermind

An unhandled error has occurred. Reload ×