-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPreProcessing.cs
More file actions
347 lines (294 loc) · 13.1 KB
/
PreProcessing.cs
File metadata and controls
347 lines (294 loc) · 13.1 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
using System;
using System.Collections;
using System.Collections.Generic;
namespace QuickChess
{
public enum Direction
{
North,
South,
East,
West,
NorthEast,
NorthWest,
SouthEast,
SouthWest,
None
};
public class PreProcessing
{
public static UInt64[,] directions;
public static Direction[,] directionBetween;
public static UInt64[,] squaresBetween;
public static UInt64[] precomputedKnightAttacks;
public static UInt64[] precomputedBlackPawnMoves;
public static UInt64[] precomputedWhitePawnMoves;
public static UInt64[] precomputedBlackPawnAttacks;
public static UInt64[] precomputedWhitePawnAttacks;
public static UInt64[] precomputedKingMoves;
public static UInt64[] precomputedBlackKingCastling;
public static UInt64[] precomputedWhiteKingCastling;
public const int whiteKingSquare = 4;
public const int blackKingSquare = 60;
public const int whiteKingSideCastle = 6;
public const int whiteQueenSideCastle = 2;
public const int blackKingSideCastle = 62;
public const int blackQueenSideCastle = 58;
public const int PAWN = 0;
public const int ROOK = 1;
public const int KNIGHT = 2;
public const int BISHOP = 3;
public const int QUEEN = 4;
public const int KING = 5;
public static int[] Sliders = {
0, 1, 0, 1, 1, 0
};
public static int[] Diagonals = {
0, 0, 0, 0, 1, 1, 1, 1
};
public static int[] Upwards = {
1, 0, 1, 0, 1, 1, 0, 0
};
public const UInt64 kingSideMask = (1UL << whiteKingSideCastle) | (1UL << blackKingSideCastle);
public const UInt64 queenSideMask = (1UL << whiteQueenSideCastle) | (1UL << blackQueenSideCastle);
public const UInt64 whiteKingSidePieceCheckingMask = (1UL << 5) | (1UL << 6);
public const UInt64 whiteQueenSidePieceCheckingMask = (1UL << 1) | (1UL << 2) | (1UL << 3);
public const UInt64 blackKingSidePieceCheckingMask = (1UL << 61) | (1UL << 62);
public const UInt64 blackQueenSidePieceCheckingMask = (1UL << 57) | (1UL << 58) | (1UL << 59);
public const int UP = 8;
public const int DOWN = -8;
public const int RIGHT = 1;
public const int LEFT = -1;
public static Dictionary<Direction, int[]> directionOffsets = new Dictionary<Direction, int[]>() {
{Direction.North, new int[] {0, 1}},
{Direction.South, new int[] {0, -1}},
{Direction.East, new int[] {1, 0}},
{Direction.West, new int[] {-1, 0}},
{Direction.NorthEast, new int[] {1, 1}},
{Direction.NorthWest, new int[] {-1, 1}},
{Direction.SouthEast, new int[] {1, -1}},
{Direction.SouthWest, new int[] {-1, -1}}
};
public static void PreProcess()
{
directions = new UInt64[8,64];
squaresBetween = new UInt64[64,64];
directionBetween = new Direction[64,64];
precomputedKnightAttacks = new UInt64[64];
precomputedWhitePawnMoves = new UInt64[64];
precomputedBlackPawnMoves = new UInt64[64];
precomputedWhitePawnAttacks = new UInt64[64];
precomputedBlackPawnAttacks = new UInt64[64];
precomputedKingMoves = new UInt64[64];
precomputedBlackKingCastling = new UInt64[64];
precomputedWhiteKingCastling = new UInt64[64];
UInt64 One = (UInt64) 1;
for (int x0 = 0; x0 < 8; x0 ++)
{
for (int y0 = 0; y0 < 8; y0 ++)
{
int i0 = y0 * 8 + x0;
for (int x1 = 0; x1 < 8; x1 ++)
{
for (int y1 = 0; y1 < 8; y1 ++)
{
int i1 = y1 * 8 + x1;
if (i0 == i1) continue;
bool isDiagonal = IsDiagonal (x0, y0, x1, y1);
bool isHV = IsHV (x0, y0, x1, y1);
if (!(isDiagonal || isHV)) {
directionBetween[i0,i1] = Direction.None;
squaresBetween[i0,i1] = 0UL;
continue;
}
if (isDiagonal && y1 > y0 && x1 > x0)
{
directionBetween[i0,i1] = Direction.NorthEast;
} if (isDiagonal && y1 > y0 && x1 < x0)
{
directionBetween[i0,i1] = Direction.NorthWest;
} if (isDiagonal && y1 < y0 && x1 > x0)
{
directionBetween[i0,i1] = Direction.SouthEast;
} if (isDiagonal && y1 < y0 && x1 < x0)
{
directionBetween[i0,i1] = Direction.SouthWest;
}
if (isHV && y1 > y0)
{
directionBetween[i0,i1] = Direction.North;
} if (isHV && y1 < y0)
{
directionBetween[i0,i1] = Direction.South;
} if (isHV && x1 > x0)
{
directionBetween[i0,i1] = Direction.East;
} if (isHV && x1 < x0)
{
directionBetween[i0,i1] = Direction.West;
}
Direction dirBetween = directionBetween[i0,i1];
UInt64 binary = 0;
int offsetX = directionOffsets[dirBetween][0];
int offsetY = directionOffsets[dirBetween][1];
int tx = x0 + offsetX;
int ty = y0 + offsetY;
while ((tx != x1) || (ty != y1))
{
int ti = ty * 8 + tx;
binary |= One << ti;
tx += offsetX;
ty += offsetY;
}
squaresBetween[i0,i1] = binary;
}
}
}
}
for (int x = 0; x < 8; x ++)
{
for (int y = 0; y < 8; y ++)
{
int i = y * 8 + x;
// Get cardinal directions
for (int d = 0; d < 8; d ++)
{
UInt64 binary = 0;
int offsetX = directionOffsets[(Direction)d][0];
int offsetY = directionOffsets[(Direction)d][1];
int tx = x + offsetX;
int ty = y + offsetY;
while ((tx >= 0 && tx < 8) && (ty >= 0 && ty < 8))
{
int ti = ty * 8 + tx;
binary |= One << ti;
tx += offsetX;
ty += offsetY;
}
directions[d,i] = binary;
}
// Get knight moves
/*
. . . . . . . .
. . 1 . 1 . . .
. 1 . . . 1 . .
. . . N . . . .
. 1 . . . 1 . .
. . 1 . 1 . . .
. . . . . . . .
. . . . . . . .
*/
#region KnightCalculations
UInt64 knightMoves = 0;
if (y < 6 && x < 7)
{
knightMoves |= 1UL << (i + (UP * 2)) + (RIGHT * 1);
}
if (y < 6 && x > 0)
{
knightMoves |= 1UL << (i + (UP * 2)) - (RIGHT * 1);
} if (y >= 2 && x < 7)
{
knightMoves |= 1UL << (i - (UP * 2)) + (RIGHT * 1);
} if (y >= 2 && x > 0)
{
knightMoves |= 1UL << (i - (UP * 2)) - (RIGHT * 1);
} if (y < 7 && x < 6)
{
knightMoves |= 1UL << (i + (UP * 1)) + (RIGHT * 2);
} if (y < 7 && x >= 2)
{
knightMoves |= 1UL << (i + (UP * 1)) - (RIGHT * 2);
} if (y > 0 && x < 6)
{
knightMoves |= 1UL << (i - (UP * 1)) + (RIGHT * 2);
} if (y > 0 && x >= 2)
{
knightMoves |= 1UL << (i - (UP * 1)) - (RIGHT * 2);
}
precomputedKnightAttacks[i] = knightMoves;
#endregion
#region Pawns
UInt64 whitePawnMoves = 0;
UInt64 blackPawnMoves = 0;
UInt64 whitePawnAttacks = 0;
UInt64 blackPawnAttacks = 0;
if (y < 7 && x < 7)
{
whitePawnAttacks |= 1UL << i + UP + RIGHT;
} if (y < 7 && x > 0)
{
whitePawnAttacks |= 1UL << i + UP - RIGHT;
} if (y > 0 && x < 7)
{
blackPawnAttacks |= 1UL << i - UP + RIGHT;
} if (y > 0 && x > 0)
{
blackPawnAttacks |= 1UL << i - UP - RIGHT;
}
if (y < 7)
{
whitePawnMoves |= 1UL << i + UP;
} if (y > 0)
{
blackPawnMoves |= 1UL << i + DOWN;
}
if (y == 1)
{
whitePawnMoves |= 1UL << i + UP * 2;
} if (y == 6)
{
blackPawnMoves |= 1UL << i + DOWN * 2;
}
precomputedWhitePawnMoves[i] = whitePawnMoves;
precomputedBlackPawnMoves[i] = blackPawnMoves;
precomputedWhitePawnAttacks[i] = whitePawnAttacks;
precomputedBlackPawnAttacks[i] = blackPawnAttacks;
#endregion
#region King
UInt64 kingMoves = 0;
if (y < 7) kingMoves |= 1UL << (i + UP);
if (y > 0) kingMoves |= 1UL << (i + DOWN);
if (x < 7) kingMoves |= 1UL << (i + RIGHT);
if (x > 0) kingMoves |= 1UL << (i + LEFT);
if (y < 7 && x < 7) kingMoves |= 1UL << (i + UP + RIGHT);
if (y > 0 && x > 0) kingMoves |= 1UL << (i + DOWN + LEFT);
if (y < 7 && x > 0) kingMoves |= 1UL << (i + UP + LEFT);
if (y > 0 && x < 7) kingMoves |= 1UL << (i + DOWN + RIGHT);
precomputedKingMoves[i] = kingMoves;
#endregion
#region Castling
UInt64 whiteCastlingValue = 0;
UInt64 blackCastlingValue = 0;
if (i == whiteKingSquare)
{
whiteCastlingValue |= (1UL << whiteKingSideCastle);
whiteCastlingValue |= (1UL << whiteQueenSideCastle);
}
if (i == blackKingSquare)
{
blackCastlingValue |= (1UL << blackKingSideCastle);
blackCastlingValue |= (1UL << blackQueenSideCastle);
}
precomputedWhiteKingCastling[i] = whiteCastlingValue;
precomputedBlackKingCastling[i] = blackCastlingValue;
#endregion
}
}
}
public static bool IsDiagonal (float x0, float y0, float x1, float y1)
{
/*
. . . .
. . . X
. . Y .
. X . Y
*/
return Math.Abs(x1 - x0) == Math.Abs(y1 - y0);
}
public static bool IsHV (float x0, float y0, float x1, float y1)
{
return (x0 == x1) ^ (y0 == y1);
}
}
}