sjakk/
constants.rs

1//! Provides some useful constants, that are used throughout the crate.
2use crate::Square;
3
4/// Represents all possible directions in chess, in which piece can move.
5///
6///  northwest    north   northeast
7///  noWe         nort         noEa
8///  `       
9///              \  |  /
10///  west       <-  0 ->       east
11///              /  |  \
12///
13///  soWe         sout         soEa
14///  southwest    south   southeast
15///  `
16#[derive(Debug)]
17pub enum Direction {
18    North,
19    NorthEast,
20    East,
21    SouthEast,
22    South,
23    SouthWest,
24    West,
25    NorthWest,
26}
27
28pub const CLEAR_RANK: [u64; 8] = [
29    u64::MAX << 8,
30    u64::MAX << 16 | 0xFF_u64,
31    (u64::MAX << 16 | 0xFF_u64) << 8 | 0xFF_u64,
32    (u64::MAX << 16 | 0xFF_u64) << 16 | 0xFFFF_u64,
33    (u64::MAX >> 16 | 0xFF_u64.reverse_bits()) >> 16 | 0xFFFF_u64.reverse_bits(),
34    (u64::MAX >> 16 | 0xFF_u64.reverse_bits()) >> 8 | 0xFF_u64.reverse_bits(),
35    u64::MAX >> 16 | 0xFF_u64.reverse_bits(),
36    u64::MAX >> 8,
37];
38
39pub const MASK_RANK: [u64; 8] = [
40    !CLEAR_RANK[0],
41    !CLEAR_RANK[1],
42    !CLEAR_RANK[2],
43    !CLEAR_RANK[3],
44    !CLEAR_RANK[4],
45    !CLEAR_RANK[5],
46    !CLEAR_RANK[6],
47    !CLEAR_RANK[7],
48];
49
50const FILE: u64 = u64::from_ne_bytes([
51    0b00000001, 0b00000001, 0b00000001, 0b00000001, 0b00000001, 0b00000001, 0b00000001, 0b00000001,
52]);
53
54pub const MASK_FILE: [u64; 8] = [
55    FILE,
56    FILE << 1_u8,
57    FILE << 2_u8,
58    FILE << 3_u8,
59    FILE << 4_u8,
60    FILE << 5_u8,
61    FILE << 6_u8,
62    FILE << 7_u8,
63];
64
65pub const CLEAR_FILE: [u64; 8] = [
66    !MASK_FILE[0],
67    !MASK_FILE[1],
68    !MASK_FILE[2],
69    !MASK_FILE[3],
70    !MASK_FILE[4],
71    !MASK_FILE[5],
72    !MASK_FILE[6],
73    !MASK_FILE[7],
74];
75
76pub const INDEX64: [u64; 64] = [
77    0, 1, 48, 2, 57, 49, 28, 3, 61, 58, 50, 42, 38, 29, 17, 4, 62, 55, 59, 36, 53, 51, 43, 22, 45,
78    39, 33, 30, 24, 18, 12, 5, 63, 47, 56, 27, 60, 41, 37, 16, 54, 35, 52, 21, 44, 32, 23, 11, 46,
79    26, 40, 15, 34, 20, 31, 10, 25, 14, 19, 9, 13, 8, 7, 6,
80];
81
82pub const LSB_64_TABLE: [u64; 64] = [
83    63, 30, 3, 32, 59, 14, 11, 33, 60, 24, 50, 9, 55, 19, 21, 34, 61, 29, 2, 53, 51, 23, 41, 18,
84    56, 28, 1, 43, 46, 27, 0, 35, 62, 31, 58, 4, 5, 49, 54, 6, 15, 52, 12, 40, 7, 42, 45, 16, 25,
85    57, 48, 13, 10, 39, 8, 44, 20, 47, 38, 22, 17, 37, 36, 26,
86];
87
88pub const DEBRUIJ_T: &[u8] = &[
89    0, 47, 1, 56, 48, 27, 2, 60, 57, 49, 41, 37, 28, 16, 3, 61, 54, 58, 35, 52, 50, 42, 21, 44, 38,
90    32, 29, 23, 17, 11, 4, 62, 46, 55, 26, 59, 40, 36, 15, 53, 34, 51, 20, 43, 31, 22, 10, 45, 25,
91    39, 14, 33, 19, 30, 9, 24, 13, 18, 8, 12, 7, 6, 5, 63,
92];
93
94pub const DEBRUIJ_M: u64 = 0x03f7_9d71_b4cb_0a89;
95
96pub const WHITE_PAWNS: u64 = 0b0000000000000000000000000000000000000000000000001111111100000000_u64;
97pub const BLACK_PAWNS: u64 = WHITE_PAWNS << 40;
98pub const WHITE_AND_BLACK_PAWNS: u64 = WHITE_PAWNS | BLACK_PAWNS;
99
100pub const WHITE_ROOKS: u64 = 0b0000000000000000000000000000000000000000000000000000000010000001_u64;
101pub const BLACK_ROOKS: u64 = WHITE_ROOKS << 56;
102
103pub const WHITE_KNIGHTS: u64 =
104    0b0000000000000000000000000000000000000000000000000000000001000010_u64;
105pub const BLACK_KNIGHTS: u64 = WHITE_KNIGHTS << 56;
106
107pub const WHITE_BISHOPS: u64 =
108    0b0000000000000000000000000000000000000000000000000000000000100100_u64;
109pub const BLACK_BISHOPS: u64 = WHITE_BISHOPS << 56;
110
111pub const WHITE_QUEENS: u64 =
112    0b0000000000000000000000000000000000000000000000000000000000001000_u64;
113pub const BLACK_QUEENS: u64 = WHITE_QUEENS << 56;
114
115pub const WHITE_KING: u64 = 0b0000000000000000000000000000000000000000000000000000000000010000_u64;
116pub const BLACK_KING: u64 = WHITE_KING << 56;
117
118pub const WHITE_PIECES: u64 =
119    WHITE_PAWNS | WHITE_ROOKS | WHITE_KNIGHTS | WHITE_BISHOPS | WHITE_QUEENS | WHITE_KING;
120pub const BLACK_PIECES: u64 =
121    BLACK_PAWNS | BLACK_ROOKS | BLACK_KNIGHTS | BLACK_BISHOPS | BLACK_QUEENS | BLACK_KING;
122
123pub const ALL_PIECES: u64 = WHITE_PIECES | BLACK_PIECES;
124
125pub const WHITE_SQUARES: u64 = 0x55AA55AA55AA55AA;
126pub const BLACK_SQUARES: u64 = 0xAA55AA55AA55AA55;
127
128pub const WHITE_LONG_CASTLE_MASK: u64 = 0b10001;
129pub const WHITE_SHORT_CASTLE_MASK: u64 = 0b10001 << 4;
130pub const BLACK_SHORT_CASTLE_MASK: u64 = WHITE_SHORT_CASTLE_MASK.reverse_bits();
131pub const BLACK_LONG_CASTLE_MASK: u64 = WHITE_LONG_CASTLE_MASK.reverse_bits();
132
133pub const ALL_SQUARES: [Square; 64] = [
134    Square(0),
135    Square(1),
136    Square(2),
137    Square(3),
138    Square(4),
139    Square(5),
140    Square(6),
141    Square(7),
142    Square(8),
143    Square(9),
144    Square(10),
145    Square(11),
146    Square(12),
147    Square(13),
148    Square(14),
149    Square(15),
150    Square(16),
151    Square(17),
152    Square(18),
153    Square(19),
154    Square(20),
155    Square(21),
156    Square(22),
157    Square(23),
158    Square(24),
159    Square(25),
160    Square(26),
161    Square(27),
162    Square(28),
163    Square(29),
164    Square(30),
165    Square(31),
166    Square(32),
167    Square(33),
168    Square(34),
169    Square(35),
170    Square(36),
171    Square(37),
172    Square(38),
173    Square(39),
174    Square(40),
175    Square(41),
176    Square(42),
177    Square(43),
178    Square(44),
179    Square(45),
180    Square(46),
181    Square(47),
182    Square(48),
183    Square(49),
184    Square(50),
185    Square(51),
186    Square(52),
187    Square(53),
188    Square(54),
189    Square(55),
190    Square(56),
191    Square(57),
192    Square(58),
193    Square(59),
194    Square(60),
195    Square(61),
196    Square(62),
197    Square(63),
198];
199
200pub const CENTRAL_SQUARES: [Square; 4] = [Square::D4, Square::E4, Square::D5, Square::E5];
201
202pub const KINGSIDE_CASTLING_SQUARES: [[Square; 2]; 2] =
203    [[Square::F1, Square::G1], [Square::F8, Square::G8]];
204pub const KINGSIDE_CASTLING_TARGET_SQUARES: [[Square; 2]; 2] =
205    [[Square::E1, Square::G1], [Square::E8, Square::G8]];
206
207pub const QUEENSIDE_CASTLING_SQUARES: [[Square; 2]; 2] =
208    [[Square::C1, Square::D1], [Square::C8, Square::D8]];
209pub const QUEENSIDE_CASTLING_TARGET_SQUARES: [[Square; 2]; 2] =
210    [[Square::E1, Square::C1], [Square::E8, Square::C8]];