Introduction to Computer Science II

Project 2 for 2007-Sep-23

Mastermind


Overview

Here, we play a little game using arrays. Write good, clear code here because you're going to modify it later.


The game of Mastermind

There is a simple board game, know as Mastermind, that is meant to test your deductive abilities. In this game, one player selects a sequence of four colored pegs and places them behind a barrier so that they second player cannot see them. Then, the second player has a limited number of opportunities to guess what the colored sequence is. With each guess, the first player must provide an indication of how close the second player's attempt is. If the second player guesses the sequence correctly, then she wins; if the second player runs out of opportunities to guess, then the first player wins.

Specifics: Imagine that there are pegs of 7 colors, each of which we will designate by its first letter: red, orange, yellow, green, blue, indigo, and violet. Now assume that the first player, the secret-keeper, randomly selects the following sequence of pegs:

bovb

Now assume that the second player, the guesser, attempts the following guess:

ivvo

The secret-keeper must now inform the guesser of the accuracy of the guess. This information comes the form of two numbers:

The secret-keeper communicates these two numbers to the guesser. (Note that these numbers carry no information as to which pegs of the guess were matched.) The guesser, now armed with this information, gets to guess again. The game ends when:


Your assignment


Write a program that plays the game of Mastermind. Specifically, your program should play the role of the secret-keeper, making the user the guesser. The interface should be textual, and the transcript of a game should look like this:

Welcome to Mastermind!
I have chosen a secret sequence of 4 colors. Try to guess it.

Turn #1:
Enter four colors: rigo
  Exact matches:     1
  Misplaced matches: 1

Turn #2:
Enter four colors: vogb
  Exact matches:     0
  Misplaced matches: 2

Turn #3:
Enter four colors: goog
  Exact matches:     2
  Misplaced matches: 2

Turn #4:
Enter four colors: ggoo
  Exact matches:     4
  Misplaced matches: 0
  You win!
    

Supporting information

In order to play this game, there are some special methods that will be helpful to you.

Getting an entry from the keyboard: To allow the user to enter a sequence of four characters on a single line (representing four colors), you need the ability to read a String from the keyboard input. Here is a simple example of a program that reads such a String and then prints it back out to the user. It uses the Scanner class to read in the keyboard, and then uses the Scanner object's nextLine() method to get a single line of text from the keyboard and return it as a String.

import java.util.Scanner;

public class Example {

  public static void main (String[] args) {

    Scanner input = new Scanner(System.in);
    System.out.print("Enter a line of text: ");
    String entry = input.nextLine();
    System.out.println("Here is what you entered: " +
                       entry);

  } // main ()

} // class Example
    

Transforming the keyboard entry: Once you've obtained a String that contains the user's guess, you need to transform that input into an array of char in order to manipulate it throughout your program. Luckily, String objects contain a useful method, named toCharArray(), to perform that transformation. It creates a new array of char, copies the characters from the String into this new array, and then returns a pointer to the array. Here's an example of how to use it:

  String s = "Here's some arbitrary string.";
  char[] ca = s.toCharArray();
    

Submitting your work


From within your project-2 subdirectory, use the following command to submit you work when you are done:

cs12-submit project-2 Mastermind.java

Note that if you use this command more than once, I will still receive every version of what you've submitted. So, don't worry about clobbering an old submission, and feel free to re-submit if you improve some part of your solution.


This assignment is due Sunday, 2007-Sep-23, at 11:59 pm

Scott F. H. Kaplan
Last modified: Fri Sep 21 12:06:15 EDT 2007