# An Optimal Mastermind (4,7) Strategy and More Results in the Expected Case

AAn Optimal Mastermind (4,7) Strategy andMore Results in the Expected Case

Geoﬀroy

Ville ∗ March 2013

Abstract

This paper presents an optimal strategy for solving the 4 peg-7 colorMastermind MM(4,7) in the expected case (4.676) along with optimalstrategies or upper bounds for other values. The program developed isusing a depth-ﬁrst branch and bound algorithm relying on tight upperbound, dynamic lower bound evaluation and guess equivalence to prunesymmetric tree branches.

Mastermind is a code-breaking game for two players. This peg game was in-vented in 1970 by Mordecai Meirowitz. One of the players – the code-maker – chooses a secret code of four pegs of six possible repeatable colors, thus onecode among 1296 possibilities. The other player – the code-breaker – tries tobreak the code by making guesses, i.e., submitting one code. The code-makeranswers using four pegs of two colors: a black peg means that a guess pegmatches both color and position of a code peg, whereas a white peg means thata guess peg matches the color but not the position of a code peg. An answercontaining less than four pegs indicates that one or more guess pegs are notpart of the code. The answer is global so the code-breaker does not know whichblack/white peg corresponds to which guess peg. The code-breaker has 8 to 12guesses (depending of the board) to ﬁnd the correct code.The classic board game MM(4,6) can also be played with a virtual seventhcolor by leaving one or several holes in the code, MM(4,7). It can be generalizedto any number of pegs or colors, MM(p,c).Much research has been done on this game and its variants. Knuth [10]proposed a strategy that requires 4.478 guesses, on average, to ﬁnd the code(expected case) while always ﬁnding the code in a maximum of 5 guesses (worstcase). While 5 guesses is the optimal (minimal worst case), several authorsproposed diﬀerent other one-step-ahead heuristic algorithms to reduce the av-erage number of cases until Koyama and Lai [12] found the optimal strategy forthe expected case to be 4.340 using a depth-ﬁrst backtracking algorithm. Thisoptimal strategy requires 6 guesses. Some authors also solved theoretically afew expected cases (Chen and Lin [3], Goddard [5]) or worst cases (Jäger andPeczarski [9]). ∗ < gville. mastermind at gmail. com > a r X i v : . [ c s . G T ] M a y igher cases were soon tackled through diﬀerent approaches like geneticalgorithms (Merelo-Guervós et al. [14]) and upper bounds of the expected casewere found.The game of Bulls and Cows, which may date back a century or more anddoes not allow color repetition, is also worthy of mention as some Mastermindresearch started on this problem (Chen and Lin [4]).Another variant called Static Mastermind is also popular. In this game, allthe guesses are made at once. Then, given all the answers, the code can bededuced with one additional guess. Minimizing this number of guesses also ledto much research (e.g., Goddard [6] or Bogomolny and Greenwell [2]).To pursue the optimal strategy track in the expected case and to take thelist of results published by Goddard [5] a step further, a depth-ﬁrst branch andbound algorithm with a tight upper bound at the start, a dynamic lower boundevaluation during the resolution, and detecting guess-equivalence at each stepusing the symmetries of the colors that were played was developed.Section 2 deﬁnes mathematical notations and ways of presenting the prob-lem. Section 3 reviews in more details past work on both MM(4,6) and MM(5,8)and presents or reproduces results of detailed heuristic models. Section 4 de-scribes theoretical established results. Section 5 explains in detail ways to reducecomputing time of the program used to ﬁnd MM(4,7). Section 6 presents resultson the expected value. The conclusion makes suggestion for further research. Throughout this article, the following mathematical notations will be used: c = p = N p,c = p pegs and c colors = c p (3) G p = p pegs (and c colors) = p ( p + 3)2 (4) E ( p, c ) = best average in the expected case for MM(p,c) (5) W ( p, c ) = best worst case for MM(p,c) (6) M M ∗ ( p, c ) = MM(p,c) with possible guesses only (7)While Equation 3 is obvious, Equation 4 requires more explanation. It canbe obtained two ways.The ﬁrst one is inferred from Table 1. All grading possibilities are on theupper left part of the table. Moreover, on the diagonal, all gradings are possiblebut ( p − , for obvious reasons. Thus, G p = whole table (cid:122) (cid:125)(cid:124) (cid:123) ( p + 1) × ( p + 1) − diagonal (cid:122) (cid:125)(cid:124) (cid:123) ( p + 1)2 + p = ( p + 1) p + 2 p p ( p + 3)2 . 2able 1: The 14 possible grades in MM(4,7)b w 0 1 2 3 40 (0,0) (0,1) (0,2) (0,3) (0,4)1 (1,0) (1,1) (1,2) (1,3) x2 (2,0) (2,1) (2,2) x x3 (3,0) x x x x4 (4,0) x x x xThe second way comes from the fact that G p can also be seen as the numberof ways to solve b + w + z = p ( z integer), minus the impossible solution of ( b, w ) = ( p − , . Therefore, G p = (cid:18) p + 3 − − (cid:19) − p + 2)!2! p ! − p + 1)( p + 2) −

22 = p + 3 p p ( p + 3)2 . In the MM(4,7) case, N , = 2401 and G = 14 . The total number of codes can also be thought of as the sum of all the numberof possible combinations of i colors.Let C i be the number of possibles codes of p pegs of exactly i colors chosenamong c . C i is the way of choosing i colors among c multiplied by the numberof ways of setting these i colors in a code of length p , denoted Z pi . As there areno more than p colors and c possibilities, ∀ ( p, c ) , N p,c = c p = min ( c,p ) (cid:88) i =1 C i = min ( c,p ) (cid:88) i =1 (cid:18) ci (cid:19) Z pi . where Z pi is the sum of all possible distributions of the i colors and givenby the following formula: Z pi = (cid:88) n + n + ··· + n i = p ( n + n + · · · + n i )! n ! n ! · · · n i ! = (cid:88) n + n + ··· + n i = p p ! n ! n ! · · · n i ! Z p = 1 because there is only one way of placing p pegs of the same colorand, ∀ p ≤ c, Z pp = p ! because it is the number of ways of coding p pegs of atleast p colors.For example, in the MM(4,7) game, N , = (cid:18) (cid:19) (cid:18) (cid:19) ( 4!3!1! + 4!2!2! + 4!1!3! ) + (cid:18) (cid:19) (3 × (cid:18) (cid:19) Let’s picture a tree representation of a solution (Figure 1). Each node is a guessand each branch an answer. A ( p, branch leads to an end node, also calleda leaf node. Any given solution tree has two attributes, its depth D and itsexternal path length L .To compare diﬀerent strategies, a uniform distribution of codes is assumed.Consequently, the two complementary ways of measuring a strategy performanceare the maximum number of guesses needed in the worst case W , and the averagenumber of guesses in the expected case E . They can be expressed in terms of D and L as follows: W = D + 1 (8) E = LN (9)In Figure 1, D = 2 and L = 1 × × × , thus W = 3 and E = = 2 . . More generally, if f i is the number of codes found at guess i ,then N = (cid:80) wi =1 f i and L = (cid:80) wi =1 if i .Minimizing W and minimizing E are two diﬀerent goals. For example, theoptimal value of E for MM(4,6) is E (4 ,

6) = 5625 / . , with 6 movesin the worst case[12]. But Knuth [10] showed that one could always ﬁnd theanswer in < questions ( W (4 ,

6) = 5 in fact) with an increase in the numberof moves.Finally, as both an artistic representation and an illustration of the unbal-anced nature of a solution tree, Figure 2 depicts an optimal solution of MM(3,6)in the form of a circular tree or ﬂower.

Let g be the grading function. g is commutative, i.e., given 2 codes a and b , g ( a, b ) = g ( b, a ) . Therefore, if a guess a is graded (2,0) for example, the solutionlies among the codes graded (2,0) with a .All Mastermind algorithms are based on this fundamental property. A tree with a single node has a depth of 0. The external path length is the sum of the path lengths to each leaf node.

121 122 126 153 323 623 (3,0) (2,0) (2,0) (1,0) (1,0) (1,0) (1,1) (2,0) (3,0) (2,0) (1,0) (1,0) (1,0) (2,0) (2,0) (2,0) (3,0) (1,0) (1,0) (1,1) (1,2) (1,0) (1,0) (1,0) (3,0) (1,0) (1,0) (1,0) (1,0) (1,0) (1,0) (1,0) (3,0) (2,0) (0,1) (1,0) (1,0) (1,1) (1,0) (2,0) (3,0) (0,2)Diﬀ. grades 3 3 4 2 3 4 Knuth was the ﬁrst to show that using guesses that are not valid codes whenplayed could reduce the solution tree depth.This remarkable result is not intuitive and is illustrated by Table 2. It showsa case in which 6 codes are left to be found, and guessing a seventh discriminatesall the codes in one question ( L = 1 × × ) whereas playing any ofthe 6 codes requires at least two ( L = 1 × × × in the case of4 diﬀerent answers).For example, when solving MM(4,6) using Knuth’s algorithm but with pos-sible guesses only, namely MM*(4,6) , the result goes up to / . ;after 2 guesses only, 7 (1+6) codes are found while the original algorithm reveals14 (1+13) codes after two guesses.Therefore and unfortunately, the number of codes to try at each step is notthe monotonously decreasing set that one would hope for, it remains constantminus the previous guesses. Solving the problem requires memory and processortime. Some well-known heuristics have been developed to this end. The ﬁrst algorithm one might think of programming is extremely simple yeteﬃcient. It starts with the ﬁrst code then:1. submits a guess and stores the result,2. chooses, in lexical order, the next guess compatible with the stored results,and3. stops when ( b, w ) = ( p, .On the one hand, its main advantages are simplicity and very low computa-tion and memory cost. Only previous guesses and grades are stored. Each newcode is created along the way and graded against the codes in history. Thereis no need to store the set of possible solutions at each step. On the otherhand, since it only uses possible codes and does not try to aim for any opti-mization, its value is very high. When starting with , the expected case is We will see in subsection 5.2 how solving a possible case can still be useful. ,

801 4 . Expect. size ,

696 4 . ,

723 4 . ,

668 4 . ,

625 4 . / . , with 9 guesses in the worst case. Shapiro [17] describedthis strategy.Unlike the algorithms described in subsection 3.3, it chooses the next possiblesolution solely on the lexical order basis. Thus, if the ﬁrst guess makes theprogram jump faster toward a suitable reduced range of possible codes, theresult improves. Swaszek [18] tried choosing the guesses randomly and foundan expected case of 4.638.A non random approach can also improve the result. The ﬁrst possible codein lexical order is still chosen at each step. If is forced as the ﬁrst guess,the expected case reduces to / . with 8 guesses. If the ﬁrstguess is , giving information about the colors that are usually explored atthe very end, the result becomes / . and 7 guesses.As shown in the next section, there is no best ﬁrst code: depending on whichalgorithm is used, one code suits it best. Other algorithms look one step ahead and choose the next guess accordingly.All these algorithms sort, for all guesses, the remaining possible solutions bygrade into subsets. They diﬀer by the way, based on these subsets, they selecttheir next guess. When several guesses are eligible, the ﬁnal choice is made byselecting the possible ones, if any, among them and ultimately choosing the ﬁrstin lexical order.Limitations are twofold. Obviously, starting with the best ﬁrst-step guessonly optimizes the next step, while another might have optimized the next twosteps and be globally better. Using another sorting policy than lexical orderamong the eligible guesses at each step has an impact on the results whichshows that guesses are not all equivalent (impact on the following steps).Knuth [10] showed that a guess that would minimize the maximum numberof codes in each of k subsets ( Max. size algorithm, minimize max ( n i ) i =1 ..k ) hasan average of / . . The best ﬁrst code (lexical order, one stepahead) for this algorithm is .The next guess can also be selected by minimizing the expected size of the k subsets (Irving [8] , minimize (cid:80) k n i N ), maximizing their entropy (Neuwirth I did not have access to that paper. I did not have access to that article either and quote it through what other articlesdescribe. is among the ‘best’ ﬁrst guess of Table 3. Irving used this strategy for the ﬁrst two guesses and did an exhaustive search after. Thepure strategy was used here for all the steps. ,

265 5 . ,

613 4 . ,

409 4 . ,

382 4 . ,

388 4 . ,

633 5 .

970 10

Max. size ,

966 5 . Expect. size ,

287 5 . Entropy , . Most parts ,

834 5 .

549 9 [15] , maximize (cid:80) k − p i Log ( p i ) with p i = n i /N ) or maximizing their number(Kooi [11], maximize k ).Table 3 summarizes the results for MM(4,6). Most parts gives the lowestscore in expected value ( / . ) while Max. size gives the lowestnumber of guesses in the worst case (5). Recreating this table, the same resultsas [11] were found except for

Entropy (5723 versus 5722 ).The results of these algorithms on the MM(4,7) case are presented in Ta-ble 4. This time, Entropy performs better. As for MM(4,6), diﬀerent results areobtained when starting with diﬀerent ﬁrst guesses or choosing an order otherthan lexical.The results of these algorithms for MM(5,8) were compared with the resultsof Heeﬀer and Heeﬀer [7]. The standard deviation algorithm introduced in thepaper was not implemented because of its poor performance. The results found,presented in Table 5, are very diﬀerent. The result of

Consistency cannot becompared with the random runs performed by the authors. But for

Max. size ,5.614 was found versus 5.670, for

Expect. size

Entropy

Most parts , was used as the ﬁrst guess (ﬁrst inlexical order) rather than reported in the article. Still, the diﬀerenceis signiﬁcant (5.549 versus 5.693). The source of the discrepancies was notfound. Even though these diﬀerences, the algorithms perform in the sameorder:

Entropy is the winner, followed by

Expect. size , Most parts , Max. size and ﬁnally

Consistency . In terms of worst case,

Entropy has one code thatrequires 8 guesses. There should be a way to ﬁnd 7 guesses by increasing theexpected average.As (p,c) increases, the solution tree size prevents the use of these algorithmsand other methods such as genetic algorithm come into play. Merelo and al I did not have access to his article.The pure strategy was kept. When running the program taking the last code in lexical order, 5722 is found. All the MM(4,6) results are quoted in the article and supposedly reproduced by theauthors. Furthermore, for MM(5,8), the table of partitions and choice for the ﬁrst guess arethe same. The authors could not be reached.

Entropy

This section presents theoretical results already established. MM(p,1) andMM(1,c) are obvious. MM(2,c) is solved for the general case. MM(3,c) is solvedfor the pessimistic case.

As there is only one code p (cid:122) (cid:125)(cid:124) (cid:123) , ∀ p, (cid:40) E ( p,

1) = 1 W ( p,

1) = 1 (10) If c = 1 , MM(1,c) is equivalent to the previous case and takes one guess to solve.If c = 2 , it takes two guesses ( = (1,0) and = (0,0)- (1,0)). So E = = and W = 2 .Similarly, for c = n , it always takes n guesses. Thus, W = n and E = 1 n × n (cid:88) i =1 i = 1 n × n ( n + 1)2 = n + 12 Finally, ∀ c, (cid:40) E (1 , c ) = c +12 W (1 , c ) = c (11) Chen and Lin [3] and Goddard [5] solved this model for E and W and obtainedthe following results: E (2 ,

2) = 2 , and ∀ c ≥ , E (2 , c ) = n + 51 n − n + 4824 n , n even n + 51 n − n + 6924 n , n odd (12) ∀ c ≥ , W (2 , c ) = (cid:98) c/ (cid:99) + 2 (13)The results for c = 2 , · · · , , presented in Tables 6 and 7, verify these formulasfor E and W .As Jäger and Peczarski [9] already noticed, all results for W , presented inGoddard [5], do not all satisfy Equation 13.9 .4 Partial MM(3,c) Jäger and Peczarski [9] proved that: ∀ c ≥ , W (3 , c ) = (cid:98) ( c − / (cid:99) + 4 (14)and computed W (3 , c ) c =2 , , = (3 , , .Once again, the results for c = 2 , · · · , , presented in Tables 6 and 7, verifythis formula for W .This article shows more results for W and introduces other theoretical in-equalities in the pessimistic case.I am unaware of any paper on E (3 , c ) . For higher cases, one must be. . . patient. The goal was the optimal in theexpected case, so the rest of this article will focus on more E ( p, c ) results forcertain values of ( p, c ) .A depth-ﬁrst branch and bound algorithm, similar in principle to the depth-ﬁrst backtracking algorithm used by Koyama and Lai [12] for MM(4,6), wasdeveloped. It also explores systematically all possible combinations by branchand goes to the lowest level but saves the best value found (incumbent). Assoon as the current branch reaches a higher score, the exploration stops anda new branch is explored. A lower bound evaluation with dynamic update isalso used to stop a branch exploration before it actually reaches a higher score:reaching a higher predictive score is suﬃcient. A tight upper bound comes asa proper incumbent and allows eﬃcient pruning from the start. Finally, somesymmetries of the problem are also detected to avoid exploring all codes at eachlevels.Given the problem size, optimizing computation time and memory space iscrucial. Three main ideas guided this optimization:1. Use as many shortcuts as possible,2. Prune the tree as much as possible using a good upper bound from thestart and a dynamic lower bound evaluation,3. Use symmetry as much as possible to avoid exploring similar branches. Some obvious cases encountered along the resolution can be solved right awayand can shorten the computation time. To manage the program complexitythroughout the years of this work, only simple ones were implemented.For any set of size k < G p codes, if one of them can discriminate all ofthe others, an absolute minimum is found and this guess can be played rightaway. The external path is increased by × × ( k −

1) = 2 k − and 2guesses are needed at most. Otherwise, if a non-possible code can do the same(see Table 2 for such an example), the external path is increased by one more The best solution of level n + 1 is always sought for each combination of level n . × × k = 2 k ) and at most two guesses are still needed. Each time suchconﬁgurations are found, a shortcut can be taken.Conversely, when a code creates only one subset, nothing is gained and theprogram can backtrack immediately.There exist reﬁnements of the ﬁrst shortcut. The k = 1 case is trivial (justplay the code!) and can be treated upstream separately. The k = 2 case can alsobe treated upstream after noticing that simply playing one of the codes followedby the second exactly represents the optimum described. The k = 3 case couldalso be treated separately but has an impact on the number of guesses. Indeed,either one of the three codes can segregate the other two and an optimum isfound ( × × ), or playing each of the three codes in sequence leadsto 6 ( × × × ) as playing a non-possible code segregating allothers would also lead us to ( × × ). But in the latter case, 3 guessesare nevertheless needed instead of 2. This k = 3 case was not implemented assuch. This issue is addressed in the conclusion.Another shortcut deals with an answer of (0,0) after the ﬁrst guess. Whensolving the possible case for MM(p,c), namely MM*(p,c), the (0,0) answersmeans that none of the k colors used in the ﬁrst guess are part of the code andthe branch itself is therefore MM*(p,c-k). In this case, when the program isrun to obtain a value and not a solution, using previously found results savesa lot of time. A similar scheme could be applied in the general case. Indeed,in the same situation, but using all possible colors, it would be the same assolving the MM(p,c-k) but with more available colors (one in fact, as explainedin subsection 5.4). Saved MMe(p,c) data (e for extended ) from previous runscould also be used in a run where the result is desired rather than the solution.This shortcut was implemented for possible solutions and not for the generalcase. Note that these values are also used for upper bounds, as explained inthe next section, and allow easy detection of discrepancies when testing newversions of the program.Finally, as obvious as it may seem, storing all pairwise gradings into a tablesaves computing time at each node. As there is a memory/speed trade-oﬀ here,an option to turn it oﬀ comes in handy. Keeping the best value found so far allows branch pruning each time this in-cumbent is exceeded. This saves a lot of unnecessary computation and is thebasis of the algorithm. But the higher the incumbent, the more unnecessarybranches are explored. A good upper bound at the beginning accelerates thepruning process from the start.An excellent upper bound is the optimal value for MM*(p,c). Such comput-ing option was introduced in the program. This version is obviously much fasterbecause the number of codes is reduced at each step rather than remaining con-stant. The ﬁnal result, while not optimal in the general case , is already prettyclose. For example, in Table 8, the values for MM*(4,6) and MM*(4,7) are bet-ter than those found with the one-step-ahead algorithms. This ‘possible’ versionof the program is fed, in turn, an upper bound from a heuristic computation. Note that the results are the same for 9 cases between Table 6 and Table 8. .3 A dynamic lower bound evaluation To be eﬃcient, the pruning process requires, for each branch, a lower boundevaluation beforehand and a dynamic update along its resolution. The pruningoccurs sooner as any overrun above the incumbent is not only detected but alsopredicted.A classic lower bound can be computed imagining that the remaining codesare found using a perfect tree. Such a tree would have a maximum branchingfactor of G p at each internal node and would be perfectly balanced. After q questions , all the nodes are leaves. For such a tree, let L ( p, q ) denotes theexternal path length and T ( p, q ) the total number of leaves. Then, ∀ q ≥ , T ( p, q ) = q (cid:88) i =1 ( G p − i − = q − (cid:88) i =0 ( G p − i (15)One question implies 1 leaf node ( ( p, immediately). Two questions imply G p branches and ( p, for all G p − leaves at question 2, thus T ( p,

2) = 1+( G p −

1) = G p is the total number of nodes and L ( p,

2) = 1 × × ( G p −

1) = 2 G p − .This idea was explained in the shortcut section.Given a number of nodes of M , and if the perfect tree of M nodes can befound in q + 1 questions, the external path is equal to the path of q + 1 questionsminus the total number found in previous questions: ∀ q > , T ( p, q ) < M ≤ T ( p, q + 1) = >L ( p, q + 1 , M ) = q (cid:88) i =1 i ( G p − i − + ( q + 1)( M − T q )= ( q + 1) M − [( q + 1) T q − q − (cid:88) i =0 ( G p − i ( i + 1)]= ( q + 1) M − q − (cid:88) i =0 ( G p − i ( q + 1 − i − q + 1) M − q − (cid:88) i =0 ( G p − i ( q − i ) (cid:124) (cid:123)(cid:122) (cid:125) S Let S ( p, q ) denote the Sum of external path lengths after q questions in anoptimal tree, i.e., ∀ q ≥ , S ( p, q ) = q − (cid:88) i =0 ( G p − i ( q − i ) , and S ( p,

0) = 0 (16)then, given a p value, the lower bound equation becomes T q < M ≤ T q +1 = > L q +1 = ( q + 1) M − S q (17) Depth is q -1 by deﬁnition. S and T are related by recursion equations, ∀ q ≥ , S q +1 = q (cid:88) i =0 ( G − i ( q + 1 − i )= 1 × ( q + 1) + q (cid:88) i =1 ( G − i ( q + 1 − i )= ( q + 1) + q − (cid:88) j =0 ( G − j +1 ( q + 1 − j − q + 1) + ( G − q − (cid:88) j =0 ( G − j ( q − j ) ⇒ S q +1 = ( q + 1) + ( G − S q (18)Similarly, T q +1 = T q + ( G − q = 1 + ( G − T q (19)And the name of S is justiﬁed by ﬁnally noticing that, ∀ q ≥ , S q +1 = q (cid:88) i =0 ( G − i ( q + 1 − i )= q (cid:88) i =0 ( G − i ( q − i ) + q (cid:88) i =0 ( G − i ⇒ S q +1 = S q + T q +1 (20)This lower bound is simple and can be computed eﬃciently (Equation 17)but it does not perform too well on big sets.To improve its value, the computation for a node is the sum of the lowerbounds of each subset for this node. The evaluation for each subset is replacedby its real value whenever known and the comparison with the incumbent isdone at each level instead of at the end.There exists another way of improving the value of the lower bound. Indeed,while a perfect tree based on the maximum branching factor G can be imagined,when trying all remaining codes against all guesses, most often k < G subsetsare found. From that point on in the branch, no more than k subsets will befound. So a higher lower bound can be computed with a branching factor of k instead of G . The Equation 19 and Equation 20 allow computation and cachingof all values from 2 to G to improve speed.This makes the computation highly dynamic and the score converges fasterto the real value. Symmetry is a key element in the resolution. Many articles mention externalprograms like

Nauty to reduce the number of codes by exploiting all symme-tries of the game. For speed purposes, a trade-oﬀ had to made between reﬁning http://cs.anu.edu.au/~bdm/nauty/ min ( p, c ) -color cases are also covered in this way. At this stage, itis obvious that neither the colors nor the colors order matter as everything issymmetrical. This complete symmetry is lost once one guess is played, but someinteresting properties still remain.At any stage, any color that has never been played before – call it a free color – is symmetrical with any other free color. For example, in MM(4,7) andwhen played after a ﬁrst guess of , the three guesses , and form an interchangeable set of codes, as well as { , , } or even{ , , , , , }. Note that all exclusive free color codeskeep the complete symmetry of the beginning. Zero colors also play a role. Let zero color be any color that is not part ofthe code. For example, if gets an answer of (0,0), the three combinations , and are all equivalent for the next guess. In fact, any zero color can be replaced by any other zero color and ultimately the same one. Asa result, any exclusive zero color guess does not provide additional informationand can be ﬁltered out.Another opportunity to identify zero colors comes from the case where b + w = p . All the pegs are of the right color even though some of them are not inthe proper order. Therefore all other colors are zero colors.A signature is assigned to each code based on the respective properties of free and zero colors. The generic signature is equal to the code where any zero colors is replaced by the letter ’z’ and a free color is replaced, in order ofappearance, by a letter in alphabetical order. All codes with the same signatureare case equivalent and only a class representative needs to be tested. Notethat for a given code, its signature evolves along a branch according to the free and zero colors at the given level. The signature mechanism is especiallyeﬃcient when c ≥ p . Additional simpliﬁcation is anticipated in the case of codesmade solely of free and zero colors but the required level of eﬀort could not bededicated to conclude.For example, for MM(4,7), the program starts with the well known 5 codes ( , , , and ) identiﬁed by this signature method. In the branch, never more than 361 of the 2400 possible codes are tried, 41 inthe case of a (0,0) answer. For the ﬁrst guess or exclusive free color codes, the order of all colors is also reorganized. I did not have access to Neuwirth [15] who seems to be the ﬁrst to have introduced thisnotion. From the description in other articles, the main idea of a class of codes given thehistory of guesses is respected. The ﬁrst in lexical order is chosen. These 5 codes are respectively the representatives of 7, 168 (3+1), 126 (2+2), 1260 and840 codes. Note that

168 + 126 = 294 . See subsection 2.2 for details .5 A documented example The following simpliﬁed MM(3,4) output illustrates how all these pieces worktogether.

With 3 pegs and 4 colors:- there are 9 possible ways of grading,- starting set has 64 possible combs,The solver will use:- for first level, the reduced set of 3 combs (111,112,123),- an upper bound of 207.Erase 111 (223>=207)With the first set :123 = 194112 = 196----- 123 (E=194,g=9)<123,0>=1444 ->2 () E:194/S:2<123,30>=1123 ->1 () E:194/S:3<123,3>=2231 ->5 () E:194/S:8<123,12>=3Will try 57 combs finally112 ->9 (8) E:195/S:17<123,20>=9Will try 63 combs finally134 ->30 (26) E:199/S:47<123,1>=9Will try 63 combs finally244 ->29 (26) E:202/S:76<123,11>=12Will try 63 combs finally134 ->40 (38) E:204/S:116<123,10>=12Will try 63 combs finally244 ->41 (38) E:207/S:157Min reached already (E:207 or S:157 >= 207). Next one.----- 112 (E=196,g=9)<112,30>=1112 ->1 () E:196/S:1<112,12>=2121 ->5 () E:196/S:6<112,2>=5Symmetry: 33 versus 63Will try 32 combs finally232 ->15 (14) E:197/S:21<112,11>=8Symmetry: 33 versus 63 ill try 33 combs finally123 ->24 (23) E:198/S:45<112,0>=8Symmetry: 11 versus 63Will try 11 combs finally334 ->26 (23) E:201/S:71<112,20>=9Symmetry: 33 versus 63Will try 33 combs finally123 ->30 (26) E:205/S:101<112,1>=14Symmetry: 33 versus 63Will try 33 combs finally233 ->47 (46) E:206/S:148<112,10>=17Symmetry: 33 versus 63Will try 33 combs finally134 ->58 (58) E:206/S:206112 = 206Found it in 206, starting with 112Average = 3.22Full path is:[111] 112->(20)->123->(10)->111->(30)->Found

All optimal MM(p,c) results are presented in two tables. Table 6 shows theexpected path lengths while Table 7 shows the expected averages. Note thatsince MM(2,c) is solved, the ﬁrst line of both tables is for validation purposes.Already known results (Koyama and Lai [12], Goddard [5]) were found to-gether with ﬁve others unpublished to my knowledge (in bold). For MM(4,7), E (4 ,

7) = 11228 / . with a worst case of 6 guesses using asthe ﬁrst guess. All these results have the same worst case as the ones in Table 1of Jäger and Peczarski [9] but for MM(4,6) (the famous 6 versus 5 case).Table 6: Optimal path lengths in the expected case for MM(p,c)p c 2 3 4 5 6 7 8 92

18 73 206 451 854 1 , ,

359 3 ,

44 246 905 2 ,

463 5 , ,

97 816 , , Table 8 shows the expected path lengths for all optimal results in the possiblecase. This table contains 8 more upper bonds (in bold). These values maybe helpful when comparing with other algorithms or even trying to solve theoptimal case. For MM*(5,6), only one code requires 7 steps. I therefore believethat W (5 , ≤ . MM*(5,8) is yet to be found to improve the upper bound of5.489 found in subsection 3.3.Finally, Table 9 presents the results from solving MMe(p,c) (solving MM(p,c)with more than c colors) at guess 2. Most of these results are extracted fromruns in the case where a ﬁrst guess received a (0,0) answer. These results couldbe used to save time when solving higher cases. They were corrected for directcomparison with Table 6. Results are the same but for the cells in gray. Forthese ‘gray’ cases, a zero color is used among the guesses, most often in the ﬁrstone (second in the global resolution) when for the others only free colors are The f i are (1/8/78/717/1473/124) The computation of L does not start at guess one but at guess 2, (cid:80) wi =1 ( i + 1) f i = (cid:80) wi =1 if i + (cid:80) wi =1 f i = L + N Table 7: Optimal results in the expected case ( E ( p, c ))p c 2 3 4 5 6 7 8 92 .

000 2 .

333 2 .

813 3 .

240 3 .

667 4 .

041 4 .

438 4 . .

250 2 .

704 3 .

219 3 .

608 3 .

954 4 . .

607 4 . .

750 3 .

037 3 .

535 3 .

941 4 . . .

031 3 . . . . .

18 73 206 455 864 1 ,

503 2 ,

439 3 ,

44 247 908 2 ,

476 5 ,

660 11 , ,

838 35 ,

97 824 3 , ,

572 36 ,

920 86 ,

225 2 , ,

416 74 , , used. It seems to indicate that MM(p,c)=MMe(p,c) whenever c ≥ p − , i.e.,the fewer the colors compared to the number of pegs, the sooner a zero color isneeded to discriminate the remaining codes. This conjecture has to be assessedon more examples and conﬁrmed theoretically.Table 9: Optimal path lengths in the expected case for MMe(p,c)p c 1 2 3 4 5 6 7 82 ,

474 2 , , , An optimal MM(4,7) strategy was found along with other optimal strategies inthe expected case. Additional tight upper bounds (optimal in the possible case)for other cases are also presented as well as an upper bound for MM(5,8).The signature scheme is an eﬃcient way of only testing a class representa-tive and reducing the number of codes tried at each step. The dynamic lowerbound mechanism also gives good results. The MM*(p,c) upper bound is a goodstarting value.After many years of pursuing an optimal MM(4,7) strategy, the program inits present form has reached its limits in terms of speed and memory space. Perlwas used to easily test and implement new ideas over the years while managingthe complexity of the program. Further results require a faster and memory-optimized language.Following are a few ideas that have not been implemented but should enhancethe resolution.The code signature when dealing exclusively with free and zero colors shouldbe studied and may lead to less representatives.A generic program for both computing and ﬁnding the optimal is not thebest solution. To illustrate, let’s return to the case where 3 codes are left. We18aw that playing each code one by one would lead to the same L , but with moreguesses, than a true discriminating code as in Table 2. A ﬁrst phase to ﬁnd theoptimal L followed by a second one to ﬁnd the solution itself, with a possiblelower tree depth, would be globally faster. In the second phase, the ﬁrst guessis assumed to be the same and the minimal external length is already known.This would allow the implementation of the (0,0) ﬁrst answer for the genericcase and not only for the possible case. This idea by itself can lead to sometheoretical work that would further help understand the mechanism of zero and free colors.Following the same idea, I am certain that more results could also be storedand reused in higher cases. This is where external programs could play animportant role to detect such cases and cut the tree by solution blocks. Thecase of b + w = p at the ﬁrst step, while marginal, is such an example. Or othercases where k never-used-before colors are tried and obtain a (0,0) answer evenat a second or further guess. The MMe results could then also be applied.As computer memory grows, classic computer-algorithm optimizations couldalso be implemented. More intermediate computations could be saved and usedalong the way and a two-step ahead mechanism could be programmed to go forthe best guess ﬁrst (using the fact that playing a then b is equivalent to playing b and a for the rest of the branch). End games could also be introduced forthat matter. This overhead is acceptable for bigger size problems.Such optimizations would lead to a three-step resolution. A good startingupper bound would be computed through any heuristic algorithm. The ﬁrst stepwould use this value to run the possible version with all possible optimizationsto ﬁnd a tighter upper bound. The second step, with all codes, all shortcutsand all optimizations would only search for the optimal L , no solution wouldbe recorded but the ﬁrst two guesses leading to this value. Finally, once theoptimal value is found, a last pass would ﬁnd a solution by exploring a smallsearch space using the saved guesses. This last one could eventually focus on alow worst case.I hope these results will help current researchers of this ﬁeld but also giveideas to newcomers, as Rosu [16] did for me at the start, several years ago. References [1] L. Berghman, D. Goossens, and R. Leus. Eﬃcient solutions for Mastermindusing genetic algorithms.

Computers and Operations Research , 36(6):1880–1885, 2009.[2] Alex Bogomolny and Don Greenwell. Invitation to mastermind. MAA On-line, 1996-2013. URL .[3] Shan-Tai Chen and Shun-Shii Lin. Optimal Algorithms for 2 × n Master-mind Games – A Graph-Partition Approach. The Computer Journal , 47(5):602–611, 2004.[4] Shan-Tai Chen and Shun-Shii Lin. Optimal Algorithms for 2 × n AB By itself, this speeds up the process.

Journal of Information Scienceand Engineering , 20(1):105–126, 2004.[5] W. Goddard. Mastermind revisited.

Journal of Combinatorial Mathematicsand Combinatorial Computing , 51:215–220, 2004. URL .[6] Wayne Goddard. Static mastermind.

Journal of Combinatorial Math-ematics and Combinatorial Computing , 47:225–236, 2003. URL .[7] Albrecht Heeﬀer and Harold Heeﬀer. Near-optimal strategies for thegame of Logik.

International Computer Games Association Journal ,2008. URL http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.71.9209 .[8] Robert W. Irving. Towards and optimum mastermind strategy.

Journal ofRecreational Mathematics , 11(2):81–87, 1978.[9] Gerold Jäger and Marcin Peczarski. The number of pessimistic guesses inGeneralized Mastermind.

Information Processing Letters , 109(12):635–641,2009.[10] D.E. Knuth. The computer as Master Mind.

Journal of Recreational Math-ematics , 9(1):1–6, 1976.[11] Barteld Kooi. Yet another mastermind strategy.

International ComputerGames Association Journal , 28(1):13–20, 2005.[12] Kenji Koyama and Tony W. Lai. An optimal Mastermind Strategy.

Journalof Recreational Mathematics , 25(4):251–256, 1993.[13] Juan Julián Merelo, Antonio Miguel Mora, Thomas Philip Runarsson, andCarlos Cotta. Assessing eﬃciency of diﬀerent evolutionary strategies play-ing mastermind. In Georgios N. Yannakakis and Julian Togelius, editors,

CIG , pages 38–45. IEEE, 2010.[14] J.J. Merelo-Guervós, P. Castillo, and V.M. Rivas. Finding a needle in ahaystack using hints and evolutionary computation: the case of evolution-ary MasterMind.

Applied Soft Computing Journal , 6(2):170–179, 2006.[15] E. Neuwirth. Some strategies for mastermind.

Mathematical Methods ofOperations Research , 26(1):B257–B278, 1982.[16] Radu Rosu. Analysis of the game of mastermind – the m n case.Technical report, Undergraduate thesis, North Carolina State University,1997. URL .[17] Ehud Shapiro. Playing mastermind logically. SIGART Newsletter , July1983(85):28–29, 1983.[18] P. F. Swaszek. The mastermind novice.