How to win at Mastermind

Mastermind is a strategy game between two players wherein one player thinks of a colour code, and the other player has to guess what the code is using hints provided by the first player. It's quite a fun game, but if you're not very good it can be a bit infuriating.

There are quite a few algorithms described out there that will solve any Mastermind puzzle in less than 5 moves on average. However, they are… less than practical. Some of them rely on deep heuristics, while others rely on eliminating many combinations, and it's just not worth it.

HOWEVER.

There are a couple of Mastermind strategies that are humanly possible, and which work almost every time. In this article, we will focus solely on a strategy which works best if colours are allowed to be repeated. While it does work when colours aren't repeated, it doesn't work as well, and you might be forced to lose on occasion. Once I figure out a decent strategy for non-repeating colours, I'll update this article.

I will be representing the game in text. Rather than colours, we will use numbers, as the actual colours themselves are quite irrelevant. The positions that they are in will be represented by letters, A, B, C, and D, from left to right. We will also use numbers to represent the red and white pegs. The first number will represent how many red pegs there are (that is, how many colours are in the right position), and the second number will represent how many white pegs there are (how many colours are in the code but in the wrong position). The following is a completed game showing this notation:

A B C D
=======
1 1 1 1  |  0,0
2 2 2 2  |  1,0
2 3 3 3  |  0,2
3 2 4 4  |  3,0
3 2 4 5  |  2,2
3 2 5 4  |  4,0
- - - -  |  -,-
- - - -  |  -,-
- - - -  |  -,-
- - - -  |  -,-
=======
3 2 5 4

You may begin to get a small idea of the strategy here already. Let's see how correct you are!

Step one is always the same: choose a colour (referred to now as 1), and only use that colour for the first guess. This will immediately tell you how many of that colour there are, and there will be no white pegs. If there are any 1s in the code, then they must be in the correct place.

1 1 1 1  |  1,0
- - - -  |  -,-
- - - -  |  -,-
- - - -  |  -,-
- - - -  |  -,-
- - - -  |  -,-
- - - -  |  -,-
- - - -  |  -,-
- - - -  |  -,-
- - - -  |  -,-
=======
? ? ? ?

We now immediately know that there must be exactly one 1 in the code. All future guesses will include one of those.

Step two is to replace three of the colours with the second colour (2), so for example:

1 1 1 1  |  1,0
1 2 2 2  |  0,2
- - - -  |  -,-
- - - -  |  -,-
- - - -  |  -,-
- - - -  |  -,-
- - - -  |  -,-
- - - -  |  -,-
- - - -  |  -,-
- - - -  |  -,-
=======
? ? ? ?

We know now that 1 is not in position A, and 2 is not in position B, C, or D. This means that 2 must be in position A, but we aren't positive where exactly 1 is.

We now replace two of the 2s with the third colour (3). We know that 2 must be in position A, so we can place it there and never move it. We aren't entirely sure where 1 goes, but it could be in position B, so we will check there:

1 1 1 1  |  1,0
1 2 2 2  |  0,2
2 1 3 3  |  1,1
- - - -  |  -,-
- - - -  |  -,-
- - - -  |  -,-
- - - -  |  -,-
- - - -  |  -,-
- - - -  |  -,-
- - - -  |  -,-
=======
? ? ? ?

We can now ascertain a few things. First, there are only two red and white pegs total, which means that there can't be any 3s. If there were, then they would have appeared in the indicator pegs, but they haven't. We also know that the 1 can't appear in position B, as we already know that the 2 is locked in place, meaning that the 1 must be out of place. We now replace the 3 with the fourth colour (4), moving the 1 to position C.

1 1 1 1  |  1,0
1 2 2 2  |  0,2
2 1 3 3  |  1,1
2 4 1 4  |  2,2
- - - -  |  -,-
- - - -  |  -,-
- - - -  |  -,-
- - - -  |  -,-
- - - -  |  -,-
- - - -  |  -,-
=======
? ? ? ?

Aha! This shows that all colours are correct, as there are a total of 4 indicator pegs, the same as the number of positions. However, we're still not there just yet. Note that two of the colours are still incorrect. This means that they must be swapped. We know that the 2 is correct, and we also know that swapping the 4s won't do anything. Therefore, the 1 needs to move, and since it can't be in positions A or B either, it must be in position D. We have now cracked the code, with 5 guesses remaining.

1 1 1 1  |  1,0
1 2 2 2  |  0,2
2 1 3 3  |  1,1
2 4 1 4  |  2,2
2 4 4 1  |  4,0
- - - -  |  -,-
- - - -  |  -,-
- - - -  |  -,-
- - - -  |  -,-
- - - -  |  -,-
=======
2 4 4 1

Let's try another example. Once again, we start by guessing all 1s:

1 1 1 1  |  0,0
- - - -  |  -,-
- - - -  |  -,-
- - - -  |  -,-
- - - -  |  -,-
- - - -  |  -,-
- - - -  |  -,-
- - - -  |  -,-
- - - -  |  -,-
- - - -  |  -,-
=======
? ? ? ?

Uh oh, the first guess doesn't appear to have been very helpful at all. No problems, though: we can just replace all the 1s with 2s instead.

1 1 1 1  |  0,0
2 2 2 2  |  2,0
- - - -  |  -,-
- - - -  |  -,-
- - - -  |  -,-
- - - -  |  -,-
- - - -  |  -,-
- - - -  |  -,-
- - - -  |  -,-
- - - -  |  -,-
=======
? ? ? ?

Okay, cool. So we know that there must be exactly two 2s in the code. Next, we replace the other two 2s with 3s.

1 1 1 1  |  0,0
2 2 2 2  |  2,0
2 2 3 3  |  1,1
- - - -  |  -,-
- - - -  |  -,-
- - - -  |  -,-
- - - -  |  -,-
- - - -  |  -,-
- - - -  |  -,-
- - - -  |  -,-
=======
? ? ? ?

This tells us that there are no 3s in the code, and that exactly one of the 2s must be in position C or D. We now move on to the 4s, gently moving a 2 along.

1 1 1 1  |  0,0
2 2 2 2  |  2,0
2 2 3 3  |  1,1
2 4 2 4  |  1,1
- - - -  |  -,-
- - - -  |  -,-
- - - -  |  -,-
- - - -  |  -,-
- - - -  |  -,-
- - - -  |  -,-
=======
? ? ? ?

Same result as before, which is slightly frustrating. We move on to the 5s.

1 1 1 1  |  0,0
2 2 2 2  |  2,0
2 2 3 3  |  1,1
2 4 2 4  |  1,1
2 5 5 2  |  0,2
- - - -  |  -,-
- - - -  |  -,-
- - - -  |  -,-
- - - -  |  -,-
- - - -  |  -,-
=======
? ? ? ?

Believe it or not, we have now figured out the code, assuming there are only 6 colours. We know that the two 2s can't be in positions A and D, and therefore they must be in positions B and C. We also know that, as none of the previous colours have worked so far, and there is only one colour left, then the last colour must be in positions A and D. We've cracked the code, with 4 guesses to spare.

1 1 1 1  |  0,0
2 2 2 2  |  2,0
2 2 3 3  |  1,1
2 4 2 4  |  1,1
2 5 5 2  |  0,2
6 2 2 6  |  4,0
- - - -  |  -,-
- - - -  |  -,-
- - - -  |  -,-
- - - -  |  -,-
=======
6 2 2 6

I do just want to go back on this for a bit. Let's go back to the fourth guess. What would have happened if, instead of getting 1 red peg and 1 white peg, we got 2 red pegs and 1 white peg?

1 1 1 1  |  0,0
2 2 2 2  |  2,0
2 2 3 3  |  1,1
2 4 2 4  |  2,1
- - - -  |  -,-
- - - -  |  -,-
- - - -  |  -,-
- - - -  |  -,-
- - - -  |  -,-
- - - -  |  -,-
=======
? ? ? ?

Let's figure out the code together. If we think about it, we can realise that one of the 2s is in the wrong place, and one of the 4s is in the right place. We can tell this by realising that if both of the 2s were in the right place, then there couldn't be any white pegs, as one of the 4s would also have to be in the right place. We move on to the 5s, keeping one of the 4s in place:

1 1 1 1  |  0,0
2 2 2 2  |  2,0
2 2 3 3  |  1,1
2 4 2 4  |  2,1
2 4 5 2  |  0,3
- - - -  |  -,-
- - - -  |  -,-
- - - -  |  -,-
- - - -  |  -,-
- - - -  |  -,-
=======
? ? ? ?

Instantly we can figure out the code. We know that the 2s must be in positions B and C due to the same reasoning as before. We also know that the 4 we kept was the wrong 4, and there's only one other place for the 4 to go (position D). There is only one colour left, colour 6, and that must go in position A.

1 1 1 1  |  0,0
2 2 2 2  |  2,0
2 2 3 3  |  1,1
2 4 2 4  |  2,1
2 4 5 2  |  0,3
6 2 2 4  |  4,0
- - - -  |  -,-
- - - -  |  -,-
- - - -  |  -,-
- - - -  |  -,-
=======
6 2 2 4

One more example, and that should be enough for you to get a pretty good idea as to how this works. We start with all 1s as usual.

1 1 1 1  |  0,0
- - - -  |  -,-
- - - -  |  -,-
- - - -  |  -,-
- - - -  |  -,-
- - - -  |  -,-
- - - -  |  -,-
- - - -  |  -,-
- - - -  |  -,-
- - - -  |  -,-
=======
? ? ? ?

Unlucky, but not a complete loss. We move on to the next colour.

1 1 1 1  |  0,0
2 2 2 2  |  0,0
- - - -  |  -,-
- - - -  |  -,-
- - - -  |  -,-
- - - -  |  -,-
- - - -  |  -,-
- - - -  |  -,-
- - - -  |  -,-
- - - -  |  -,-
=======
? ? ? ?

Irritatingly, we have no 2s either. It looks like we're going to have to move on to the 3s.

1 1 1 1  |  0,0
2 2 2 2  |  0,0
3 3 3 3  |  1,0
- - - -  |  -,-
- - - -  |  -,-
- - - -  |  -,-
- - - -  |  -,-
- - - -  |  -,-
- - - -  |  -,-
- - - -  |  -,-
=======
? ? ? ?

Finally. We now know for sure that there is exactly one 3, somewhere in the code. We can now try and work out where it is.

1 1 1 1  |  0,0
2 2 2 2  |  0,0
3 3 3 3  |  1,0
3 4 4 4  |  1,1
- - - -  |  -,-
- - - -  |  -,-
- - - -  |  -,-
- - - -  |  -,-
- - - -  |  -,-
- - - -  |  -,-
=======
? ? ? ?

What can we figure out from this? Well, we know that there is one 3 and one 4 in the code. Furthermore, we can show that the 3 is in the wrong position, while one of the 4s is in the right position: if the 3 was in the right position, then the 4 would have to be in the right position as well, but it can't be. We will now move the 3 through the positions, leaving one 4 each time.

1 1 1 1  |  0,0
2 2 2 2  |  0,0
3 3 3 3  |  1,0
3 4 4 4  |  1,1
5 3 4 5  |  1,2
- - - -  |  -,-
- - - -  |  -,-
- - - -  |  -,-
- - - -  |  -,-
- - - -  |  -,-
=======
? ? ? ?

Instantly we know that the code will have 3, 4, 5, and 6, in some order. This is an interesting case, as although repetitions were allowed, we didn't end up seeing any. Sometimes this happens, and this method certainly is not efficient for it, but it will still work.

Can we work out the code yet? Unfortunately not: there are still 6 possible codes left (challenge: find them all!), and we don't have 6 guesses. With that in mind, we will have to make something of a blind guess in order to eliminate some possibilities. Ideally, we want to keep the previous guess as intact as possible, and only change one colour; this will allow us to make some big insights. Let's try 5 3 4 6 and see what happens.

1 1 1 1  |  0,0
2 2 2 2  |  0,0
3 3 3 3  |  1,0
3 4 4 4  |  1,1
5 3 4 5  |  1,2
5 3 4 6  |  1,3
- - - -  |  -,-
- - - -  |  -,-
- - - -  |  -,-
- - - -  |  -,-
=======
? ? ? ?

This has big implications. We know that the 6 is out of place, and from what we have already learned, we can immediately work out that it belongs in position A. Swapping 5 and 6 and not doing anything else will give us 2 red pegs at least. Since we can't figure anything else out right now, we might as well do that and see what we get.

1 1 1 1  |  0,0
2 2 2 2  |  0,0
3 3 3 3  |  1,0
3 4 4 4  |  1,1
5 3 4 5  |  1,2
5 3 4 6  |  1,3
6 3 4 5  |  2,2
- - - -  |  -,-
- - - -  |  -,-
- - - -  |  -,-
=======
? ? ? ?

We know we need to swap two items. There's not much else we can really do here, so let's swap the 4 and 5:

1 1 1 1  |  0,0
2 2 2 2  |  0,0
3 3 3 3  |  1,0
3 4 4 4  |  1,1
5 3 4 5  |  1,2
5 3 4 6  |  1,3
6 3 4 5  |  2,2
6 3 5 4  |  1,3
- - - -  |  -,-
- - - -  |  -,-
=======
? ? ? ?

Doesn't look like that worked. What if we had swapped the 3 and 4 instead?

1 1 1 1  |  0,0
2 2 2 2  |  0,0
3 3 3 3  |  1,0
3 4 4 4  |  1,1
5 3 4 5  |  1,2
5 3 4 6  |  1,3
6 3 4 5  |  2,2
6 3 5 4  |  1,3
6 4 3 5  |  1,3
- - - -  |  -,-
=======
? ? ? ?

Oh, dear. Doesn't look like that guess has worked well either. With that, however, we only have one option which makes sense after every guess, so it better be the right one.

1 1 1 1  |  0,0
2 2 2 2  |  0,0
3 3 3 3  |  1,0
3 4 4 4  |  1,1
5 3 4 5  |  1,2
5 3 4 6  |  1,3
6 3 4 5  |  2,2
6 3 5 4  |  1,3
6 4 3 5  |  1,3
6 5 4 3  |  4,0
=======
6 5 4 3

Hurrah.

This kind of thing can happen, occasionally. You still need to do some pretty clever thinking in order to work out the code, but it can be done. That said, sometimes there are cases where it's never guaranteed that you'll win, which can be very annoying, but you will certainly win more times than you lose.

Free Web Hosting