Introduction to Computer Science I

Project 2


The game of Mastermind

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:

  1. The number of exact matches: The number of pegs in the guess that are the correct color in the correct position.

  2. 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.


A Mastermind program

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
    

The Support methods

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:


Your assignment

Use the Support methods to write your program, Mastermind. Notice that your program must use the Support methods in the following ways:

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.


Submitting your work

When your work is complete, use the cs11-submit program, like so:

cs11-submit project-2 Mastermind.java

The updated due date for this project is Thursday, April 10th at 11:59 pm

Scott F. H. Kaplan
Last modified: Tue Apr 8 11:30:51 EDT 2008