Mastermind is the classic code-breaking game: the computer hides a row of coloured pegs and you have ten guesses to crack it, using black and white pegs as clues. Play Mastermind online free here with the classic 4 pegs and 6 colours, bigger 5 peg and 8 colour games, repeats and blanks on or off, a daily code, the number version called Bulls and Cows, a two-player pass-and-play mode, and a mode where you make the code and the computer cracks it with Knuth's five-guess algorithm, with every step explained.
One side is the code maker and the other is the code breaker. The code maker chooses a secret row of coloured pegs. In the classic game that is four pegs, each one of six colours, and a colour may appear more than once. The code breaker has ten guesses. After each guess the code maker answers with a few small pins that say how close the guess was, and the code breaker uses those answers to narrow the possibilities until the code is found or the guesses run out.
Here the computer is the code maker when you play the Classic or Daily mode. Tap a colour to drop it into the next hole of the current row, tap a hole to select it and replace its colour, and press Guess when the row is full. The row moves up into the board with its black and white pins next to it, and you start the next one.
- Pick Classic, Daily code, Bulls and Cows, Computer cracks yours, or Two players.
- Fill the row with your guess and press Guess.
- Read the pins: black and white tell you how close you are.
- Use every clue to rule out codes that cannot be right, then guess again.
- Crack the code before you run out of guesses.
A black peg means one of your pegs has the right colour in the right hole. A white peg means one of your pegs has a colour that is in the code, but in a different hole. No peg means that colour is not in the code, or you already used up all the copies it has. The pins are always shown black first, then white, then empty, and their position says nothing about which hole they refer to. A guess is a win when all four pins are black.
Repeated colours need care. The code RRBG has two reds. If you guess RRRR you get two black pegs (the two reds in the right holes) and no white pegs, because the code has no more reds for the other two guesses to match. If you guess GGGG you get one black peg for the green in the last hole and no white pegs. Each peg of the code can be matched only once. That counting rule is exactly what makes repeated colours a harder puzzle than a code with all different colours.
Classic: the computer makes a random code and you have ten guesses (twelve in the bigger variants). Your wins, average number of guesses and best game are saved.
Daily code: a classic code that is the same for everyone today. Crack it in as few guesses as you can and compare with friends.
Bulls and Cows: the same game with digits. The code is four different digits from 0 to 9. A bull is a right digit in the right place, a cow is a right digit in the wrong place. See the Bulls and Cows section below.
Computer cracks yours: you make the secret code and the computer, using Knuth's algorithm, tries to find it. You press Next guess (or Auto play) and read an explanation of why it chose each guess.
Two players: pass and play on one device. Player 1 makes a code, locks it, and hands over the device. Player 2 tries to crack it. Then the roles swap, and the player who needed fewer guesses wins.
Before a Classic, Computer cracks yours or Two players game you can change the rules. Use 4 or 5 pegs and 6 or 8 colours. Turn repeats off to make every colour in the code different, which shrinks the code count and makes the puzzle gentler. Turn blanks on to allow an empty hole as one more option, which makes the game harder. The start screen shows how many possible codes there are, and the number of guesses grows to twelve for the bigger variants.
For scale: the classic game has 6 x 6 x 6 x 6 = 1,296 possible codes. With 5 pegs and 8 colours (sometimes sold as Super Mastermind) there are 32,768. With repeats off in the classic game there are only 360. If a colour is hard to tell apart on your screen, turn on Symbols on pegs and every colour also gets its own shape.
Open with two colours in pairs, such as red red blue blue. A first guess with four different colours feels more adventurous, but the two-pair opening splits the 1,296 codes into groups that are on average smaller, which is why it is also the opening Donald Knuth used.
After that, play consistent guesses: pick a guess that could still be the secret code given every clue so far. If the guess is not the code, at least it is still a candidate, and every answer then tells you something. The Hint button does exactly this for you: it fills the row with a random code that fits all previous clues. If you can follow the hint and say why it fits, you are learning the game.
Test colours before positions. Early on, focus on discovering which colours are in the code. Later, move pegs around to find their holes. Keep a note of what each clue ruled out: if a guess scored nothing, those colours are out for good, which helps a lot.
Be careful with duplicates. If a guess with two reds scores one white, you know there is at least one red but you do not yet know there are two. Guesses that mix a new colour with a repeated old one are an easy way to count how many of each colour the code has.
In 1977 Donald Knuth showed that the classic game can always be won in five guesses or fewer. His method has a clear idea behind it: after every guess, keep the list of codes that are still possible, then pick the next guess that makes the worst case as small as possible.
The steps are: first, guess 1122 (two colours, two of each). Second, look at the pins you get and throw away every code that would have given a different pin result against that guess. Third, for each candidate next guess, imagine every possible answer and count how many codes would remain. The size of the biggest group is the worst case for that guess. Pick the guess with the smallest worst case, and if there is a tie prefer a guess that could itself be the code, because it has a chance to win outright. Repeat until the pins are all black.
Run the algorithm on all 1,296 codes and the result is: 1 code is cracked on the first guess, 6 on the second, 62 on the third, 533 on the fourth and the remaining 694 on the fifth. The average is about 4.48 guesses, and no code ever needs six. We test this in our code: every one of the 1,296 secrets is cracked in five guesses or fewer.
Choose Computer cracks yours, make any code (or press Random code) and lock it in. The computer now plays as the code breaker. Each time you press Next guess it computes the guess Knuth's method would choose. A panel under the board explains the step: how many codes still fit the clues and what the worst case would be after this guess. On the classic game it never needs more than five guesses, which you can watch for yourself. For the bigger variants the computer looks at a sample of the remaining codes instead of every one, so it is quick, but the 5-guess promise only applies to the classic 4-peg, 6-colour game with repeats.
This is the fastest way to learn better play. Try to predict its next guess, then compare. You will notice it often plays a guess that looks strange but cuts the number of remaining codes dramatically.
Bulls and Cows is the pen-and-paper ancestor of Mastermind. The secret is four different digits, so there are 10 x 9 x 8 x 7 = 5,040 possible codes. After each guess you hear how many bulls (right digit, right place) and how many cows (right digit, wrong place). Here the board shows digits instead of coloured pegs and writes the bulls and cows next to each guess. With best play the number can always be found in seven guesses or fewer, and the average is around five. You have ten guesses here.
It plays like Mastermind with a number keypad, and it is also known as Cows and Bulls or Moo. Good Bulls and Cows play uses exactly the same tools: a smart opening such as 1234 (or 0123), counting what each clue rules out, and consistent guesses once you have enough information.
The daily code is the same for every player on a given day, so you can compare results. Your best guess count for the day is saved. Statistics for Classic and Bulls and Cows show games played, games won, your average guesses per win and your best result. They stay in your browser.
If you enjoy Wordle you already know the Mastermind loop. In both games you guess, get feedback about right-place and wrong-place pieces, and cut down the space of possible answers with logic. Mastermind uses coloured pegs and four holes; Wordle uses letters. The consistent-guess idea carries over directly, and the minimax thinking behind Knuth's algorithm is also how solvers rate Wordle openings.
Mastermind was designed in 1970 by Mordecai Meirowitz, an Israeli postmaster and telecommunications expert, and released by the British toy company Invicta Plastics in the early 1970s. It was inspired by the older pen-and-paper game Bulls and Cows and went on to sell tens of millions of copies. Its fixed, tiny rules (a few pegs, a few colours, a handful of guesses) hide a surprisingly deep search problem, which is why computer scientists have studied it for decades.