Mastermind is a game played by two players, the codemaker and the codebreaker. The game begins with the codemaker selecting a code, a sequence of four colors (digits, pegs or other symbols) (c1, c2, c3, c4) chosen from a set of six colors (repetitions allowed). The codebreaker will then try to guess the code. After each guess (g1, g2, g3, g4), the codemaker responds with two numbers. The first, the number of exact matches, is the number of times ck = gk , k = 1, ... , 4. The second, the number of near matches, is the number of colors in the guess which are the right color, but in the wrong position. In the applet bellow the number of exact matches is indicated by small red dots,
while pink dots define the second number. In the real game the codemaker uses small pegs for the same purpose.
Mastermind is a game of deduction. The task is to uncover 1 secret code selected from a set numbering 1296 (= 64) elements. With every judiciously chosen guess and a (frank) response, the set of possibilities shrinks. When only one element remains it is bound to be the secret code. If colors are represented by numbers starting with 0, then 0011 or 0012 are good candidates for the first guess. Afterwards, to come up with propitious guesses, the codebreaker may employ various problem-solving strategies [7], like pattern searching, establishing subgoals, or even proof by contradiction.
In other respects too Mastermind relates to the language and body of mathematics. To start from the beginning, have you noticed that the rules of the game as presented in the opening paragraph are ambiguous? If a guess contains the same color in more than 1 position, but the secret code contains it only once, an interesting situation may arise. For example, what should the correct response be to the guess 1123 if the secret code is 0145? Implicit in the rules are two assumptions:
Accordingly, in the example there is 1 exact match and no color matches. According to D. Knuth [5], the clearest way to state the rules exactly (at least when you speak to a mathematician or a computer) is to frame them into the mathematical language. Let ni is the number of times color i is in the code, and mi is the number of times color i is in the guess. Then color i is matched min(ni, mi) times. The total number of color matches is the sum of matches for all individual colors. Since exact matches are evaluated first, the number of near matches is calculated by:
Another little piece of mathematics comes from the question of how different are different secret codes. We suggested above that 0011 would be a good starting guess. Is 1122 any worse? Of course not. They are very much the same in that ordering of colors is to a great extent an arbitrary matter, as in fact is ordering of the positions. This device can be used by teachers [7, p xii] to invent seemingly different games. Better yet, students may be encouraged to look for patterns that make games equivalent.
Mastermind also admits meaningful modifications. The obvious one is to allow different number of colors and positions. In another variant, colors can't be repeated. This reduces the number of valid codes from 1296 to 360. Surprisingly [8], the game is hardly simplified: for a variety of strategies, the expected number of moves to solve either variant is very much the same. In yet another variant, the response may only contain the number of exact matches. This does make the game more difficult.
Two strategies are implemented in the applet below: Knuth's and a simple strategy of randomly picking a consistent guess, starting with 0012. The latter finds the code on average in under five guesses. This however will sometimes require seven guesses (but see a letter from Stephan Rafler).
Now, you are the codemaker. You must assist computer to arrive at the secret code by providing feedback to his/her guesses. To this end click on the two numbers to the right of every row. (In the game with only exact matches only one number is displayed.)
In [1], Chvatal mentions a problem, suggested by Pierre Duchet, of finding the minimum number of guesses the codebreaker needs to make all at once, without waiting for the answers, to determine the code. For example, if we consider a Mastermind game with two positions and three colors to choose from, then the responses to the two guesses (0, 2), (1, 2) will determine the code. You can see this by listing the 9 possible codes and the codemaker's responses to these two guesses to see that these response vectors are all distinct:
(0, 0) - (10 00)
(0, 1) - (10 01)
(0, 2) - (20 10)
(1, 0) - (01 10)
(1, 1) - (00 10)
(1, 2) - (10 20)
(2, 0) - (02 01)
(2, 1) - (01 02)
(2, 2) - (10 10).
We call such a game static Mastermind. The game is reminiscent of two coin weighing problems. The first is the famous 12 coins problem (known also as the the Oddball Problem): to determine with three weighings a single counterfeit coin from a set of 12 otherwise identical coins. Coins are split into groups that are weighed against each other on a balance scale. There is a solution in which the groups are determined up-front with the result of three weighings leading to the counterfeit coin in a unique manner.
The second problem is on an altogether different scale of difficulty: Suppose we are given n coins, which look quite alike, but some of which are counterfeit. A scale is given which will allow any number of the coins to be weighed together. By weighing a subset of the n coins we are able to determine the number of good coins in the subset. The question is: what is the
minimal number of weighings that will determine which are the good coins. (Subsets have to be identified ahead of time, before anything is weighed.) The problem was posed by Erdös and Rényi [2] in 1963. Their paper gives asymptotic results. Think of the desired subsets as sequences of zeros and ones, the ones indicating which coins are in the subset. Determining the number of good coins in the subset is the response giving the number of exact matches for the ones.
In the first applet at the top of the page, you may try the static variant by checking the Static box. The applet always displays responses to the same sequence of guesses and the codebreaker is given only one chance to come up with the secret code.
The applet below can be used to discover those sequences of guesses we used in the static Mastermind game for various numbers of positions and colors. For small numbers, the applet shows a table of responses to various guesses (the top row) and the (potentially) secret codes (the left column.) Click on the color vectors in the top row that you want to try. These will be highlighted in blue. As you do this, the color vectors that become shaded in red are the ones that are not as yet determined by the blue guesses. Add to or modify your selection (clicking on a selected vector will unselect it) to try to get rid of all the red ones. Try this with the example above (set the colors to 3 and pegs to 2). Try to find solutions for other cases. For larger numbers, the applet only displays the possible guesses. As before, the selected guesses appear in blue, the undetermined ones in red.
For the standard Mastermind game of four positions and six colors we know from Knuth's result that at least four static guesses will be needed to determine the code. It is not hard to argue that, for this static problem, at least five guesses are needed. The work of Knuth, and Koyama and Lai's, sited above, strongly suggests that the static problem can't be done in five
guesses, but no proof of this is known.
Greenwell [3] has found six guesses that always determine the Mastermind code:
(0, 1, 1, 0), (1, 2, 4, 3), (2, 2, 0, 0), (3, 4, 1, 3), (4, 5, 4, 5), (5, 5, 3, 2)
(In September 2002, Andy Lewicki, University of Nebraska - Lincoln, furnished two more six guess combinations:
(0, 3, 5, 1), (2, 2, 5, 0), (3, 2, 0, 3), (4, 1, 4, 1), (4, 4, 0, 5), (5, 5, 2, 3), and
(0, 3, 4, 0), (2, 2, 5, 0), (3, 2, 0, 3), (4, 1, 4, 1), (4, 4, 0, 5), (5, 5, 2, 3)
With such an uneven distribution of numbers these sixlets seem very strange indeed.)
You can verify this by using the applet above. Select the six guesses given above and you will notice that all 1296 possible codes are determined by this set. Try to find another solution. Is there a solution with five guesses? Try to find one and let us know if you do!
Our current knowledge on the number of static guesses depending on the number of pegs and colors is summarized in the following table:
Entries without the question mark have been verified by exhaustive search. Those with the question mark have been found with the help of the above applet. We thus do not have complete confidence that they are optimal.