Here, we play a little game using arrays. Write good, clear code here because you're going to modify it later.
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:
Exact matches: The number of pegs in the guess that match pegs in the secret both in color and position. In our example, the 3rd peg of the guess is the only exact match because it is the only place where the guess and the secret match in both color and position; specifically, violate in the 3rd position. Thus, for this example, the number of exact matches is 1.
Misplaced matches: The number of pegs in the guess that match pegs in the secret by color, but not by position; that is, it's the right color, but in the wrong location in the sequence. In our example, notice that the 4th peg is orange, just like the 2nd peg of the secret; thus, it counts as a misplaced match. Also note that the 2nd peg of the guess, which is violet, might have been a misplaced match for the 3rd peg of the secret, but we do not count it as such because that 3rd peg of the secret is already an exact match with the 3rd peg of the guess. Indeed, any one peg can participate in a match no more than once.
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:
The guesser presents a guess that yields four exact matches, thus winning the game.
The guesser does not win the game within 10 turns, and thus loses the game.
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!
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();
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.