-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGamePlay.java
363 lines (325 loc) · 12.2 KB
/
GamePlay.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
import java.util.ArrayList;
import java.util.List;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
import com.sun.net.httpserver.HttpExchange;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
//Note the presence of implicit recursion, minimax method calls minSearch, minSearch calls maxSearch, and maxSearch calls minSearch
public class GamePlay {
public static Board gameBoard;
public static void init() {
gameBoard = new Board();
}
public static void main(String[] args) {
init();
// default port and delay
int port = 8083;
// parse command line arguments to override defaults
if (args.length > 0)
{
try
{
port = Integer.parseInt(args[0]);
}
catch (NumberFormatException ex)
{
System.err.println("USAGE: java CheckersService [port]");
System.exit(1);
}
}
// set up an HTTP server to listen on the selected port
try
{
InetSocketAddress addr = new InetSocketAddress(port);
HttpServer server = HttpServer.create(addr, 1);
server.createContext("/move.html", new MoveHandler());
server.start();
}
catch (IOException ex)
{
ex.printStackTrace(System.err);
System.err.println("Could not start server");
}
}
//This is it. The Heart. The Minimax algorithm.
public static List<Integer> minimax(int depth) {
//best heuristic, we need to maximize this
int bestHeur = -99999;
List<List<Integer>> holder = new ArrayList<List<Integer>>(1); //holding all possible moves as we go by
List<Integer> toRet = new ArrayList<Integer>(2); //best move
for(int i = 0; i < 8; i++) {
for(int j = 0; j < 8; j++) {
//go through each piece on the board
//if white, that is, computer's turn, we need to 'max' because we are the computer
if((gameBoard.cboard[i][j] == 1) || (gameBoard.cboard[i][j] == 3)) {
//the monster getMoves function from the Board class being used here
holder = gameBoard.getMoves(i, j);
//For each move possible, ie, moving through the holder arraylist
for(int k = 0; k < holder.size(); k++) {
int listSize = holder.get(k).size();
int destj = holder.get(k).get(listSize-1); //destj and desti are the final locations of piece (i, j) here
int desti = holder.get(k).get(listSize-2); //we do this because the previous elements in the sub-list of the
//(contd)arraylist holder (because it is a list of lists) might be the pieces taken (killed) by (i, j) in the turn
//Do move
boolean kinged = false;
gameBoard.cboard[desti][destj] = gameBoard.cboard[i][j];
if((gameBoard.cboard[i][j] == 1) && (desti == 7)) {
gameBoard.cboard[desti][destj] = 3; //Kinged if we reach the opposite side of the board!
kinged = true;
}
gameBoard.cboard[i][j] = 0; //becaause we moved
//removeds here holds the rest of 'holder' after the final destination i, j have been seen
List<Integer> removeds = new ArrayList<Integer>(1);
//if removeds' size is still 1 it means nothing has been removed!
for(int l = 0; l < listSize-2; l += 2) {
int remi = holder.get(k).get(l);
int lnext = l + 1;
int remj = holder.get(k).get(lnext);
int piece = gameBoard.cboard[remi][remj];
gameBoard.cboard[remi][remj] = 0;
removeds.add(remi);
removeds.add(remj);
removeds.add(piece);
}
//Move done
//minSearch is called after minimax because now we assume that at next stage of the game tree, opponent will
//(contd) minimize our (computer's) heuristic
int value = minSearch(depth-1);
if(value > bestHeur) {
toRet = holder.get(k);
toRet.add(i);
toRet.add(j);
bestHeur = value;
}
//Undo move
gameBoard.cboard[i][j] = gameBoard.cboard[desti][destj];
if(kinged) gameBoard.cboard[i][j] = 1; //Unkinged if had been kinged in previous move
gameBoard.cboard[desti][destj] = 0;
if(removeds.size() > 1) {
for(int m = 0; m < removeds.size(); m += 3) {
int remi = removeds.get(m);
int mn1 = m + 1;
int mn2 = m + 2;
int remj = removeds.get(mn1);
gameBoard.cboard[remi][remj] = removeds.get(mn2);
}
}
//Move undone
}
}
//If can do any take, do it. Remove those pieces from board
if(toRet.size() > 4) {
int retSize = toRet.size();
int fromi = toRet.get(retSize-2);
int fromj = toRet.get(retSize-1);
int toi = toRet.get(retSize-4);
int toj = toRet.get(retSize-3);
gameBoard.cboard[toi][toj] = gameBoard.cboard[fromi][fromj];
if((gameBoard.cboard[toi][toj] == 1) && (toi == 7)) gameBoard.cboard[toi][toj] = 3;
if((gameBoard.cboard[toi][toj] == 2) && (toi == 0)) gameBoard.cboard[toi][toj] = 4;
gameBoard.cboard[fromi][fromj] = 0;
for(int k = 0; k < retSize-4; k += 2) {
int remoi = toRet.get(k);
int remoj = toRet.get(k+1);
gameBoard.cboard[remoi][remoj] = 0;
}
return toRet;
}
}
}
int retSize = toRet.size();
int fromi = toRet.get(retSize-2);
int fromj = toRet.get(retSize-1);
int toi = 0;
int toj = 0;
if(retSize > 2) {
toi = toRet.get(retSize-4);
toj = toRet.get(retSize-3);
}
gameBoard.cboard[toi][toj] = gameBoard.cboard[fromi][fromj];
if((gameBoard.cboard[toi][toj] == 1) && (toi == 7)) gameBoard.cboard[toi][toj] = 3;
if((gameBoard.cboard[toi][toj] == 2) && (toi == 0)) gameBoard.cboard[toi][toj] = 4;
gameBoard.cboard[fromi][fromj] = 0;
for(int i = 0; i < retSize-4; i += 2) {
int remoi = toRet.get(i);
int remoj = toRet.get(i+1);
gameBoard.cboard[remoi][remoj] = 0;
}
return toRet;
}
//minSearch is pretty much the same as minimax, but looking from the opponent's view
public static int minSearch(int depth) {
if(depth == 0) return gameBoard.score();
//hence bestHeur is +infinity, the opponent will minimize this
int bestHeur = 99999;
List<List<Integer>> holder = new ArrayList<List<Integer>>(1);
for(int i = 0; i < 8; i++) {
for(int j = 0; j < 8; j++) {
//if user's turn, that is, black
if((gameBoard.cboard[i][j] == 2) || (gameBoard.cboard[i][j] == 4)) {
holder = gameBoard.getMoves(i, j);
//For each move
for(int k = 0; k < holder.size(); k++) {
int listSize = holder.get(k).size();
int destj = holder.get(k).get(listSize-1);
int desti = holder.get(k).get(listSize-2);
//Do move
boolean kinged = false;
gameBoard.cboard[desti][destj] = gameBoard.cboard[i][j];
if((gameBoard.cboard[i][j] == 2) && (desti == 0)) {
gameBoard.cboard[desti][destj] = 4; //Kinged
kinged = true;
}
gameBoard.cboard[i][j] = 0;
List<Integer> removeds = new ArrayList<Integer>(1);
//if removeds' size is still 1 it means nothing has been removed!
for(int l = 0; l < listSize-2; l += 2) {
int remi = holder.get(k).get(l);
int lnext = l + 1;
int remj = holder.get(k).get(lnext);
int piece = gameBoard.cboard[remi][remj];
gameBoard.cboard[remi][remj] = 0;
removeds.add(remi);
removeds.add(remj);
removeds.add(piece);
}
//Move done
//call maxSearch here, to get back to the computer's viewpoint.
//note: minimax calls minSearch and minSearch calls maxSearch, and maxSearch calls minSearch.
//implicit recursion here
int value = maxSearch(depth-1);
if(value < bestHeur) {
bestHeur = value;
}
//Undo move
gameBoard.cboard[i][j] = gameBoard.cboard[desti][destj];
if(kinged) gameBoard.cboard[i][j] = 2;
gameBoard.cboard[desti][destj] = 0;
if(removeds.size() > 1) {
for(int m = 0; m < removeds.size(); m += 3) {
int remi = removeds.get(m);
int mn1 = m + 1;
int mn2 = m + 2;
int remj = removeds.get(mn1);
gameBoard.cboard[remi][remj] = removeds.get(mn2);
}
}
//Move undone
}
}
}
}
return bestHeur;
}
//maxSearch! back to computer's viewpoint. difference is that minsearch and maxsearch return the optimized heuristics from
//each of their perspectives, then minimax transforms them into the best move and returns the moves arraylist (takens and destinations)
public static int maxSearch(int depth) {
if(depth == 0) return gameBoard.score();
int bestHeur = -99999;
List<List<Integer>> holder = new ArrayList<List<Integer>>(1);
for(int i = 0; i < 8; i++) {
for(int j = 0; j < 8; j++) {
//if computer's turn, white.
if((gameBoard.cboard[i][j] == 1) || (gameBoard.cboard[i][j] == 3)) {
holder = gameBoard.getMoves(i, j);
//For each move
for(int k = 0; k < holder.size(); k++) {
int listSize = holder.get(k).size();
int destj = holder.get(k).get(listSize-1);
int desti = holder.get(k).get(listSize-2);
//Do move
boolean kinged = false;
gameBoard.cboard[desti][destj] = gameBoard.cboard[i][j];
if((gameBoard.cboard[i][j] == 1) && (desti == 7)) {
gameBoard.cboard[desti][destj] = 3; //Kinged
kinged = true;
}
gameBoard.cboard[i][j] = 0;
List<Integer> removeds = new ArrayList<Integer>(1);
//if removeds' size is still 1 it means nothing has been removed!
for(int l = 0; l < listSize-2; l += 2) {
int remi = holder.get(k).get(l);
int lnext = l + 1;
int remj = holder.get(k).get(lnext);
int piece = gameBoard.cboard[remi][remj];
gameBoard.cboard[remi][remj] = 0;
removeds.add(remi);
removeds.add(remj);
removeds.add(piece);
}
//Move done
int value = minSearch(depth-1);
if(value > bestHeur) {
bestHeur = value;
}
//Undo move
gameBoard.cboard[i][j] = gameBoard.cboard[desti][destj];
if(kinged) gameBoard.cboard[i][j] = 1;
gameBoard.cboard[desti][destj] = 0;
if(removeds.size() > 1) {
for(int m = 0; m < removeds.size(); m += 3) {
int remi = removeds.get(m);
int mn1 = m + 1;
int mn2 = m + 2;
int remj = removeds.get(mn1);
gameBoard.cboard[remi][remj] = removeds.get(mn2);
}
}
//Move undone
}
}
}
}
return bestHeur;
}
public static class MoveHandler implements HttpHandler
{
@Override
public void handle(HttpExchange ex) throws IOException
{
System.err.println(ex.getRequestURI());
String q = ex.getRequestURI().getQuery();
char[] cha = q.toCharArray();
int leg = cha.length;
int[] inta = new int[leg];
for(int i = 0; i < leg; i++) {
inta[i] = Character.getNumericValue(cha[i]);
}
int fromi = inta[0];
int fromj = inta[1];
int toi = inta[2];
int toj = inta[3];
boolean taken = false;
if((Math.abs(fromi - toi)) > 1) {
taken = true;
if(fromi != 9) {
int takeni = (fromi + toi) / 2;
int takenj = (fromj + toj) / 2;
gameBoard.cboard[takeni][takenj] = 0;
}
}
if(fromi != 9) {
gameBoard.cboard[toi][toj] = gameBoard.cboard[fromi][fromj];
if((gameBoard.cboard[toi][toj] == 1) && (toi == 7)) gameBoard.cboard[toi][toj] = 3;
if((gameBoard.cboard[toi][toj] == 2) && (toi == 0)) gameBoard.cboard[toi][toj] = 4;
gameBoard.cboard[fromi][fromj] = 0;
}
List<Integer> results = new ArrayList<Integer>(2);
if((taken) && (fromi == 9)) results = minimax(3);
if(!taken) results = minimax(3);
// write the response as JSON
String encoded = "{\"board\":\"";
for(int i = 0; i < 8; i++) {
for(int j = 0; j < 8; j++) encoded = encoded + Integer.toString(gameBoard.cboard[i][j]);
}
encoded = encoded + "\"}";
ex.getResponseHeaders().add("Access-Control-Allow-Origin", "*");
byte[] responseBytes = encoded.getBytes();
ex.sendResponseHeaders(HttpURLConnection.HTTP_OK, responseBytes.length);
ex.getResponseBody().write(responseBytes);
ex.close();
}
}
}