CS 132: Intro to Computer Science II

Spring 1999

Assignment 5

Arrays and Class Implementation

Due Monday March 1

For this assignment, you will complete a class implementation and use it to write a program that runs the game Mastermind. In this game, the computer secretly and randomly selects a sequence of four colors from a pool of six colors (i.e. red, yellow, blue, green, black, white). For example, the computer may choose "red, yellow, blue, yellow" as its sequence. The player then tries to learn the color sequence by making a series of guesses. With each guess by the player, the computer responds with the following feedback:

Using the computer's feedback, the player continues to make guesses until the player guesses the computer's sequence. Listed below is an example game (the player's input is in bold type):
whoville% java Game
Creating color code...

For each of your guesses, type in each color followed by enter
Enter color for position 1: red
Enter color for position 2: blue
Enter color for position 3: blue
Enter color for position 4: red

        Number of Code White: 0
        Number of Code Black: 1

Enter color for position 1: white
Enter color for position 2: blue
Enter color for position 3: white
Enter color for position 4: white

        Number of Code White: 0
        Number of Code Black: 2

Enter color for position 1: white
Enter color for position 2: red
Enter color for position 3: black
Enter color for position 4: black

        Number of Code White: 0
        Number of Code Black: 2

Enter color for position 1: green
Enter color for position 2: green
Enter color for position 3: yellow
Enter color for position 4: yellow

        Number of Code White: 0
        Number of Code Black: 1

Enter color for position 1: green
Enter color for position 2: green
Enter color for position 3: black
Enter color for position 4: black

        Number of Code White: 0
        Number of Code Black: 1

Enter color for position 1: white
Enter color for position 2: blue
Enter color for position 3: green
Enter color for position 4: black

        Number of Code White: 0
        Number of Code Black: 3

Enter color for position 1: white
Enter color for position 2: blue 
Enter color for position 3: yellow
Enter color for position 4: black

        Number of Code White: 0
        Number of Code Black: 4

Congratulations!  You've guessed the code in 7 tries.

To simplify your program's design, you should use the RowOfColors class. Each RowOfColors object represents a sequence of four colors such as the computer's chosen sequence as well as each of the player's guesses. The RowOfColors class provides an assortment of methods central to the game. They include:

The definition and partial implementation of the RowOfCards class is in the directory ~miller/cs132/lab5 . For the first part of the lab, you will need to complete the implementations of the public methods numCodeBlack and numCodeWhite, whose solutions will be discussed in class. The solution to "Code White" depends on determining how many of each color is in a row, which is handled by the private method colorCount. This method also needs to be implemented.

Once these methods have been implemented, you should test them in the main method of the RowOfColors class. You should leave your test code there when turning in your lab.

Finally, create a new file called Game.java (class Game), which uses the RowOfColors class and implements the Mastermind description (see the sample output above, but you should modify display as appropriate). Most of the game should run inside the static main method, but you should create and call at least one additional static method inside your Game class.

Submission

Turn in hardcopies of RowOfColors.java and Game.java and a script of you running the game.