How to Play Mastermind — Knuth’s Algorithm + Beginner Strategy

Mastermind was invented in 1970 by Mordecai Meirowitz, an Israeli postmaster and telecommunications expert. The codebreaker version has 6 colours and 4 pegs — that’s 1,296 possible codes. World-class solvers find any code in ≤5 guesses. Here’s the algorithm.
The math: information theory
With 6 colours and 4 positions, every guess gets feedback: black peg = right colour + right position; white peg = right colour, wrong position. The total information from any guess is at most log₂(15) ≈ 3.9 bits. To narrow 1,296 possibilities to 1, you need log₂(1296) ≈ 10.3 bits. So 3 guesses should suffice if information is maximal.
In practice it’s 4-5 guesses because feedback isn’t fully informational and you have to guess specific codes, not feedback-maximising abstract codes.
Strategy 1: First guess — 1122
Donald Knuth proved in 1976 that the optimal first guess is two pairs (e.g., 1-1-2-2 or any colour pair). This guarantees you can finish in 5 guesses or fewer for ANY hidden code.
Other reasonable first guesses: 1-1-2-3, 1-2-3-4 (all different). All test multiple colours.
Strategy 2: Process the feedback
After your first guess, you know how many pegs of which colours are in the code. Use this to eliminate possibilities.
Example: First guess 1-1-2-2 gets 1 black + 1 white. That means: exactly 1 peg matches in position; exactly 1 matches a position but is misplaced. Total: 2 pegs of colours {1, 2} are in the code; 2 are not.
Knuth’s algorithm: enumerate all possible codes; eliminate those inconsistent with all feedback so far; pick the guess that maximally splits the remaining possibilities.
Strategy 3: Don’t reuse known-wrong colours
If your first guess included a 5 and you got no feedback indicating a 5 is present, never use 5 again.
Beginner mistake: reusing colours you’ve eliminated. Every guess of a known-wrong colour is a wasted slot.
Strategy 4: The split-and-conquer
When you have ~10 remaining possibilities, your next guess should aim to halve them. Pick a code that the feedback distinguishes from the others.
Even simpler: pick any remaining possibility. With 8 possibilities left, a guess has at least 5 distinct feedback responses — meaning the next guess narrows to ≤2.
Strategy 5: The endgame
By guess 4-5 you usually have 1-3 possibilities. Pick any one and guess. Worst case you’re wrong once; best case you’re right.
If you have 1 possibility, that’s the answer. If you have 2-3, statistically the centre of your remaining possibilities (in some sense) is your best bet.
Hard mode: 8 colours, 5 pegs
Beyond classic Mastermind: 8 colours × 5 pegs = 32,768 possibilities. World-class solvers handle this in 6-7 guesses. Some Mastermind variants allow blank pegs (representing absence of colour); these add even more possibilities.
Our Mastermind has the classic 6 colours × 4 pegs setup. Master this before chasing harder variants.
Practice
Play our Mastermind. Set the goal: solve every code in ≤5 guesses. Once you can do this 90% of the time, you’re at master level.








