-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHeuristic.java
More file actions
360 lines (297 loc) · 14.2 KB
/
Heuristic.java
File metadata and controls
360 lines (297 loc) · 14.2 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
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
// Owen Gregson
// Artificial Intelligence
// TTT Checkpoint #3
// Dec 18, 2024
import java.util.*;
public class Heuristic {
// central positions
private static final int BOARD_SIZE = Coordinate.NCubed;
private static final int LINE_LENGTH = Coordinate.N;
private static final Set<Integer> CENTER_POSITIONS = new HashSet<>(Arrays.asList(
21, 22, 25, 26,
29, 30, 33, 34
));
// middle plane corner positions
private static final Set<Integer> PCORNER_POSITIONS = new HashSet<>(Arrays.asList(
16, 19, 28, 31,
32, 35, 44, 47
));
// board corner positions
private static final Set<Integer> BCORNER_POSITIONS = new HashSet<>(Arrays.asList(
0, 3, 12, 15,
48, 51, 60, 63
));
private static long computePositionMask(Set<Integer> positions) {
long mask = 0L;
for (int pos : positions) {
mask = Bit.set(mask, pos);
}
return mask;
}
private static final long CENTER_POSITIONS_MASK = computePositionMask(CENTER_POSITIONS);
private static final long PCORNER_POSITIONS_MASK = computePositionMask(PCORNER_POSITIONS);
private static final long BCORNER_POSITIONS_MASK = computePositionMask(BCORNER_POSITIONS);
private static final long FULL_MASK = -1L >>> (64 - BOARD_SIZE);
private static final int[][] LINES_BY_POSITION = new int[BOARD_SIZE][];
static {
// Pre-compute all lines that touch each position. This greatly speeds up
// fork/double-threat detection because we avoid repeatedly walking the
// global Line list.
List<Integer>[] temp = new ArrayList[BOARD_SIZE];
for (int i = 0; i < BOARD_SIZE; i++) {
temp[i] = new ArrayList<>();
}
for (int i = 0; i < Line.lines.length; i++) {
long mask = Line.lines[i].positions();
long bits = mask;
while (bits != 0) {
int pos = Long.numberOfTrailingZeros(bits);
temp[pos].add(i);
bits &= (bits - 1);
}
}
for (int i = 0; i < BOARD_SIZE; i++) {
LINES_BY_POSITION[i] = temp[i].stream().mapToInt(Integer::intValue).toArray();
}
}
/**
* Evaluates the board from the perspective of the specified player.
*
* @param board The current board state.
* @param player The player for whom the evaluation is being performed.
* @return An integer score representing the desirability of the board state.
*/
public static long evaluate(Board board, Player player, Weights w) {
long score = 0;
Player opponent = player.other();
long playerPositions = (player == Player.X) ? board.xPositions : board.oPositions;
long opponentPositions = (player == Player.X) ? board.oPositions : board.xPositions;
long occupiedPositions = playerPositions | opponentPositions;
for (Line line : Line.lines) {
long linePositions = line.positions();
long playerLinePositions = linePositions & playerPositions;
long opponentLinePositions = linePositions & opponentPositions;
int playerCount = Bit.countOnes(playerLinePositions);
int opponentCount = Bit.countOnes(opponentLinePositions);
int emptyCount = LINE_LENGTH - playerCount - opponentCount;
// blocked lines do not contribute; they cannot yield a win for either side.
if (playerCount > 0 && opponentCount > 0) {
score -= w.BLOCKED_LINE_PENALTY;
continue;
}
if (playerCount > 0) {
int centerCount = Bit.countOnes(playerLinePositions & CENTER_POSITIONS_MASK);
score += getScore(playerCount, centerCount, emptyCount, w);
} else if (opponentCount > 0) {
int centerCount = Bit.countOnes(opponentLinePositions & CENTER_POSITIONS_MASK);
score -= getScore(opponentCount, centerCount, emptyCount, w) * w.OPPONENT_SCORE_MULTIPLIER;
} else {
// fully open lines favour the side to move slightly
score += w.OPEN_LINE_BONUS;
}
if (opponentCount == LINE_LENGTH - 1 && emptyCount == 1) {
score -= w.IMMEDIATE_THREAT_PENALTY;
}
if (playerCount == LINE_LENGTH - 1 && emptyCount == 1) {
score += w.IMMEDIATE_WIN_BONUS;
}
}
// positional control
int playerCenterControl = Bit.countOnes(playerPositions & CENTER_POSITIONS_MASK);
int opponentCenterControl = Bit.countOnes(opponentPositions & CENTER_POSITIONS_MASK);
score += playerCenterControl * w.CENTER_CONTROL_MULTIPLIER;
score -= opponentCenterControl * w.OPPONENT_CENTER_CONTROL_MULTIPLIER;
int playerPCorners = Bit.countOnes(playerPositions & PCORNER_POSITIONS_MASK);
int opponentPCorners = Bit.countOnes(opponentPositions & PCORNER_POSITIONS_MASK);
score += playerPCorners * w.PCORNER_CONTROL_MULTIPLIER;
score -= opponentPCorners * w.OPPONENT_PCORNER_CONTROL_MULTIPLIER;
int playerBCorners = Bit.countOnes(playerPositions & BCORNER_POSITIONS_MASK);
int opponentBCorners = Bit.countOnes(opponentPositions & BCORNER_POSITIONS_MASK);
score += playerBCorners * w.BCORNER_CONTROL_MULTIPLIER;
score -= opponentBCorners * w.OPPONENT_BCORNER_CONTROL_MULTIPLIER;
// fork pressure and defensive awareness
int playerForks = evaluatePotentialForks(playerPositions, occupiedPositions, opponentPositions);
int opponentForks = evaluatePotentialForks(opponentPositions, occupiedPositions, playerPositions);
score += (long) playerForks * w.PLAYER_FORKS_MULTIPLIER;
score -= (long) opponentForks * w.OPPONENT_FORKS_MULTIPLIER;
int playerDoubleThreats = evaluateDoubleThreats(playerPositions, opponentPositions, occupiedPositions);
int opponentDoubleThreats = evaluateDoubleThreats(opponentPositions, playerPositions, occupiedPositions);
score += (long) playerDoubleThreats * w.DOUBLE_THREAT_BONUS;
score -= (long) opponentDoubleThreats * w.OPPONENT_DOUBLE_THREAT_PENALTY;
int playerImmediateWinningSquares = countImmediateWinningSquares(playerPositions, opponentPositions, occupiedPositions);
int opponentImmediateWinningSquares = countImmediateWinningSquares(opponentPositions, playerPositions, occupiedPositions);
score += (long) playerImmediateWinningSquares * w.IMMEDIATE_WIN_SQUARE_BONUS;
score -= (long) opponentImmediateWinningSquares * w.OPPONENT_IMMEDIATE_WIN_SQUARE_PENALTY;
int blockedThreatIntersections = evaluateBlockedIntersections(playerPositions, opponentPositions);
score += (long) blockedThreatIntersections * w.BLOCKED_THREAT_INTERSECTION_BONUS;
// Opponent's ability to fork next move (prevents tunnel vision)
int opponentPotentialForks = evaluateOpponentPotentialForks(board, opponent, occupiedPositions);
score -= (long) opponentPotentialForks * w.OPPONENT_POTENTIAL_FORKS_PENALTY;
// Mobility: prefer states with more options and space to maneuver.
int mobility = Coordinate.NCubed - Bit.countOnes(occupiedPositions);
score += mobility * w.MOBILITY_MULTIPLIER;
return score;
}
private static int getScore(int count, int centerCount, int emptyCount, Weights w) {
int baseScore = switch (count) {
case 1 -> w.SCORE_ONE;
case 2 -> w.SCORE_TWO;
case 3 -> w.SCORE_THREE;
case 4 -> w.SCORE_FOUR;
default -> 0;
};
baseScore += centerCount * w.CENTER_MULTIPLIER;
if (count == 2 && emptyCount == 2) {
baseScore += w.OPEN_TWO_BONUS;
}
if (emptyCount == 1 && count == 2) {
// almost a fork point
baseScore += w.NEAR_FORK_BONUS;
}
if (count == 3 && emptyCount == 1) {
baseScore += w.CLOSED_THREE_BONUS;
}
return baseScore;
}
/**
* Evaluates potential forks for the player.
*
* @param playerPositions Bitmask of player's positions.
* @param occupiedPositions Bitmask of occupied positions.
* @return The number of potential forks.
*/
private static int evaluatePotentialForks(long playerPositions, long occupiedPositions, long opponentPositions) {
int forkCount = 0;
long emptySpaces = ~occupiedPositions & FULL_MASK;
while (emptySpaces != 0) {
int pos = Long.numberOfTrailingZeros(emptySpaces);
emptySpaces &= (emptySpaces - 1);
int supportingLines = 0;
for (int lineIdx : LINES_BY_POSITION[pos]) {
Line line = Line.lines[lineIdx];
long mask = line.positions();
if ((mask & opponentPositions) != 0) {
continue; // blocked
}
int playerCount = Bit.countOnes(mask & playerPositions);
int emptyCount = LINE_LENGTH - playerCount - Bit.countOnes(mask & opponentPositions);
if (playerCount == 2 && emptyCount == 2) {
supportingLines++;
}
}
if (supportingLines >= 2) {
forkCount++;
}
}
return forkCount;
}
private static int countImmediateWinningSquares(long playerPositions, long opponentPositions, long occupiedPositions) {
int winningSquares = 0;
long emptySpaces = ~occupiedPositions & FULL_MASK;
while (emptySpaces != 0) {
int pos = Long.numberOfTrailingZeros(emptySpaces);
emptySpaces &= (emptySpaces - 1);
int winningLines = 0;
for (int lineIdx : LINES_BY_POSITION[pos]) {
Line line = Line.lines[lineIdx];
long mask = line.positions();
if ((mask & opponentPositions) != 0) {
continue;
}
int playerCount = Bit.countOnes(mask & playerPositions);
int emptyCount = LINE_LENGTH - playerCount - Bit.countOnes(mask & opponentPositions);
if (playerCount == LINE_LENGTH - 1 && emptyCount == 1) {
winningLines++;
}
}
if (winningLines > 0) {
winningSquares += winningLines;
}
}
return winningSquares;
}
/**
* Evaluates the opponent's potential to create forks in their next move.
*
* @param board The current board state.
* @param opponent The opponent player.
* @param occupiedPositions Bitmask of occupied positions.
* @return The number of potential forks the opponent can create in their next move.
*/
private static int evaluateOpponentPotentialForks(Board board, Player opponent, long occupiedPositions) {
int potentialForks = 0;
// simulate the opponent's move
long emptyPositions = ~occupiedPositions & FULL_MASK;
while (emptyPositions != 0) {
int position = Long.numberOfTrailingZeros(emptyPositions);
emptyPositions &= (emptyPositions - 1);
long newOpponentPositions = (opponent == Player.X)
? Bit.set(board.xPositions, position)
: Bit.set(board.oPositions, position);
long newOccupiedPositions = occupiedPositions | Bit.positionMask(position);
long playerPositions = (opponent == Player.X) ? board.oPositions : board.xPositions;
int forks = evaluatePotentialForks(newOpponentPositions, newOccupiedPositions, playerPositions);
if (forks > 0) {
potentialForks += forks;
}
}
return potentialForks;
}
private static int evaluateDoubleThreats(long playerPositions, long opponentPositions, long occupiedPositions) {
int doubleThreats = 0;
long emptySpaces = ~occupiedPositions & FULL_MASK;
while (emptySpaces != 0) {
int pos = Long.numberOfTrailingZeros(emptySpaces);
emptySpaces &= (emptySpaces - 1);
int threatLines = 0;
for (int lineIdx : LINES_BY_POSITION[pos]) {
Line line = Line.lines[lineIdx];
long mask = line.positions();
if ((mask & opponentPositions) != 0) {
continue;
}
int playerCount = Bit.countOnes(mask & playerPositions);
int emptyCount = LINE_LENGTH - playerCount - Bit.countOnes(mask & opponentPositions);
int newCount = playerCount + 1;
int newEmpty = emptyCount - 1;
if (newCount == 4) {
// winning immediately is even better than a fork
threatLines += 2;
} else if (newCount == 3 && newEmpty == 1) {
threatLines++;
}
}
if (threatLines >= 2) {
doubleThreats++;
}
}
return doubleThreats;
}
private static int evaluateBlockedIntersections(long playerPositions, long opponentPositions) {
int blocked = 0;
long tempPlayer = playerPositions;
while (tempPlayer != 0) {
int pos = Long.numberOfTrailingZeros(tempPlayer);
tempPlayer &= (tempPlayer - 1);
int blockedLines = 0;
for (int lineIdx : LINES_BY_POSITION[pos]) {
Line line = Line.lines[lineIdx];
long mask = line.positions();
long playerMask = mask & playerPositions;
long opponentMask = mask & opponentPositions;
if (playerMask == Bit.positionMask(pos) && Bit.countOnes(opponentMask) == LINE_LENGTH - 2) {
int occupiedCount = Bit.countOnes(playerMask | opponentMask);
int emptyCount = LINE_LENGTH - occupiedCount;
if (emptyCount == 1) {
blockedLines++;
}
}
}
if (blockedLines > 1) {
// occupying this square simultaneously disrupted multiple near-threats
blocked++;
}
}
return blocked;
}
}