-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathattacks.cpp
More file actions
65 lines (52 loc) · 2.61 KB
/
Copy pathattacks.cpp
File metadata and controls
65 lines (52 loc) · 2.61 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
#include "attacks.h"
#include "bit_utils.h"
#include "masks.h"
// Initialize the global lookup tables
std::uint64_t knight_attacks[64];
std::uint64_t king_attacks[64];
// Given a square index, calculate the pseudo-legal moves for a Knight masking wrapping
std::uint64_t mask_knight_attacks(int square) {
std::uint64_t attacks = 0ULL;
std::uint64_t bitboard = 0ULL;
// Place a piece on the specified square
set_bit(bitboard, square);
// Calculate all 8 possible knight offsets.
// If an offset pushes the piece off the side of the board and it wraps,
// the respective NOT_FILES mask will eliminate the illegal wrapped bits.
// Shifts upwards (North)
if ((bitboard << 17) & NOT_A_FILE) attacks |= (bitboard << 17); // North-North-East
if ((bitboard << 15) & NOT_H_FILE) attacks |= (bitboard << 15); // North-North-West
if ((bitboard << 10) & NOT_AB_FILE) attacks |= (bitboard << 10); // East-East-North
if ((bitboard << 6) & NOT_GH_FILE) attacks |= (bitboard << 6); // West-West-North
// Shifts downwards (South)
if ((bitboard >> 15) & NOT_A_FILE) attacks |= (bitboard >> 15); // South-South-East
if ((bitboard >> 17) & NOT_H_FILE) attacks |= (bitboard >> 17); // South-South-West
if ((bitboard >> 6) & NOT_AB_FILE) attacks |= (bitboard >> 6); // East-East-South
if ((bitboard >> 10) & NOT_GH_FILE) attacks |= (bitboard >> 10); // West-West-South
return attacks;
}
// Given a square index, calculate the pseudo-legal moves for a King masking wrapping
std::uint64_t mask_king_attacks(int square) {
std::uint64_t attacks = 0ULL;
std::uint64_t bitboard = 0ULL;
// Place a piece on the specified square
set_bit(bitboard, square);
// Lateral / Vertical offsets
if (bitboard << 8) attacks |= (bitboard << 8); // North
if (bitboard >> 8) attacks |= (bitboard >> 8); // South
if ((bitboard << 1) & NOT_A_FILE) attacks |= (bitboard << 1); // East
if ((bitboard >> 1) & NOT_H_FILE) attacks |= (bitboard >> 1); // West
// Diagonal offsets
if ((bitboard << 9) & NOT_A_FILE) attacks |= (bitboard << 9); // North-East
if ((bitboard << 7) & NOT_H_FILE) attacks |= (bitboard << 7); // North-West
if ((bitboard >> 7) & NOT_A_FILE) attacks |= (bitboard >> 7); // South-East
if ((bitboard >> 9) & NOT_H_FILE) attacks |= (bitboard >> 9); // South-West
return attacks;
}
// Populate the static jump tables arrays using our masking functions
void init_leapers() {
for (int square = 0; square < 64; square++) {
knight_attacks[square] = mask_knight_attacks(square);
king_attacks[square] = mask_king_attacks(square);
}
}