Repository navigation
Expand file tree
/
Copy pathmove_gen.cpp
More file actions
365 lines (334 loc) · 11.2 KB
/
Copy pathmove_gen.cpp
File metadata and controls
365 lines (334 loc) · 11.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
361
362
363
364
365
#include "move_gen.h"
#include "board.h"
#include <algorithm>
using namespace std;
uint64_t getKnightMoves(uint64_t POS, Board &board, bool isWhite) {
uint64_t abFile = board.aFile | board.bFile;
uint64_t ghFile = board.gfile | board.hfile;
uint64_t ownPieces = isWhite ? board.whiteBoard : board.blackBoard;
uint64_t attacks = (POS << 17 & ~board.aFile) | (POS << 15 & ~board.hfile) |
(POS << 10 & ~abFile) | (POS << 6 & ~ghFile) |
(POS >> 17 & ~board.hfile) | (POS >> 15 & ~board.aFile) |
(POS >> 10 & ~ghFile) | (POS >> 6 & ~abFile);
return attacks & ~ownPieces;
}
uint64_t getRookMoves(uint64_t pos, Board &board, bool isWhite) {
int sq = __builtin_ctzll(pos);
int rank = sq / 8, file = sq % 8;
uint64_t attack = 0;
uint64_t sameColorPieces = isWhite ? board.whiteBoard : board.blackBoard;
for (int rr = rank + 1; rr < 8; rr++) {
uint64_t s = 1ULL << (rr * 8 + file);
attack |= s;
if (board.allPieces & s) {
if (s & sameColorPieces)
attack ^= s;
break;
}
}
for (int rr = rank - 1; rr >= 0; rr--) {
uint64_t s = 1ULL << (rr * 8 + file);
attack |= s;
if (board.allPieces & s) {
if (s & sameColorPieces)
attack ^= s;
break;
}
}
for (int ff = file + 1; ff < 8; ff++) {
uint64_t s = 1ULL << (rank * 8 + ff);
attack |= s;
if (board.allPieces & s) {
if (s & sameColorPieces)
attack ^= s;
break;
}
}
for (int ff = file - 1; ff >= 0; ff--) {
uint64_t s = 1ULL << (rank * 8 + ff);
attack |= s;
if (board.allPieces & s) {
if (s & sameColorPieces)
attack ^= s;
break;
}
}
return attack;
}
uint64_t getBishopMoves(uint64_t pos, Board &board, bool isWhite) {
int sq = __builtin_ctzll(pos);
int rank = sq / 8, file = sq % 8;
uint64_t attack = 0;
uint64_t sameColorPieces = isWhite ? board.whiteBoard : board.blackBoard;
for (int i = 1; i < 8; i++) {
if (rank + i > 7 || file + i > 7)
break;
uint64_t s = 1ULL << ((rank + i) * 8 + (file + i));
attack |= s;
if (board.allPieces & s) {
if (s & sameColorPieces)
attack ^= s;
break;
}
}
for (int i = 1; i < 8; i++) {
if (rank + i > 7 || file - i < 0)
break;
uint64_t s = 1ULL << ((rank + i) * 8 + (file - i));
attack |= s;
if (board.allPieces & s) {
if (s & sameColorPieces)
attack ^= s;
break;
}
}
for (int i = 1; i < 8; i++) {
if (rank - i < 0 || file + i > 7)
break;
uint64_t s = 1ULL << ((rank - i) * 8 + (file + i));
attack |= s;
if (board.allPieces & s) {
if (s & sameColorPieces)
attack ^= s;
break;
}
}
for (int i = 1; i < 8; i++) {
if (rank - i < 0 || file - i < 0)
break;
uint64_t s = 1ULL << ((rank - i) * 8 + (file - i));
attack |= s;
if (board.allPieces & s) {
if (s & sameColorPieces)
attack ^= s;
break;
}
}
return attack;
}
uint64_t getQueenMove(uint64_t pos, Board &board, bool isWhite) {
return getRookMoves(pos, board, isWhite) |
getBishopMoves(pos, board, isWhite);
}
uint64_t getKingMoves(uint64_t pos, Board board, bool isWhite) {
uint64_t ownPieces = isWhite ? board.whiteBoard : board.blackBoard;
uint64_t attack = (pos << 8) | (pos >> 8) | (pos << 1 & ~board.aFile) |
(pos >> 1 & ~board.hfile) | (pos << 9 & ~board.aFile) |
(pos >> 9 & ~board.hfile) | (pos << 7 & ~board.hfile) |
(pos >> 7 & ~board.aFile);
return attack & ~ownPieces;
}
uint64_t getPawnMoves(uint64_t pos, Board &board, bool isWhite) {
uint64_t moves = 0;
if (isWhite) {
uint64_t single = pos << 8;
if (single && !(single & board.allPieces)) {
moves |= single;
if ((pos & 0xFF00) && !(single << 8 & board.allPieces))
moves |= (single << 8);
}
uint64_t leftCap = (pos << 7) & ~board.hfile;
uint64_t rightCap = (pos << 9) & ~board.aFile;
moves |= (leftCap | rightCap) & board.blackBoard;
if (board.enPassantSq != -1) {
uint64_t epBit = 1ULL << board.enPassantSq;
if ((leftCap | rightCap) & epBit)
moves |= epBit;
}
} else {
uint64_t single = pos >> 8;
if (single && !(single & board.allPieces)) {
moves |= single;
if ((pos & 0x00FF000000000000ULL) && !(single >> 8 & board.allPieces))
moves |= (single >> 8);
}
uint64_t leftCap = (pos >> 7) & ~board.aFile;
uint64_t rightCap = (pos >> 9) & ~board.hfile;
moves |= (leftCap | rightCap) & board.whiteBoard;
if (board.enPassantSq != -1) {
uint64_t epBit = 1ULL << board.enPassantSq;
if ((leftCap | rightCap) & epBit)
moves |= epBit;
}
}
return moves;
}
bool isKingInCheck(Board &board, bool isWhite) {
uint64_t kingPos = isWhite ? board.whiteKing : board.blackKing;
if (!kingPos)
return false;
bool opp = !isWhite;
uint64_t occupied = board.allPieces;
// Pawns
uint64_t oppPawns = isWhite ? board.blackPawns : board.whitePawns;
if (isWhite) {
if (((kingPos << 7) & ~board.hfile & oppPawns) ||
((kingPos << 9) & ~board.aFile & oppPawns))
return true;
} else {
if (((kingPos >> 7) & ~board.aFile & oppPawns) ||
((kingPos >> 9) & ~board.hfile & oppPawns))
return true;
}
// Knights
uint64_t oppKnights = isWhite ? board.blackKnights : board.whiteKnights;
while (oppKnights) {
uint64_t pos = oppKnights & -oppKnights;
if (getKnightMoves(pos, board, opp) & kingPos)
return true;
oppKnights &= oppKnights - 1;
}
// Sliders
auto checkSlider = [&](uint64_t pieces, auto func) {
while (pieces) {
uint64_t pos = pieces & -pieces;
if (func(pos, board, opp) & kingPos)
return true;
pieces &= pieces - 1;
}
return false;
};
if (checkSlider(isWhite ? board.blackBishops : board.whiteBishops,
getBishopMoves))
return true;
if (checkSlider(isWhite ? board.blackRooks : board.whiteRooks, getRookMoves))
return true;
if (checkSlider(isWhite ? board.blackQueen : board.whiteQueen, getQueenMove))
return true;
// King
uint64_t oppKing = isWhite ? board.blackKing : board.whiteKing;
if (oppKing && (getKingMoves(oppKing, board, opp) & kingPos))
return true;
return false;
}
constexpr int MVV_LVA_VALUES[7] = {0, 100, 300, 300, 500, 900, 10000};
static inline int scoreMove(const Move &m) {
int score = 0;
if (m.capturedPiece != NONE) {
// Multiplying the victim by a large factor ensures the victim's value
// dominates Subtracting the attacker's value guarantees that weaker
// attackers are preferred Example: PxQ (9000 - 100 = 8900) is evaluated
// before RxQ (9000 - 500 = 8500)
score = 10 * MVV_LVA_VALUES[m.capturedPiece] - MVV_LVA_VALUES[m.piece];
}
// For promotions
if (m.promotionPiece != NONE) {
score += MVV_LVA_VALUES[m.promotionPiece];
}
return score;
}
// function to compare the moves..used for ordering the moves properly to search
// for potentially good moves first
bool compareMoves(const Move &a, const Move &b) {
return scoreMove(a) > scoreMove(b);
}
vector<Move> generateAllMoves(Board &board, bool isWhite) {
vector<Move> moves;
auto addMove = [&](int from, int to, PieceType p, PieceType cap,
bool ep = false, bool cast = false,
PieceType prom = NONE) {
Move m = {from + 1, to + 1, p, cap, prom, cast, ep};
Board temp = board;
makeMove(temp, m, isWhite);
if (!isKingInCheck(temp, isWhite))
moves.push_back(m);
};
uint64_t own = isWhite ? board.whiteBoard : board.blackBoard;
uint64_t opp = isWhite ? board.blackBoard : board.whiteBoard;
for (int i = 0; i < 64; i++) {
uint64_t bit = 1ULL << i;
if (!(bit & own))
continue;
PieceType type = NONE;
uint64_t targets = 0;
if (bit & (isWhite ? board.whitePawns : board.blackPawns)) {
type = PAWN;
targets = getPawnMoves(bit, board, isWhite);
} else if (bit & (isWhite ? board.whiteKnights : board.blackKnights)) {
type = KNIGHT;
targets = getKnightMoves(bit, board, isWhite);
} else if (bit & (isWhite ? board.whiteBishops : board.blackBishops)) {
type = BISHOP;
targets = getBishopMoves(bit, board, isWhite);
} else if (bit & (isWhite ? board.whiteRooks : board.blackRooks)) {
type = ROOK;
targets = getRookMoves(bit, board, isWhite);
} else if (bit & (isWhite ? board.whiteQueen : board.blackQueen)) {
type = QUEEN;
targets = getQueenMove(bit, board, isWhite);
} else if (bit & (isWhite ? board.whiteKing : board.blackKing)) {
type = KING;
targets = getKingMoves(bit, board, isWhite);
// Castling
if (!isKingInCheck(board, isWhite)) {
if (isWhite) {
if ((board.castlingRights & 1) && !(board.allPieces & 0x60) &&
!isKingInCheck(board, true)) { // Kingside
// Need to check intermediate square
Board t = board;
Move m = {5, 6, KING, NONE};
makeMove(t, m, true);
if (!isKingInCheck(t, true))
addMove(4, 6, KING, NONE, false, true);
}
if ((board.castlingRights & 2) &&
!(board.allPieces & 0x0E)) { // Queenside
Board t = board;
Move m = {5, 4, KING, NONE};
makeMove(t, m, true);
if (!isKingInCheck(t, true))
addMove(4, 2, KING, NONE, false, true);
}
} else {
if ((board.castlingRights & 4) &&
!(board.allPieces & (0x60ULL << 56))) {
Board t = board;
Move m = {61, 62, KING, NONE};
makeMove(t, m, false);
if (!isKingInCheck(t, false))
addMove(60, 62, KING, NONE, false, true);
}
if ((board.castlingRights & 8) &&
!(board.allPieces & (0x0EULL << 56))) {
Board t = board;
Move m = {61, 60, KING, NONE};
makeMove(t, m, false);
if (!isKingInCheck(t, false))
addMove(60, 58, KING, NONE, false, true);
}
}
}
}
while (targets) {
int to = __builtin_ctzll(targets);
PieceType cap = NONE;
uint64_t toBit = 1ULL << to;
if (toBit & (isWhite ? board.blackPawns : board.whitePawns))
cap = PAWN;
else if (toBit & (isWhite ? board.blackKnights : board.whiteKnights))
cap = KNIGHT;
else if (toBit & (isWhite ? board.blackBishops : board.whiteBishops))
cap = BISHOP;
else if (toBit & (isWhite ? board.blackRooks : board.whiteRooks))
cap = ROOK;
else if (toBit & (isWhite ? board.blackQueen : board.whiteQueen))
cap = QUEEN;
else if (toBit & (isWhite ? board.blackKing : board.whiteKing))
cap = KING;
bool ep = (type == PAWN && to == board.enPassantSq);
if (ep)
cap = PAWN;
if (type == PAWN && (to / 8 == 0 || to / 8 == 7)) {
addMove(i, to, type, cap, ep, false, QUEEN);
addMove(i, to, type, cap, ep, false, ROOK);
addMove(i, to, type, cap, ep, false, BISHOP);
addMove(i, to, type, cap, ep, false, KNIGHT);
} else {
addMove(i, to, type, cap, ep);
}
targets &= targets - 1;
}
}
sort(moves.begin(), moves.end(), compareMoves);
return moves;
}