java) Hi, I am trying to simulate the board game mastermind. In the main method,
ID: 3666573 • Letter: J
Question
java) Hi, I am trying to simulate the board game mastermind. In the main method, I am trying to compare each letter in the string guess and the code. If there is a letter in both strings that are in the same position assign the user a redPeg, and keep a running total of the number of correct letters at each position. If there us a letter in both strings but at the wrong position assign the user a white Peg. If the user's guess contains the same letters in the code(assuming the user will have a total of 4 red pegs) then the user guessed right. I mostly need help with lines 101-109. Your help will be greatly appreciated.
1 //import statements
2
3
4
5
6
7
8 import java.util.Scanner;
9
10 import java.util.Random;
11
12 public class testing
13 {
14 public static final int maxGuesses = 10;
15
16
17 public static void secretCodepegs(){
18
19 //Create an instance of Random class
20 Random ran = new Random();
21
22 //Set array of strings
23 String colors[]= {"G","B","Y","R","O","P"};
24
25 String code = "";
26
27 String randomColor;
28
29 //Create a for loop that run for 4 times
30 //to create four letter random code
31 for (int i = 0; i < 4; i++)
32 {
33
34
35 do
36 {
37 //get random colors string
38 randomColor=colors[ran.nextInt(6)];
39
40 }while(code.contains(randomColor));
41
42
43 code+=randomColor;
44 }
45
46
47 System.out.println(code);
48
49
50
51
52 }
53
54 public static void userGuess(){
55
56 String guess;
57 int guesses=0;//amount of guesses
58
59 Scanner keyboard= new Scanner(System.in);
60 System.out.println("Secret code has been simulated.");
61 System.out.println("Pick four colors to put in each hole.");
62 System.out.println("Choices are B for blue,G for green, O for orange,");
63 System.out.println("R for red,P for purple and Y for yellow.");
64 System.out.println("Enter your guess in a string format.");
65
66 System.out.print("Enter your guess: ");
67 guess = keyboard.nextLine().toUpperCase();
68 guesses++;
69
70
71
72 while (guess.length()!=4 && guess!= null) {
73 System.out.print("Enter your guess: ");
74 guess = keyboard.nextLine().toUpperCase();
75 }
76
77 System.out.println("Your guess is " + guess);
78
79 }
80
81
82 public static void main (String[] args)
83 {
84
85 int guesses=0;// The user's guess
86 int maxGuesses=10;//maximum ammaount of guess attempt
87 boolean isSink=false;
88 boolean isHit=false;
89 int redPegs=0;
90 int hits=0;
91 int blackPegs=0;
92 boolean correct;
93
94 do{
95
96 String guess;
97 String code = "";
98 secretCodepegs();
99 userGuess();
100
101 char[] guess = guess.toCharArray();
102 char[] code = code.toCharArray();
103
104
105 for(int i = 0; i < 4; i++){
106 if(guess.charAt(i)== code.charAt(i))
107 {
108 correct=true;
109
110 }
111
112 }
113
114
115
116 }
117
118
119 while(guess!=code);
120
121
122
123
124
125 }
126
127
128
129
130 }
131
Explanation / Answer
Bellow is the corrected code:
class testing
{
public static final int maxGuesses = 10;
public static void secretCodepegs(){
//Create an instance of Random class
Random ran = new Random();
//Set array of strings
String colors[]= {"G","B","Y","R","O","P"};
String code = "";
String randomColor;
//Create a for loop that run for 4 times
//to create four letter random code
for (int i = 0; i < 4; i++)
{
do
{
//get random colors string
randomColor=colors[ran.nextInt(6)];
}while(code.contains(randomColor));
code+=randomColor;
}
System.out.println(code);
}
public static void userGuess(){
String guess;
int guesses=0;//amount of guesses
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: ");
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);
}
public static void main (String[] args)
{
String guess="";
String code = "";
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 blackPegs=0;
boolean correct;
do{
secretCodepegs();
userGuess();
//char[] guess = guess.toCharArray();
// char[] code = code.toCharArray();
for(int i = 0; i < guess.length(); i++){
if(guess.charAt(i)== code.charAt(i))
{
correct=true;
}
}
}
while(guess!=code);
}
}
Out put:
RBOP
Secret code has been simulated.
Pick four colors to put in each hole.
Choices are B for blue,G for green, O for orange,
R for red,P for purple and Y for yellow.
Enter your guess in a string format.
Enter your guess: Your guess is ORYP
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.