CS 0007 Programming Assignment 3 Mastermind DUE DATE: Friday, March 25 at 11:59
ID: 3679800 • Letter: C
Question
CS 0007 Programming Assignment 3 Mastermind DUE DATE: Friday, March 25 at 11:59 PM In the game of Mastermind, one player creates a code 4 colors long out of 6 possible colors (red, orange, yellow, green, blue, or white). The other player tries to guess the code in as few attempts as possible. For each incorrect guess, the player who created the code assigns red pegs and white pegs to the guess. A red peg means that the guess has a correct color in the correct spot, while a white peg means that the guess has a correct color in the incorrect spot. The total number of pegs for each guess cannot be more than 4 (the length of a code). Below are a few examples: Code: RYWB Guess: BWYR 0 red pegs, 4 white pegs Code: RRRR Guess: OYBG 0 red pegs, 0 white pegs Code: RGWO Guess: RYBG 1 red peg, 1 white peg Code: 00BY Guess: OGBY 3 red pegs, 0 white pegs Problem Write a program that allows you to play Mastermind against the computer. The computer should generate a random code (a String made up of the characters R, O, Y, G, B, and W, representing the possible colors) and you (the user) will guess it. For each guess that isn't correct, the program should return the number of red pegs and the number of white pegs for the guess. You shoud be able to keep guessing until you win. To complete this assignment, you need to download the Mastermind.java file (attached to Assignment 3 on Coursweb) and add the required methods. You should not have to modify the main method. Remember to document each of your methods appropriately Read the main method carefully to see how the methods work together, and then work through writing each method individually .Explanation / Answer
import java.io.*;
import java.util.Random;
public class Mastermind
{
static BufferedReader stdin;
static int [] answerArray;
static int [][] guessArray;
private static Random generator;
private static int num0, num1, num2, num3;
private static int white, black;
static boolean gotQuitCommand;
public static void main(String[] args) throws IOException
{
answerArray = new int[4]; //creates an array to hold computer's number
stdin = new BufferedReader( new InputStreamReader( System.in ));
generator = new Random();
mainMenu(); // see if user wants to quit or play
// we play a new game every time through this loop
while (!gotQuitCommand) {
System.out.println(" The computer has thought of a four-digit number. Make your first guess. ");
nextNumber(); // gets random numbers
answerArray[0] = num0;
answerArray[1] = num1;
answerArray[2] = num2;
answerArray[3] = num3;
guessMenu();
mainMenu(); // see if user wants to quit or continue playing
}
System.out.println("Type enter to end");
System.in.read();
}
/* This method generates random numbers to use as digits for the
computer's number */
public static void nextNumber()
{
Random generator = new Random();
num0 = Math.abs(generator.nextInt()) % 10;
num1 = Math.abs(generator.nextInt()) % 10;
while (num1 == num0) // ensures that all numbers are different from each other
num1 = Math.abs(generator.nextInt()) % 10;
num2 = Math.abs(generator.nextInt()) % 10;
while (num2 == num1 || num2 == num0)
num2 = Math.abs(generator.nextInt()) % 10;
num3 = Math.abs(generator.nextInt()) % 10;
while (num3 == num2 || num3 == num1 || num3 == num0)
num3 = Math.abs(generator.nextInt()) % 10;
}
/* This method provides choices: play, read instructions, or quit*/
public static void mainMenu() throws IOException
{
System.out.println ("Enter p to play, i for instructions, or q to quit.");
char userInput = 'a';
String lineOfInput = stdin.readLine();
userInput = lineOfInput.charAt(0);
if (userInput == 'q') {
gotQuitCommand = true;
} else {
gotQuitCommand = false;
}
if (userInput == 'i') {
instructions();
mainMenu();
}
}
/* This prints out the array of all guesses and results, current and previous */
public static void printOutArray(int [][] array, int count)
{
System.out.println("Results:");
System.out.println();
for (int row=0; row<(count+1); row++)
{
for (int col=0; col<(guessArray[row].length-2); col++)
System.out.print(guessArray[row][col]+" ");
System.out.print(" ");
for (int col2=(guessArray[row].length-2); col2<(guessArray[row].length); col2++) {
System.out.print(guessArray[row][col2]+" ");
}
System.out.println();
}
System.out.println();
}
/* This is the main body of the game */
public static void guessMenu() throws IOException
{
int guess0, guess1, guess2, guess3;
int count = 0;
int MAX = 50; //sets up maximum number of attempts
guessArray = new int[MAX][6]; //creates an array to hold user's guesses and results
while (white != 4) { //until user guesses correctly
guess0 = 10;
while (guess0 > 9) { //ensures that all digits are between 0 and 9
System.out.print("First digit: ");
String guessString0 = stdin.readLine();
guess0 = Integer.parseInt(guessString0);
}
guess1 = guess0; //ensures that all digits are different from one another
while (guess1 == guess0 || guess1 > 9) {
System.out.print("Second digit: ");
String guessString1 = stdin.readLine();
guess1 = Integer.parseInt(guessString1);
}
guess2 = guess1;
while (guess2 == guess1 || guess2 == guess0 || guess2 > 9) {
System.out.print("Third digit: ");
String guessString2 = stdin.readLine();
guess2 = Integer.parseInt(guessString2);
}
guess3 = guess2;
while (guess3 == guess2 || guess3 == guess1 || guess3 == guess0 || guess3 > 9) {
System.out.print("Fourth digit: ");
String guessString3 = stdin.readLine();
guess3 = Integer.parseInt(guessString3);
}
System.out.println();
//Store guess-digits into array:
guessArray[count][0] = guess0;
guessArray[count][1] = guess1;
guessArray[count][2] = guess2;
guessArray[count][3] = guess3;
white = 0; //checks for digits in correct places:
for (int i = 0; i < answerArray.length; i++)
if (guessArray[count][i] == answerArray[i])
white++;
black = 0; //checks for correct digits in incorrect places:
for (int i = 0; i < answerArray.length; i++){
if (guessArray[count][i] == answerArray[(i+1)%4])
black++;
if (guessArray[count][i] == answerArray[(i+2)%4])
black++;
if (guessArray[count][i] == answerArray[(i+3)%4])
black++;
}
guessArray[count][4] = white;
guessArray[count][5] = black;
printOutArray(guessArray, count);
count++; //keeps track of number of guesses
}
System.out.println("Congratulations! That number is correct. ");
System.out.println("You guessed the number after " + count + " guesses. ");
white = 0;
black = 0;
count = 0;
}
}