-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMiniMaxGameBlack.java
436 lines (418 loc) · 11.3 KB
/
MiniMaxGameBlack.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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
package morrisGame;
import java.io.*;
import java.util.*;
public class MiniMaxGameBlack {
private int pos_eval = 0;
private int stat_est = 0;
char[] newBoard = new char[21];
Map<Integer, char[]> position = new HashMap<>();
public MiniMaxGameBlack(String currentBoard, int depth)
{
char[] board = currentBoard.toCharArray();
boolean playerBlack = true;
stat_est = miniMax(board, depth, playerBlack);
newBoard = position.get(stat_est);
}
/**
* generates moves in the midgame and endgame
* @param a board position
* @return a list of board positions
*/
public List<char[]> generateMovesMidgameEndgame(char[] b){
//if the board has 3 black pieces Return the list produced by GenerateHopping
int numBlacks = 0;
for (char elem : b)
if (elem == 'B')
numBlacks++;
//applied to the board. Otherwise return the list produced by GenerateMove
if(numBlacks == 3)
return generateHopping(b);
//applied to the board.
else
return generateMove(b);
}
/**
* generate moves created by a black piece to an adjacent location
* @param pos a board position
* @return a list of board positions
*/
private List<char[]> generateMove(char[] board){
//L = empty list
List<char[]> l = new ArrayList<>();
//for each location in board
for(int location = 0; location < board.length; location++)
{
//if board[location]==B
if(board[location] == 'B')
{
//n = list of neighbors of location
List<Integer> n = neighbors(location);
//for each j in n
for(Integer j: n)
{
//if board[j] == empty
if(board[j] == 'x')
{
//b = copy of board; b[location] = empty; b[j]=W
char[] b = board.clone();
b[location] = 'x';
b[j] = 'B';
//if closeMill(j, b) GenerateRemove(b, L)
if(closeMill(j, b))
generateRemove(b, l);
//else add b to L
else
l.add(b);
}
}
}
}
//return L
return l;
}
/**
* generate moves created by black pieces hopping
* @param pos a board position
* @return a list of board positions
*/
private List<char[]> generateHopping(char[] board){
//L = empty list
List<char[]> l = new ArrayList<>();
//for each location alpha in board
for(int alpha = 0; alpha < board.length; alpha++)
{
//if board[alpha] == B
if(board[alpha] == 'B')
{
//for each location beta in board
for(int beta = 0; beta < board.length; beta++)
{
//if board[beta] == empty {
if(board[beta] == 'x')
{
//b = copy of board; b[alpha] = empty; b[beta] = B
char[] b = board.clone();
b[alpha] = 'x';
b[beta] = 'B';
//if closeMill(beta, b) generateRemove(b, L)
if(closeMill(beta, b))
generateRemove(b,l);
//else add b to L}}
else
l.add(b);
}
}
}
}
//return L
return l;
}
/**
* generates moves create by removing a white piece
* @param pos a board position
* @param board list of board positions
*/
private void generateRemove(char[] board, List<char[]> l) {
int notWhiteMills = 0;
//for each location in board:
for(int location = 0; location < board.length; location++)
{
//if board[location]==W {
if(board[location] == 'W')
{
//if not closeMill(location, board) {
if(!closeMill(location, board))
{
//b = copy of board; b[location] = empty
char[] b = board.clone();
b[location] = 'x';
//add b to L
l.add(b);
notWhiteMills++;
}
}
}
//If no positions were added (all white pieces are in mills) add b to L.
if(notWhiteMills > 0)
l.add(board);
}
/**
* finds the neighbors of the location
* @param j a location in the array representing the board
* @return a list of locations in the array corresponding to j’s neighbors
*/
private List<Integer> neighbors(int j){
switch(j) {
case 0: return Arrays.asList(1, 2, 6);
case 1: return Arrays.asList(0, 3, 11);
case 2: return Arrays.asList(0, 3, 4, 7);
case 3: return Arrays.asList(1, 2, 5, 10);
case 4: return Arrays.asList(2, 5, 8);
case 5: return Arrays.asList(3, 4, 9);
case 6: return Arrays.asList(0, 7, 18);
case 7: return Arrays.asList(2, 6, 8, 15);
case 8: return Arrays.asList(4, 7, 12);
case 9: return Arrays.asList(5, 10, 14);
case 10: return Arrays.asList(3, 9, 11, 17);
case 11: return Arrays.asList(1, 10, 20);
case 12: return Arrays.asList(8, 13, 15);
case 13: return Arrays.asList(12, 14, 16);
case 14: return Arrays.asList(9, 13, 17);
case 15: return Arrays.asList(7, 12, 16, 18);
case 16: return Arrays.asList(13, 15, 17, 19);
case 17: return Arrays.asList(10, 12, 16, 20);
case 18: return Arrays.asList(0, 15, 19);
case 19: return Arrays.asList(16, 18, 20);
case 20: return Arrays.asList(11, 17, 19);
default: return null;
}
}
/**
* checks to see if the location closes a mill
* @param j a location in the array representing the boar
* @param b the board
* @return true if the move to j closes a mill
*/
private boolean closeMill(int j, char[] b){
//C = b[j]; C must be either W or B. Cannot be x.
char c = b[j];
if(c != 'x')
{
switch(j)
{
case 0:
if((b[2] == c && b[4] == c) || (b[6] == c && b[18] == c))
return true;
else
return false;
case 1:
if((b[3]==c && b[5]==c) || (b[11]==c && b[20]==c))
return true;
else
return false;
case 2:
if((b[0]==c && b[4]==c) || (b[7]==c && b[15]==c))
return true;
else
return false;
case 3:
if((b[1]==c && b[5]==c) || (b[10]==c && b[17]==c))
return true;
else
return false;
case 4:
if((b[0]==c && b[2]==c) || (b[8]==c && b[12]==c))
return true;
else
return false;
case 5:
if((b[1]==c && b[3]==c) || (b[9]==c && b[14]==c))
return true;
else
return false;
case 6:
if((b[0]==c && b[18]==c) || (b[7]==c && b[8]==c))
return true;
else
return false;
case 7:
if((b[2]==c && b[15]==c) || (b[6]==c && b[8]==c))
return true;
else
return false;
case 8:
if((b[6]==c && b[7]==c) || (b[4]==c && b[12]==c))
return true;
else
return false;
case 9:
if ((b[10]==c && b[11]==c) || (b[5]==c && b[14]==c))
return true;
else
return false;
case 10:
if((b[9]==c && b[11]==c) || (b[3]==c && b[17]==c))
return true;
else
return false;
case 11:
if((b[1]==c && b[20]==c) || (b[9]==c && b[10]==c))
return true;
else
return false;
case 12:
if((b[4]==c && b[8]==c) || (b[13]==c && b[14]==c) || (b[15]==c && b[18]==c))
return true;
else
return false;
case 13:
if((b[12]==c && b[14]==c) || (b[15]==c && b[18]==c))
return true;
else
return false;
case 14:
if((b[12]==c && b[13]==c) || (b[5]==c && b[9]==c) || (b[17]==c && b[20]==c))
return true;
else
return false;
case 15:
if((b[2]==c && b[7]==c) || (b[16]==c && b[17]==c) || (b[12]==c && b[18]==c))
return true;
else
return false;
case 16:
if((b[15]==c && b[17]==c) || (b[13]==c && b[19]==c))
return true;
else
return false;
case 17:
if((b[3]==c && b[10]==c) || (b[15]==c && b[16]==c) || (b[14]==c && b[20]==c))
return true;
else
return false;
case 18:
if((b[0]==c && b[6]==c) || (b[12]==c && b[15]==c) || (b[19]==c && b[20]==c))
return true;
else
return false;
case 19:
if((b[13]==c && b[16]==c) || (b[18]==c && b[20]==c))
return true;
else
return false;
case 20:
if((b[1]==c && b[11]==c) || (b[14]==c && b[17]==c) || (b[18]==c && b[19]==c))
return true;
else
return false;
default: return false;
}
}
else
return false;
}
/**
* generates moves for white pieces
* @param b a board position
* @return a list of all positions reachable by a white move
*/
private List<char[]> generateWhiteMoves(char[] b){
List<char[]> l = new ArrayList<>();
char[] tempb = new char[21];
for(int i = 0; i < b.length; i++) {
if(b[i] == 'B')
tempb[i] = 'W';
else if(b[i] == 'W')
tempb[i] = 'B';
else
tempb[i] = 'x';
}
l = generateMovesMidgameEndgame(tempb);
for(int list = 0; list < l.size(); list++)
{
for(int i = 0; i < l.get(list).length; i++)
{
if(l.get(list)[i] == 'B')
l.get(list)[i] = 'W';
else if(l.get(list)[i] == 'W')
l.get(list)[i] = 'B';
else
l.get(list)[i] = 'x';
}
}
return l;
}
/**
* Uses miniMax algorithm to generate the best move
* @param x the position of the board
* @param depth the depth that we are current searching in the tree
* @param whiteFlag whether we are max or min
* @return the static estimate of the best move
*/
private int miniMax(char[] x, int depth, boolean blackFlag) {
pos_eval++;
//if x is a leaf return static(x)
if(depth == 1) {
position.put(staticEstimate(x), x);
return staticEstimate(x);
}
//maxMin
else if(blackFlag)
{
int newDepth = depth - 1;
//set v = -infinity
int v = Integer.MIN_VALUE;
boolean whiteFlag = !blackFlag;
List<char[]> children = generateMovesMidgameEndgame(x);
//for each child y of x:
for(char[] y: children)
//v = max(v, MinMax(y))
v = Math.max(v, miniMax(y, newDepth, whiteFlag));
//return v
return v;
}
//minMax
else {
int newDepth = depth - 1;
//set v = +infinity
int v = Integer.MAX_VALUE;
boolean whiteFlag = !blackFlag;
//for each child y of x:
List<char[]> children = generateWhiteMoves(x);
for(char[] y: children)
//v = min(v, MaxMin(y))
v = Math.min(v, miniMax(y, newDepth, whiteFlag));
//return v
return v;
}
}
/**
* give a static evaluation of the position
* @param b the board position
* @return static evaluation
*/
private int staticEstimate(char[] b)
{
int numWhitePieces = 0;
int numBlackPieces = 0;
int numWhiteMoves = 0;
for(char elem : b) {
if(elem == 'B')
numBlackPieces++;
if(elem == 'W')
numWhitePieces++;
}
numWhiteMoves = generateWhiteMoves(b).size();
if(numWhitePieces <= 2)
return 10000;
else if(numBlackPieces <= 2)
return -10000;
else if (numWhiteMoves == 0)
return 10000;
else
return (1000 * (numBlackPieces - numWhitePieces) - numWhiteMoves);
}
public static void main(String[] args) {
File fin = new File(args[0]);
File fout = new File(args[1]);
int depth = Integer.parseInt(args[2]);
try {
FileInputStream fish = new FileInputStream(fin);
PrintWriter otter = new PrintWriter(new FileWriter(fout));
try (Scanner scan = new Scanner(fish)) {
String board = scan.next();
MiniMaxGameBlack bend = new MiniMaxGameBlack(board, depth);
String bob = new String(bend.newBoard);
System.out.println("Board Position: " + bob);
System.out.println("Positions evaluated by static estimation: " + bend.pos_eval);
System.out.println("MINIMAX estimate: " + bend.stat_est);
otter.write(bob);
}
fish.close();
otter.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
}