friendly_chess/chess/
constants.rs

1pub const BOARD_SIZE: usize = 128;
2pub const COLOR_MASK: u8 = 128; // 10000000
3
4pub const WHITE_PAWN_DELTAS: [i8; 4] = [-16, -32, -17, -15];
5pub const BLACK_PAWN_DELTAS: [i8; 4] = [16, 32, 17, 15];
6pub const BISHOP_DELTAS: [i8; 4] = [17, 15, -17, -15];
7pub const ROOK_DELTAS: [i8; 4] = [16, -16, 1, -1];
8pub const QUEEN_DELTAS: [i8; 8] = [16, -16, 1, -1, 17, 15, -17, -15];
9pub const KNIGHT_DELTAS: [i8; 8] = [14, 31, 18, 33, -14, -31, -18, -33];
10pub const KING_DELTAS: [i8; 10] = [1, 16, 17, 15, -1, -16, -17, -15, 2, -2];
11
12#[derive(Debug, Copy, Clone, PartialEq, Eq)]
13pub enum Color {
14    WHITE = 0,
15    BLACK = 128,
16}
17
18impl Color {
19    pub fn to_value(&self) -> u8 {
20        *self as u8
21    }
22
23    pub fn to_string(&self) -> String {
24        if self.to_value() == 0 {
25            return String::from("w");
26        } else {
27            return String::from("b");
28        }
29    }
30}
31
32// const MOVED_KING_DELTAS: [i8; 8] = [1, 16, 17, 15, -1, -16, -17, -15];
33
34// why does it have 239 items?
35// because of how the indexes of the squares on the real board are laid out due to the fact that
36// we have to use some indexes in between to represent the dummy board
37/*
38
39  what is the signifance of the fact that the diff. between each square is unique?
40  because they are unique, we can store every single diff. (offset by 119) in an array for lookup.
41  This way, we can quickly check if a square can be attacked by just finding the difference between the indexes
42*/
43
44// 10110101
45#[rustfmt::skip]
46pub const ATTACKS: [u8; 239] = [
47  20, 0, 0, 0, 0, 0, 0, 24,  0, 0, 0, 0, 0, 0,20, 0,
48   0,20, 0, 0, 0, 0, 0, 24,  0, 0, 0, 0, 0,20, 0, 0,
49   0, 0,20, 0, 0, 0, 0, 24,  0, 0, 0, 0,20, 0, 0, 0,
50   0, 0, 0,20, 0, 0, 0, 24,  0, 0, 0,20, 0, 0, 0, 0,
51   0, 0, 0, 0,20, 0, 0, 24,  0, 0,20, 0, 0, 0, 0, 0,
52   0, 0, 0, 0, 0,20, 2, 24,  2,20, 0, 0, 0, 0, 0, 0,
53   0, 0, 0, 0, 0, 2,53, 56, 53, 2, 0, 0, 0, 0, 0, 0, // Note the zero in the very middle, it basically represents the current PieceType that is being evaluated for attacks
54  24,24,24,24,24,24,56,  0, 56,24,24,24,24,24,24, 0, // But the PieceType isn't always in the middle? We can "move" it to the middle by adding 119
55   0, 0, 0, 0, 0, 2,181,56,181, 2, 0, 0, 0, 0, 0, 0, // and then applying the difference between two squares to find the index relative to the PieceType in the middle
56   0, 0, 0, 0, 0,20, 2, 24,  2,20, 0, 0, 0, 0, 0, 0,
57   0, 0, 0, 0,20, 0, 0, 24,  0, 0,20, 0, 0, 0, 0, 0,
58   0, 0, 0,20, 0, 0, 0, 24,  0, 0, 0,20, 0, 0, 0, 0,
59   0, 0,20, 0, 0, 0, 0, 24,  0, 0, 0, 0,20, 0, 0, 0,
60   0,20, 0, 0, 0, 0, 0, 24,  0, 0, 0, 0, 0,20, 0, 0,
61  20, 0, 0, 0, 0, 0, 0, 24,  0, 0, 0, 0, 0, 0,20
62];
63
64pub const BOARD_MAP: [u8; 64] = [
65    0, 1, 2, 3, 4, 5, 6, 7, 16, 17, 18, 19, 20, 21, 22, 23, 32, 33, 34, 35, 36, 37, 38, 39, 48, 49,
66    50, 51, 52, 53, 54, 55, 64, 65, 66, 67, 68, 69, 70, 71, 80, 81, 82, 83, 84, 85, 86, 87, 96, 97,
67    98, 99, 100, 101, 102, 103, 112, 113, 114, 115, 116, 117, 118, 119,
68];
69
70pub const FILES: [&str; 8] = ["a", "b", "c", "d", "e", "f", "g", "h"];