Repository navigation
Expand file tree
/
Copy pathattacks.cpp
More file actions
309 lines (262 loc) · 7.83 KB
/
Copy pathattacks.cpp
File metadata and controls
309 lines (262 loc) · 7.83 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
#include <iostream>
#include "types.h"
#include "bitboard.h"
#include <cmath>
// for program to be able to compute all possible attacks a common practice
// is to define tables with all possible attack from all squares before the chess engine starts
// this optimizes the process of looking for the best move as its not neccesery to compute all optiones
// every time the position changes
U64 rook_attacks[64];
U64 knight_attacks[64]; // done // done attacking
U64 bishop_attacks[64];
U64 king_attacks[64]; // done // done attacks but not castling
U64 queen_attacks[64];
U64 pawn_attacks[2][64]; // [color][square] //done //attacks added
// new tables for free pawns
U64 passed_pawn_masks_white[64];
U64 passed_pawn_masks_black[64];
// this is used to prevent jumping over the boundries of the board
// 0x => hexadecimal system XX => binary representation of 8 bits
const U64 not_A_file = 0xFEFEFEFEFEFEFEFEULL; // fe = 11111110
const U64 not_H_file = 0x7F7F7F7F7F7F7F7FULL; // 7F = 01111111
const U64 not_AB_file = 0xFCFCFCFCFCFCFCFCULL; // FC = 11111100
const U64 not_GH_file = 0x3F3F3F3F3F3F3F3FULL; // 3F = 00111111
U64 generate_knights_attacks(int square)
{
U64 bitboard = 0ULL;
U64 attacks = 0ULL;
on_bit(bitboard, square);
// atacks to the top
attacks |= (bitboard & not_H_file) << 17;
attacks |= (bitboard & not_A_file) << 15;
attacks |= (bitboard & not_GH_file) << 10;
attacks |= (bitboard & not_AB_file) << 6;
// atacks to the bottom
attacks |= (bitboard & not_A_file) >> 17;
attacks |= (bitboard & not_H_file) >> 15;
attacks |= (bitboard & not_AB_file) >> 10;
attacks |= (bitboard & not_GH_file) >> 6;
return attacks;
}
U64 generate_king_attacks(int square)
{
U64 bitboard = 0ULL;
U64 attacks = 0ULL;
on_bit(bitboard, square);
attacks |= (bitboard & not_H_file) << 1;
attacks |= (bitboard) << 8;
attacks |= (bitboard) >> 8;
attacks |= (bitboard & not_A_file) >> 1;
// diagonal
attacks |= (bitboard & not_H_file) << 9;
attacks |= (bitboard & not_H_file) >> 7;
attacks |= (bitboard & not_A_file) << 7;
attacks |= (bitboard & not_A_file) >> 9;
return attacks;
}
U64 generate_white_pawn_attacks(int square)
{
U64 bitboard = 0ULL;
U64 attacks_white = 0ULL;
on_bit(bitboard, square);
attacks_white |= (bitboard & not_H_file) << 9;
attacks_white |= (bitboard & not_A_file) << 7;
return attacks_white;
}
U64 generate_black_pawn_attacks(int square)
{
U64 bitboard = 0ULL;
U64 attacks_black = 0ULL;
on_bit(bitboard, square);
attacks_black |= (bitboard & not_H_file) >> 7;
attacks_black |= (bitboard & not_A_file) >> 9;
return attacks_black;
}
U64 generate_passed_pawn_mask_white(int square)
{
U64 mask = 0ULL;
int r = square / 8;
int f = square % 8;
for (int rank = r + 1; rank <= 7; rank++)
{
on_bit(mask, rank * 8 + f);
// check column to the left if pawn is not on file A
if (f > 0)
on_bit(mask, rank * 8 + (f - 1));
// check columnt to the right
if (f < 7)
on_bit(mask, rank * 8 + (f + 1));
}
return mask;
}
U64 generate_passed_pawn_mask_black(int square)
{
U64 mask = 0ULL;
int r = square / 8;
int f = square % 8;
for (int rank = r - 1; rank >= 0; rank--)
{
on_bit(mask, rank * 8 + f);
// left
if (f > 0)
on_bit(mask, rank * 8 + (f - 1));
// right
if (f < 7)
on_bit(mask, rank * 8 + (f + 1));
}
return mask;
}
void init_leaper_attacks()
{
for (int square = 0; square < 64; square++)
{
knight_attacks[square] = generate_knights_attacks(square);
king_attacks[square] = generate_king_attacks(square);
pawn_attacks[0][square] = generate_white_pawn_attacks(square);
pawn_attacks[1][square] = generate_black_pawn_attacks(square);
passed_pawn_masks_white[square] = generate_passed_pawn_mask_white(square);
passed_pawn_masks_black[square] = generate_passed_pawn_mask_black(square);
}
}
// all moves possible for sliding with occupanices considered
U64 mask_rooks_attacks(int square, U64 blockers)
{
U64 attacks = 0ULL;
int r = square / 8;
int f = square % 8;
// upp
for (int rank = r + 1; rank <= 7; rank++)
{
int sq = rank * 8 + f;
on_bit(attacks, sq);
if (get_bit(blockers, sq))
break; // if we are on blocked squere then stop going
}
// down
for (int rank = r - 1; rank >= 0; rank--)
{
int sq = rank * 8 + f;
on_bit(attacks, sq);
if (get_bit(blockers, sq))
break;
}
// right
for (int file = f + 1; file <= 7; file++)
{
int sq = r * 8 + file;
on_bit(attacks, sq);
if (get_bit(blockers, sq))
break;
}
// left
for (int file = f - 1; file >= 0; file--)
{
int sq = r * 8 + file;
on_bit(attacks, sq);
if (get_bit(blockers, sq))
break;
}
return attacks;
}
U64 mask_bishops_attacks(int square, U64 blockers)
{
U64 attacks = 0ULL;
int r = square / 8;
int f = square % 8;
// upp + right
for (int rank = r + 1, file = f + 1; rank <= 7 && file <= 7; rank++, file++)
{
int sq = rank * 8 + file;
on_bit(attacks, sq);
if (get_bit(blockers, sq))
break;
}
// down + right
for (int rank = r - 1, file = f + 1; rank >= 0 && file <= 7; rank--, file++)
{
int sq = rank * 8 + file;
on_bit(attacks, sq);
if (get_bit(blockers, sq))
break;
}
// down + left
for (int rank = r - 1, file = f - 1; rank >= 0 && file >= 0; rank--, file--)
{
int sq = rank * 8 + file;
on_bit(attacks, sq);
if (get_bit(blockers, sq))
break;
}
// upp + left
for (int rank = r + 1, file = f - 1; rank <= 7 && file >= 0; rank++, file--)
{
int sq = rank * 8 + file;
on_bit(attacks, sq);
if (get_bit(blockers, sq))
break;
}
return attacks;
}
U64 mask_queen_attacks(int square, U64 blockers)
{
return mask_rooks_attacks(square, blockers) | mask_bishops_attacks(square, blockers);
}
// function that check if a given square is attacked - used for castiling checks etc
bool is_squere_attacked(int square, int whos_attacking, Position &pos)
{
if (whos_attacking == white)
{
if (pawn_attacks[black][square] & pos.bitboards[w_pawn])
{
return true;
}
if (knight_attacks[square] & pos.bitboards[w_knight])
{
return true;
}
if (king_attacks[square] & pos.bitboards[w_king])
{
return true;
}
if (mask_rooks_attacks(square, pos.occupancies[both]) & pos.bitboards[w_rook])
{
return true;
}
if (mask_bishops_attacks(square, pos.occupancies[both]) & pos.bitboards[w_bishop])
{
return true;
}
if (mask_queen_attacks(square, pos.occupancies[both]) & pos.bitboards[w_queen])
{
return true;
}
}
else
{
if (pawn_attacks[white][square] & pos.bitboards[b_pawn])
{
return true;
}
if (knight_attacks[square] & pos.bitboards[b_knight])
{
return true;
}
if (king_attacks[square] & pos.bitboards[b_king])
{
return true;
}
if (mask_rooks_attacks(square, pos.occupancies[both]) & pos.bitboards[b_rook])
{
return true;
}
if (mask_bishops_attacks(square, pos.occupancies[both]) & pos.bitboards[b_bishop])
{
return true;
}
if (mask_queen_attacks(square, pos.occupancies[both]) & pos.bitboards[b_queen])
{
return true;
}
}
return false;
}