Project 3

Last revision:  February 18 1998

Author: YourNameHere

MasterMind

General suggestions:

(a) Make sure you know how to do a simple example by hand.

(b) With scratch work (not on computer), develop an algorithm which has steps you think Mathematica can accomplish.  Think in terms of rows, or entire lists.

(c) Use bottom-up analysis, defining and testing functions as you build up solutions to parts of the programming task.

Individually Pledged OR Pledged with One Partner

Your task is to write Mathematica code to play the game MasterMind with the computer as the codemaker.  Your Mathematica notebook should define two functions "createCode" and "tryGuess" and should display two example games.  Details about the game and the functions are given below.  As always, use good programming style, strive for clarity, and add comments or text to explain anything that might seem unclear.

The game of MasterMind centers around a hidden code.  This code consists of four characters chosen randomly from the set {"G", "B", "R", "P", "Y", "W"}.  (In the physical game, objects have the colors green, blue, red, purple, yellow, and white, from which our characters are derived.)  Repeating two or more characters in the hidden code is allowed.

For the player, the object of the game is to guess the code.  The player is allowed to make guesses of four characters and receives some information back for each guess.  This information is a list of two integers, the first of which is commonly referred to as the number of bulls, the second the number of cows.  A bull is a guessed character which does occur in the code at the same position as it does in the guess---in other words, a perfect answer for that position in the code.  A cow is a guessed character which does occur in the code, but in a different position from its position in the guess.

Your function "createCode[]" should set the global variable "hidden" to some code chosen randomly, and the function should not display the code on the screen.  Note that this operation should be a so-called "side effect", since the variable "hidden" should not be local, lest the game never start.

Your function "tryGuess[]" should take a string which has four characters in it, determine the number of bulls and cows, and return these numbers as a list.

As an example, here is an example run, supposing that "createCode" has defined "hidden" to be {"W", "G", "W", "R"}:

tryGuess["RWWY"]

tryGuess["RWWW"]

tryGuess["RWGY"]

Hint:  Determining the number of cows is not as simple as it might seem at first glance.  The total number of bulls and cows matching a character (say "W") is either one of two numbers, the number of such characters in the hidden code, or the number of such characters in the guess, whichever is smaller. Hence, one strategy would be to find the total number of bulls and cows, then to find the number of bulls, and finally to subtract to find the number of cows.