-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjjucbr.java
436 lines (402 loc) · 10.2 KB
/
jjucbr.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
import java.util.ArrayList;
public class jjucbr {
long endTime;
/**
* Takes the current board and returns the utility of all the possible moves
*
* @param matrix
* represent how the pieces are places
* @param player
* represent the player, -1 for black player and 1 for white
* player
* @param level
* how many levels you have left to search in before you return a
* value
* @param maxTime
* the maximal time the algorithm has to find all the best moves
* in
*
* @return returnMatrix a matrix that represent a value of how good a move
* is
*
*/
public int[][] getAlphaBetaMatrix(int[][] matrix, int player, int level,
int maxTime) {
long time = System.currentTimeMillis();
endTime = maxTime * 1000 + time;
long halfEndTime = maxTime * 500 + time;
int[][] returnMatrix = new int[10][10];
level = 1;
int temp = 0;
for (int i = 1; i < 9; i++) {
for (int j = 1; j < 9; j++) {
if (matrix[i][j] == 0) {
temp++;
}
}
}
// if (temp <= level) {
// level = temp;
// }
while (System.currentTimeMillis() < halfEndTime) {
int[][] movesMatrix = new int[10][10];
for (int i = 1; i < 9; i++) {
for (int j = 1; j < 9; j++) {
if (matrix[i][j] == 0) {
ArrayList<Integer> move = count(matrix, i, j, player);
if (move.get(0) != 0) {
int t = alphaBeta(matrix, i, j, -1000, 1000,
player, level, move);
if (t == 2000) {
System.out.println(level);
return returnMatrix;
}
movesMatrix[i][j] = t;
} else {
movesMatrix[i][j] = 100;
}
} else {
movesMatrix[i][j] = 100;
}
}
}
for (int i = 1; i < 9; i++) {
for (int j = 1; j < 9; j++) {
returnMatrix[i][j] = movesMatrix[i][j];
}
}
if (level == temp) {
break;
}
level++;
}
System.out.println(level);
return returnMatrix;
}
/**
* This is a recursive function that for every possible move, make that move
* and look for the best move of the opponent and calculate which position
* is the best position to place your piece. This includes the alpha-beta
* pruning which decreases the search time by ignoring moves that's not
* optimal.
*
* @param matrix
* represent how the pieces are places
* @param x
* x position of the piece that is going to be placed
* @param y
* y position of the piece that is going to be placed
* @param a
* Alpha is the maximum lower bound of possible solution
* @param b
* Beta is the minimum upper bound of possible solutions
* @param player
* represent the player, -1 for black player and 1 for white
* player
* @param level
* how many levels you have left to search in before you return a
* value
* @param move
* list of which pieces that you turn if you place a piece at
* position XY except for the first position which represent the
* change in (number of white pieces) - (number of black pieces)
* from the last state
*
* @return an integer which represent the max or the min value depending on
* player if there is a timeout 2000 is returned
*/
public int alphaBeta(int[][] matrix, int x, int y, int a, int b,
int player, int level, ArrayList<Integer> move) {
if (level > 6) {
/*
* if(System.currentTimeMillis() > endTime) { return 2000; }
*/
}
if (level == 1) {
return move.get(0);
}
// copies the matrix to newMatrix
int[][] newMatrix = new int[10][10];
for (int j = 0; j < 10; j++) {
for (int k = 0; k < 10; k++) {
newMatrix[j][k] = matrix[j][k];
}
}
// changes the matrix to as if you placed a piece at position (x,y)
newMatrix[x][y] = player;
int f = 0, g = 0;
for (int k = 1; k < move.size(); k++) {
f = move.get(k);
k++;
g = move.get(k);
newMatrix[f][g] = player;
}
boolean changed = false; // if it goes into the if(move2.get(0) != 0)
// method
for (int i = 1; i < 9; i++) {
for (int j = 1; j < 9; j++) {
if (newMatrix[i][j] == 0) {
ArrayList<Integer> move2 = count(newMatrix, i, j, player
* -1);
if (move2.get(0) != 0) {
changed = true;
move2.set(0, move2.get(0) + move.get(0));
if (player == 1) {
int temp = alphaBeta(newMatrix, i, j, a, b, player
* -1, level - 1, move2);
if (temp == 2000) {
return 2000;
}
// opponents best min value
b = Math.min(b, temp);
if (a >= b) {
return a;
}
} else {
int temp = alphaBeta(newMatrix, i, j, a, b, player
* -1, level - 1, move2);
if (temp == 2000) {
return 2000;
}
// opponents best max value
a = Math.max(a, temp);
if (a >= b) {
return b;
}
}
}
}
}
}
if (changed) {
if (player == 1) {
return b;
} else {
return a;
}
} else {
// TEST !!!!
// ----------------------------------------------------------------------------------
if (player == 1) {
return a;
}
return b;
}
}
/**
* Takes the current board and returns a list of which pieces that you turn
* if you place a piece at position XY except for the first position which
* represent the change in (number of white pieces) - (number of black
* pieces) from the last state
*
* @param matrix
* represent how the pieces are places
* @param x
* x position of the piece that is going to be placed
* @param y
* y position of the piece that is going to be placed
* @param player
* represent the player, -1 for black player and 1 for white
* player
*
* @return re list of which pieces that you turn if you place a piece at
* position XY except for the first position which represent the
* change in (number of white pieces) - (number of black pieces)
* from the last state
*/
public ArrayList<Integer> count(int[][] matrix, int x, int y, int player) {
int count = 0, a = 0, b = 0;
ArrayList<Integer> change = new ArrayList<Integer>();
ArrayList<Integer> re = new ArrayList<Integer>();
re.add(0);
if (matrix[x][y] == 0) {
int temp = 0;
if (matrix[x - 1][y - 1] == player * -1) {
int tx = x - 1, ty = y - 1;
while (matrix[tx][ty] == player * -1) {
change.add(tx);
change.add(ty);
tx--;
ty--;
temp = temp + player;
}
if (matrix[tx][ty] == player) {
for (int i = 0; i < change.size(); i++) {
a = change.get(i);
re.add(a);
i++;
b = change.get(i);
re.add(b);
}
count = count + temp;
re.set(0, count);
}
temp = 0;
change.clear();
}
if (matrix[x][y - 1] == player * -1) {
int tx = x, ty = y - 1;
while (matrix[tx][ty] == player * -1) {
change.add(tx);
change.add(ty);
temp = temp + player;
ty--;
}
if (matrix[tx][ty] == player) {
for (int i = 0; i < change.size(); i++) {
a = change.get(i);
re.add(a);
i++;
b = change.get(i);
re.add(b);
}
count = count + temp;
re.set(0, count);
}
temp = 0;
change.clear();
}
if (matrix[x + 1][y - 1] == player * -1) {
int tx = x + 1, ty = y - 1;
while (matrix[tx][ty] == player * -1) {
change.add(tx);
change.add(ty);
temp = temp + player;
ty--;
tx++;
}
if (matrix[tx][ty] == player) {
for (int i = 0; i < change.size(); i++) {
a = change.get(i);
re.add(a);
i++;
b = change.get(i);
re.add(b);
}
count = count + temp;
re.set(0, count);
}
temp = 0;
change.clear();
}
if (matrix[x + 1][y] == player * -1) {
int tx = x + 1, ty = y;
while (matrix[tx][ty] == player * -1) {
change.add(tx);
change.add(ty);
temp = temp + player;
tx++;
}
if (matrix[tx][ty] == player) {
for (int i = 0; i < change.size(); i++) {
a = change.get(i);
re.add(a);
i++;
b = change.get(i);
re.add(b);
}
count = count + temp;
re.set(0, count);
}
temp = 0;
change.clear();
}
if (matrix[x + 1][y + 1] == player * -1) {
int tx = x + 1, ty = y + 1;
while (matrix[tx][ty] == player * -1) {
change.add(tx);
change.add(ty);
temp = temp + player;
ty++;
tx++;
}
if (matrix[tx][ty] == player) {
for (int i = 0; i < change.size(); i++) {
a = change.get(i);
re.add(a);
i++;
b = change.get(i);
re.add(b);
}
count = count + temp;
re.set(0, count);
}
temp = 0;
change.clear();
}
if (matrix[x][y + 1] == player * -1) {
int tx = x, ty = y + 1;
while (matrix[tx][ty] == player * -1) {
change.add(tx);
change.add(ty);
temp = temp + player;
ty++;
}
if (matrix[tx][ty] == player) {
for (int i = 0; i < change.size(); i++) {
a = change.get(i);
re.add(a);
i++;
b = change.get(i);
re.add(b);
}
count = count + temp;
re.set(0, count);
}
temp = 0;
change.clear();
}
if (matrix[x - 1][y + 1] == player * -1) {
int tx = x - 1, ty = y + 1;
while (matrix[tx][ty] == player * -1) {
change.add(tx);
change.add(ty);
temp = temp + player;
ty++;
tx--;
}
if (matrix[tx][ty] == player) {
for (int i = 0; i < change.size(); i++) {
a = change.get(i);
re.add(a);
i++;
b = change.get(i);
re.add(b);
}
count = count + temp;
re.set(0, count);
}
temp = 0;
change.clear();
}
if (matrix[x - 1][y] == player * -1) {
int tx = x - 1, ty = y;
while (matrix[tx][ty] == player * -1) {
change.add(tx);
change.add(ty);
temp = temp + player;
tx--;
}
if (matrix[tx][ty] == player) {
for (int i = 0; i < change.size(); i++) {
a = change.get(i);
re.add(a);
i++;
b = change.get(i);
re.add(b);
}
count = count + temp;
re.set(0, count);
}
temp = 0;
change.clear();
}
}
if (count != 0) {
re.set(0, count * 2 + player);
} else {
re.set(0, 0);
}
return re;
}
}