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
#[macro_use]
mod macros;
pub mod bit_twiddles;
pub mod piece_move;
pub mod magic_helper;
pub mod masks;
pub mod mono_traits;
pub mod sq;
pub mod bitboard;
pub mod move_list;
use self::bit_twiddles::*;
use self::masks::*;
use self::sq::SQ;
use std::fmt;
pub const ALL_PIECES: [Piece; PIECE_CNT] =
[Piece::P, Piece::N, Piece::B, Piece::R, Piece::Q, Piece::K];
pub const ALL_PLAYERS: [Player; 2] = [Player::White, Player::Black];
pub static ALL_FILES: [File; FILE_CNT] = [
File::A,
File::B,
File::C,
File::D,
File::E,
File::F,
File::G,
File::H,
];
pub static ALL_RANKS: [Rank; RANK_CNT] = [
Rank::R1,
Rank::R2,
Rank::R3,
Rank::R4,
Rank::R5,
Rank::R6,
Rank::R7,
Rank::R8,
];
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum Player {
White = 0,
Black = 1,
}
impl Player {
#[inline(always)]
pub fn other_player(&self) -> Player {
match *self {
Player::White => Player::Black,
Player::Black => Player::White,
}
}
#[inline(always)]
pub fn relative_square(&self, sq: SQ) -> SQ {
assert!(sq.is_okay());
sq ^ SQ((*self) as u8 * 56)
}
#[inline(always)]
pub fn pawn_push(&self) -> i8 {
match *self {
Player::White => NORTH,
Player::Black => SOUTH,
}
}
#[inline(always)]
pub fn relative_rank_of_sq(&self, sq: SQ) -> Rank {
self.relative_rank(sq.rank())
}
#[inline(always)]
pub fn relative_rank(&self, rank: Rank) -> Rank {
ALL_RANKS[((rank as u8) ^ (*self as u8 * 7)) as usize]
}
}
impl fmt::Display for Player {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"{}",
if self == &Player::White {
"White"
} else {
"Black"
}
)
}
}
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum GenTypes {
All,
Captures,
Quiets,
QuietChecks,
Evasions,
NonEvasions
}
#[repr(u8)]
#[derive(Copy, Clone, PartialEq, Debug)]
pub enum Piece {
K = 5,
Q = 4,
R = 3,
B = 2,
N = 1,
P = 0,
}
impl Piece {
#[inline]
pub fn value(&self) -> i8 {
match *self {
Piece::P => 1,
Piece::N | Piece::B => 3,
Piece::R => 5,
Piece::Q => 8,
Piece::K => 0,
}
}
#[inline]
pub fn char_lower(&self) -> char {
match *self {
Piece::P => 'p',
Piece::N => 'n',
Piece::B => 'b',
Piece::R => 'r',
Piece::Q => 'q',
Piece::K => 'k',
}
}
#[inline]
pub fn char_upper(&self) -> char {
match *self {
Piece::P => 'P',
Piece::N => 'N',
Piece::B => 'B',
Piece::R => 'R',
Piece::Q => 'Q',
Piece::K => 'K',
}
}
}
impl fmt::Display for Piece {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let s = match *self {
Piece::P => "Pawn",
Piece::N => "Knight",
Piece::B => "Bishop",
Piece::R => "Rook",
Piece::Q => "Queen",
Piece::K => "King"
};
f.pad(s)
}
}
#[repr(u8)]
#[derive(Copy, Clone, PartialEq, Debug)]
pub enum File {
A = 0,
B = 1,
C = 2,
D = 3,
E = 4,
F = 5,
G = 6,
H = 7,
}
#[repr(u8)]
#[derive(Copy, Clone, PartialEq, Debug)]
pub enum Rank {
R1 = 0,
R2 = 1,
R3 = 2,
R4 = 3,
R5 = 4,
R6 = 5,
R7 = 6,
R8 = 7,
}
#[derive(Copy, Clone, PartialEq, Debug)]
pub enum CastleType {
KingSide = 0,
QueenSide = 1,
}
#[inline(always)]
pub fn rank_bb(s: u8) -> u64 {
RANK_BB[rank_of_sq(s) as usize]
}
#[inline(always)]
pub fn rank_of_sq(s: u8) -> Rank {
ALL_RANKS[(s >> 3) as usize]
}
#[inline(always)]
pub fn rank_idx_of_sq(s: u8) -> u8 {
(s >> 3) as u8
}
#[inline(always)]
pub fn file_bb(s: u8) -> u64 {
FILE_BB[file_of_sq(s) as usize]
}
#[inline(always)]
pub fn file_of_sq(s: u8) -> File {
unsafe {
*ALL_FILES.get_unchecked((s & 0b0000_0111) as usize)
}
}
#[inline(always)]
pub fn file_idx_of_sq(s: u8) -> u8 {
(s & 0b0000_0111) as u8
}
#[inline]
pub fn u64_to_u8(b: u64) -> u8 {
debug_assert_eq!(popcount64(b), 1);
bit_scan_forward(b)
}
#[inline]
pub fn u8_to_u64(s: u8) -> u64 {
debug_assert!(s < 64);
(1 as u64).wrapping_shl(s as u32)
}