Home/Blog/How Many Codes in Mastermind? | 4 Pegs, 6 Colors

How Many Codes in Mastermind? | 4 Pegs, 6 Colors

By PlayBrain Teamยทยท5 min read

The classic Mastermind board game asks you to crack a hidden code. Before you can develop a strategy, it helps to understand exactly how many possible codes you are up against.

How Many Possible Codes in Mastermind?

The standard version of Mastermind uses 4 pegs and 6 colors, and duplicate colors are allowed (the same color can appear more than once in the code).

The answer: 6 ร— 6 ร— 6 ร— 6 = 6โด = 1,296 possible codes.

Each peg can independently be any of the 6 colors. With 4 pegs and no restrictions, that gives you 1,296 distinct combinations.

If Duplicates Were Not Allowed

If each color could only appear once (no repeats), the math changes:

6 ร— 5 ร— 4 ร— 3 = 360 possible codes.

You would choose any color for the first peg (6 options), then any remaining color for the second (5 options), and so on. In the standard game, however, duplicates are allowed, so the answer is 1,296.

A Quick Reference Table

PegsColorsDuplicates AllowedPossible Codes
46Yes1,296
46No360
48Yes4,096
56Yes7,776
58Yes32,768

The "Master" difficulty version (5 pegs, 8 colors, duplicates) has 32,768 possible codes โ€” dramatically harder.

Why This Number Matters for Strategy

Knowing there are 1,296 codes is not just trivia. It is the foundation of optimal Mastermind strategy. The goal of each guess is to eliminate as many possible codes as possible, using the feedback (black pegs = right color, right position; white pegs = right color, wrong position).

Knuth's Algorithm

In 1977, mathematician Donald Knuth proved that any Mastermind code can be cracked in five guesses or fewer using an optimal strategy. Here is how it works:

  1. First guess: Always start with 1122. This guess (two of one color, two of another) eliminates the most possibilities on average.
  2. Use the feedback to eliminate codes. After each guess, remove all codes from your mental list that would not have produced the same feedback pattern.
  3. Pick the guess that minimizes the worst case. Choose your next guess to maximize the number of codes eliminated, regardless of what feedback you receive.

Knuth's algorithm guarantees a solution within 5 guesses. In practice, most codes are cracked in 4.

A Simpler Rule of Thumb

If you do not want to run the full algorithm in your head, follow these guidelines:

  • Spread colors. Your first guess should use 4 different colors to get the most information.
  • Eliminate, do not confirm. A guess that could be right but tells you nothing new is wasted. Prefer guesses that split the remaining possibilities roughly in half.
  • Use all feedback. Black pegs and white pegs are both crucial. A response of "0 black, 0 white" tells you that none of the colors in your guess appear in the code at all โ€” a huge elimination.

The Probability Angle

With 1,296 possible codes and an optimal first guess, the feedback you receive narrows the field:

  • Best case: You get "4 black, 0 white" on your first guess (you solved it in one!). Probability: 1/1296 โ‰ˆ 0.077%.
  • Worst case with optimal play: You need 5 guesses (guaranteed maximum with Knuth's algorithm).
  • Average case with optimal play: About 4.34 guesses.

Play Mastermind Free Online

Ready to put this knowledge to work? Play Mastermind free on PlayBrain โ€” no download, no sign-up. Our version uses the classic 4-peg, 6-color format with full feedback (black and white pegs). You have 10 guesses to crack the code.

If you enjoy logical deduction games, also try Binary Puzzle, Logic Gates, KenKen, or Sudoku for similar brain challenges.

Get weekly game picks in your inbox

New games, tips, and challenges every week. No spam.

Frequently Asked Questions about How Many Codes in Mastermind? | 4 Pegs, 6 Colors

How many possible codes are there in classic Mastermind?
Classic Mastermind with 4 pegs and 6 colors has 6^4 = 1,296 possible codes (repetition allowed). If repetition is not allowed, the number drops to 6ร—5ร—4ร—3 = 360. Most versions allow repetition, giving the codemaker 1,296 possible secrets to hide.
What is the optimal Mastermind strategy?
The minimax strategy always cracks classic Mastermind in 5 guesses or fewer. Start with 1122. Use each response to eliminate the most possible codes. The optimal first guess (1122) guarantees no more than 5 total guesses for any of the 1,296 possible codes. Donald Knuth proved this in 1977.
Can Mastermind always be solved in 5 moves?
Yes, for the classic 4-peg 6-color version. Donald Knuth proved in 1977 that the minimax algorithm solves any code in at most 5 guesses. If you start with 1122 and use the guess that minimizes the worst-case remaining codes after each response, you are guaranteed to crack the code within 5 guesses.
Is Mastermind free to play online?
Yes. Mastermind on PlayBrain is free in your browser with no download or account. The classic 4-peg 6-color version with 10 guesses and full black-and-white peg feedback. Works on desktop and mobile. Multiple difficulty modes available.
P

PlayBrain Team

Our editorial team reviews and tests every game and guide we publish. Have a question or correction? Get in touch.