chess_turn_engine/game/
castlinginfo.rs

1use super::side::Side;
2use chess_notation_parser::{CastlingType, Square};
3
4/// Path movement from source square to destination square
5pub struct Path {
6    /// Source
7    pub src: Square,
8
9    /// Destination
10    pub dst: Square,
11}
12
13/// Return king's path
14pub fn get_path_king(side: Side, castling_type: CastlingType) -> Path {
15    match side {
16        Side::White => match castling_type {
17            CastlingType::Long => Path {
18                src: Square::E1,
19                dst: Square::C1,
20            },
21            CastlingType::Short => Path {
22                src: Square::E1,
23                dst: Square::G1,
24            },
25        },
26        Side::Black => match castling_type {
27            CastlingType::Long => Path {
28                src: Square::E8,
29                dst: Square::C8,
30            },
31            CastlingType::Short => Path {
32                src: Square::E8,
33                dst: Square::G8,
34            },
35        },
36    }
37}
38
39/// Return rook's path
40pub fn get_path_rook(side: Side, castling_type: CastlingType) -> Path {
41    match side {
42        Side::White => match castling_type {
43            CastlingType::Long => Path {
44                src: Square::A1,
45                dst: Square::D1,
46            },
47            CastlingType::Short => Path {
48                src: Square::H1,
49                dst: Square::F1,
50            },
51        },
52        Side::Black => match castling_type {
53            CastlingType::Long => Path {
54                src: Square::A8,
55                dst: Square::D8,
56            },
57            CastlingType::Short => Path {
58                src: Square::H8,
59                dst: Square::F8,
60            },
61        },
62    }
63}
64
65/// Return squares over which king has to cross in order to reach final castling
66/// formation. Those squares must be check-free in order to make castling valid.
67pub fn get_king_crossing_squares(
68    side: Side,
69    castling_type: CastlingType,
70) -> &'static [Square] {
71    match side {
72        Side::White => match castling_type {
73            CastlingType::Long => &[Square::D1, Square::C1],
74            CastlingType::Short => &[Square::F1, Square::G1],
75        },
76        Side::Black => match castling_type {
77            CastlingType::Long => &[Square::D8, Square::C8],
78            CastlingType::Short => &[Square::F8, Square::G8],
79        },
80    }
81}
82
83/// Return squares all squares between the king and the rook.
84/// Those squares must be empty in order to make castling valid.
85pub fn get_required_empty_squares(
86    side: Side,
87    castling_type: CastlingType,
88) -> &'static [Square] {
89    match side {
90        Side::White => match castling_type {
91            CastlingType::Long => &[Square::D1, Square::C1, Square::B1],
92            CastlingType::Short => &[Square::F1, Square::G1],
93        },
94        Side::Black => match castling_type {
95            CastlingType::Long => &[Square::D8, Square::C8, Square::B8],
96            CastlingType::Short => &[Square::F8, Square::G8],
97        },
98    }
99}