Question: Mastermind color guessing game in Java Create a game that replicates the classic Mastermind game. The program creates a string that represents four colors out
Mastermind color guessing game in Java
Create a game that replicates the classic Mastermind game. The program creates a string that represents four colors out of 6. For example RGBP = Red, Green, Blue, and Purple. The user is prompted to enter their guess and is entered as a String. The player gets 10 chances to get it right. For every guess the program keeps track of Right Color Right Place and Right Color Wrong Place which is used as a hint for the player after each guess.
Example
Colors = RGBP
Player guess = RYOB
Right Color Right Place = 1 for the 'R'
Right Color Wrong Place = 1 for the 'B'
So far I created the colors by getting 4 random numbers as an array of integers. I then convert those into the "colors" by going through each element and assigning a color based on the integer. For example, 0 = R for Red. I then prompt the user to play, and get their input as a String. I compare the guess with the answer however, this only works if the two match perfectly. In other words, if the player guesses all colors on first try.
import java.util.Random; import java.util.Scanner;
public class Mastermind {
private static String colorsString = ""; private static int size = 4; private static int [] colors = new int[size]; public static void main(String[] args) {
//Create the colors to be guessed initiateColors(); //Prompt user to play displayMessage("Do you want to play?"); } public static void displayMessage(String message) { System.out.println(colorsString); Scanner in = new Scanner(System.in);
System.out.println(message);
String playOrNot = in.nextLine();
if(playOrNot.toLowerCase().equals("yes")) {
System.out.println("Guess four colors: "); String guess = in.nextLine(); //Check guess against the colors if(correct(guess)) { System.out.println("You Win!"); }
}else if(playOrNot == "no") { System.out.println("Goodbye"); } in.close(); } public static boolean correct(String guess) {
System.out.println("Checking answer"); if(guess.equals(colorsString)) { return true; } return false; } private static void initiateColors() { for(int i = 0; i < size; i++) { int temp = getRandom()[i]; colors[i] = temp; colorsString += getColors(colors[i]); } } private static int[] getRandom() { Random rand = new Random(); int rand_int[] = new int[size]; for(int i = 0; i < size; i++) { rand_int[i] = rand.nextInt(6); }
return rand_int; }
private static String getColors(int color) { String message = ""; switch(color) { case 0: message = "R"; // red break; case 1: message = "O"; // orange break; case 2: message = "Y"; // yellow break; case 3: message = "G"; // green break; case 4: message = "B"; // blue break; case 5: message = "P"; // purple break; } return message; } }
I know what I want to do just not sure how to do it. Here is a breakdown:
Find exact matches in the colors chosen and user's guess
RCRP = number of elements in exact match
Remove all exact matches from the colors chosen and user's guess
The rest is remainingColors and remainingGuess
Now check for Right Color Wrong Place (RCWP)
Loop through each element in remaining guess
Check if its an element of the remaining colors\
if yes, RCWP ++
delete element from remainingColors
Move to next element in remainingGuess
How can I take what I have and implement the pseudo-pseudo-code above?
UPDATE: added this
public static int RCRP(String guess) { int rcrp = 0; for(int i = 0; i < guess.length(); i++) { if(guess.charAt(i) == colorsString.charAt(i)) { rcrp++; } } return rcrp; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
