-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAI.java
More file actions
291 lines (248 loc) · 9.24 KB
/
AI.java
File metadata and controls
291 lines (248 loc) · 9.24 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
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
//Brian Pomerantz
import java.util.*;
public class AI {
private boolean side;
private int depth;
public AI(boolean side, int depth) {
this.side = side;
this.depth = depth;
}
//Calculates the best move using a MinMax algorithm with ALphaBeta Pruning
//Input: ArrayList of legal moves
//Output: The particular legal move which maximizes (or minimizes) the engine's score
public String move(Board bd) {
ArrayList moves = bd.legalMoves(side);
//Stalemate
if (moves.size() == 0) {
return '\u00ab' + "-" + '\u00ab';
}
byte alpha = Byte.MIN_VALUE, beta = Byte.MAX_VALUE;
byte best = -1, val, bestVal;
if (side) {bestVal = Byte.MIN_VALUE;}
else {bestVal = Byte.MAX_VALUE;}
// OMP PARALLEL BLOCK BEGINS
{
__omp_Class0 __omp_Object0 = new __omp_Class0();
// shared variables
__omp_Object0.best = best;
__omp_Object0.bestVal = bestVal;
__omp_Object0.alpha = alpha;
__omp_Object0.beta = beta;
__omp_Object0.moves = moves;
__omp_Object0.bd = bd;
__omp_Object0.depth = depth;
__omp_Object0.side = side;
// firstprivate variables
try {
jomp.runtime.OMP.doParallel(__omp_Object0);
} catch(Throwable __omp_exception) {
System.err.println("OMP Warning: Illegal thread exception ignored!");
System.err.println(__omp_exception);
}
// reduction variables
// shared variables
best = __omp_Object0.best;
val = __omp_Object0.val;
bestVal = __omp_Object0.bestVal;
alpha = __omp_Object0.alpha;
beta = __omp_Object0.beta;
moves = __omp_Object0.moves;
bd = __omp_Object0.bd;
depth = __omp_Object0.depth;
side = __omp_Object0.side;
}
// OMP PARALLEL BLOCK ENDS
//Resign if opponent has forced mate within depth
//Alternatively, the engine could chose a random move
if (best == -1) {
if (side){return "0-1";}
else{return "1-0";}
}
return (String) moves.get(best);
}
//AlphaBeta function
//Recursively determines best possible move assuming perfect play within given depth
//Discontinues branch of search tree if necessarily worse than previously calculated branch
//More efficient than MinMax
private byte alphabeta(Board bd, int dep, byte alpha, byte beta, boolean maxPlayer) {
if (dep == 0) {
return bd.score();
}
if (maxPlayer) {
ArrayList list = bd.legalMoves(true);
//Checkmate
if (bd.checkmate(false)) {
return Byte.MIN_VALUE;
}
//Stalemate
if (list.size() == 0) {
return 0;
}
for (int s = 0; s < list.size(); s++) {
Board nBD = new Board(bd.getBoard());
byte tempVal = alphabeta(nBD.move((String) list.get(s), true), dep-1, alpha, beta, false);
if (tempVal > alpha) {
alpha = tempVal;
}
if (beta <= alpha) {
break;
}
}
return alpha;
}
else {
ArrayList list = bd.legalMoves(false);
//Checkmate
if (bd.checkmate(true)) {
return Byte.MAX_VALUE;
}
//Stalemate
if (list.size() == 0) {
return 0;
}
for (int s = 0; s < list.size(); s++) {
Board nBD = new Board(bd.getBoard());
byte tempVal = alphabeta(nBD.move((String) list.get(s), false), dep-1, alpha, beta, true);
if (tempVal < beta) {
beta = tempVal;
}
if (beta <= alpha) {
break;
}
}
return beta;
}
}
// OMP PARALLEL REGION INNER CLASS DEFINITION BEGINS
private class __omp_Class0 extends jomp.runtime.BusyTask {
// shared variables
byte best;
byte val;
byte bestVal;
byte alpha;
byte beta;
ArrayList moves;
Board bd;
int depth;
boolean side;
// firstprivate variables
// variables to hold results of reduction
public void go(int __omp_me) throws Throwable {
// firstprivate variables + init
// private variables
// reduction variables, init to default
// OMP USER CODE BEGINS
{
{ // OMP FOR BLOCK BEGINS
// copy of firstprivate variables, initialized
// copy of lastprivate variables
// variables to hold result of reduction
boolean amLast=false;
{
// firstprivate variables + init
// [last]private variables
// reduction variables + init to default
// -------------------------------------
jomp.runtime.LoopData __omp_WholeData2 = new jomp.runtime.LoopData();
jomp.runtime.LoopData __omp_ChunkData1 = new jomp.runtime.LoopData();
__omp_WholeData2.start = (long)( 0);
__omp_WholeData2.stop = (long)( moves.size());
__omp_WholeData2.step = (long)(1);
jomp.runtime.OMP.setChunkStatic(__omp_WholeData2);
while(!__omp_ChunkData1.isLast && jomp.runtime.OMP.getLoopStatic(__omp_me, __omp_WholeData2, __omp_ChunkData1)) {
for(;;) {
if(__omp_WholeData2.step > 0) {
if(__omp_ChunkData1.stop > __omp_WholeData2.stop) __omp_ChunkData1.stop = __omp_WholeData2.stop;
if(__omp_ChunkData1.start >= __omp_WholeData2.stop) break;
} else {
if(__omp_ChunkData1.stop < __omp_WholeData2.stop) __omp_ChunkData1.stop = __omp_WholeData2.stop;
if(__omp_ChunkData1.start > __omp_WholeData2.stop) break;
}
for(byte i = (byte)__omp_ChunkData1.start; i < __omp_ChunkData1.stop; i += __omp_ChunkData1.step) {
// OMP USER CODE BEGINS
{
Board nBD = new Board(bd.getBoard());
val = alphabeta(nBD.move((String) moves.get(i), side), depth, alpha, beta, !side);
//System.out.println(moves.get(i) + ": " + val);
if (side && val > bestVal) {
// OMP CRITICAL BLOCK BEGINS
synchronized (jomp.runtime.OMP.getLockByName("")) {
// OMP USER CODE BEGINS
{
bestVal = val;
best = i;
}
// OMP USER CODE ENDS
}
// OMP CRITICAL BLOCK ENDS
if (val > alpha) {
// OMP CRITICAL BLOCK BEGINS
synchronized (jomp.runtime.OMP.getLockByName("")) {
// OMP USER CODE BEGINS
{
alpha = val;
}
// OMP USER CODE ENDS
}
// OMP CRITICAL BLOCK ENDS
}
}
if (!side && val < bestVal) {
// OMP CRITICAL BLOCK BEGINS
synchronized (jomp.runtime.OMP.getLockByName("")) {
// OMP USER CODE BEGINS
{
bestVal = val;
best = i;
}
// OMP USER CODE ENDS
}
// OMP CRITICAL BLOCK ENDS
if (val < beta) {
// OMP CRITICAL BLOCK BEGINS
synchronized (jomp.runtime.OMP.getLockByName("")) {
// OMP USER CODE BEGINS
{
beta = val;
}
// OMP USER CODE ENDS
}
// OMP CRITICAL BLOCK ENDS
}
}
if (beta <= alpha) {
return;
}
}
// OMP USER CODE ENDS
if (i == (__omp_WholeData2.stop-1)) amLast = true;
} // of for
if(__omp_ChunkData1.startStep == 0)
break;
__omp_ChunkData1.start += __omp_ChunkData1.startStep;
__omp_ChunkData1.stop += __omp_ChunkData1.startStep;
} // of for(;;)
} // of while
// call reducer
jomp.runtime.OMP.doBarrier(__omp_me);
// copy lastprivate variables out
if (amLast) {
}
}
// set global from lastprivate variables
if (amLast) {
}
// set global from reduction variables
if (jomp.runtime.OMP.getThreadNum(__omp_me) == 0) {
}
} // OMP FOR BLOCK ENDS
}
// OMP USER CODE ENDS
// call reducer
// output to _rd_ copy
if (jomp.runtime.OMP.getThreadNum(__omp_me) == 0) {
}
}
}
// OMP PARALLEL REGION INNER CLASS DEFINITION ENDS
}