Project 2
For this assignment, you must implement the game of
Mastermind. Below, we'll describe the rules of this
game, and then the interface that we expect your program to
use.
Rules of the game
-
In the game of Mastermind, there is a player and an
adversary. For this assignment, the computer will be
the adversary, and the user will be the player.
-
The game begins with adversary choosing, and keeping secret,
a sequence of four pegs. These pegs come in six colors:
- Blue
- White
- Green
- Yellow
- Orange
- Red
-
The player must try to guess the sequence of peg colors
select by the adversary:
-
The player selects a sequence of four pegs.
-
The player submits this sequence to the adversary as a
guess.
-
The adversary determines how many pegs from the guess are
of the correct color and in the correct position in the
sequence.
-
The adversary uses red and white markers to tell the
player how good the guess was. For every peg in the
player's guess that was the correct color and in the
correct place, the adversary gives the user one
black peg. For every peg that was the correct
color but not in the correct place, the adversary
gives the player one white peg.
-
If the guess is completely correct (all four pegs of the
correct color, and in the correct places in the
adversary's sequence), then the game is over, and the
player wins.
-
Otherwise, if the player has submitted 10 guesses without
winning, then the game is over, and the adversary wins.
-
Otherwise, the player guesses again, starting at step 1.
The interface of Mastermind on computer
Since we are requiring only a text interface, we must choose
characters that represent the colored pegs and the colored
markers. The colors of the pegs will be designated by the first
letter of the color, like so:
-
b = Blue
-
w = White
-
g = Green
-
y = Yellow
-
o = Orange
-
r = Red
So, a sequence "bbyr" represents a peg sequence of
blue, blue, yellow, and then red.
We must also choose characters to represent the markers that the
adversary uses to communicate with the player. Your adversary
should respond with:
-
c = Correct (both color and position in the sequence)
-
m = Misplaced (correct color, wrong position in the sequence)
An example:
-
Imagine that the adversary picks the sequence
"bbyr".
-
The player then guesses "ybrg".
-
The blue peg is the correct color, and in the correct
position.
-
The yellow and red pegs are the correct colors, but in the
wrong position.
-
The green peg is not even the right color.
-
The adversary responds to the player by printing,
"cmm", indicating that one peg was of the correct
color and in the correct place, and two other pegs were the
correct color and in the wrong place.
The program you have to write
Write a program that plays the game of Mastermind. The computer
should randomly select the sequence of four pegs from the
possible six colors, and then allow the user as many as 10
guesses. When the game is over (either the adversary or the
player has won), the user should be given the option to play
again.
A key part of this assignment is to write the program as a group
of methods that call one another. Here are some suggestions for
methods that you might want to write for this program:
-
A method that randomly selects a sequence of four peg colors.
-
A method that gets a sequence of four peg colors from the
user.
-
A method that determines how many pegs in the user's guess are
the right color and in the correct position.
-
A method that determines how many pegs in the user's guess are
the right color and in the incorrect position.
-
A method that prints, using the marker characters, the
correctness of the user's guess.
-
A method that asks if the player wants to play again.
There may be more methods that you choose to write!
What you must submit
You will need to create transcripts of your running program.
The script command will record everything you do into a
text file. Thus, if you type...
script foo.script
Everything you type from that point onward will be recorded into
the file named foo.script. When you want to stop
recording, you simply type:
exit
Using this command, you record transcripts of two slightly
different versions of your Mastermind program:
-
A "cheater's" version that immediately reveals the computer's
secret color sequence before the user starts to guess. We
need this version because without it, we will have a great
deal of difficulty in determining whether or not the program
is working correctly. For this version, the transcript should
record the program's responses to the following entries, in
this order:
-
A guess with no correct colors.
-
A guess with two colors in the wrong place.
(The response should be mm.)
-
A guess that uses the rightmost color twice,
where one use of that color is in the correct location,
and the other is in an incorrect location.
-
One guess that is all the same color, where that color is
one of those in the answer.
-
The correct guess.
-
A regular version that does not show you the computer's secret
color sequence until the game is over. For this version, the
transcript should record you simply playing the game (and,
assumedly, trying to win.)
Use the cs11-submit command to turn in your Java code,
a script of playing the "cheater's" version, and a script of
playing the regular version. Submit this work as
project-2. Note that you should demonstrate that your
program is robust, and that it correctly responds to all kinds
of guesses (ones that are incorrect, ones that have both correct
and misplaced pegs, and ones that are fully correct.)
The project is due Sunday, 14-Nov-2004, at 11:59 pm
Scott F. Kaplan
Last modified: Thu Nov 4 22:07:11 EST 2004