How Many Codes in Mastermind? | 4 Pegs, 6 Colors
See 4 more games mentioned in this article
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
| Pegs | Colors | Duplicates Allowed | Possible Codes |
|---|---|---|---|
| 4 | 6 | Yes | 1,296 |
| 4 | 6 | No | 360 |
| 4 | 8 | Yes | 4,096 |
| 5 | 6 | Yes | 7,776 |
| 5 | 8 | Yes | 32,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:
- First guess: Always start with 1122. This guess (two of one color, two of another) eliminates the most possibilities on average.
- 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.
- 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?
What is the optimal Mastermind strategy?
Can Mastermind always be solved in 5 moves?
Is Mastermind free to play online?
Explore Collections
PlayBrain Team
Our editorial team reviews and tests every game and guide we publish. Have a question or correction? Get in touch.
Related Articles
Best Logic Puzzle Games Online Free | No Download
10 best free logic puzzle games in your browser: Sudoku, Nonogram, KenKen, Mastermind, Kakuro + 5 more. No download, no ads, pure deduction.
Games to Play When Bored | 50 Free Browser Games
Beat boredom with 50 free browser games: quick challenges, chill games, competitive scoring, and deep sessions. No download, instant play.
Best Free Puzzle Games 2026 | 20 Brain Puzzles You Can Play
20 free browser puzzle games handpicked for 2026. Number puzzles, logic grids, spatial challenges, word games, and classics. No download, no signup.
Free Escape Room Games Online | Play in Browser
Play free escape room and puzzle games in your browser: Escape Room, Mastermind, Logic Gates, Minesweeper + more. No download, instant play.
How to Win Mastermind | Strategy to Crack Any Code
Crack any Mastermind code in 5 moves or fewer. Optimal opening moves, color elimination strategy, and the logic behind every guess. Play and practice free.
Logic Gates Game Free Online | Learn AND, OR, NOT, XOR
Play Logic Gates free online . solve puzzles by combining AND, OR, NOT, XOR circuits. Learn how computers think through interactive puzzles. No download.