Free logic puzzles to play and print

Mastermind Game Online: Code Breaker

Guess the secret colour code. Black pegs mean right colour, right place; white pegs mean right colour, wrong place.

Mastermind is a code-breaking game: you crack a secret row of coloured pegs, and each guess returns black and white peg feedback to guide the next one. This free code breaker lets you play the same game in your browser. It works as a mastermind game online with no sign-up, and nothing you enter is uploaded. Three tabs cover the main ways to play. In Play, the computer picks the code and you guess it. In Computer guesses, you think of a code and the computer does the guessing. In Solver, you enter guesses and scores from a real board game, and the tool suggests your next move. A Bulls and Cows preset is also available for a digit-based version of the game.

How to use the code breaker

  1. Open the Play tab to have the computer pick a secret code. Open Computer guesses if you would rather think of a code and let the computer guess it. Use Solver for a game you are playing on a physical board.
  2. Set the puzzle size with Code length (3 to 6, default 4) and Colors (4 to 10, default 6). Decide whether Repeated colors allowed stays on (it is on by default). Set Guesses allowed from 6 to 15 or Unlimited (default 10). Or pick a preset: Classic: 4 pegs, 6 colors, Large: 5 pegs, 8 colors, or Bulls and cows (digits).
  3. Enter a guess by tapping an empty hole and then a colour. On a keyboard, you can type the numbers 1 to 9 and 0 instead.
  4. Press Enter or click Submit guess.
  5. Read the black and white pegs that appear next to your row, and use them to choose your next guess.
  6. Press Backspace to delete a digit, or click Clear row to empty the current row.
  7. If you are stuck, click Give up to reveal the code.

Turn on the optional possible codes left counter to see how many codes still fit your answers. Statistics for games, wins and average guesses are saved in your browser for each set of settings, and a game in progress survives a page reload. Use Copy link to share a game. If you are colour-blind, turn on Numbers on the pegs so each colour shows a number.

What are the rules of Mastermind?

The secret code is a row of pegs, and each peg is one of several colours. The code length and the number of colours decide how hard the puzzle is. With 4 pegs and 6 colours there are 1,296 possible codes. With 5 pegs and 8 colours there are 32,768. With 6 pegs and 10 colours there are 1,000,000, the largest setting. Each guess is a row you build from the same colours, and you keep guessing until you match the code or run out of guesses.

What do black and white pegs mean?

A black peg means one of your pegs has the right colour in the right place. A white peg means one of your pegs has the right colour but in the wrong place. No peg is counted twice, so the scoring is a fair count of matches.

The rule is: white = (sum over each colour of the smaller of its count in the secret and its count in the guess) minus black. For example, with secret 1122 and guess 1213, the first peg matches in place, giving 1 black. Colour 1 appears twice in each row, colour 2 appears once in the guess, so the shared count is 3. Subtracting the 1 black leaves 2 white. A score of zero black and zero white tells you that none of those colours is in the code, so you can rule them out.

Can every code be solved in five guesses?

For the 4-peg, 6-colour game, yes. Donald Knuth showed in 1977 that a minimax strategy finds every code in at most five guesses. The average is about 4.48 guesses. This tool checked that claim against all 1,296 codes. The opening guess 1122 is Knuth’s choice for this setting.

How does the computer guess so efficiently?

After each answer, the computer keeps only the codes that agree with every score so far. It then picks the guess whose worst-case result leaves the fewest remaining codes. That is minimax: it plans for the answer that would give it the most trouble. In very large games, where full minimax is too slow, it instead guesses a code that is consistent with all the answers.

What is the best strategy for a human player?

Start with a guess that uses two colours twice, such as AABB, because it tests two colours and their positions at once. Choose guesses that could still be the answer, so you learn something new each time. Track which colours are absent, since a 0 black, 0 white score removes those colours entirely. The possible codes left counter helps you check your reasoning.

What is Bulls and Cows?

Bulls and Cows is an older pencil-and-paper code-breaking game that uses digits instead of coloured pegs. The secret code uses the digits 0 to 9 with no repeats, usually 3 to 5 digits long. A bull is a correct digit in the correct place. A cow is a correct digit in the wrong place. A 4-digit code has 5,040 possible codes. The Bulls and cows (digits) preset sets the tool up for this version.

Frequently asked questions

Is this the official Mastermind game?

Mastermind is a trademark of its owner. This is an independent, free code-breaking game and is not affiliated with that owner, so the tool uses the generic name “code breaker”.

Is it free, and do I need an account?

Yes, the code breaker is free and needs no sign-up. Nothing you enter is uploaded, and your statistics are kept in your browser.

Does it work on a phone or tablet?

You can enter guesses by tapping an empty hole and then a colour, so touch input is supported. The number keys and Backspace are for keyboards.

How many guesses do I get?

The default is 10 guesses. In Guesses allowed you can choose any number from 6 to 15, or Unlimited if you want to take as long as you need.

Which setting is the hardest?

The 6-peg, 10-colour setting is the largest, with 1,000,000 possible codes. Start with the Classic preset if you are new to the game.

Can I share a game with someone else?

Yes. Click Copy link to copy a link to the current game.

Can I use Solver for a game I am playing on a board?

Yes. Enter each guess and its black and white score, and Solver shows how many codes remain, lists up to 50 of them, and suggests your next guess.