Mastermind

Author

Eric W. Weisstein
October 24, 2002

This notebook downloaded from http://mathworld.wolfram.com/notebooks/Games/Mastermind.nb.

For more information, see Eric's MathWorld entry http://mathworld.wolfram.com/Mastermind.html.

Code

Mastermind[g_List?VectorQ,c_List?VectorQ]:=Module[
{i,
b=Count[Equal@@@Transpose[{g,c}],True]
},
{b,Plus@@Table[Min@@(Count[#,i]&/@{g,c}),{i,6}]-b}
]

MastermindResponses[g_List]:=Map[Function[f,Mastermind[f,#]]/@g&,Outer[List,Sequence@@Table[Range[6],{4}]],{4}]

MastermindSolution[g_List,r_List]:=Position[MastermindResponses[g],r]

©2005 Wolfram Research, Inc. except for portions noted otherwise

Example

static={{1,2,2,1},{2,3,5,4},{3,3,1,1},{4,5,2,4},{5,6,5,6},{6,6,4,3}};

MastermindSolution[static,
{{0,0},{0,2},{0,0},{0,2},{2,1},{2,0}}
]

Static Solutions

6^4

Definitive

Union[Flatten[MastermindResponses[static],3]]//Length

Changing slightly leads to ambiguity

Union[Flatten[MastermindResponses[{{2,2,2,1},{2,3,5,4},{3,3,1,1},{4,5,2,4},{5,6,5,6},{6,6,4,3}}],3]]//Length

The number of possibilities to check is

6^(6 4)

which is much to large to do exhaustively.  In the case of five gueses,

6^(5 4)

N[%]//ScientificForm

the number is also too large to be practical.