-
Notifications
You must be signed in to change notification settings - Fork 4
/
Evaluator.java
executable file
·354 lines (288 loc) · 9.45 KB
/
Evaluator.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
// CS171 Fall 2012 Final Project
//
// Nealon Young
// ID #81396982
import java.util.ArrayList;
public class Evaluator {
private static final int emptyPosition = 0;
private static final int aiPosition = 1;
private static final int humanPosition = 2;
private static int[][] positionWeights;
private static int[][] winSequence;
private static int[][] openEndSequence;
private static int[][] threatSequence;
private static int[][] sevenTrapSequence;
//
// intialize() generates piece sequences for the current game board
//
public static void initialize() {
createPositionWeights();
createWinSequence();
createOpenEndSequence();
createThreatSequence();
createSevenTrapSequence();
}
//
// inBounds() returns true if the given x and y coordinates are within the bounds of the board
//
private static boolean inBounds(int x, int y) {
return ((0 <= x) && (x < AIGame.width) && (0 <= y) && (y < AIGame.height)) ? true : false;
}
//
// matchPosition() scans the game board for a sequence eminating from the (x, y) position, looking
// in a direction determined by the deltaX and deltaY parameters, deltaS determines whether to
// reverse the sequence
//
private static boolean match(AIGameState state, int x , int y, int[] sequence, int deltaX, int deltaY, int deltaS) {
int s = (deltaS > 0) ? 0 : sequence.length - 1;
if (inBounds(x + deltaX * (sequence.length - 1), y + deltaY * (sequence.length - 1))) {
for (int i = 0; i < sequence.length; i++) {
if (state.boardPositions[x][y] != sequence[s]) {
return false;
}
x += deltaX;
y += deltaY;
s += deltaS;
}
return true;
}
return false;
}
//
// matchPosition() scans the game board for a sequence eminating from the (x, y) position, direction
// specifies whether the sequence should be reversed
//
private static boolean matchPosition(AIGameState state, int x, int y, int[] sequence, int direction) {
//
// Look horizontally right
//
if (match(state, x, y, sequence, 1, 0, direction)) {
return true;
}
//
// Look vertically up
//
if (match(state, x, y, sequence, 0, 1, direction)) {
return true;
}
//
// Look diagonally right upwards
//
if (match(state, x, y, sequence, 1, 1, direction)) {
return true;
}
//
// Look diagonally right downwards
//
if (match(state, x, y, sequence, 1, -1, direction)) {
return true;
}
return false;
}
//
// countPositionForward() scans the game board for the given sequence, and returns the number of occurences
//
private static int countPositionForward(AIGameState state, int[] sequence) {
int sequenceCount = 0;
for (int i = 0; i < AIGame.width; i++) {
for (int j = 0; j < AIGame.height; j++) {
if (matchPosition(state, i, j, sequence, 1)) {
sequenceCount++;
}
}
}
return sequenceCount;
}
//
// countPositionBackward() scans the game board for the given sequence, and returns the number of occurences
//
private static int countPositionBackward(AIGameState state, int[] sequence) {
int sequenceCount = 0;
for (int i = 0; i < AIGame.width; i++) {
for (int j = 0; j < AIGame.height; j++) {
if (matchPosition(state, i, j, sequence, -1)) {
sequenceCount++;
}
}
}
return sequenceCount;
}
//
// evalWins() counts the number of win sequences (k in a row) on the game board for the given player
//
private static int evalWins(AIGameState state, boolean isAi) {
int player = (isAi) ? aiPosition : humanPosition;
return countPositionForward(state, winSequence[player]);
}
private static void createWinSequence() {
winSequence = new int[humanPosition + 1][AIGame.winLength];
for (int i = aiPosition; i <= humanPosition; i++) {
for (int j = 0; j < AIGame.winLength; j++) {
winSequence[i][j] = i;
}
}
}
//
// evalThreats() counts the number of threat sequences (k-1 in a row) on the game board for the given player
//
private static int evalThreats(AIGameState state, boolean isAi) {
int player = (isAi) ? aiPosition : humanPosition;
return (countPositionForward(state, threatSequence[player]) + countPositionBackward(state, threatSequence[player]));
}
//
// createThreatSequences() generates threat sequences based on the game parameters (width, height, K)
//
private static void createThreatSequence() {
threatSequence = new int[humanPosition + 1][AIGame.winLength];
for (int i = aiPosition; i <= humanPosition; i++) {
threatSequence[i][0] = emptyPosition;
for (int j = 1; j < AIGame.winLength; j++) {
threatSequence[i][j] = i;
}
}
}
//
// evalOpenEnds() counts the number of open end sequences (empty position, k-2 player pieces, 2 empty positions) on the game board
// for the given player
//
private static int evalOpenEnds(AIGameState state, boolean isAi) {
int player = (isAi) ? aiPosition : humanPosition;
return (countPositionForward(state, openEndSequence[player]) + countPositionBackward(state, openEndSequence[player]));
}
//
// createOpenEndSequence() generates open end sequences based on the game parameters (width, height, K)
//
private static void createOpenEndSequence() {
openEndSequence = new int[humanPosition + 1][AIGame.winLength + 1];
for (int i = aiPosition; i <= humanPosition; i++) {
openEndSequence[i][0] = emptyPosition;
for (int j = 1; j < (AIGame.winLength - 1); j++) {
openEndSequence[i][j] = i;
}
openEndSequence[i][AIGame.winLength - 1] = emptyPosition;
openEndSequence[i][AIGame.winLength] = emptyPosition;
}
}
//
// evalThreats() counts the number of 'seven trap' sequences (k-1 in a row) on the game board for the given player
//
private static int evalSevenTraps(AIGameState state, boolean isAi) {
int player = (isAi) ? aiPosition : humanPosition;
int sevenTraps = 0;
for (int i = 0; i < AIGame.width; i++) {
for (int j = 0; j < AIGame.height - 1; j++) {
if (match(state, i, j, sevenTrapSequence[player], -1, 0, 1)) {
if (match(state, i, j + 1, sevenTrapSequence[player], -1, -1, 1)) {
sevenTraps++;
}
}
if (match(state, i, j, sevenTrapSequence[player], 1, 0, 1)) {
if (match(state, i, j + 1, sevenTrapSequence[player], 1, -1, 1)) {
sevenTraps++;
}
}
}
}
for (int i = 0; i < AIGame.width; i++) {
for (int j = 1; j < AIGame.height; j++) {
if (match(state, i, j, sevenTrapSequence[player], -1, 0, 1)) {
if (match(state, i, j - 1, sevenTrapSequence[player], -1, 1, 1)) {
sevenTraps++;
}
}
if (match(state, i, j, sevenTrapSequence[player], 1, 0, 1)) {
if (match(state, i, j - 1, sevenTrapSequence[player], 1, 1, 1)) {
sevenTraps++;
}
}
}
}
return sevenTraps;
}
//
// createSevenTrapSequence() generates 'seven trap' sequences based on the game parameters (width, height, K)
//
private static void createSevenTrapSequence() {
sevenTrapSequence = new int[humanPosition + 1][AIGame.winLength];
for (int i = aiPosition; i <= humanPosition; i++) {
sevenTrapSequence[i][0] = emptyPosition;
for (int j = 1; j < AIGame.winLength; j++) {
sevenTrapSequence[i][j] = i;
}
}
}
//
// evalPositionWeights() finds the sum of the weights of positions held by the given player
//
private static int evalPositionWeights(AIGameState state, boolean isAi) {
int player = (isAi) ? aiPosition : humanPosition;
int score = 0;
for (int i = 0; i < AIGame.width; i++) {
for (int j = 0; j < AIGame.height; j++) {
if (state.boardPositions[i][j] == player) {
score += positionWeights[i][j];
}
}
}
return score;
}
//
// createPositionWeights() generates weights for each position on the board, based on the game parameters (width, height, K)
//
private static void createPositionWeights() {
positionWeights = new int[AIGame.width][AIGame.height];
int width = AIGame.width;
int height = AIGame.height;
int k = AIGame.winLength;
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
if (inBounds(i + (k - 1), j)) {
for (int l = 0; l < k; l++) {
positionWeights[i + l][j]++;
}
}
if (inBounds(i, j + (k - 1))) {
for (int l = 0; l < k; l++) {
positionWeights[i][j + l]++;
}
}
if (inBounds(i + (k - 1), j + (k - 1))) {
for (int l = 0; l < k; l++) {
positionWeights[i + l][j + l]++;
}
}
if (inBounds(i + (k - 1), j - (k - 1))) {
for (int l = 0; l < k; l++) {
positionWeights[i + l][j - l]++;
}
}
}
}
}
//
// Weights to use for the different features in eval()
//
private static final int[] evalWeights = {1000000, 100, 10, 15, 20};
//
// eval() is a MFEF that returns the score for a given game state by combining 5 weighted factors
//
public static int eval(AIGameState state) {
int[] aiScores = {0, 0, 0, 0, 0};
int[] humanScores = {0, 0, 0, 0, 0};
aiScores[0] = evalWins(state, true);
humanScores[0] = evalWins(state, false);
aiScores[1] = evalThreats(state, true);
humanScores[1] = evalThreats(state, false);
aiScores[2] = evalOpenEnds(state, true);
humanScores[2] = evalOpenEnds(state, false);
aiScores[3] = evalPositionWeights(state, true);
humanScores[3] = evalPositionWeights(state, false);
aiScores[4] = evalSevenTraps(state, true);
humanScores[4] = evalSevenTraps(state, false);
int finalScore = 0;
for (int i = 0; i < aiScores.length; i++) {
finalScore += (evalWeights[i] * (aiScores[i] - humanScores[i]));
}
return finalScore;
}
}