Skip to Content

Mastermind Strategy: How to Always Solve the Hidden Secret Code

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.

Guessing Four Different Colors Mastermind Strategy
This player decided to guess four different colors for their first guess. They got lucky and received four key pegs. Therefore they know that the four colors in the code are black, yellow, green, and blue. One of these four colors are also in the correct position.

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.

Receiving two key pegs after guessing four different colors.
After guessing four different colors for their first guess, this player received two key pegs. This shows that they have two colors correct and one is in the right position. Assuming that there are no duplicate colors in the code and there are only six color options, the two colors in the final code must be white and red. They now need to figure out which two colors they guessed are in the code.

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 second guess using the Mastermind strategy of guessing four different colors
For their second guess this player decided to keep the yellow and black peg from their first guess. They added in the white and red peg that they didn’t use in their first guess. They ended up receiving three white key pegs. This tells the player three pieces of information. First either black or yellow are in the code. The other color has to be green or blue. Finally there can’t be a black peg in the first spot as the player didn’t receive a black key peg from their second guess and black remained in the same spot as the first guess.

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.

Receiving three key pegs from the first guess
For their first guess this player received three key pegs. This means that three of the four colors they picked are in the final code. Only one was placed in the correct position though.

Choose one of your original colors to remove from your next guess. Add in one of the colors you didn’t originally pick. The number of key pegs you receive from your second guess determines what you should do next. Two and four key pegs are the best results. Four means you now know the four colors. Two means you picked the wrong color to remove from your guess. You also picked the wrong color to add to your second guess. Replace another one of the three colors you picked in your first guess with the color you removed in your second guess. Also replace the new color from your second guess with the color you have yet to use in a guess.

Mastermind strategy after receiving three key pegs
After receiving three key pegs this player removed the yellow peg from their second guess. They replaced it with a red peg which was a color they didn’t use in their first guess. They ended up receiving two key pegs for their guess. This is actually good news. Since the number of key pegs went down, this player now knows that yellow has to be in the final code. Removing it reduced the number of key pegs the player received. They also learned that red is not in the code as they lost a key peg. Therefore white has to be in the code as it is the last color that they haven’t guessed yet.

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.

Receiving three key pegs after the second guess
For their second guess this player once again received three key pegs. This means that either adding the red peg or removing the yellow peg was correct. The other decision was wrong. This could mean that removing yellow was correct, but it had to be replaced with the white peg (the only color not picked yet). Otherwise it could mean that red and yellow are in the final code. In this case either black, green or blue can’t be in the final code.

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.

Guess all the same color Mastermind strategy
This player decided to guess green in all four spots of the code. With this guess they will find out if green is in the code.

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.

Guessing all the same color in your first guess
After guessing all greens for their first guess, this player received one black key peg. This player now knows that there has to be one green peg in the 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).

Guessing a second color using the Mastermind strategy
This player received no key pegs for guessing all greens. Therefore they know no greens are in the final code. For their second guess they decide to guess all blue pegs.

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.

Guessing a second color
For their first guess this player received one key peg. They know there must be one green peg in the code. So they leave one green peg in their second guess. They will fill the rest of their second guess with a new color, blue.

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.

Results of second guess using the all one color Mastermind strategy
This player only received one white key peg from their second guess. This tells them that no blue pegs are in the code since they didn’t receive any more key pegs than their first guess. It also tells them that the green peg is not in the first position as they only received a white key peg.

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.

Guess the same color results from the second guess
For their second guess this player received two key pegs. They now know there is one green and one blue peg in the final code. They also learned that the green peg cannot be in the first position. It can’t be in the first position because the player received a white key peg. Only the green or one of the blue pegs could be in the right position. There is no way to move the blue peg to a new position without also moving the green peg. Therefore one of the blue pegs has to be in the right position. This means the green and blue pegs have to be in positions two, three or four.

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.

Third guess using the Mastermind all one color strategy
At this point the player knows that there is one green and one blue peg in the code. They also know that neither can be in position one. Therefore they put the green peg in position two and the blue peg in position three. They fill in the remaining two spots with red pegs.

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.

Third guess results using the all one color strategy
For their third guess this player received one black and two white key pegs. This player now knows three of the final colors: one green, one blue, and one red. They also know either the green, blue or one of the red pegs are in the correct position.

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.

Making the fourth guess
Based on previous guesses this player decided to keep the blue peg in the same position as it was in the previous two guesses. This is because they received a black key peg for each guess where blue was in position three. They also moved the red and green pegs to different positions. Finally they filled in the empty space with a yellow peg.

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.

Results of fourth guess using the all one color Mastermind strategy
The player received very useful information from their fourth guess. They received three black key pegs. Since there are no additional key pegs from their previous guess, they know yellow can’t be in the code. They also learned that red has to be in position two, blue is in position three, and green is in position four.

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.

Final guess using all one color Mastermind strategy
For their next guess this player replaced the yellow peg with a white peg. This turned out to be the right color. This player has solved the code within five guesses.

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.

Guess All One Color Mastermind strategy example
For their first guess this player used all green pegs. They received no key pegs. The player now knows that no green pegs are in the code.
Guess two
For their second guess this player chose all red pegs. Once again they received no key pegs. They now know neither green or red are in the code.
Guess three
For their third guess the player choose blue. They finally received one key peg. There is one blue peg in the code.
Fourth guess using the all one color Mastermind strategy
For their fourth guess the player used one blue and three yellow pegs. They received one black and two white key pegs. The final code has two yellow pegs in it. The player also knows that the blue peg cannot be in the first space. They know this because because they only received one black key peg. Since they received two white key pegs one has to correspond to one of the yellow pegs. Since a yellow peg is in the wrong position, the only new place you can move it is the first position.
Fifth guess
For the fifth guess this player moved a yellow peg to the first position since they know it must be there. They moved the blue peg to a new position, kept the other yellow peg in a position it was in the previous guess, and filled in the last space with a new color. They ended up receiving one black and two white key pegs. Since they know the yellow peg has to be in the first position, they know the other yellow and the blue peg have to be in the wrong positions. They also know that black is not in the code as they didn’t receive another key peg.
Sixth guess
For their sixth guess the player kept the yellow peg in the first position. They moved the other yellow peg and the blue peg to a new position. Finally they filled in the last position with a white peg. They received four key pegs from the guess. They now know all four colors. Since they received three white pegs, they know the final three pegs are not in the right positions.
Guessing the code using the all one color Mastermind strategy
This player finally figured out the code. They already knew the four colors, but they needed to move the last three pegs to new positions. Based on previous guesses the player knew that the blue peg had to be moved to the last position since it was previously placed in the other three positions and never received a black peg. A similar situation occurred with the second yellow peg. The player tried it in the second and fourth position and didn’t receive another black peg. Therefore it had to be in the third position. The white peg was then placed in the remaining space in 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

Guessing two different colors for the first three guesses
For each of your first three guesses you should guess two different colors. You do not have to follow the same color patterns that I did. Each guess should have two different colors in it though.

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.

Receiving four key pegs from guessing two different colors
From this players first guess they received four key pegs. They don’t have to make guesses with the other colors as they already know the four colors in the code. Due to receiving two black and two white key pegs, they also know some information about position. Since there are only two colors in the code, they know that one of the green and one of the yellow pegs are in the right position. They need to swap the positions of one of the green and one of the yellow pegs to get the final code.

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.

Receiving zero key pegs after guessing two colors
After guessing green and yellow for their first guess, this player received no key pegs. They now know neither green or yellow can be in the code.

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.

Receiving all four key pegs from two guesses
After the first two guesses this player has received a total of four key pegs. They do not have to guess blue and white since they know they can’t be in the code. The player now has to figure out the final distribution of these four colors. From the first guess there can either be two green, two yellow, or one of both colors. From the second guess there can be two red, two black or one of both.

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.

Receive three key pegs on one guess
On their first guess this player received three key pegs. They didn’t get any key pegs from their next two guesses. This means that the entire code consists of green and yellow 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.

Figuring out color distribution using two color Mastermind strategy
For their fourth guess this player picked all green pegs. They received three black pegs back. They now know there are three green and one yellow peg in the 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.

Guessing the code
Since this player knows three of the pegs are green and one is yellow, they made a guess with that combination of pegs. They guessed the correct layout of the pegs. In this situation the player could have actually skipped the fourth guess entirely. Since they received three key pegs from their first guess they know that one color had to have three pegs while the other only had one peg. They could have placed three pegs of one color and then placed one peg of the other color. If they didn’t receive four key pegs from the guess, they would know to swap the distribution of the two colors.

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.

Received three total key pegs from the two color Mastermind strategy
From their first three guesses this player received a total of three key pegs. They know there must be either a blue or white peg in the code. Their first guess received two key pegs and one key peg is missing entirely from the first three guesses. These two pieces of information reveal that there must either be three green or three yellow pegs in the code. It has to be three of one color because otherwise the first guess would have received three key pegs.

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.

Received no key pegs for fourth guess
For their fourth guess this player played four green pegs. They received no key pegs. Therefore they know three yellow key pegs are in the 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.

Received three key pegs from the fifth guess
For their fifth guess this player has decided to place three yellow pegs since they know these are in the code. They also chose to play a white peg from the third guess. They ended up receiving three key pegs, two black and one white. From these key pegs the player knows that white is not in the code so blue has to be.

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.

Solving the code with the two color Mastermind strategy
The player actually learned a lot of information from the key pegs received from the fifth guess. They learned blue had to be in the code. They also learned that a yellow peg has to be in position three since they received a white key peg. A yellow peg is the only one that could be in the wrong position. Looking back to the first guess the player knows that only one yellow can be in positions three and four since they only received one black peg. With all this information the player has figured out the code in six guesses.

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.

Finding a missing color from the code
Based on their first three guesses this player knows that two of the following colors can’t be in the code: green, yellow, red, and black.

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.

Finding a color not in the code
Based on their first three guesses this player knows that green and yellow can’t be in the code.

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.

Finding a color not in the final code using the two color Mastermind strategy
Since the player does not yet know a color that is not in the code, they use their fourth guess to find one. They guess four green pegs. Since they received no key pegs, they know green can’t be in the code. This means that a yellow peg has to be in the code (based on the first guess). Since the player received a black key peg from the first guess, they also know that the yellow peg has to be in either the third or fourth position.

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.

Solving the code using the two color Mastermind strategy
For their fifth guess this player decided to play two blue pegs in the first two positions. They fill the other two spots with green pegs that they know can’t be in the code. The player receives no key pegs. They now know that blue isn’t in the code. Based on guess three, they now know the code must have two white pegs. Since they received a white and black key peg from the third guess, they also know that one white peg has to be in position one or two, and the other has to be in position three or four.
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.

Trying to figure out the code
For their fifth guess this player received one black key peg. This gives the player a lot of information. It tells the player that one of the first two colors is blue due to the black key peg. The player also knows that a white peg has to be the first or second color due to the third guess. The player received one white and one black key peg. Based on the fifth guess the black peg corresponds to one of the blue pegs. Therefore the white key peg was for a white peg meaning that it was in the wrong position. Therefore the white peg has to be in either the first or second position. From the first and fourth guesses this player can also figure out that a yellow peg has to be in position three or four. The player still needs to figure out if red or black is in the final code. For their next guess they should use the information they have learned to make a guess such as blue, white, yellow, red.

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.

Trying to solve the code using the two color strategy
For their fifth guess this player received one white key peg. From this information they know that a blue peg has to be in position three or four. From guess three they also know a white peg has to be in position three or four. Finally they know a yellow peg has to be in position one or two due to the first guess.
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.

Solving the Mastermind code
For their fifth guess this player received two black key pegs. This player now knows that the first two colors must be blue. From the first guess they also know that yellow has to be in position three or four. They still need to figure out if red or black is the last color in the code that will be placed in position three or four.

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.

Solving the code
This player received one black and white key peg from their last guess. From this information they know that one of the first two colors has to be blue. One of the last two colors also have to be blue. From the first guess the player also knows that yellow has to be the third or fourth color in the code.

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

Solving the code using the two color Mastermind strategy
Since they received two white keys from their last guess, this player knows that a blue peg has to be in positions three and four. From the first guess they know that a yellow peg has to be in position one or two. They still need to figure out if red or black is in the code.

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.

Solving the final position of a color
For their first two guesses this player has received one black key peg. The player knows that green has to be in the code. Since they didn’t move the green peg from the first position between the two guesses, they know that must be its correct 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.

Trying the final the final positions for colors
Between their second and third guess this player acquired one additional black key peg. They know the final code has one blue, one green, and one yellow peg. Two of them are in the correct positions. One of them is wrong. Based on the second guess this player knows that neither green or yellow can be placed in the first position. Therefore the blue peg in the first position must be correct. Either the green or yellow peg must also be in the correct 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.

Solving for the final position of colors in Mastermind
For their fourth guess this player kept the blue and green pegs in the same positions. They moved the yellow peg and added the red peg. Since the number of black pegs went down, the player now knows that the yellow peg was in the correct position in guess three and green needs to be moved to position three or four.

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.

Solving position using white key pegs
In their first guess this player received a black key peg. For their second guess they received a white key peg. This tells the player that the green peg is in the wrong position. Its correct position must be two, three or four.

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.

Solving the final color positions in Mastermind
From guesses two through four this player learned quite a bit about the positioning of the colors. In guess two the player kept yellow in the same position and moved green to a new position. They ended up receiving less black key pegs. This means that yellow cannot be in position three. Green also can’t be in position two. For guess four the player kept yellow in the same position to figure out green’s position. Since they received a black key peg, they now know green has to be in position four. The player also knows that yellow has to be in position two. Guess two eliminated yellow from being in position one, while guesses three and four eliminated position three.

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.

Solving the positions of colors in Mastermind
In their previous guess this player received two black and one white key peg. From their previous guess they kept blue and green in the same positions and moved yellow to a new position. Since all of the key pegs are now black, the player now knows that blue has to be first. Yellow is second. Green is fourth. The player just has to figure out the fourth color which is placed in the third position.

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.