# Mastermind Strategy: How to Always Solve the Hidden Secret Code

Originally released back in 1971 Mastermind has become a classic abstract strategy game. The basic premise of the game is that one player creates a four color code in secret. The other player then has to try and figure out the code by making guesses. After each guess the player who made the code gives you information. They will give you key pegs for each correct color and for colors that are in the correct spaces. The goal is to figure out the code as quickly as possible.

For more details on how to play the game, check out our Mastermind How to Play guide.

Most players play Mastermind where they make random guesses. By randomly guessing you usually will be able to figure out the code before you run out of guesses. This is a fun way to play the game, but it is far from optimal. As you are competing to solve it faster than the other player, you want to figure out the code in as few guesses as possible. Here is where Mastermind strategy come into play.

There are a number of different Mastermind strategies that you can implement. I am going to walk you through some of the best strategies that you can implement while playing the game. I will preface this by saying that there are mathematical Mastermind strategies that are better than the ones that I will discuss. These strategies require a pretty strong mathematical background or a computer program to assist you in implementing them. Therefore I am not going to get into them in this post. The Mastermind strategies that I will discuss still work really well and are pretty easy to follow.

Before I get into discussing Mastermind strategy, I wanted to quickly mention that these strategies may impact your enjoyment of the game. Each of these take a methodical approach to the game and thus are optimal to just randomly guessing the code. Some people might find Mastermind to be less fun when implementing these strategies. They almost always will allow you to solve the code quicker though.

When you are looking at Mastermind strategies there are two main things that you are looking for. You need to figure out what colors are in the code, and in what position each color should be placed. Generally you want to figure out the colors first since they are needed to figure out the positions. To reduce the number of guesses though, once you figure out a color that is in the code you should try to find its final position. The strategies below help you figure out the colors quickly, but you also need to use some of your own reasoning to find their final positions.

## Mastermind Strategy: How to Figure Out the Colors in the Code

One key thing to consider before choosing a Mastermind strategy is what version of the game you are playing. You can actually play the game in two ways. Some versions of the game recommend one way while other recommend the other. Basically it comes down to whether you are going to allow duplicate colors in the final code.

The version of Mastermind that does not allow duplicates is considerably easier to play. This is because the number of possible codes is significantly lower. Adding duplicates adds significantly more potential options for the secret code as you can have two, three, or four of all of the different colors. Most of the strategies I detail below are geared more towards allowing duplicates. They can still be used if there are no duplicates. You probably won’t get quite as big of an improvement though.

There is one unique strategy that you can choose to implement if you are not playing Mastermind with duplicates. I would not recommend using this strategy if you are playing with duplicates though. This Mastermind strategy is pretty high risk high reward. It can help you solve the code really quickly. It could also take you more time than if you would implement one of the other strategies.

### Guessing Four Different Colors

You might initially think you should guess four different colors for your first guess. This makes logical sense as you can check four different colors at one time. In some situations this can really pay off.

If you receive four key pegs you figured out all four colors on your first guess. You just saved quite a few guesses that you otherwise would have had to make to find the four colors. You now only have to figure out the order of the four colors.

Receiving two key pegs can also be useful. This tells you that two of the colors you guessed are correct. The more beneficial information is that you know the two colors that you didn’t guess have to be in the code. This is assuming that you are playing a version of Mastermind with only six colors. You now just need to figure out which of the two colors in your original guess were correct. This can take a couple guesses as you systematically eliminate colors from your original guess.

Put the two colors you didn’t guess in your first guess in your next guess. Choose two colors from your first guess to include as well. If the number of key pegs you receive from your second guess is higher than your first guess, one (3 key pegs) or both of the colors (4 key pegs) that you kept in your guess were correct. If it stays at two, you now know the two colors you didn’t put in your second guess have to be the other two colors in the code.

The problem with this strategy is if you receive three key pegs. This is actually the worst case scenario. Three of the colors from your original guess are correct and one is wrong. Meanwhile one of the two colors that you didn’t pick are in the code. The problem with this scenario is you need to figure out which color from your original guess isn’t in the code. At the same time you also need to figure out which color you didn’t pick is actually in the code. This can take quite a few guesses to figure out.

If you receive three key pegs again, you don’t gain a lot of information. You either removed the wrong color from your second guess or you added the wrong color. It may take a couple of guesses to figure out which happened.

## Mastermind Strategy: Guessing All the Same Color

I consider this the simplest Mastermind strategy that actually helps reduce your number of guesses quite a bit. This strategy works whether the code has duplicates, or all different colors. Its main emphasis is figuring out the colors in the code. When you figure out that a color is in the code, you will then move it around to figure out its final position.

When I give examples in this explanation I will be assigning numbers to each color. It doesn’t matter what number you assign to each color. It is just a simpler way of explaining what you should do for each guess.

### First Guess Using This Mastermind Strategy

For your first guess choose one of the colors and assign it number one. For your first guess you will fill in the entire guess with color number one. Your guess will be 1111.

Next you will analyze what key pegs you receive. If you receive no key pegs, you now know that color 1 is not in the final code. If you receive one or more key pegs, you now know that many pegs of color 1 are in the final code.

### Second Guess

For your second guess you will use the information you received from the first guess. If you received no key pegs from your first guess, fill in the whole second guess with color 2 (2222).

If you received one or more key pegs, fill that many spaces with the first color. Fill in the remaining spaces with color 2. For example if you received one key peg from the first guess your second guess should be 1222.

### Third Guess

What you receive from your second guess can then be used to figure out colors and positioning. If you received no additional key pegs fro your second guess, you know color 2 is not in the final code. Any additional key pegs you receive tell you how many pegs match color 2.

For example for your second guess you used 1222 and received two key pegs. You now know that there is one peg of color 1 and one peg of color 2 in the final code.

Additionally if there is at least one peg of color 1 in the final code, look at the colors of the key pegs you received in your second guess. If you received one or more white key peg, you placed the color 1 peg(s) in the wrong position. Move it to another position for your next guess. If you only received black/red pegs, you know that a color 1 peg is in the correct position.

For your third guess you need to factor in the information you gained about color and position. For example you received one black/red key peg and one white peg from your second guess (1222). You now know that there is one color 1 and one color 2 peg in the final code. The rest of the code should be filled in with color 3. You also know that color 1 can’t be in position one since you received a white peg. The color 2 peg also can’t be in position one since it was in the correct position in the previous guess (you received a black/red key peg). Therefore color 1 has to be in positions 2-4 and color 2 has to be in positions 2-4. For your next guess you could make a guess such as 3123.

### Fourth Guess

By the fourth guess you likely will know some of the final colors and hopefully have figured out the final position for one peg.

First look at how many key pegs you received from the third guess. If you received more than the second guess, any additional pegs represent color 3 pegs that are in the final code. For example if you previously had two key pegs and you now have three, there is one color 3 peg in the final code.

For each missing key peg from your previous guess, fill in the corresponding number of spaces with color 4 pegs.

As for positioning this becomes a little more complicated. It depends on what color key pegs you received on previous guesses. You need to factor these things together to try and figure out what colors are in the right positions. Basically for every white peg you receive you need to move one of your correct colors to a different position. For each black/red peg you should keep one of your correct colors in its current position. For more details on figuring out positioning check out the Mastermid Strategies For Determining Positioning section below.

### Fifth and Sixth Guesses

At this point count up how many key pegs you received for your last guess. If you received all four key pegs, you have figured out the four colors in the final code. If you haven’t received four key pegs yet, you still have figured out all of the colors in the final code.

For your fifth guess use color 5 to fill in spots equal to the number of colors you don’t know. If for example you don’t know one color, add one peg of color 5 to your next guess. If you still haven’t figured out all of the colors by your sixth guess, fill any remaining spots with the sixth color.

By the end of your sixth turn you should know all four colors of the final code. It is pretty common to find all four colors before your sixth turn. The only way you won’t know all six colors by your sixth guess is if you are playing with more than six colors.

### Example Using This Mastermind Strategy

Here is a complete example using a different code to show how you can use this Mastermind strategy to figure out the code.

## Mastermind Strategy: Guessing Two Colors at a Time

This next strategy for Mastermind is a little more complicated than the previous strategy. Instead of mostly only guessing one color each turn, you will guess two colors at the same time. If you implement this strategy correctly, you can use it to solve the Mastermind code quicker.

For this strategy I will assign one number to each color. It doesn’t matter what color you assign to each number. This is only to make the strategy easier to follow.

### First Three Guesses Using this Mastermind Strategy

For your first three guesses you will pick two different colors. You will place two of one color in the first two spots and two of another color in the other two spots. You will repeat this three times so you use all six colors.

For example you will make the following three guesses.

1122

3344

5566

What result you get from these guesses will tell you a lot of information about what colors are in the final code.

The best result is obviously to receive four key pegs from one of your guesses. This means that you have already figured out all four colors. All you have to do now is figure out the order of the four colors. This is an unlikely outcome though.

The next best result is to receive zero key pegs. This is useful because it means that the two colors in the guess cannot be in the final code. You have just reduced the code to four different colors. Using the colors from this guess is important to figuring out the order of the pegs.

Otherwise you will receive one, two or three key pegs from a guess. These all give you information, but are not as valuable as receiving four or zero key pegs.

If you have received four key pegs before you make the third guess, stop making guesses. You don’t have to make the remaining guess(es) because you know none of those colors can be in the final code.

After the first three guesses you already should know a lot of information about what colors are in the final code. You will now narrow down the final colors as well as start to figure out their final positions. Before your next guess you will analyze your first three guesses. There are three possible options.

### Only One Guess Received Key Pegs

First you should look for a situation where two of your guesses received zero key pegs while the other received two, three or four key pegs. If this happened, you now know the entire code consists of the two colors from your guess that received key pegs.

For your next guess you should pick one of those two colors and fill the entire row with that color. The number of key pegs you receive tells you how many of that color are in the final code. Any missing key pegs are the other color.

For example lets say guess 1122 was the only one of your first three guesses to receive key pegs. In your next guess you choose 1111. You receive three key pegs. This means that three of the pegs in the final code are color 1 and one peg is color 2. Colors 3, 4, 5, and 6 are not present in the final code.

Since you now know the distribution of the colors, you will have to rearrange the colors. Make a guess with the corresponding number of pegs of the two colors. If you receive all black/red pegs, you have solved the code. If you receive white pegs, swap the corresponding number of pegs from the two different colors until you find the correct code.

For more information about how to figure out the final positioning, check out the Mastermind Strategies for Determining Positioning section below.

### Only Received Three Key Pegs Total

If you ended up only receiving three total key pegs from your first three guesses, you actually learn quite a bit. In this case one guess should have gotten two key pegs and another should have gotten one. Three of the final colors are of one of the two colors from the guess that received two key pegs. For example if the 3344 guess received two key pegs, you now know that three of the colors in the final code are either 333 or 444. The code can’t have two of one color and one of the other because you would have received three key pegs from the 3344 guess.

To determine which color has three pegs in the final code, make a guess using just one of the two colors. If you receive key pegs back (it should be three), that color is represented three times in the final code.

For your next guess fill in three of the spots with the color you figured out is in the code. Then choose one of the two colors in the first three guess that received one key peg. Fill in the remaining space with a peg of that color. If you receive four key pegs for the guess, you now know all four colors. If you only receive three key pegs, you know it must be the other color.

At this point you know all of the colors in the final code. You just need to position them in the right places. See the Mastermind Strategies For Determining Positioning section below for more details on how to figure out the final positions.

### Find A Color Not in the Final Code

If the key pegs you received from the first three guesses did not fit either of the previous two situations, you should follow this strategy. This is the most likely scenario. It will occur if one guess received two key pegs and the other two guesses received one key peg. It can also occur if two guesses each received two key pegs and the other received no key pegs.

First you want to find a color that is not in the final code. Hopefully one of your first three guesses received zero key pegs. This is useful since you know those colors can’t be in the final code. This will be useful to figure out what colors are in the final code and what order they are in.

If none of your first three guesses received zero key pegs, for your next guess you need to find a color that is not in the final code. Choose one of your guesses that received one key peg. Repeat one of the colors from that guess in every single spot of your next guess. For example for the guess 1122, you can then guess 1111.

What you receive from this guess tells you whether the color you guessed is in the code. If you received one key peg from the previous guess, you know color 1 is in the code and 2 isn’t. If you don’t receive a key peg, you know color 2 is in the code and 1 isn’t.

### Figuring Out Positioning Of First Color

Now that you know a color that is not in the code, you should go back to your first three guesses. Find the guess that received the most key pegs (it should have received two key pegs). If two guesses received two key pegs, you can pick either of them. Choose the first color from the guess and fill in the first two spaces of the next guess with that color. Fill in the remaining spaces with a color you know is not in the code.

For this section I will use and example where the original guess 5566 received two key pegs, and I am making the guess 5511. I have already determined that color 1 is not in the final code.

What key pegs you receive can help you determine what colors are in the final code and possibly their position.

Zero Key Pegs

If you receive zero key pegs from your guess, you now know that there are two pegs of the other color from your initial guess. In my example you now know that there are two color 6 pegs in the final code.

You should now look at the colors of the initial two key pegs you received (the 5566 guess). If both were black/red you know that the final two pegs have to be the other color. (The final code must be XX66) If both are white you know the first two colors in the code are the other color. (The final code must be 66XX) Finally if one is white and the other is black, you know there is one peg of color 6 in the first two spots and one in the last two spots.

One Key Peg

If you only receive one key peg, you know that there is one of each color in the final code. In my current example there would be one color 5 peg in the final code, and there would be one color 6 peg.

Now look at the colors of the key pegs from the most recent guess and the initial guess.

If you received a black/red key peg from your most recent guess, you know that color 5 has to be in position one or two. You know this because color 5 was only in position one and two in the previous guess. If your initial guess had two black/red pegs you also know that color 6 must be in position three or four. This is because color 6 was only in the last two positions in the initial guess. If your initial guess had one white and one black/red peg, you know that color 6 must also be in the first two positions.

Should you get a white key peg from your most recent guess, you know that color 5 has to be in position three or four. It can’t be in position one or two or you would have received a black/red key peg. If your initial guess (5566) has one white and one black/red peg, you also know that color 6 has to be in position three or four. This is because color six was only in positions three and four in the initial guess and it was in the correct position. If you initially received two white key pegs you know that color 5 is in position three or four and color 6 is in position one or two.

Two Key Pegs

If you receive two key pegs from your most recent guess, you know that the color you just guessed has to be in the final code twice.

Should you get two black/red pegs, you know that color 5 has to be in position one and two.

If you receive one black/red peg and one white peg, you know that there is one color 5 peg in position one or two, and one color 5 peg in position three or four.

Finally if you receive two white pegs, you know that color 5 is in positions three and four.

At this point you should know the general position of at least two of the colors in the final code. If you already know the other colors in the final code, you will mix these in with the positions you already know. Should you not know one of the colors yet, fill in any empty spaces in your code with one of the colors that may still be in the code. After a couple guesses you should find the final position for each color and solve the code.

## Mastermind Strategies For Determining Positioning

You should probably first focus on figuring out the four colors in the final code. While you are doing this though, you also need to start working on figuring out the final positions for each color.

Once you know a color is in the final code, you should start working towards finding its correct position. This is where the color of the key pegs you receive become important. Black/red pegs tell you a peg is in the exact right spot. You can receive information from white pegs as well though.

### Strategy For Black/Red Key Pegs in Mastermind

For each black/red peg you receive you should keep one of the pegs from your previous guess in the same position in your next guess. If you follow the above strategies for figuring out the colors in the final code, you should know at least one of the final colors. Thus you should have a pretty good idea of knowing which color to keep in its current position.

When you make a new guess, check the number of key pegs you receive. If you receive the same number of pegs and none of the black/red pegs have switched to white, you know you kept the color in the right position.

Should the total number of pegs increase and there are white pegs, you don’t know if you kept the peg in the right spot or if one of the new colors you figured out was placed in the right position.

Keep a number of pegs in the same position as the number of black key pegs you received. If after your next guess you have less black/red pegs, you know that you kept the wrong color in the same position. For your next guess you should move one of the colors to its position from the previous guess.

### Strategy For White Key Pegs in Mastermind

A white peg can be quite useful in some situations.

If you know one or more of the final colors and you only receive white pegs, all of the colors you know are in the wrong positions. Should there only be one white key peg, move the color you know to a different position.

If you receive two or more white pegs and no black/red pegs, you may be tempted to move around all of the colors that you know. This makes sense because you know that they are currently not in the right position. Instead I would suggest only moving one of the colors you know to a different position. Only changing one color’s position can be helpful because you then know if you moved it to the right position. If you picked up a black/red peg, you know that you moved it to the right position. If you didn’t pick up a black/red peg, you know you need to move it to another position. Keep doing this until you get a black/red peg.

After you have turned one of the colors key peg into a red/black peg, you can then start working on the other colors that you received white pegs for.

This strategy for Mastermind can also be useful when you have a mixture of white and black/red pegs. If you only move the position of one peg each guess, you can figure out if you are moving the right peg. Should you have more black/red pegs after your next guess, you know you moved the right color. If you have less black/red pegs, you know that you moved the wrong peg and know to move it back to its previous position on a future turn.

Figuring out the final position of each color one at a time allows you to eventually figure out the final position for all of the colors. Moving multiple colors at the same time can give you mixed information. It may take a couple guesses to fully figure out the information you received.

## Wrapup of Mastermind Strategies

These Mastermind strategies are not going to be perfect. As long as you don’t make a mistake, you will solve the code before you run out of guesses. In most cases you should be able to solve the code within 4-6 guesses. Sometimes it will take a little longer and other times you will get lucky and figure it out quicker.

Based on how the game is played though, you could still lose following one of the Mastermind strategies detailed above. The other player could get luckier and figure out your colors before you find theirs. Implementing one or more of these strategies is likely to improve your odds of winning the game. If the other player makes random guesses, you will likely beat them most of the time. If they also use a strategy while playing Mastermind, it may come down to who is luckier with their guesses.

I hope you found this strategy guide for Mastermind to be helpful. If you have any questions feel free to leave a comment and I will try to help you as quickly as possible.