-
Notifications
You must be signed in to change notification settings - Fork 1
/
Game.java
309 lines (264 loc) · 9.01 KB
/
Game.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
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
package tictactoe;
import java.util.ArrayList;
import java.util.Random;
import static tictactoe.Main.scan;
public class Game {
private final char[] BOARD = new char[] {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '};
private boolean running = false;
private char mark_to_draw;
private char markDrawn;
private String modeP1;
private String modeP2;
private final String p1;
private final String p2;
private boolean isFirst = true;
private boolean isBestToWin = true;
// Constructor
public Game(String inputCommand) {
drawTheField();
mark_to_draw = 'X';
String[] input = inputCommand.split(" +");
if(input[1].equals("user")) {
p1 = "user";
} else {
p1 = "bot";
modeP1 = input[1];
}
if(input[2].equals("user")) {
p2 = "user";
} else {
p2 = "bot";
modeP2 = input[2];
}
}
public void run() {
running = true;
while (running) {
nextTurn();
drawTheField();
check();
}
}
public void drawTheField() {
System.out.println(
"----------" + "\n" +
"| " + BOARD[6] + " " + BOARD[7] + " " + BOARD[8] + " |" + "\n" +
"| " + BOARD[3] + " " + BOARD[4] + " " + BOARD[5] + " |" + "\n" +
"| " + BOARD[0] + " " + BOARD[1] + " " + BOARD[2] + " |" + "\n"
+ "----------"
);
}
// calculate the nextTurn
private void nextTurn() {
if (mark_to_draw == 'X') {
if (p1.equals("user")) {
userTurn();
} else {
botTurn(modeP1);
}
} else {
if (p2.equals("user")) {
userTurn();
} else {
botTurn(modeP2);
}
}
if (isFirst) {
isFirst = false;
}
}
// empty cell: _ , cell with mark x: 'X', cell with mark o: 'O'
private void userTurn() {
int x;
while (true) {
try {
System.out.print("Enter the empty spot position: ");
x = scan.nextInt();
if (x > 9 || x < 1) {
System.out.println("Spot position should be from 1 to 9!");
} else if (BOARD[x - 1] != ' ') {
System.out.println("This spot is occupied! Choose another one!");
} else break;
} catch (final Exception e) {
System.out.println("You should enter numbers!");
scan.next();
}
}
BOARD[x - 1] = mark_to_draw;
updateMark();
// read newline char
scan.nextLine();
}
private void botTurn(String botPower) {
System.out.println("Making move level \"" + botPower + "\"");
switch (botPower) {
case "easy":
botMoveEasy(makeAvailableSpotsArray());
break;
case "medium":
botMoveMedium(makeAvailableSpotsArray());
break;
case "hard" :
botMoveHard();
}
updateMark();
}
// Return a collection of available spots
private ArrayList<Integer> makeAvailableSpotsArray() {
ArrayList<Integer> availableSpots = new ArrayList<>(9);
for (int i = 0; i < 9; i++) {
if (BOARD[i] == ' ')
availableSpots.add(i);
}
return availableSpots;
}
// pick randomly available spot
private void botMoveEasy(ArrayList<Integer> availableSpots) {
Random rand = new Random();
int randSpot = rand.nextInt(availableSpots.size());
int spot = availableSpots.get(randSpot);
BOARD[spot] = mark_to_draw;
}
// check for every available spots if there is a win
// then that the best to pick
// if the no winning found then try to block opponent's best spot to pick
// lastly if nothing works, choose random spot
private void botMoveMedium (ArrayList<Integer> emptyPlaces) {
boolean win = false;
for (int i : emptyPlaces) {
BOARD[i] = mark_to_draw;
win = isWin();
if (!win) {
BOARD[i] = ' ';
} else {
break;
}
}
if (!win) {
mark_to_draw = (mark_to_draw == 'X') ? 'O' : 'X';
for (int i : emptyPlaces) {
BOARD[i] = mark_to_draw;
win = isWin();
if (win) {
mark_to_draw = (mark_to_draw == 'X') ? 'O' : 'X';
BOARD[i] = mark_to_draw;
break;
} else {
BOARD[i] = ' ';
}
}
}
if (!win) {
mark_to_draw = (mark_to_draw == 'X') ? 'O' : 'X';
botMoveEasy(emptyPlaces);
}
}
// using minimax: choosing the best move where the win is possible
private void botMoveHard () {
if (isFirst) {
botMoveEasy(makeAvailableSpotsArray());
} else
{
int score;
int bestScore = -1;
int move = 0;
// loop through all the available spots
for (int i = 0; i < BOARD.length; i++) {
if (BOARD[i] == ' ') {
BOARD[i] = mark_to_draw;
score = minimax(false, 0);
BOARD[i] = ' ';
// must go in the branch where the win is possible
if (score > bestScore && isBestToWin) {
bestScore = score;
move = i;
isBestToWin = false;
}
}
}
isBestToWin = true;
BOARD[move] = mark_to_draw;
}
}
private int minimax (boolean isMaximizing, int depth) {
if (isWin()) {
if (isMaximizing) {
return -1;
} else {
isBestToWin = true;
return 1;
}
}
if (isDraw()) {
return 0;
}
if (isMaximizing) {
int score;
int bestScore = -1;
for (int i = 0; i < BOARD.length; i++) {
if (BOARD[i] == ' ') {
BOARD[i] = mark_to_draw;
score = minimax(false, depth + 1);
BOARD[i] = ' ';
if (score > bestScore) {
bestScore = score;
}
}
}
return bestScore;
} else {
int score;
int bestScore = 1;
for (int i = 0; i < BOARD.length; i++) {
if (BOARD[i] == ' ') {
BOARD[i] = (mark_to_draw == 'X') ? 'O' : 'X';
score = minimax(true, depth + 1);
BOARD[i] = ' ';
if (score < bestScore) {
bestScore = score;
}
}
}
return bestScore;
}
}
// update mark_to_draw and markDrawn
private void updateMark() {
markDrawn = mark_to_draw;
if (mark_to_draw == 'X') {
mark_to_draw = 'O';
} else {
mark_to_draw = 'X';
}
}
// check for a win or a draw
private void check() {
if (isWin()) {
System.out.println(markDrawn + " wins");
running = false;
} else if (isDraw()) {
System.out.println("Draw");
running = false;
}
}
private boolean isWin() {
// total 8 conditions one of them could be true if there is a win
return (BOARD[0] == BOARD[1] && BOARD[1] == BOARD[2] && BOARD[1] != ' ' ||
BOARD[3] == BOARD[4] && BOARD[4] == BOARD[5] && BOARD[4] != ' ' ||
BOARD[6] == BOARD[7] && BOARD[7] == BOARD[8] && BOARD[7] != ' ' ||
BOARD[6] == BOARD[3] && BOARD[3] == BOARD[0] && BOARD[3] != ' ' ||
BOARD[7] == BOARD[4] && BOARD[4] == BOARD[1] && BOARD[4] != ' ' ||
BOARD[8] == BOARD[5] && BOARD[5] == BOARD[2] && BOARD[5] != ' ' ||
BOARD[6] == BOARD[4] && BOARD[4] == BOARD[2] && BOARD[4] != ' ' ||
BOARD[0] == BOARD[4] && BOARD[4] == BOARD[8] && BOARD[4] != ' '
) ;
}
private boolean isDraw() {
for (char symbols : BOARD) {
if (symbols == ' ') {
return false;
}
}
return true;
}
}