-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAI.java
More file actions
267 lines (216 loc) · 7.72 KB
/
AI.java
File metadata and controls
267 lines (216 loc) · 7.72 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
/*
* TODO:
* Prog Deepening - assign # depending on where in candMoves the move is
* search for the inverse of the # (use candMoves.length()-#)
*
* If rating jumps by >100, search one more move - don't want to
* capture on the last evaluation
*/
// ##################################################################################### //
import java.util.*;
/** AI - This enables the engine to look ahead
*
* It contains a branching algorithm known as Alpha-Beta.
* It calls itself recursively, making a desicion tree
* until a leaf node is reached.
**/
public class AI
{
static String currentPath = "";
static String bestPath = "";
static int alpha = -1000000000;
static int beta = 1000000000;
// static LinkedList<Integer> rateTemp = new LinkedList<Integer>();
static int lastRate = 0; // Rating jumps mean eval ended on a capture -> more depth, breadth
/** Reset each turn **/
public static void reset()
{
currentPath = "";
bestPath = "";
alpha = -1000000000;
beta = 1000000000;
}
/** Alpha-Beta Algorithm:
* Get list of best (# = breadth) moves in order.
* Make moves and evaluate by calling think recursively until depth = 0.
* Alpha (white) is trying to maximize, while Beta (black) is trying to minimize.
* Assuming black is CPU, select the lowest score of the end positions.
**/
public static int think(int depth, int breadth)
{
// FOR TESTING //
System.out.println("\n\n#######################");
System.out.println("# CURRENT: " + currentPath);
System.out.println("# BEST: " + bestPath);
System.out.println("#######################\n\n");
// ------------------------------------------------------------------------------------- //
String candMoves = listMoves(breadth);
if("".equals(candMoves) && !Board.whoseMove) // White is checkmated
return -1000000000;
if("".equals(candMoves) && Board.whoseMove) // Black is checkmated
return 1000000000;
// If it's a leaf node
if (depth == 0 || breadth <= 1)
{
int endVal = StratEval.globalEval(Board.chessBoard);
System.out.println("\nEndVal:"+endVal+"\nCURRENT PATH: " + currentPath + "\n");
System.out.println("\n BEST PATH: " + bestPath + "\n");
System.out.println("");
return endVal;
}
int valTemp = 0;
int value = 0;
if(!Board.whoseMove) {value = -1000000000;}
else {value = 1000000000;}
if(!"".equals(candMoves))
{
for(int i=0; i<candMoves.length(); i+=5) // Make each move, going down each path
{
Board.makeMove(candMoves.substring(i, i+5)); System.out.println("MAKE:"+candMoves.substring(i, i+5));
currentPath += candMoves.substring(i, i+5);
valTemp = think(depth-1,breadth-1);
Board.undoMove(candMoves.substring(i, i+5)); System.out.println("UNDO:"+candMoves.substring(i, i+5));
// Alpha-Beta pruning
if(Board.whoseMove && valTemp > beta)
{
System.out.println("Alpha > Beta"); beta = 1000000000;
currentPath = currentPath.replaceAll(candMoves.substring(i, i+5), ""); // Update currentPath
return valTemp;
}
if(!Board.whoseMove && valTemp < alpha)
{
System.out.println("Beta < Alpha"); alpha = -1000000000;
currentPath = currentPath.replaceAll(candMoves.substring(i, i+5), ""); // Update currentPath
return valTemp;
}
if(!Board.whoseMove && valTemp > value)
alpha = value = valTemp;
if(Board.whoseMove && valTemp < value)
{ beta = value = valTemp; bestPath = currentPath; }
currentPath = currentPath.replaceAll(candMoves.substring(i, i+5), ""); // Update currentPath
}
}
// Once all candidate moves are analyzed
return value;
}
// ------------------------------------------------------------------------------------- //
public static String listMoves(int breadth)
{
/* Sorting:
* Each move is rated and sorted from best to worst
* The top moves (# = breadth) are considered the candMoves
* Sorting algorithm is simple - think about implementing Quick Sort
*/
String list = Board.possibleMoves();
int length = list.length();
String temp = "";
String candMoves = "";
int maxRateW = -1000000000;
int maxRateB = 1000000000;
LinkedList<Integer> rateTemp = new LinkedList<Integer>();
for (int i = 0; i < length; i += 5) // go through every move substring and rate
{
try {
String move = list.substring(i, i + 5);
Board.makeMove(move);
// ------------------ \\ Custom Evaluation // ------------------ \\
// To motivate the engine to investigate moving pieces.
// This is not included in StratEval because it does not effect the position.
int valTest = 0;
if (Board.whoseMove) // White evaluation (BLACK TO MOVE)
{
valTest = StratEval.globalEval(Board.chessBoard);
int r = Character.getNumericValue(move.charAt(0));
int c = Character.getNumericValue(move.charAt(1));
int j = 8*r + c; // The piece that is being moved
if(BB.pTakesB[j] && BB.piecesW[j]) // If it is attacked by a pawn
valTest += (BB.valueOf(j)/2);
if (King.kingSafeB()) // Examine checks
valTest += 100;
}
if (!Board.whoseMove) // Black evaluation (WHITE TO MOVE)
{
valTest = StratEval.globalEval(Board.chessBoard);
int r = Character.getNumericValue(move.charAt(0));
int c = Character.getNumericValue(move.charAt(1));
int j = 8*r + c; // The piece that is being moved
if(BB.pTakesW[j] && BB.piecesB[j]) // If it is attacked by a pawn
valTest += (BB.valueOf(j)/2);
if (King.kingSafeW()) // Examine checks
valTest -= 100;
}
rateTemp.add(valTest);
Board.undoMove(list.substring(i, i + 5));
} catch (Exception e) {}
}
if (length/5 < breadth) // Else it will try to evaluate moves that don't exist
{ breadth = length/5; } // depth++ ?
// SORTING //
for (int j = 0; j < breadth; j++) {
Object rate = 0;
for (int k = 0; k < length; k += 5)
{
try {
rate = rateTemp.get(k / 5);
} catch (Exception e) {}
if(Board.whoseMove) // Black tries for lowest score
{
if ((int) rate < maxRateB) {
maxRateB = (int) rate;
try {
temp = list.substring(k, k + 5);
} catch (Exception e) {}
list = list.replaceAll(temp, "");
rateTemp.remove(rate);
}
}
else { // White tries for highest score
if (((int)rate > maxRateW)) {
maxRateW = (int)rate;
try {
temp = list.substring(k, k + 5);
} catch (Exception e) {}
list = list.replaceAll(temp, "");
rateTemp.remove(rate);
}
}
}
candMoves += temp;
list = list.replaceAll(temp, "");
maxRateW = -1000000000;
maxRateB = 1000000000;
}
System.out.println("CANDIDATE MOVES: "+candMoves);
return candMoves;
}
/** -- QuickSort - A faster algorithm to sort candidate moves -- **/
// TODO: Make it deal with ratings as well as moves
LinkedList<Integer> quickSort(LinkedList<Integer> arr, int left, int right) {
int index = partition(arr, left, right);
if (left < index - 1)
quickSort(arr, left, index - 1);
if (index < right)
quickSort(arr, index, right);
return arr;
}
int partition(LinkedList<Integer> arr, int left, int right)
{
int i = left, j = right;
int tmp;
int pivot = (int) arr.get((left + right) / 2);
while (i <= j) {
while ((int)arr.get(i) < pivot)
i++;
while ((int)arr.get(j) > pivot)
j--;
if (i <= j) {
tmp = (int)arr.get(i);
arr.set(i, arr.get(j));
arr.set(j, tmp);
i++;
j--;
}
};
return i;
}
}