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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
|
#include <array>
#include <cassert>
#include <cstddef>
#include <cstdint>
#include <cstdlib>
#include <iostream>
#include <sys/types.h>
#include <vector>
#include "board/board.hpp"
#include "moves.hpp"
constexpr std::array<std::uint64_t, 64> computeKnightAttacks() {
std::array<std::uint64_t, 64> attacks{};
constexpr std::array<int, 8> knight_moves{-10, 6, 15, 17, 10, -6, -15, -17};
for (int i = 0; i < 64; i++) {
uint64_t bb = 0;
for (int offset : knight_moves) {
int next = i + offset;
if (next >= 64 || next < 0) {
continue;
}
const int fileDelta = (next % 8) - (i % 8);
if (fileDelta != 1 && fileDelta != 2 && fileDelta != -1 &&
fileDelta != -2) {
continue;
};
bb |= 1ULL << next;
}
attacks[static_cast<size_t>(i)] = bb;
}
return attacks;
}
constexpr std::array<std::uint64_t, 64> KNIGHT_ATTACKS = computeKnightAttacks();
static void GenerateKnightMoves(Game *b, std::vector<Move> &moves,
bool GenerateQuietMoves) {
uint64_t knights = b->PieceBitboards[b->turn][KNIGHT];
while (knights != 0) {
int from = __builtin_ctzll(knights);
knights &= knights - 1;
uint64_t knight_attacks = KNIGHT_ATTACKS[static_cast<size_t>(from)];
while (knight_attacks != 0) {
int next = __builtin_ctzll(knight_attacks);
knight_attacks &= knight_attacks - 1;
bool hasFriendlyPiece =
b->turn ? (b->WhitePieceBitboard & (1ULL << next)) > 0
: (b->BlackPieceBitboard & (1ULL << next)) > 0;
bool isCaptuare = (b->PieceBitboard & (1ULL << next)) > 0;
if (hasFriendlyPiece) {
continue;
}
if (!GenerateQuietMoves && !isCaptuare) {
continue;
}
moves.push_back(
{.From = IndexToPosition(from), .To = IndexToPosition(next)});
}
}
};
void GeneratePawnMoves(Game *b, int from, std::vector<Move> &moves,
bool quietMoves) {
Piece pawn = b->pieces[from];
if (pawn.type != PAWN) {
assert(false && "Calling generate pawn moves on non pawn");
return;
}
Position position = IndexToPosition(from);
int step = (pawn.color ? 1 : -1) * 8;
int next = from + step;
// wrap check
if (position.rank + (pawn.color ? 1 : -1) < 0 ||
position.rank + (pawn.color ? 1 : -1) >= 8) {
return;
}
// if piece it want to move to is none and it as legal move
if (b->pieces[next].type == NONEPIECE && quietMoves) {
if (position.rank + (pawn.color ? 1 : -1) == 0 ||
position.rank + (pawn.color ? 1 : -1) == 7) {
moves.push_back({
.From = position,
.To = IndexToPosition(next),
.promotion = QUEEN,
});
moves.push_back({
.From = position,
.To = IndexToPosition(next),
.promotion = ROOK,
});
moves.push_back({
.From = position,
.To = IndexToPosition(next),
.promotion = BISHOP,
});
moves.push_back({
.From = position,
.To = IndexToPosition(next),
.promotion = KNIGHT,
});
} else {
moves.push_back({.From = position, .To = IndexToPosition(next)});
}
int startingRank = pawn.color ? 1 : 6;
int twoSteps = from + (step * 2);
if (position.rank == startingRank &&
b->pieces[twoSteps].type == NONEPIECE) {
moves.push_back({position, IndexToPosition(twoSteps)});
}
};
for (int fileOffset : {-1, 1}) {
int targetFile = position.file + fileOffset;
int targetRank = position.rank + (pawn.color ? 1 : -1);
if (targetFile < 0 || targetFile >= 8) {
continue;
}
int target = (targetRank * 8) + targetFile;
if (IndexToPosition(target) == b->enPassant && b->canEnpassant) {
moves.push_back({.From = position, .To = IndexToPosition(target)});
}
if (b->pieces[target].type != NONEPIECE &&
b->pieces[target].color != pawn.color) {
if (targetRank == 0 || targetRank == 7) {
moves.push_back({
.From = position,
.To = IndexToPosition(target),
.promotion = QUEEN,
});
moves.push_back({.From = position,
.To = IndexToPosition(target),
.promotion = ROOK});
moves.push_back({
.From = position,
.To = IndexToPosition(target),
.promotion = BISHOP,
});
moves.push_back({
.From = position,
.To = IndexToPosition(target),
.promotion = KNIGHT,
});
} else {
moves.push_back({.From = position, .To = IndexToPosition(target)});
moves.push_back({.From = position, .To = IndexToPosition(target)});
}
}
}
};
void GenerateKingMoves(Game *g, int from, std::vector<Move> &moves,
bool GenerateQuietMoves) {
if (g->pieces[from].type != KING) {
assert(false && "calling generate king moves on non king");
return;
}
if (g->pieces[from].color != g->turn) {
assert(false && "calling generate king moves on king of opposite color");
return;
}
constexpr std::array<int, 8> king_moves{-1, 1, 8, -8, -9, 9, -7, 7};
for (int offset : king_moves) {
int next = from + offset;
if (next >= 64 || next < 0) {
continue;
}
if (std::abs((next % 8) - (from % 8)) > 1) {
continue;
};
if (g->pieces[next].type != NONEPIECE) {
if (g->pieces[next].color == g->turn) {
continue;
};
}
if (!GenerateQuietMoves && g->pieces[next].type == NONEPIECE) {
continue;
}
moves.push_back({IndexToPosition(from), IndexToPosition(next)});
}
};
std::vector<Move> GetPseudoLegalMoves(Game *g, bool GenerateQuietMoves) {
std::vector<Move> moves;
moves.reserve(40);
GenerateKnightMoves(g, moves, GenerateQuietMoves);
constexpr std::array<int, 4> rook_Moves{-1, 1, 8, -8};
constexpr std::array<int, 4> bishop_Moves{-9, 9, -7, 7};
uint64_t piece_bitboard = g->PieceBitboard;
while (piece_bitboard != 0) {
int i = __builtin_ctzll(piece_bitboard);
piece_bitboard &= piece_bitboard - 1;
Piece piece = g->pieces[i];
if (piece.type == NONEPIECE) {
assert(false && "got none piece in piece bitboard");
}
if (piece.color != g->turn) {
continue;
}
if (piece.type == PAWN) {
GeneratePawnMoves(g, i, moves, GenerateQuietMoves);
}
if (piece.type == BISHOP) {
GenerateSlidingMoves(g, i, bishop_Moves, moves, GenerateQuietMoves);
}
if (piece.type == ROOK) {
GenerateSlidingMoves(g, i, rook_Moves, moves, GenerateQuietMoves);
}
if (piece.type == QUEEN) {
GenerateSlidingMoves(g, i, rook_Moves, moves, GenerateQuietMoves);
GenerateSlidingMoves(g, i, bishop_Moves, moves, GenerateQuietMoves);
}
if (piece.type == KING) {
GenerateKingMoves(g, i, moves, GenerateQuietMoves);
if (GenerateQuietMoves) {
GenerateCastlingMoves(i, g, moves);
}
};
};
return moves;
}
bool IsSquareAttacked(Game *g, Position square, bool byColor) {
int target = PositionToIndex(square);
// Pawn attacks: a pawn of byColor attacks diagonally "forward" from its
// own perspective. White pawns (rank increases toward rank 7) attack from
// one rank below the target; Black pawns attack from one rank above.
int pawnRank = square.rank + (byColor ? -1 : 1);
if (pawnRank >= 0 && pawnRank < 8) {
for (int fileOffset : {-1, 1}) {
int file = square.file + fileOffset;
if (file < 0 || file >= 8)
continue;
Piece p = g->pieces[pawnRank * 8 + file];
if (p.type == PAWN && p.color == byColor)
return true;
}
}
// Knight attacks
constexpr std::array<int, 8> knightOffsets{-17, -15, -10, -6, 6, 10, 15, 17};
for (int offset : knightOffsets) {
int from = target + offset;
if (from < 0 || from >= 64)
continue;
int fileDiff = std::abs((from % 8) - (target % 8));
if (fileDiff != 1 && fileDiff != 2)
continue;
Piece p = g->pieces[from];
if (p.type == KNIGHT && p.color == byColor)
return true;
}
// King attacks
constexpr std::array<int, 8> kingOffsets{-9, -8, -7, -1, 1, 7, 8, 9};
for (int offset : kingOffsets) {
int from = target + offset;
if (from < 0 || from >= 64)
continue;
if (std::abs((from % 8) - (target % 8)) > 1)
continue;
Piece p = g->pieces[from];
if (p.type == KING && p.color == byColor)
return true;
}
// Rooks + queens (orthogonal rays)
constexpr std::array<int, 4> rookDirs{-8, 8, -1, 1};
for (int dir : rookDirs) {
int pos = target;
while (true) {
int next = pos + dir;
if (next < 0 || next >= 64)
break;
if ((dir == 1 || dir == -1) && next / 8 != pos / 8)
break; // horizontal wrap
Piece p = g->pieces[next];
if (p.type != NONEPIECE) {
if (p.color == byColor && (p.type == ROOK || p.type == QUEEN))
return true;
break;
}
pos = next;
}
}
// Bishops + queens (diagonal rays)
constexpr std::array<int, 4> bishopDirs{-9, 9, -7, 7};
for (int dir : bishopDirs) {
int pos = target;
while (true) {
int next = pos + dir;
if (next < 0 || next >= 64)
break;
if (std::abs((next % 8) - (pos % 8)) != 1)
break; // diagonal wrap
Piece p = g->pieces[next];
if (p.type != NONEPIECE) {
if (p.color == byColor && (p.type == BISHOP || p.type == QUEEN))
return true;
break;
}
pos = next;
}
}
return false;
}
std::vector<Move> GetLegalMoves(Game *g, bool quietMove) {
std::vector<Move> moves = GetPseudoLegalMoves(g, quietMove);
std::vector<Move> legalMoves;
legalMoves.reserve(moves.size());
for (const Move &move : moves) {
UndoMove undo = MakeMove(move, g);
bool legal = true;
Position kingPosition = FindKing(g, !g->turn);
// opponent attacks our king?
if (IsSquareAttacked(g, kingPosition, g->turn)) {
legal = false;
}
UnMakeMove(undo, g);
if (legal) {
legalMoves.push_back(move);
}
}
return legalMoves;
}
GameState GetNewGameState(Game *g) {
// make sure we dont override game ending states
if (g->state == DRAW || g->state == WHITE_WON || g->state == BLACK_WON) {
return g->state;
}
if (g->halfMoveClock >= 100) {
g->state = DRAW;
}
auto legalMoves = GetLegalMoves(g);
if (legalMoves.empty()) {
Position kingPosition = FindKing(g, g->turn);
bool check = IsSquareAttacked(g, kingPosition, !g->turn);
if (check) {
g->state = g->turn ? BLACK_WON : WHITE_WON;
} else {
g->state = DRAW;
}
}
return g->state;
}
Position IndexToPosition(int i) {
auto rank = static_cast<uint8_t>(i / 8); // 0-7
auto file = static_cast<uint8_t>(i % 8); // 0-7
return {.rank = rank, .file = file};
}
void GenerateSlidingMoves(Game *b, int from,
const std::array<int, 4> &directions,
std::vector<Move> &moves, bool GenerateQuietMoves) {
for (uint i = 0; i < directions.size(); i++) {
int direction = directions[i];
int i2 = from;
while (true) {
i2 += direction;
int oldFile = (i2 - direction) % 8;
int newFile = i2 % 8;
if (direction == 7 || direction == -7 || direction == 9 ||
direction == -9) {
if (std::abs(newFile - oldFile) != 1) {
break;
}
}
if (i2 >= 64 || i2 < 0) {
break;
}
if ((direction == 1 || direction == -1) &&
(i2 / 8 != (i2 - direction) / 8)) {
break;
}
if (b->pieces[i2].color == b->turn && b->pieces[i2].type != NONEPIECE) {
break;
}
if (!GenerateQuietMoves && b->pieces[i2].type == NONEPIECE) {
continue;
}
moves.push_back(
{.From = IndexToPosition(from), .To = IndexToPosition(i2)});
if (b->pieces[i2].color != b->turn && b->pieces[i2].type != NONEPIECE) {
break;
}
}
};
};
void GenerateCastlingMoves(int from, Game *g, std::vector<Move> &moves) {
Piece piece = g->pieces[from];
if (piece.type != KING) {
assert(false && "Calling generate castling moves on non king piece");
std::cout << "Internal error\n";
exit(1);
}
if (from != 60 && !g->turn) {
return;
}
if (from != 4 && g->turn) {
return;
}
bool oneToRight = (g->PieceBitboard & (1ULL << (from + 1))) > 0;
bool twoToRight = (g->PieceBitboard & (1ULL << (from + 2))) > 0;
bool oneToLeft = (g->PieceBitboard & (1ULL << (from - 1))) > 0;
bool twoToLeft = (g->PieceBitboard & (1ULL << (from - 2))) > 0;
bool threeToLeft = (g->PieceBitboard & (1ULL << (from - 3))) > 0;
Position kingPosition = FindKing(g, g->turn);
bool check = IsSquareAttacked(g, kingPosition, !g->turn);
if (check) {
return;
}
auto pathIsSafe = [&](int step) {
return !IsSquareAttacked(g, IndexToPosition(from + step), !g->turn) &&
!IsSquareAttacked(g, IndexToPosition(from + (2 * step)), !g->turn);
};
if (g->turn) {
// white
if (!oneToRight && !twoToRight && g->whiteCastleKing && pathIsSafe(1)) {
moves.push_back(
{.From = IndexToPosition(from), .To = IndexToPosition(from + 2)});
}
if (!oneToLeft && !twoToLeft && !threeToLeft && g->whiteCastleQueen &&
pathIsSafe(-1)) {
moves.push_back(
{.From = IndexToPosition(from), .To = IndexToPosition(from - 2)});
}
}
if (!g->turn) {
// black
if (!oneToRight && !twoToRight && g->blackCastleKing && pathIsSafe(1)) {
moves.push_back(
{.From = IndexToPosition(from), .To = IndexToPosition(from + 2)});
}
if (!oneToLeft && !twoToLeft && !threeToLeft && g->blackCastleQueen &&
pathIsSafe(-1)) {
moves.push_back(
{.From = IndexToPosition(from), .To = IndexToPosition(from - 2)});
}
}
}
|