Java help) I am trying to simulate the board game mastermind. For the code below
ID: 3667479 • Letter: J
Question
Java help) I am trying to simulate the board game mastermind. For the code below how do I keep a running total of the variable whitePegs for every letter the user guesses that is in the string but not in the same position as the random code. Aswell, as running total of redPegs for every letter the user guesses that is in the random code and in the right position. Lastly show total redpegs and whitepegs after each guess with an * mark. I need hep with lines 85-125. Your help would be grealty appreciated.
-What I want the output to be if for example the secret code is PBGY(Ofcourse the person doesn't see the code)
Enter your guess: PYOR
Red pegs:*
White pegs:*
Enter your guess:PYGB
Red pegs:**
White pegs:**
Enter your guess:YGBP
Red pegs:
White pegs:****
1
2
3
4 //import statements
5
6
7
8 import java.util.Scanner;
9
10 import java.util.Random;
11
12 public class testing
13 {
14
15
16 public static void main (String[] args)
17 {
18
19 int guesses=0;// The user's guess
20 int maxGuesses=10;//maximum ammaount of guess attempt
21 boolean isSink=false;
22 boolean isHit=false;
23 int redPegs=0;
24 int hits=0;
25 int whitePegs=0;
26 boolean correct;
27 boolean contains=false;
28 String again ="y";
29
30 //Create an instance of Random class
31 Random ran = new Random();
32
33 //Set array of strings
34 String colors[]= {"G","B","Y","R","O","P"};
35
36 String code = "";
37 //create a string variable randomColor
38 String randomColor;
39
40 //Create a for loop that run for 4 times
41 //to create four letter random code
42 for (int i = 0; i < 4; i++)
43 {
44
45 //run do while unitl the code doesnot
46 //contain the repeated colors code
47 do
48 {
49 //get random colors string
50 randomColor=colors[ran.nextInt(6)];
51
52 }while(code.contains(randomColor));
53
54 //concatenate the randomColor
55 code+=randomColor;
56 }
57
58 //print code
59 // System.out.println(code);
60
61
62 // int guesses=0;//amount of guesses
63 // while(again.compareToIgnoreCase("y")==0){
64 Scanner keyboard= new Scanner(System.in);
65 System.out.println("Secret code has been simulated.");
66 System.out.println("Pick four colors to put in each hole.");
67 System.out.println("Choices are B for blue,G for green, O for orange,");
68 System.out.println("R for red,P for purple and Y for yellow.");
69 System.out.println("Enter your guess in a string format.");
70
71 System.out.print("Enter your guess: ");
72 String guess = keyboard.nextLine().toUpperCase();
73 guesses++;
74
75
76 while (guess.length()!=4 && guess!= null) {
77 System.out.print("Enter your guess: ");
78 guess = keyboard.nextLine().toUpperCase();
79 }
80
81 System.out.println("Your guess is " + guess);
82
83
84
85 do{
86
87
88
89
90
91
92 // java string compare example
93 while (guess.compareTo(code) == 0)
94 {
95 // this line WILL print
96 System.out.println("Congradualtions you guessed the right colors.");
97 System.out.println("Red pegs:****");
98 System.out.println("White pegs:");
99 System.out.print("Enter your guess: ");
100 guess = keyboard.nextLine().toUpperCase();
101
102 // System.out.print("Do you want to enter another(y=yes) quit(anything else)?");
103 // again=keyboard.nextLine();
104 }
105
106 if(guess.compareTo(code) != 0) {
107
108 System.out.print("Enter your guess: ");
109 guess = keyboard.nextLine().toUpperCase();
110
111 }
112
113 if (guesses>maxGuesses)
114 {
115 System.out.print("You are out of guesses");
116
117
118 }
119
120
121
122 }
123
124
125 while(guess!=code && guesses<maxGuesses);
126
127
128 }
129
130
131
132
133
134
135
136 }
Explanation / Answer
import java.util.Scanner;
import java.util.Random;
public class testing{
public static void main (String[] args){
int guesses=0;// The user's guess
int maxGuesses=10;//maximum ammaount of guess attempt
boolean isSink=false;
boolean isHit=false;
int redPegs=0;
int hits=0;
int whitePegs=0;
boolean correct;
boolean contains=false;
String again ="y";
//Create an instance of Random class
Random ran = new Random();
//Set array of strings
String colors[]= {"G","B","Y","R","O","P"};
String code = "";
//create a string variable randomColor
String randomColor;
//Create a for loop that run for 4 times
//to create four letter random code
for (int i = 0; i < 4; i++){
//run do while unitl the code doesnot
//contain the repeated colors code
do{
//get random colors string
randomColor=colors[ran.nextInt(6)];
}while(code.contains(randomColor));
//concatenate the randomColor
code+=randomColor;
}
//print code
// System.out.println(code);
// int guesses=0;//amount of guesses
// while(again.compareToIgnoreCase("y")==0){
Scanner keyboard= new Scanner(System.in);
System.out.println("Secret code has been simulated.");
System.out.println("Pick four colors to put in each hole.");
System.out.println("Choices are B for blue,G for green, O for orange,");
System.out.println("R for red,P for purple and Y for yellow.");
System.out.println("Enter your guess in a string format.");
System.out.print("Enter your guess: ");
String guess = keyboard.nextLine().toUpperCase();
guesses++;
while (guess.length()!=4 && guess!= null) {
System.out.print("Enter your guess: ");
guess = keyboard.nextLine().toUpperCase();
}
System.out.println("Your guess is " + guess);
for(int i = 0; i < 4; i++){
int x = code.indexOf(guess.charAt(i));
if(x != -1){
if(x == i) redPegs++;
else whitePegs++;
}
}
System.out.print("Red pegs:");
for(int i = 0; i < redPegs; i++) System.out.print("*");
System.out.print(" White pegs:");
for(int i = 0; i < whitePegs; i++) System.out.print("*");
System.out.println("");
do{
// java string compare example
while (guess.compareTo(code) == 0){
// this line WILL print
System.out.println("Congradualtions you guessed the right colors.");
System.out.println("Red pegs:****");
System.out.println("White pegs:");
// System.out.print("Do you want to enter another(y=yes) quit(anything else)?");
// again=keyboard.nextLine();
}
if(guess.compareTo(code) != 0) {
System.out.print("Enter your guess: ");
guess = keyboard.nextLine().toUpperCase();
for(int i = 0; i < 4; i++){
int x = code.indexOf(guess.charAt(i));
if(x != -1){
if(x == i) redPegs++;
else whitePegs++;
}
}
System.out.print("Red pegs:");
for(int i = 0; i < redPegs; i++) System.out.print("*");
System.out.print(" White pegs:");
for(int i = 0; i < whitePegs; i++) System.out.print("*");
System.out.println("");
}
if (guesses>maxGuesses){
System.out.print("You are out of guesses");
}
redPegs = 0;
whitePegs = 0;
}
while(guess!=code && guesses<maxGuesses);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.