-
Notifications
You must be signed in to change notification settings - Fork 0
/
MoveGeneratorLUT.cpp
427 lines (349 loc) · 12.9 KB
/
MoveGeneratorLUT.cpp
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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
#include "chess.h"
// generates sliding moves using a lookup table (only for bishop, rook and queen)
// moves for other pieces are generated using the 088 move generator
MoveLUTItem MoveGeneratorLUT::slidingMoveTable[1456+896+560]; // for queen, rook and bishop
uint32 MoveGeneratorLUT::slidingModeStart[3][64]; // start indices from all board positions for all sliding pieces
// adds the temp. moves generated by 088 MoveGenerator to the lookup table
void MoveGeneratorLUT::addGeneratedMoves(uint32 &lutIndex, uint32 next1)
{
uint32 n = MoveGenerator::nMoves;
next1 = lutIndex + n;
for (uint32 j=0; j < n; j++)
{
slidingMoveTable[lutIndex].tosq = MoveGenerator::moves[j].dst;
slidingMoveTable[lutIndex].next0 = lutIndex + 1;
slidingMoveTable[lutIndex].next1 = next1;
lutIndex++;
}
assert((lutIndex == next1));
MoveGenerator::nMoves = 0;
}
// initialize the look up tables used for table driven sliding move generation
void MoveGeneratorLUT::init()
{
uint32 lutIndex = 0;
BoardPosition pos;
pos.chance = 0;
Move tempMoves[MAX_SINGLE_PIECE_MOVES];
// initialize the 088 MoveGenerator to help generating the look up table
MoveGenerator::pos = &pos;
MoveGenerator::chance = 0;
MoveGenerator::moves = tempMoves;
MoveGenerator::nMoves = 0;
// 1. bishop moves
for (uint32 i=0; i < 64; i++)
{
uint32 rank = i / 8;
uint32 file = i % 8;
slidingModeStart[SLIDING_PIECE_INDEX(BISHOP)][i] = lutIndex;
// clear the board and place a single piece on it at the location 'i'
Utils::clearBoard(&pos);
uint32 curPos = INDEX088(rank, file);
pos.board[curPos] = COLOR_PIECE(WHITE, BISHOP);
uint32 overWriteNext1FromHere = lutIndex;
MoveGenerator::generateSlidingMoves(curPos, 0xf); // north-west
addGeneratedMoves(lutIndex, 0);
MoveGenerator::generateSlidingMoves(curPos, 0x11); // north-east
if (MoveGenerator::nMoves) overWriteNext1FromHere = lutIndex;
addGeneratedMoves(lutIndex, 0);
MoveGenerator::generateSlidingMoves(curPos, -0x11); // south-west
if (MoveGenerator::nMoves) overWriteNext1FromHere = lutIndex;
addGeneratedMoves(lutIndex, 0);
MoveGenerator::generateSlidingMoves(curPos, -0xf); // south-east
if (MoveGenerator::nMoves) overWriteNext1FromHere = lutIndex;
addGeneratedMoves(lutIndex, 0);
for(uint32 j = overWriteNext1FromHere; j<lutIndex; j++)
slidingMoveTable[j].next1 = 0;
slidingMoveTable[lutIndex - 1].next0 = 0;
}
// 2. rook moves
for (uint32 i=0; i < 64; i++)
{
uint32 rank = i / 8;
uint32 file = i % 8;
slidingModeStart[SLIDING_PIECE_INDEX(ROOK)][i] = lutIndex;
// clear the board and place a single piece on it at the location 'i'
Utils::clearBoard(&pos);
uint32 curPos = INDEX088(rank, file);
pos.board[curPos] = COLOR_PIECE(WHITE, ROOK);
uint32 overWriteNext1FromHere = lutIndex;
MoveGenerator::generateSlidingMoves(curPos, 0x10); // up
addGeneratedMoves(lutIndex, 0);
MoveGenerator::generateSlidingMoves(curPos, -0x10); // down
if (MoveGenerator::nMoves) overWriteNext1FromHere = lutIndex;
addGeneratedMoves(lutIndex, 0);
MoveGenerator::generateSlidingMoves(curPos, 0x1); // right
if (MoveGenerator::nMoves) overWriteNext1FromHere = lutIndex;
addGeneratedMoves(lutIndex, 0);
MoveGenerator::generateSlidingMoves(curPos, -0x1); // left
if (MoveGenerator::nMoves) overWriteNext1FromHere = lutIndex;
addGeneratedMoves(lutIndex, 0);
for(uint32 j = overWriteNext1FromHere; j<lutIndex; j++)
slidingMoveTable[j].next1 = 0;
slidingMoveTable[lutIndex - 1].next0 = 0;
}
// 3. queen moves
for (uint32 i=0; i < 64; i++)
{
uint32 rank = i / 8;
uint32 file = i % 8;
slidingModeStart[SLIDING_PIECE_INDEX(QUEEN)][i] = lutIndex;
// clear the board and place a single piece on it at the location 'i'
Utils::clearBoard(&pos);
uint32 curPos = INDEX088(rank, file);
pos.board[curPos] = COLOR_PIECE(WHITE, QUEEN);
uint32 overWriteNext1FromHere = lutIndex;
MoveGenerator::generateSlidingMoves(curPos, 0x10); // up
addGeneratedMoves(lutIndex, 0);
MoveGenerator::generateSlidingMoves(curPos, -0x10); // down
if (MoveGenerator::nMoves) overWriteNext1FromHere = lutIndex;
addGeneratedMoves(lutIndex, 0);
MoveGenerator::generateSlidingMoves(curPos, 0x1); // right
if (MoveGenerator::nMoves) overWriteNext1FromHere = lutIndex;
addGeneratedMoves(lutIndex, 0);
MoveGenerator::generateSlidingMoves(curPos, -0x1); // left
if (MoveGenerator::nMoves) overWriteNext1FromHere = lutIndex;
addGeneratedMoves(lutIndex, 0);
MoveGenerator::generateSlidingMoves(curPos, 0xf); // north-west
if (MoveGenerator::nMoves) overWriteNext1FromHere = lutIndex;
addGeneratedMoves(lutIndex, 0);
MoveGenerator::generateSlidingMoves(curPos, 0x11); // north-east
if (MoveGenerator::nMoves) overWriteNext1FromHere = lutIndex;
addGeneratedMoves(lutIndex, 0);
MoveGenerator::generateSlidingMoves(curPos, -0x11); // south-west
if (MoveGenerator::nMoves) overWriteNext1FromHere = lutIndex;
addGeneratedMoves(lutIndex, 0);
MoveGenerator::generateSlidingMoves(curPos, -0xf); // south-east
if (MoveGenerator::nMoves) overWriteNext1FromHere = lutIndex;
addGeneratedMoves(lutIndex, 0);
for(uint32 j = overWriteNext1FromHere; j<lutIndex; j++)
slidingMoveTable[j].next1 = 0;
slidingMoveTable[lutIndex - 1].next0 = 0;
}
//printf("lutIndex is now: %d\n", lutIndex);
}
#if 0
void MoveGeneratorLUT::generateSlidingMoves(uint32 origPiece, uint32 index88, uint32 index)
{
uint32 side2moveBit = chance + 1;
uint32 *table = (uint32 *) slidingMoveTable;
// taken from http://chessprogramming.wikispaces.com/Table-driven+Move+Generation
uint32 slidingPiece = SLIDING_PIECE_INDEX(origPiece);
uint32 node = slidingModeStart[slidingPiece][index];
do {
node = table[node];
uint32 tosq = node & 0xFF; // MoveLUTItem.tosq
uint32 piece = pos->board[tosq];
moves[nMoves].capturedPiece = piece;
moves[nMoves].dst = tosq;
moves[nMoves].flags = 0;
moves[nMoves].src = index88;
nMoves += (piece & side2moveBit) == 0; // add the move only if it was empty or opponent square
node = (node >> (piece & 0x1C)) & 0xFFF; // magic to jump to correct next index
}
while (node);
}
#endif
// this more readable version is actually a bit faster :-/
// uselss magic jumps and strance encoded bits (at least on x86)
void MoveGeneratorLUT::generateSlidingMoves(uint32 origPiece, uint32 index88, uint32 index)
{
uint32 side2moveBit = chance + 1;
// taken from http://chessprogramming.wikispaces.com/Table-driven+Move+Generation
uint32 slidingPiece = SLIDING_PIECE_INDEX(origPiece);
uint32 lutIndex = slidingModeStart[slidingPiece][index];
do {
MoveLUTItem node = slidingMoveTable[lutIndex];
uint32 tosq = node.tosq;
uint32 piece = pos->board[tosq];
moves[nMoves].capturedPiece = piece;
moves[nMoves].dst = tosq;
moves[nMoves].flags = 0;
moves[nMoves].src = index88;
nMoves += (piece & side2moveBit) == 0; // add the move only if it was empty or opponent square
lutIndex = ISEMPTY(piece) ? node.next0 : node.next1;
}
while (lutIndex);
}
int MoveGeneratorLUT::generateMoves (BoardPosition *position, Move *generatedMoves)
{
pos = position;
moves = generatedMoves;
nMoves = 0;
chance = pos->chance;
// still uses code from 0x88 move generator
// for non-sliding pieces
// TODO: unify everything to use lookup tables
// for knight move and king's normal moves it should be simple
// special handling for pawn and king special moves needed
// loop through all the squares in the board
// TODO: maybe keep a list of squares containing white and black pieces?
uint32 i, j;
for (i = 0; i < 8; i++)
{
for (j = 0; j < 8; j++)
{
uint32 index088 = INDEX088(i, j);
uint32 colorpiece = position->board[index088];
if(IS_OF_COLOR(colorpiece, chance))
{
uint32 piece = PIECE(colorpiece);
switch(piece)
{
case BISHOP:
case ROOK:
case QUEEN:
generateSlidingMoves(piece, index088, i*8 | j);
break;
case PAWN:
generatePawnMoves(index088);
break;
case KNIGHT:
generateKnightMoves(index088);
break;
case KING:
generateKingMoves(index088);
break;
}
}
}
}
return nMoves;
}
// static members have to be defined seperately.. what the crap! ?
BoardPosition *MoveGeneratorLUT::pos;
Move *MoveGeneratorLUT::moves;
uint32 MoveGeneratorLUT::nMoves;
uint32 MoveGeneratorLUT::chance;
// TODO: get rid of everything below this by using lookup table for pawn, knight and king too
void MoveGeneratorLUT::addMove(uint32 src, uint32 dst, uint8 oldPiece, uint8 flags)
{
moves[nMoves].src = (uint8) src;
moves[nMoves].dst = (uint8) dst;
moves[nMoves].capturedPiece = (uint8) oldPiece;
moves[nMoves].flags = flags;
nMoves++;
}
void MoveGeneratorLUT::addPromotions(uint32 src, uint32 dst, uint8 oldPiece)
{
addMove(src, dst, oldPiece, PROMOTION_QUEEN);
addMove(src, dst, oldPiece, PROMOTION_KNIGHT);
addMove(src, dst, oldPiece, PROMOTION_ROOK);
addMove(src, dst, oldPiece, PROMOTION_BISHOP);
}
void MoveGeneratorLUT::generatePawnMoves(uint32 curPos)
{
uint32 finalRank = chance ? 0 : 7;
uint32 curRank = RANK(curPos);
// pawn advancement
// single square forward
uint32 offset = chance ? -16 : 16;
uint32 newPos = curPos + offset;
uint32 newRank = RANK(newPos);
if (ISVALIDPOS(newPos) && ISEMPTY(pos->board[newPos]))
{
if(newRank == finalRank)
{
// promotion
addPromotions(curPos, newPos, EMPTY_SQUARE);
}
else
{
addMove(curPos, newPos, EMPTY_SQUARE, 0);
// two squares forward
uint32 startRank = chance ? 6 : 1;
if (curRank == startRank)
{
newPos += offset;
if (ISEMPTY(pos->board[newPos]))
addMove(curPos, newPos, EMPTY_SQUARE, 0);
}
}
}
// captures
offset = chance ? -15 : 15;
newPos = curPos + offset;
uint32 capturedPiece = pos->board[newPos];
if (ISVALIDPOS(newPos) && IS_ENEMY_COLOR(capturedPiece, chance))
{
if(newRank == finalRank)
{
// promotion
addPromotions(curPos, newPos, capturedPiece);
}
else
{
addMove(curPos, newPos, capturedPiece, 0);
}
}
offset = chance ? -17 : 17;
newPos = curPos + offset;
capturedPiece = pos->board[newPos];
if (ISVALIDPOS(newPos) && IS_ENEMY_COLOR(capturedPiece, chance))
{
if(newRank == finalRank)
{
// promotion
addPromotions(curPos, newPos, capturedPiece);
}
else
{
addMove(curPos, newPos, capturedPiece, 0);
}
}
// En-passent
if (pos->enPassent)
{
uint32 enPassentFile = pos->enPassent - 1;
uint32 enPassentRank = chance ? 4 : 3;
if ((curRank == enPassentRank) && (abs(FILE(curPos) - enPassentFile) == 1))
{
uint32 finalRank = chance ? 5 : 2;
newPos = INDEX088(finalRank, enPassentFile);
addMove(curPos, newPos, COLOR_PIECE(!chance, PAWN), EN_PASSENT);
}
}
}
void MoveGeneratorLUT::generateOffsetedMove(uint32 curPos, uint32 offset)
{
uint32 newPos = curPos + offset;
if(ISVALIDPOS(newPos))
{
uint8 capturedPiece = pos->board[newPos];
if (!IS_OF_COLOR(capturedPiece, chance))
addMove(curPos, newPos, capturedPiece, 0);
}
}
void MoveGeneratorLUT::generateOffsetedMoves(uint32 curPos, const uint32 jumpTable[], int n)
{
for (int i = 0; i < n; i++)
{
generateOffsetedMove(curPos, jumpTable[i]);
}
}
void MoveGeneratorLUT::generateKnightMoves(uint32 curPos)
{
const uint32 jumpTable[] = {0x1F, 0x21, 0xE, 0x12, -0x12, -0xE, -0x21, -0x1F};
generateOffsetedMoves(curPos, jumpTable, 8);
}
void MoveGeneratorLUT::generateKingMoves(uint32 curPos)
{
// normal moves
const uint32 jumpTable[] = {0xF, 0x10, 0x11, 0x1, -0x1, -0x11, -0x10, -0xF};
generateOffsetedMoves(curPos, jumpTable, 8);
// castling
uint32 castleFlag = chance ? pos->blackCastle : pos->whiteCastle ;
if ((castleFlag & CASTLE_FLAG_KING_SIDE) && ISEMPTY(pos->board[curPos+1]) && ISEMPTY(pos->board[curPos+2]))
{
// no need to check for king's and rook's position as if they have moved, the castle flag would be zero
// TODO: check if any of the squares involved is in check!
addMove(curPos, curPos + 0x2, EMPTY_SQUARE, CASTLE_KING_SIDE);
}
if ((castleFlag & CASTLE_FLAG_QUEEN_SIDE) && ISEMPTY(pos->board[curPos-1]) &&
ISEMPTY(pos->board[curPos-2]) && ISEMPTY(pos->board[curPos-3]))
{
// no need to check for king's and rook's position as if they have moved, the castle flag would be zero
// TODO: check if any of the squares involved is in check!
addMove(curPos, curPos - 0x2, EMPTY_SQUARE, CASTLE_QUEEN_SIDE);
}
}