-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAttackValidator.cpp
More file actions
346 lines (262 loc) · 10.6 KB
/
Copy pathAttackValidator.cpp
File metadata and controls
346 lines (262 loc) · 10.6 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
//
// Created by sahoo on 28-06-2026.
//
#include "AttackValidator.h"
#include "Utils.h"
#include "ChessBoard.h"
// precomputed attack masks indexed by source square
namespace AttackTables {
uint64_t KnightAttacks[64] = {0};
uint64_t KingAttacks[64] = {0};
// pawnAttacks[0] for white pawns, pawnAttacks[1] for black pawns
uint64_t PawnAttacks[2][64] = {0};
// define the initializer
AttackTableInitializer::AttackTableInitializer() {
AttackValidator::initKnightAttacks();
AttackValidator::initKingAttacks();
AttackValidator::initPawnAttacks();
}
// create the global instance
AttackTableInitializer initializer;
}
// initializes knight attack table computation
void AttackValidator::initKnightAttacks() {
// step values for all eight possible knight moves
int dr[8] = {2, 2, -2, -2, 1, 1, -1, -1};
int df[8] = {1, -1, 1, -1, 2, -2, 2, -2};
// generate attack mask for every board squares
for (int square = 0; square < 64; square++) {
// attack mask for the square
uint64_t attacks = 0;
int rank = square >> 3;
int file = square & 7;
// explores all 8 possible moves from a square
for (int i = 0; i < 8; i++) {
int newRank = rank + dr[i];
int newFile = file + df[i];
// ignores moves which leaves the board
if (newRank >= 0 && newRank < 8 && newFile >= 0 && newFile < 8) {
// computes & inserts the position into the attack mask
int targetSquare = newRank * 8 + newFile;
attacks |= (1ULL << targetSquare);
}
}
// stores complete attack masks for the square
AttackTables::KnightAttacks[square] = attacks;
}
}
// initializes king attack table computation
void AttackValidator::initKingAttacks() {
// step values for all eight possible king moves
int dr[8] = {1, -1, -1, 1, 1, 0, -1, 0};
int df[8] = {1, -1, 1, -1, 0, 1, 0, -1};
// generates attack mask for every square in board
for (int square = 0; square < 64; square++) {
// attack mask for the square
uint64_t attacks = 0;
int rank = square >> 3;
int file = square & 7;
// explores all 8 possible moves from a square
for (int i = 0; i < 8; i++) {
int newRank = rank + dr[i];
int newFile = file + df[i];
// ignores moves which leaves the board
if (newRank >= 0 && newRank < 8 && newFile >= 0 && newFile < 8) {
// computes & inserts the position into the attack mask
int targetSquare = newRank * 8 + newFile;
attacks |= (1ULL << targetSquare);
}
}
// stores complete attack masks for the square
AttackTables::KingAttacks[square] = attacks;
}
}
// initializes pawn attack table computation
void AttackValidator::initPawnAttacks() {
// generates attack mask for every square in board
for (int square = 0; square < 64; square++) {
// attack mask for white and black pawns for a square
uint64_t whiteAttacks = 0;
uint64_t blackAttacks = 0;
int rank = square >> 3;
int file = square & 7;
// step values for left capture move
int wRank1 = rank + 1;
int wFile1 = file - 1;
// step values for right capture move
int wRank2 = rank + 1;
int wFile2 = file + 1;
// left diagonal capture
if (wRank1 < 8 && wFile1 >= 0) {
// computes & inserts the position into the attack mask
int targetSquare = wRank1 * 8 + wFile1;
whiteAttacks |= (1ULL << targetSquare);
}
// right diagonal capture
if (wRank2 < 8 && wFile2 < 8) {
int targetSquare = wRank2 * 8 + wFile2;
whiteAttacks |= (1ULL << targetSquare);
}
// step values for left capture move
int bRank1 = rank - 1;
int bFile1 = file - 1;
// step values for right capture move
int bRank2 = rank - 1;
int bFile2 = file + 1;
// left diagonal capture
if (bRank1 >= 0 && bFile1 >= 0) {
int targetSquare = bRank1 * 8 + bFile1;
blackAttacks |= (1ULL << targetSquare);
}
// right diagonal capture
if (bRank2 >= 0 && bFile2 < 8) {
int targetSquare = bRank2 * 8 + bFile2;
blackAttacks |= (1ULL << targetSquare);
}
// store attack masks of pawns
AttackTables::PawnAttacks[0][square] = whiteAttacks;
AttackTables::PawnAttacks[1][square] = blackAttacks;
}
}
// returns true if a square is being attacked by knights of given color
bool AttackValidator::isAttackedByKnight(ChessBoard &board, int square, bool attackerIsWhite) {
// gets the bitboard containing all knights of the specified color
uint64_t knights = attackerIsWhite ? board.getPieceBitboard(whiteKnight) : board.getPieceBitboard(blackKnight);
// iterate through every knight on the board
while (knights) {
// extracts the least significant set bit, which represents one knight
int fromSquare = __builtin_ctzll(knights);
// removes the processed knight from the bitboard
knights &= knights - 1;
// lookup all squares this knight attacks
uint64_t attackMask = AttackTables::KnightAttacks[fromSquare];
// if the target square is in the attack mask, it is under attack
if (attackMask & (1ULL << square)) {
return true;
}
}
return false;
}
// returns true if a square is being attacked by king of given color
bool AttackValidator::isAttackedByKing(ChessBoard &board, int square, bool attackerIsWhite) {
// gets the bitboard containing all kings of the specified color
uint64_t kings = attackerIsWhite ? board.getPieceBitboard(whiteKing) : board.getPieceBitboard(blackKing);
// iterate through king on the board
while (kings) {
// extracts the least significant set bit, which represents one king
int kingSquare = __builtin_ctzll(kings);
// removes the processed king from the bitboard
kings &= kings - 1;
// gets the attack mask from the pre-computed lookup tables
uint64_t attackMask = AttackTables::KingAttacks[kingSquare];
// if the target square is in the attack mask, it is under attack
if (attackMask & (1ULL << square)) {
return true;
}
}
return false;
}
// returns true if a square is being attacked by pawns of given color
bool AttackValidator::isAttackedByPawn(ChessBoard &board, int square, bool attackerIsWhite) {
uint64_t pawns = attackerIsWhite ? board.getPieceBitboard(whitePawn) : board.getPieceBitboard(blackPawn);
int color = attackerIsWhite ? 0 : 1;
while (pawns) {
int pawnSquare = __builtin_ctzll(pawns);
pawns &= pawns - 1;
uint64_t attackMask = AttackTables::PawnAttacks[color][pawnSquare];
if (attackMask & (1ULL << square)) {
return true;
}
}
return false;
}
// returns true if a square is being attacked by rooks of given color
bool AttackValidator::isAttackedByRook(ChessBoard &board, int square, bool attackerIsWhite) {
uint64_t occupancy = board.getAllOccupancy();
// queen also behaves like rook
uint64_t rooks = attackerIsWhite ? (board.getPieceBitboard(whiteRook) | board.getPieceBitboard(whiteQueen))
: (board.getPieceBitboard(blackRook) | board.getPieceBitboard(blackQueen));
// loop through all rooks and queen
while (rooks) {
int rookSquare = __builtin_ctzll(rooks);
rooks &= rooks - 1;
// check four directions
Direction dirs[4] = {up, down, left, right};
// walks in four directions one by one
for (int i = 0; i < 4; i++) {
// gets the path in the direction from that square
const std::vector<int> &ray = AttackTables::rayTable.getRay(rookSquare, dirs[i]);
// walks in the direction square by square
for (int sq : ray) {
// the current square on the ray is exactly the target square
if (sq == square) {
return true;
}
// stops if the path is blocked
if (Utils::checkBit(occupancy, sq)) {
break;
}
}
}
}
return false;
}
// returns true if a square is being attacked by bishops of given color
bool AttackValidator::isAttackedByBishop(ChessBoard &board, int square, bool attackerIsWhite) {
uint64_t occupancy = board.getAllOccupancy();
// queen also behaves like bishop
uint64_t bishops = attackerIsWhite ? (board.getPieceBitboard(whiteBishop) | board.getPieceBitboard(whiteQueen))
: (board.getPieceBitboard(blackBishop) | board.getPieceBitboard(blackQueen));
Direction dirs[4] = {upLeft, upRight, downLeft, downRight};
while (bishops) {
int bishopSquare = __builtin_ctzll(bishops);
bishops &= bishops - 1;
for (int i = 0; i < 4; i++) {
const std::vector<int> &ray = AttackTables::rayTable.getRay(bishopSquare, dirs[i]);
for (int sq : ray) {
if (sq == square) {
return true;
}
if (Utils::checkBit(occupancy, sq)) {
break;
}
}
}
}
return false;
}
// returns true if a square is being attacked by queens of given color
bool AttackValidator::isAttackedByQueen(ChessBoard &board, int square, bool attackerIsWhite) {
// queen is the combination of rook and bishop
bool rookAttack = isAttackedByRook(board, square, attackerIsWhite);
if (rookAttack) {
return true;
}
bool bishopAttack = isAttackedByBishop(board, square, attackerIsWhite);
if (bishopAttack) {
return true;
}
return false;
}
// returns true if a square is being attacked by pieces of given color
bool AttackValidator::isSquareUnderAttack(ChessBoard &board, int square, bool attackerIsWhite) {
if (isAttackedByPawn(board, square, attackerIsWhite)) {
return true;
}
if (isAttackedByKnight(board, square, attackerIsWhite)) {
return true;
}
if (isAttackedByKing(board, square, attackerIsWhite)) {
return true;
}
if (isAttackedByBishop(board, square, attackerIsWhite)) {
return true;
}
if (isAttackedByRook(board, square, attackerIsWhite)) {
return true;
}
if (isAttackedByQueen(board, square, attackerIsWhite)) {
return true;
}
return false;
}