forked from awh32/EECS-314
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMastermind.java
154 lines (146 loc) · 3.41 KB
/
Mastermind.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import java.util.Scanner;
public class Mastermind {
public static void main(String[] args){
//Initialize the scanner for input.
Scanner input = new Scanner(System.in);
//Start the game.
System.out.println("Are you a Masterind? \n Player 1, enter your code:");
String code = input.nextLine();
//Ask player 2 to guess.
System.out.println("Player 2, begin to guess:");
//Player 2 begins to guess.
boolean solved = false;
while (solved == false){
/* Initialize all the variables needed.
* They will initialize each time through the loop to 0.
*/
int correctcolors = 0;
int correctspots = 0;
int codeR = 0;
int codeO = 0;
int codeY = 0;
int codeG = 0;
int codeB = 0;
int codeP = 0;
int guessR = 0;
int guessO = 0;
int guessY = 0;
int guessG = 0;
int guessB = 0;
int guessP = 0;
//Get Player 2's guess.
String guess = input.nextLine();
//Check for correct solution right away.
if (code == guess) {
System.out.print("Winner!");
}
else {
//Count the correct spots.
for(int i = 0; i < code.length(); i++) {
if(code.charAt(i)==guess.charAt(i)) {
correctspots++;
}
}
//Count the frequencies of colors in the code.
for(int j = 0; j < code.length(); j++){
if(code.charAt(j) == 'R'){
codeR++;
}
if (code.charAt(j) == 'O'){
codeO++;
}
if (code.charAt(j) == 'Y'){
codeY++;
}
if (code.charAt(j) == 'G'){
codeG++;
}
if (code.charAt(j) == 'B'){
codeB++;
}
if (code.charAt(j) == 'P'){
codeP++;
}
}
//Count the frequencies of colors in the guess
for(int k = 0; k < guess.length(); k++){
if(guess.charAt(k) == 'R'){
guessR++;
}
if (guess.charAt(k) == 'O'){
guessO++;
}
if (guess.charAt(k) == 'Y'){
guessY++;
}
if (guess.charAt(k) == 'G'){
guessG++;
}
if (guess.charAt(k) == 'B'){
guessB++;
}
if (guess.charAt(k) == 'P'){
guessP++;
}
}
/* Compare the frequencies of the guess and the code.
* Update correct colors as needed.
* If codeColor >= guess, use the guess.
* But if codeColor < guess, use the code.
*/
if (codeR >= guessR) {
correctcolors += guessR;
}
else {
correctcolors += codeR;
}
if (codeO >= guessO) {
correctcolors += guessO;
}
else {
correctcolors += codeO;
}
if (codeY >= guessY) {
correctcolors += guessY;
}
else {
correctcolors += codeY;
}
if (codeG >= guessG) {
correctcolors += guessG;
}
else {
correctcolors += codeG;
}
if (codeB >= guessB) {
correctcolors += guessB;
}
else {
correctcolors += codeB;
}
if (codeP >= guessP) {
correctcolors += guessP;
}
else {
correctcolors += codeP;
}
/*If Player 2 gets correct # of colors and correct # of spots
* Say Player 2 got it right and set solved = true to break
* out of the loop.
*/
if (correctcolors == 4 && correctspots == 4) {
System.out.print("You got it right!");
solved = true;
}
/* Else just tell Player 2 the # of colors and # of spots
* that Player 2 got correct for that guess.
*/
else {
System.out.printf("Correct color %s, Correct spot %s", correctcolors, correctspots);
}
}
}
//Close the Scanner.
input.close();
}
}