Mastermind Game with a Twist

Name: Simona Davidoaia
Contact: simodav@gmail.com
Math 213W - Mathematics with Mathematica, Fall 2009 
Date: December 3, 2009

Abstract
 
                My project is simulating the game MasterMind on the computer.The game MasterMind is played by two people a code maker and a code breaker.The code maker selects a code that is a combination of four colored pegs out of six possible colors (colors can be repeated and their order is important).The code breaker will try to guess the code.After each guess the code maker will give two clues.First clue reveals how many colors are correct and they have the correct position and the second clue reveals how colors are correct yet are wrongly positioned.The object of the game is that the code breaker will find out the code.In the classical MasterMind game,            there are two ways to end the game.The game ends if the code breaker did not find the code in eight tries or if the code breaker finds the code in eight or less tries.
The twist from the classical MasterMind game ,that I simulated in this project, consists in that the code breaker has unlimited times to try to find the code,and that seem to easy up the game,yet s/he's cannot see the previous tries so in that sense my twist from the classical game makes the game more challenging.

Section 1. Introduction

1.1 Background

	I used to play the game Mastermind with friends. So this game is dear to me for the memories that brings along with its name. The uniqueness of the strategies that my friends and I used in solving the code it got me thinking that I could make an algorithm for solving the game. I would of liked to test my algorithm's efficiency by haveing the computer play the game numerous times, keeping the count of numbers of times the game was lost and number of times the game was won(find the code in eight  or less tries). 
Because of my luck of time and luck of experience in using Mathematica I resumed my project to simulating the game on the computer.

1.2 Problem Statement

How can simulate the game Mastermind with the help of Mathematica?

1.3 Useful Commands

The commands which I used for my project:
Built in commands for color and shapes:  Yellow, Blue,..., Disk
Commands for displaying objects: GraphicsGrid, Pane, ToString, Style
Commands used for calculation: Total, Subtract
Conditionals: If
Command that creates buttons: Button
Commands that will choose selectively from lists: RandomSample, First
Commands that will help you locate elements of a list and count them: Position, Length
Commands that will apply action to elements of a list: Delete, Manipulate 

New commands that I have encountered:
DynamicModule command represents an object that will maintain something, in this case the shape as a disk, while the color will be dynamic.
EventHandler command displays an action (the color being dynamic),when an event occurs (the click of a mouse ).

1.4 File Structure
	
	The game Mastermind with a Twist I constructed in this file , needs to have six defined colored disks that will be used in groups of four to generate a color code and an answer.
 Firstly, I created the colored disks, then the function “code” that will randomly choose colors to create the code. The farther needed function was “guess” that will display four disks that will change their colors when clicked by mouse. To give clues to the player that guesses the code, the computer has to compare the code with the guess. The comparison will result in two clues: one refers to colors that are correct and in the right position, and one that refers to the colors that are part of the code yet they are wrongly positioned in the guess. I called this function “OutputData”. At the end when I manipulated the function together I created buttons such as “new game” that will reset the computer generated code and display the “guess” function’s evaluation,  “submit your answer” that will display the evaluation of the “OutputData” function and “reveal the answer” button that will display the code.

Section 2. Main Work

2.1 The six colored disks that I use to generate a four colored code

colorlist={Green,Red, Blue, Orange,Yellow,Pink};
GraphicsGrid[{{Graphics[{Green, Disk[]}],Graphics[{Red, Disk[]}],Graphics[{Blue, Disk[]}],Graphics[{Orange, Disk[]}],Graphics[{Yellow, Disk[]}],Graphics[{Pink, Disk[]}]}}]

2.2 Computer generates a four color code by randomly choosing colors for four disks

c1=First[RandomSample[colorlist]];
c2=First[RandomSample[colorlist]];
c3=First[RandomSample[colorlist]];
c4=First[RandomSample[colorlist]];
code=GraphicsGrid[{{Graphics[{c1,Disk[]}],Graphics[{c2,Disk[]}],Graphics[{c3,Disk[]}],Graphics[{c4,Disk[]}]}}]

2.3 Four disks that can have six interchangeable different colors, to guess the code

col1=col2=col3=col4=Green;
GraphicsGrid[{{
DynamicModule[{},EventHandler[Style[Graphics[{Dynamic[col1],Disk[]}]],{"MouseClicked":>(col1=col1/.{
Green->Red,Red->Blue,Blue->Orange,Orange->Yellow,Yellow->Pink,Pink->Green})}]],
DynamicModule[{},EventHandler[Style[Graphics[{Dynamic[col2],Disk[]}]],{"MouseClicked":>(col2=col2/.{
Green->Red,Red->Blue,Blue->Orange,Orange->Yellow,Yellow->Pink,Pink->Green})}]],
DynamicModule[{},EventHandler[Style[Graphics[{Dynamic[col3],Disk[]}]],{"MouseClicked":>(col3=col3/.{
Green->Red,Red->Blue,Blue->Orange,Orange->Yellow,Yellow->Pink,Pink->Green})}]],
DynamicModule[{},EventHandler[Style[Graphics[{Dynamic[col4],Disk[]}]],{"MouseClicked":>(col4=col4/.{
Green->Red,Red->Blue,Blue->Orange,Orange->Yellow,Yellow->Pink,Pink->Green})}]]}}]

2.4 Compare the colors from the code with colors from the guess and give clues

codelist={c1,c2,c3,c4};
guesslist={col1,col2,col3,col4};
cp=Total[Boole[{c1===col1,c2===col2,c3===col3,c4===col4}]];
Print["cp=",cp," corrent color(s) in the correct space(s)"]

newcodelist=Delete[codelist,Position[{c1===col1,c2===col2,c3===col3,c4===col4},True]];
newguesslist=Delete[guesslist,Position[{c1===col1,c2===col2,c3===col3,c4===col4},True]];

newcodelist=If[!c1===col1,Delete[newcodelist,Position[newcodelist,col1]],newcodelist];
newcodelist=If[!c2===col2,Delete[newcodelist,Position[newcodelist,col2]],newcodelist];
newcodelist=If[!c3===col3,Delete[newcodelist,Position[newcodelist,col3]],newcodelist];
newcodelist=If[!c4===col4,Delete[newcodelist,Position[newcodelist,col4]],newcodelist];
c=Subtract[Length[newguesslist],Length[newcodelist]];
Print[" c=",c," correct color(s) in the wrong space(s)"]

2.5Manipulation of all previous parts toghether - Mastermind Game with a twist

col1=col2=col3=col4=Green;
guess=GraphicsGrid[{{DynamicModule[{},EventHandler[Style[Graphics[{Dynamic[col1],Disk[]}]],{"MouseClicked":>(col1=col1/.{Green->Red,Red->Blue,Blue->Orange,Orange->Yellow,Yellow->Pink,Pink->Green})}]],DynamicModule[{},EventHandler[Style[Graphics[{Dynamic[col2],Disk[]}]],{"MouseClicked":>(col2=col2/.{Green->Red,Red->Blue,Blue->Orange,Orange->Yellow,Yellow->Pink,Pink->Green})}]],DynamicModule[{},EventHandler[Style[Graphics[{Dynamic[col3],Disk[]}]],{"MouseClicked":>(col3=col3/.{Green->Red,Red->Blue,Blue->Orange,Orange->Yellow,Yellow->Pink,Pink->Green})}]],DynamicModule[{},EventHandler[Style[Graphics[{Dynamic[col4],Disk[]}]],{"MouseClicked":>(col4=col4/.{Green->Red,Red->Blue,Blue->Orange,Orange->Yellow,Yellow->Pink,Pink->Green})}]]}}];


OutputData[x_]:=Module[{cp,c},cp=Total[Boole[{c1===col1,c2===col2,c3===col3,c4===col4}]];
codelist={c1,c2,c3,c4};
guesslist={col1,col2,col3,col4};
newcodelist=Delete[codelist,Position[{c1===col1,c2===col2,c3===col3,c4===col4},True]];
newguesslist=Delete[guesslist,Position[{c1===col1,c2===col2,c3===col3,c4===col4},True]];
newcodelist=If[!c1===col1,Delete[newcodelist,Position[newcodelist,col1]],newcodelist];
newcodelist=If[!c2===col2,Delete[newcodelist,Position[newcodelist,col2]],newcodelist];
newcodelist=If[!c3===col3,Delete[newcodelist,Position[newcodelist,col3]],newcodelist];
newcodelist=If[!c4===col4,Delete[newcodelist,Position[newcodelist,col4]],newcodelist];
c=Subtract[Length[newguesslist],Length[newcodelist]];{cp,c}];


OutputData[];
{{c1,c2,c3,c4},{col1,col2,col3,col4}};
textbox[]:="   Your answer has:      "<>ToString[First[OutputData[code]]]<>"  correct color(s) in the correct space    "<>ToString[Last[OutputData[code]]]<>"   correct color(s) in the wrong space";
Manipulate[Pane[GraphicsGrid[{{If[toggle==0,Style["Push New Game",20],Graphics[guess]]},{Style[tb,FontSize->20]},{reveal}},Alignment->{Center,Center},ImageSize->{650}],{700,600},Alignment->{Center,Center}],{{code,toggle=0;
c1=First[RandomSample[colorlist]];
c2=First[RandomSample[colorlist]];
c3=First[RandomSample[colorlist]];
c4=First[RandomSample[colorlist]];
code=GraphicsGrid[{{Graphics[{c1,Disk[]}],Graphics[{c2,Disk[]}],Graphics[{c3,Disk[]}],Graphics[{c4,Disk[]}]}}]," "},Button["new game",toggle=1;reveal=" ";
c1=First[RandomSample[colorlist]];
c2=First[RandomSample[colorlist]];
c3=First[RandomSample[colorlist]];
c4=First[RandomSample[colorlist]];
code=GraphicsGrid[{{Graphics[{c1,Disk[]}],Graphics[{c2,Disk[]}],Graphics[{c3,Disk[]}],Graphics[{c4,Disk[]}]}}];]&},{{tb," "," "},Button["Submit your answer",tb=textbox[];]&},{{reveal," "," "},Button["Reveal the code",reveal=Graphics[code];]&}]

Section 3. Conclusion and Future Work

3.1 Summary

Since the game relayed greatly on the display of the colored pieces (disks), the built in Mathematica commands for color and shapes came in very handy in setting up this game. While working on this project I got to learn how to use new commands like DynamicModule and EventHandler that changes the color displayed on the disks. Along the way I took notice of other very neat functions that Mathematica has. I did not need to use those functions on my project, yet I have them in the back of my mind for future projects. The most wonderful parts of the project were, the challenges that I faced in finding a solution for testing the correct color in the wrong position and getting the game manipulated. It help me better in my critical thinking and gave me the satisfaction of an accomplishment when was all done.

3.2 Discussion

We spoke in the classroom about impartial games. MasterMind game is an impartial game if the computer is generating the secret code and two people are taking turns on being the code breakers.
 It seems problematic to find out a strategy to win an impartial MasterMind Game.

3.3 Future Work

For the future, I will like to work a bit more on the display of the game and maybe even tackle the idea of testing the efficiency of an algorithm that solves MasterMind game.

Section 4. References

           1.Professor Christopher Hanusa
	2. http://www.cut-the-knot.org/Curriculum/Games/Mastermind.shtml