-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGuessNum.java
More file actions
executable file
·200 lines (155 loc) · 5.67 KB
/
GuessNum.java
File metadata and controls
executable file
·200 lines (155 loc) · 5.67 KB
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package mastermind;
import java.util.function.Supplier;
import java.util.Scanner;
public class GuessNum {
static Scanner input = new Scanner(System.in);
static BaseData baseData = new BaseData();
static ErrorHandler errHandle = new ErrorHandler();
public static void main(String[] args) {
String yesNo = "";
outerLoop: while (true) {
while (true) {
printMessage(() -> "Computer V.S Player (y/n) ? ");
yesNo = input.nextLine();
errHandle.chkYesNoError(yesNo);
if (errHandle.getError()) {
printMessage(() -> errHandle.getErrMsg());
} else {
break;
}
}
switch (Choose.valueOf(yesNo.toUpperCase())) {
case Y:
computer();
break;
case N:
player01();
break;
}
start();
String result = player2();
switch (Choose.valueOf(result.toUpperCase())) {
case Y:
break;
case N:
printMessage(() -> "Bye Bye~~~");
break outerLoop;
}
}
}
private static void start() {
for (int i = 0; i < 60; i++) {
System.out.println();
}
// System.out.println("Ans:" + baseData.getCorrectAns());
printMessage(() -> "--- Player2 Start to guess ---");
printMessage(() -> "Type '0' to give up the game.");
baseData.setGuessTimes(1);
}
public static String endGameMessage(String msg) {
printMessage(() -> msg);
String contin = "N";
while (true) {
printMessage(() -> "Do you want to Continue (y/n)?");
contin = input.nextLine();
errHandle.chkYesNoError(contin);
if (errHandle.getError()) {
System.out.println(errHandle.getErrMsg());
} else {
break;
}
}
return contin;
}
private static String hints(String guess) {
int countBlack = 0;
int countWhite = 0;
String correctAns = baseData.getCorrectAns();
baseData.setWhite(0);
baseData.setBlack(0);
for (int i = 0; i < baseData.getMaxColourNum(); i++) {
if (guess.charAt(i) == correctAns.charAt(i)) {
correctAns = correctAns.substring(0, i) + "B" + correctAns.substring(i + 1);
baseData.setBlack(++countBlack);
}
}
for (int i = 0; i < baseData.getMaxColourNum(); i++) {
for (int j = 0; j < baseData.getMaxColourNum(); j++) {
if (guess.charAt(i) == correctAns.charAt(j)) {
baseData.setWhite(++countWhite);
correctAns = correctAns.substring(0, j) + "W" + correctAns.substring(j + 1);
}
}
}
return "Black: " + baseData.getBlack() + " " + "White: " + baseData.getWhite();
}
public static Boolean validaGuessInput(String input) {
errHandle.chkPegsColour(input, baseData);
Boolean err01 = errHandle.getError();
if (err01) {
printMessage(() -> errHandle.getErrMsg());
}
errHandle.chkPegsNumber(input, baseData);
Boolean err02 = errHandle.getError();
if (err02) {
printMessage(() -> errHandle.getErrMsg());
}
return err01 || err02;
}
public static void computer() {
String ans = "";
for (int i = 0; i < baseData.getMaxColourNum(); i++) {
int ansEle = (int) (Math.random() * (baseData.getMaxColourValue() - baseData.getMinColourValue() + 1)
+ baseData.getMinColourValue());
ans += String.valueOf(ansEle);
}
baseData.setCorrectAns(ans);
}
public static void player01() {
printMessage(() -> "Player1 input the answer: ");
String playerAnsInput = input.nextLine();
while (true) {
Boolean err = validaGuessInput(playerAnsInput);
if (err) {
printMessage(() -> "Player1 input the answer: ");
playerAnsInput = input.nextLine();
} else {
break;
}
}
baseData.setCorrectAns(String.valueOf(playerAnsInput));
}
public static String player2() {
String guess = "";
Boolean win = false;
while (true) {
int guesTimes = baseData.getGuessTimes();
int maxGuesTimes = baseData.getMaxGuesstimes();
System.out.println("Step" + guesTimes + ": Please input your guess: ");
guess = input.nextLine();
char[] giveUp = guess.toCharArray();
if (guess.length() == 1 && giveUp[0] == '0') {
break;
// System.exit(1);
}
Boolean err = validaGuessInput(guess);
if (err) {
continue;
}
System.out.println("Guess " + guesTimes + ": " + guess + "\t" + hints(guess));
if (baseData.getBlack() == baseData.getMaxColourNum()) {
win = true;
break;
}
if (guesTimes >= maxGuesTimes) {
break;
}
baseData.setGuessTimes(guesTimes + 1);
}
String endGameMsg = !win ? "You Lose!! Correct Answer is " + baseData.getCorrectAns() : "You Win!!!";
return endGameMessage(endGameMsg);
}
public static void printMessage(Supplier<?> supplier) {
System.out.println(supplier.get());
}
}