About Mastermind

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

  1. Pick a mode before starting: vs Computer (the classic — the computer hides a 4-peg code, you guess), Pass & Play (one player sets the code, hands the device over for the other to crack), or AI Guesses (you set the code, watch the AI use Knuth's algorithm to break it). Press New Game to deal a fresh code.
  2. Build your guess by clicking the empty pegs in the current row, then tapping a color from the palette. The standard ruleset uses 6 colors and 4 positions with duplicates allowed — meaning the secret can contain any color more than once (so 1,296 possible codes total). Press Submit Guess when all four positions are filled.
  3. 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.
  4. Knuth's opening guess is to start with two pairs of distinct colors (e.g. red-red-blue-blue). This guarantees you eliminate at least 256 possibilities on the first turn, regardless of the secret code. A pure rainbow guess (4 different colors) is also strong because it tests more colors at once.
  5. 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.
  6. 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.
  7. Crack the code within 10 attempts to win. Donald Knuth proved in 1977 that any 4-peg, 6-color code is solvable in at most 5 guesses with optimal play — try to beat 6 guesses consistently before chasing the 5-guess ceiling. Press Rules any time for a quick refresher.

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 ads, 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.