-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrecomputedData.java
More file actions
392 lines (322 loc) · 11.7 KB
/
PrecomputedData.java
File metadata and controls
392 lines (322 loc) · 11.7 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
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
import java.util.Arrays;
import java.util.ArrayList;
import java.util.List;
public class PrecomputedData {
public static byte[][][] orthogonalMoveData = precomputeOrthogonalMoves();
public static byte[][][] diagonalMoveData = precomputeDiagonalMoves();
public static byte[][][] queenMoveData = precomputeQueenMoveData();
public static byte[][][] knightMoveData = precomputeKnightMoveData();
public static byte[][][] kingMoveData = precomputeKingMoveData();
public static byte[][][] whitePawnMoveData = precomputeWhitePawnMoveData();
public static byte[][][] blackPawnMoveData = precomputeBlackPawnMoveData();
public static byte[][][] kingCheckData = precomputeKingCheckData();
public static byte[][][] precomputeOrthogonalMoves() {
byte[][][] orthogonalMoveData = new byte[64][4][];
for (byte i = 0; i < 64; i++) {
orthogonalMoveData[i] = calculateOrthogonalFromIndex(i);
}
return orthogonalMoveData;
}
public static byte[][][] precomputeDiagonalMoves() {
byte[][][] diagonalMoveData = new byte[64][4][];
for (byte i = 0; i < 64; i++) {
diagonalMoveData[i] = calculateDiagonalFromIndex(i);
}
return diagonalMoveData;
}
public static byte[][][] precomputeQueenMoveData() {
byte[][][] queenMoveData = new byte[64][8][];
for (byte i = 0; i < 64; i++) {
queenMoveData[i] = calculateQueenMovesFromIndex(i);
}
return queenMoveData;
}
public static byte[][][] precomputeKnightMoveData() {
byte[][][] knightMoveData = new byte[64][8][];
for (byte i = 0; i < 64; i++) {
knightMoveData[i] = calculateKnightMovesFromIndex(i);
}
return knightMoveData;
}
public static byte[][][] precomputeKingMoveData() {
byte[][][] kingMoveData = new byte[64][8][];
for (byte i = 0; i < 64; i++) {
kingMoveData[i] = calculateKingMovesFromIndex(i);
}
return kingMoveData;
}
public static byte[][][] precomputeWhitePawnMoveData() {
byte[][][] whitePawnMoveData = new byte[64][8][];
for (byte i = 0; i < 64; i++) {
whitePawnMoveData[i] = calculateWhitePawnMovesFromIndex(i);
}
return whitePawnMoveData;
}
public static byte[][][] precomputeBlackPawnMoveData() {
byte[][][] blackPawnMoveData = new byte[64][8][];
for (byte i = 0; i < 64; i++) {
blackPawnMoveData[i] = calculateBlackPawnMovesFromIndex(i);
}
return blackPawnMoveData;
}
public static byte[][][] precomputeKingCheckData() {
byte[][][] kingCheckData = new byte[64][16][];
for (byte i = 0; i < 64; i++) {
kingCheckData[i] = calculateKingCheckDataFromIndex(i);
}
return kingCheckData;
}
public static byte[][] calculateOrthogonalFromIndex(byte index) {
byte column = (byte) (Math.floor(index / 8));
byte row = (byte) (index % 8);
byte[] right = new byte[7 - row];
for (byte i = 1; i < 8 - row; i++) {
right[i - 1] = (byte) (i + row + (column * 8));
}
byte[] left = new byte[row];
for (byte i = 1; i - 1 < row; i++) {
left[i - 1] = (byte) (-i + row + (column * 8));
}
byte[] up = new byte[column];
for (byte i = 1; i - 1 < column; i++) {
up[i - 1] = (byte) (row + ((column + -i) * 8));
}
byte[] down = new byte[7 - column];
for (byte i = 1; i < 8 - column; i++) {
down[i - 1] = (byte) (row + ((column + i) * 8));
}
return new byte[][] { up, down, left, right };
}
public static byte[][] calculateDiagonalFromIndex(byte index) {
byte column = (byte) (Math.floor(index / 8));
byte row = (byte) (index % 8);
byte[] upRight = new byte[Math.min(7 - column, 7 - row)];
for (byte i = 1; i <= Math.min(7 - column, 7 - row); i++) {
upRight[i - 1] = (byte) (index + i * 8 + i);
}
byte[] upLeft = new byte[Math.min(7 - column, row)];
for (byte i = 1; i <= Math.min(7 - column, row); i++) {
upLeft[i - 1] = (byte) (index + i * 8 - i);
}
byte[] downRight = new byte[Math.min(column, 7 - row)];
for (byte i = 1; i <= Math.min(column, 7 - row); i++) {
downRight[i - 1] = (byte) (index - i * 8 + i);
}
byte[] downLeft = new byte[Math.min(column, row)];
for (byte i = 1; i <= Math.min(column, row); i++) {
downLeft[i - 1] = (byte) (index - i * 8 - i);
}
return new byte[][] { upRight, upLeft, downRight, downLeft };
}
public static byte[][] calculateQueenMovesFromIndex(byte index) {
byte[][] queenMoves = new byte[8][];
byte[][] orthogonal = calculateOrthogonalFromIndex(index);
queenMoves[0] = orthogonal[0];
queenMoves[1] = orthogonal[1];
queenMoves[2] = orthogonal[2];
queenMoves[3] = orthogonal[3];
byte[][] diagonal = calculateDiagonalFromIndex(index);
queenMoves[4] = diagonal[0];
queenMoves[5] = diagonal[1];
queenMoves[6] = diagonal[2];
queenMoves[7] = diagonal[3];
return queenMoves;
}
public static byte[][] calculateKnightMovesFromIndex(byte index) {
byte[][] knightMoves = new byte[8][];
byte[] xOffset = { 2, 1, -1, -2, -2, -1, 1, 2 };
byte[] yOffset = { 1, 2, 2, 1, -1, -2, -2, -1 };
byte row = (byte) (Math.floor(index / 8)); // Assuming a 1-dimensional board is mapped to 8x8 grid.
byte col = (byte) (index % 8);
for (byte i = 0; i < 8; i++) {
byte newRow = (byte) (row + yOffset[i]);
byte newCol = (byte) (col + xOffset[i]);
// Check if the new position is within the board boundaries
if (newRow >= 0 && newRow < 8 && newCol >= 0 && newCol < 8) {
byte newIndex = (byte) (newRow * 8 + newCol);
byte[] moveArray = { newIndex };
knightMoves[i] = moveArray;
} else {
knightMoves[i] = new byte[0];
}
}
return knightMoves;
}
public static byte[][] calculateKingMovesFromIndex(byte index) {
byte[][] kingMoves = new byte[8][];
byte[] xOffset = { 1, 1, 1, -1, -1, -1, 0, 0 };
byte[] yOffset = { 1, 0, -1, 1, 0, -1, 1, -1 };
byte row = (byte) (Math.floor(index / 8)); // Assuming a 1-dimensional board is mapped to 8x8 grid.
byte col = (byte) (index % 8);
for (byte i = 0; i < 8; i++) {
byte newRow = (byte) (row + yOffset[i]);
byte newCol = (byte) (col + xOffset[i]);
// Check if the new position is within the board boundaries
if (newRow >= 0 && newRow < 8 && newCol >= 0 && newCol < 8) {
byte newIndex = (byte) (newRow * 8 + newCol);
byte[] moveArray = { newIndex };
kingMoves[i] = moveArray;
} else {
kingMoves[i] = new byte[0];
}
}
return kingMoves;
}
public static byte[][] calculateWhitePawnMovesFromIndex(byte index) {
byte[][] whitePawnMoves = new byte[5][];
byte[] xOffset = { 0, 0 };
byte[] yOffset = { -1, -2 };
byte row = (byte) (index / 8);
byte col = (byte) (index % 8);
byte moveForwardCount = (row == 6) ? (byte) (2) : (byte) (1);
byte[] pawnMovesForward = new byte[moveForwardCount];
// Forward Moves
for (byte i = 0; i < moveForwardCount; i++) {
byte newRow = (byte) (row + yOffset[i]);
byte newCol = (byte) (col + xOffset[i]);
// Check if the new position is within the board boundaries
if (newRow >= 0 && newRow < 8 && newCol >= 0 && newCol < 8) {
byte newIndex = (byte) (newRow * 8 + newCol);
pawnMovesForward[i] = newIndex;
} else {
pawnMovesForward = new byte[0];
break;
}
}
whitePawnMoves[0] = pawnMovesForward;
// Check for enPassant
byte checkRightCol = (byte) (col + 1);
if (checkRightCol >= 0 && checkRightCol < 8) {
byte newIndex = (byte) (row * 8 + checkRightCol);
byte[] moveArray = { newIndex };
whitePawnMoves[1] = moveArray;
} else {
whitePawnMoves[1] = new byte[0];
}
byte checkLeftCol = (byte) (col - 1);
if (checkLeftCol >= 0 && checkLeftCol < 8) {
byte newIndex = (byte) (row * 8 + checkLeftCol);
byte[] moveArray = { newIndex };
whitePawnMoves[2] = moveArray;
} else {
whitePawnMoves[2] = new byte[0];
}
byte diaAtkLeftRow = (byte) (row - 1);
byte diaAtkLeftCol = (byte) (col - 1);
if (diaAtkLeftRow >= 0 && diaAtkLeftRow < 8 && diaAtkLeftCol >= 0 && diaAtkLeftCol < 8) {
byte newIndex = (byte) (diaAtkLeftRow * 8 + diaAtkLeftCol);
byte[] moveArray = { newIndex };
whitePawnMoves[3] = moveArray;
} else {
whitePawnMoves[3] = new byte[0];
}
byte diaAtkRightRow = (byte) (row - 1);
byte diaAtkRightCol = (byte) (col + 1);
if (diaAtkRightRow >= 0 && diaAtkRightRow < 8 && diaAtkRightCol >= 0 && diaAtkRightCol < 8) {
byte newIndex = (byte) (diaAtkRightRow * 8 + diaAtkRightCol);
byte[] moveArray = { newIndex };
whitePawnMoves[4] = moveArray;
} else {
whitePawnMoves[4] = new byte[0];
}
return whitePawnMoves;
}
public static byte[][] calculateBlackPawnMovesFromIndex(byte index) {
byte[][] blackPawnMoves = new byte[5][];
byte[] xOffset = { 0, 0 };
byte[] yOffset = { 1, 2 };
byte row = (byte) (index / 8);
byte col = (byte) (index % 8);
byte moveForwardCount = (row == 1) ? (byte) (2) : (byte) (1);
byte[] pawnMovesForward = new byte[moveForwardCount];
// Forward Moves
for (byte i = 0; i < moveForwardCount; i++) {
byte newRow = (byte) (row + yOffset[i]);
byte newCol = (byte) (col + xOffset[i]);
// Check if the new position is within the board boundaries
if (newRow >= 0 && newRow < 8 && newCol >= 0 && newCol < 8) {
byte newIndex = (byte) (newRow * 8 + newCol);
pawnMovesForward[i] = newIndex;
} else {
pawnMovesForward = new byte[0];
break;
}
}
blackPawnMoves[0] = pawnMovesForward;
// Check for enPassant
byte checkRightCol = (byte) (col + 1);
if (checkRightCol >= 0 && checkRightCol < 8) {
byte newIndex = (byte) (row * 8 + checkRightCol);
byte[] moveArray = { newIndex };
blackPawnMoves[1] = moveArray;
} else {
blackPawnMoves[1] = new byte[0];
}
byte checkLeftCol = (byte) (col - 1);
if (checkLeftCol >= 0 && checkLeftCol < 8) {
byte newIndex = (byte) (row * 8 + checkLeftCol);
byte[] moveArray = { newIndex };
blackPawnMoves[2] = moveArray;
} else {
blackPawnMoves[2] = new byte[0];
}
// a
byte diaAtkLeftRow = (byte) (row + 1);
byte diaAtkLeftCol = (byte) (col - 1);
if (diaAtkLeftRow >= 0 && diaAtkLeftRow < 8 && diaAtkLeftCol >= 0 && diaAtkLeftCol < 8) {
byte newIndex = (byte) (diaAtkLeftRow * 8 + diaAtkLeftCol);
byte[] moveArray = { newIndex };
blackPawnMoves[3] = moveArray;
} else {
blackPawnMoves[3] = new byte[0];
}
byte diaAtkRightRow = (byte) (row + 1);
byte diaAtkRightCol = (byte) (col + 1);
if (diaAtkRightRow >= 0 && diaAtkRightRow < 8 && diaAtkRightCol >= 0 && diaAtkRightCol < 8) {
byte newIndex = (byte) (diaAtkRightRow * 8 + diaAtkRightCol);
byte[] moveArray = { newIndex };
blackPawnMoves[4] = moveArray;
} else {
blackPawnMoves[4] = new byte[0];
}
return blackPawnMoves;
}
public static byte[][] calculateKingCheckDataFromIndex(byte index) {
byte[][] kingCheckData = new byte[28][];
byte[][] orthogonal = calculateOrthogonalFromIndex(index);
kingCheckData[0] = orthogonal[0];
kingCheckData[1] = orthogonal[1];
kingCheckData[2] = orthogonal[2];
kingCheckData[3] = orthogonal[3];
byte[][] diagonal = calculateDiagonalFromIndex(index);
kingCheckData[4] = diagonal[0];
kingCheckData[5] = diagonal[1];
kingCheckData[6] = diagonal[2];
kingCheckData[7] = diagonal[3];
byte[][] knight = calculateKnightMovesFromIndex(index);
kingCheckData[8] = knight[0];
kingCheckData[9] = knight[1];
kingCheckData[10] = knight[2];
kingCheckData[11] = knight[3];
kingCheckData[12] = knight[4];
kingCheckData[13] = knight[5];
kingCheckData[14] = knight[6];
kingCheckData[15] = knight[7];
byte[][] king = calculateKingMovesFromIndex(index);
kingCheckData[16] = king[0];
kingCheckData[17] = king[1];
kingCheckData[18] = king[2];
kingCheckData[19] = king[3];
kingCheckData[20] = king[4];
kingCheckData[21] = king[5];
kingCheckData[22] = king[6];
kingCheckData[23] = king[7];
// Pawn Attacks are really just the diagonal king attacks :}
kingCheckData[24] = king[5];
kingCheckData[25] = king[2];
// Above are black, below are white
kingCheckData[26] = king[3];
kingCheckData[27] = king[0];
return kingCheckData;
}
}