About Mastermind
Mastermind is a code-breaking game where one player (or the computer) creates a secret code of 4 colored pegs, and the other player tries to guess it.
By the ToolFluency team · Updated October 2026
Play Mastermind free online. Crack the secret color code in limited guesses using logic and deduction. Supports two-player code-setting mode. No sign-up needed.
How to use
- Pick a mode: Break the code (the classic: the computer hides a 4-peg code and you crack it), 2 players (player 1 sets the code while player 2 looks away, then player 2 cracks it), or Computer breaks it (you set the code and watch the computer solve it with Knuth's five-guess method). Press New game for a fresh code.
- Build a guess by tapping a colour: it drops into the highlighted hole and the highlight moves to the next empty one. Tap any hole in the current row to move the highlight, and use Erase (Backspace) to clear one. On a keyboard, keys 1 to 6 pick a colour. The standard game uses 6 colours and 4 holes with repeats allowed, so there are 1,296 possible codes. Press Check guess (Enter) once all four holes are filled.
- Read the feedback pegs to the right of each guess. A black (or red) peg means one of your pegs is the right color in the right position. A white peg means a color is in the code but in the wrong position. The feedback pegs are unordered — they tell you how many, not which specific slots are correct.
- Knuth's opening guess is two pairs of colours (for example red-red-blue-blue). Whatever key pegs come back, at most 256 of the 1,296 codes are still possible after it. Four different colours is another solid opening because it tests more colours at once.
- After each guess, change only 1-2 pegs at a time so the feedback delta isolates which specific change caused the score to shift. Swapping all four positions at once gives you ambiguous feedback — you won't know which substitution earned the new black or white peg.
- Track which colors are confirmed in vs out: zero feedback pegs means none of those colors appear in the secret (eliminate them from future guesses). Total feedback equal to 4 means you have all the right colors and just need to permute their positions.
- Crack the code within 10 guesses to win. Donald Knuth showed in 1977 that any 4-peg, 6-colour code can be solved in at most 5 guesses with the right method, so aim to finish in 6 or fewer consistently. Your results, best game and streak are saved in your browser. Press ? any time for the rules and a worked example, and turn on Show numbers on pegs if the colours are hard to tell apart.
Frequently asked questions
How do you play Mastermind?
Mastermind is a code-breaking game where one player (or the computer) creates a secret code of 4 colored pegs, and the other player tries to guess it. After each guess, you receive feedback in the form of black and white pegs: a black peg means you have the right color in the right position, and a white peg means a correct color is in the wrong position. The feedback pegs are not ordered, so you do not know which of your pegs earned which feedback — this is what makes the deduction challenging. You must crack the code within a limited number of guesses.
Can two players play?
Yes — in two-player mode, Player 1 sets the secret code and Player 2 tries to crack it. After the round, you can swap roles so the guesser becomes the code-maker. This is how the original board game works and adds a social element to the puzzle. The code-maker can try to choose codes that are especially difficult to guess, such as codes with duplicate colors or unusual patterns that defy common opening strategies.
What is the best strategy for Mastermind?
The most effective strategy is systematic elimination. Start with a guess that uses as many different colors as possible to quickly determine which colors are in the code. On subsequent guesses, change only one or two colors at a time so you can precisely attribute changes in feedback to specific slots. Donald Knuth published a mathematically optimal strategy in 1977 that guarantees cracking any 4-peg, 6-color code in at most five guesses. The key insight is to choose each guess so that it maximally reduces the remaining possibilities, regardless of which feedback you receive.
What do the feedback pegs mean exactly?
A black peg means one of your guessed colors is exactly the right color in exactly the right position. A white peg means one of your guessed colors appears somewhere in the secret code, but you placed it in the wrong position. Crucially, the feedback pegs are presented in no particular order — a black peg on the left does not necessarily correspond to your leftmost guess slot. Also, each peg in the secret code can only match one peg in your guess, so duplicates are handled carefully. If the code has one red and you guess two reds, you will only get one feedback peg for red.
How was Mastermind invented?
Mastermind was invented in 1970 by Mordecai Meirowitz, an Israeli postmaster and telecommunications expert. He initially struggled to find a manufacturer, but the game was eventually published by Invicta Plastics in the UK in 1971. It became a massive worldwide hit, selling over 50 million copies. The game is based on an older pencil-and-paper game called Bulls and Cows, which uses numbers instead of colors. Mastermind's elegant design — simple rules but deep logical complexity — has made it a staple of game theory and computer science education.
Can the secret code contain duplicate colors?
Yes — the secret code can use the same color more than once, and this is one of the trickiest aspects of the game. A code like Red-Red-Blue-Red is perfectly valid. When duplicates are present, the feedback logic becomes more nuanced: each code peg can only pair with one guess peg. If the code has two reds and you guess three reds, only two feedback pegs will be generated for red. Beginners often overlook duplicates, so always consider the possibility when your feedback does not match your expectations.
Is this game free?
Completely free, with no sign-up and no downloads. Runs entirely in your browser on any device. If you enjoy logic and deduction games, try
Minesweeper,
Sudoku, or
Number Sequence Puzzle for more brain-teasing challenges.
Part of ToolFluency’s library of free online tools for Games & Fun. No account needed, no data leaves your device.