Play a game of Mastermind. The computer creates a random pattern, and the human user tries to discover it.
You can play this game online at http://www.web-games-online.com/mastermind/index.php. More detailed instructions are at http://www.pressmantoy.com/instructions/instruct_mastermind.html.
To get the maximum benefit from this assignment,
While there are exceptions, concise code is almost always simpler and better than verbose code. Higher order functions (once you are familiar with them) are almost always easier to read than recursive code.
Provide a function main (with no arguments) to start the program.
Ask the user whether he or she wants to play a game. As possible answers, accept:
y for "yes," meaning to start a new game, with a random pattern for the user to guess. The program should allow the user to play multiple games.n for "no," meaning the program should quit.t for "test." Instead of generating a random pattern for the user to guess, the program asks the user to enter a pattern. Thereafter the game proceeds as usual.We will use:
r o y g b v.r g o g. Colors may be repeated.
rgog. This is to make it easy to type. If you also want to allow spaces and/or capital letters, you may, but accept this format as well.W for "white," meaning a peg is the correct color but in the wrong place, and K for "black," meaning a peg is both the correct color and in the right place (some versions of the game use red instead of black for this).
K and W inform the user how many pegs are the correct color, and how many are in the correct place, but it does not inform the user which pegs those are. Print out the scoring with all the Ks (if any) before all the Ws (if any). r g o g K K W. red ") so that you get neat columns.After each move input by the user, the program should print out every move made so far in this game, and how it was scored, most recent move first. For example, if the player has just entered vooy, the program might display
v o o y K K W
o v v y K K W
o r y o W W
r y g b W
(where the actual pattern is y o v y).
Be sure to congratulate the user when he/she finds the solution!
A law of functional programming is that when you call a function with the same parameters as before, you get the same result as before. This obviously isn't what you want from a random number generator! Therefore, you need to isolate the randomness in the imperative part of the program: the IO monad.
http://koweycode.blogspot.com/2008/07/simple-random-numbers-in-haskell.html is probably all you need to know about random numbers in Haskell; if not, you can find a lot more at http://www.vex.net/~trebla/haskell/random.xhtml.
For the same reason, you cannot manipulate an external "state" in a function. All the information the function can access is provided by the parameters. Therefore: The entire "state" of the program must be represented by the values of one or more parameters.
In this assignment, for example, the "state" that you need is the sequence of moves and scores made by the player, along with the correct pattern. Functional programming encourages minimizing the amount of state that needs to be carried around.
While you can do quite a bit of work in the imperative IO monad, this is the equivalent of trying to write a Java program in Haskell. It is basically fighting with the language, instead of letting the spirit of the language guide you. Try to write Haskell code in Haskell, not Java code.
Your program is due before 6am Friday, October 26, submitted via Blackboard. Usual late penalties will apply.