Mastermind is a simple board game designed to test your deduction. The game is played with pegs of various colors. For discussion, we will assume that the available peg colors are:
The game begins with an adversary selecting four of the pegs. This sequence of four colors is the secret. The player then has 10 opportunities to guess what that secret sequence of colors is. Each guess is its own sequence of four of the colored pegs. After each guess is presented, the adversary must indicate how close the guess is to the actual secret. Specifically, the adversary must present two values:
The number of exact matches: The number of pegs in the guess that are the correct color in the correct position.
The number of misplaced matches: The number of pegs in the guess that are the correct color but are in the incorrect position.
For example, consider the following secret and guess:
0 1 2 3 <-- position number
yellow green green blue <-- secret
green blue green green <-- guess
In this example, there is 1 exact match at position 2, because both the secret and the guess contain a green peg at that position. Additionally, there are 2 misplaced matches. Specifically, the blue peg at position 1 in the guess is a misplaced match for the blue peg at position 3 in the secret. Moreover, the green peg at position 0 of the guess is a misplaced match for the green peg at position 1 of the secret. Note that the remaining green peg in position 3 of the guess does not also count as a misplaced match. That is because each peg can be matched only once. Once a peg is counted as part of an exact or a misplaced match, that peg cannot be used for any other match.
If the player guesses the secret -- 4 exact matches -- then the player wins the game. If the player fails to guess the secret after 10 guesses, then the player loses the game.
For this project, you are going to write your own game of Mastermind. The program will use the first letter of each color to represent the pegs. Thus, a game of Mastermind would look something like this:
(remus) > java Mastermind DEBUG: The secret is goro [Guess #1]: quux Please enter 4 colors, where each color must be the the first letter from the following list of colors: red orange yellow green blue indigo violet [Guess #1]: rrrr 1 exact matches 0 misplaced matches [Guess #2]: orog 0 exact matches 4 misplaced matches [Guess #3]: oooo 2 exact matches 0 misplaced matches [Guess #4]: gorr 3 exact matches 0 misplaced matches [Guess #5]: gorb 3 exact matches 0 misplaced matches [Guess #6]: goro 4 exact matches 0 misplaced matches You win! You guessed correctly in 6 guesses
I am providing a collection of methods that you must use in writing your program. First, to obtain a copy of those methods, login to remus/romulus, create and change into a project-2 directory, and then use this command:
cp ~sfkaplan/public/cs11/project-2/Support.class .
This file contains the methods listed below. Note that these methods have two purposes: first, to provide important constants such as the length of a secret or guess, or a list of the valid colors; second, to handle all aspects of the user interface. That is, your code should not print anything to the player (except debugging information for yourself), nor should it obtains any input from the player. All user interface operations should be performed using these methods:
char[] Support.getValidColorInitials ()
Return an array of characters, where each character represents one of the colors that the secret and guesses may contain.
int Support.getSecretLength ()
Return the number of pegs required for a valid secret or guess.
int Support.getMaxGuessesAllowed ()
Return the number of guesses that a player may take before losing the game.
char[] Support.getGuess (int guessNumber)
Obtain from the player a valid sequence of peg colors as the next guess, and return that entry as an array of characters.
The caller must provide which guess number this is for the player.
void Support.showResults (int exactMatches, int misplacedMatches)
Report (print) the number of exact matches and misplaced matches to the player.
void Support.setSecret (char[] secret)
Provide the secret which, for debugging purposes, it printed to the user.
void Support.reportOutcome (boolean playerWon, int guesses)
Report whether the player won the game, and how many guesses the player used.
Use the Support methods to write your program, Mastermind. Notice that your program must use the Support methods in the following ways:
When generating the secret, it must call Support.getSecretLength to find out how many pegs the secret must contain. It must also call Support.getValidColorInitials to determine which colors the secret may contain.
It must call Support.getGuess in order to obtain a guess from the user.
It must count the guesses obtained from the player. Moreover, the player must be allowed exactly Support.getMaxGuessesAllowed guesses, and the player must lose if she does not guess correctly within that may guesses.
Each guess must be evaluated for exact and inexact matches. The results of that evaluation must be presented to the player by calling Support.showResults.
When the game is over, the final outcome must be presented to the user by calling Support.reportOutcome.
Note that I will test your program with a modified version of the Support methods. Do not assume, for example, that Support.getSecretLength will always return 4.
When your work is complete, use the cs11-submit program, like so:
cs11-submit project-2 Mastermind.java