BenBot 1.7.5
A chess engine
Loading...
Searching...
No Matches
CastlingRights.hpp
Go to the documentation of this file.
1/*
2 * ======================================================================================
3 *
4 * ░▒▓███████▓▒░░▒▓████████▓▒░▒▓███████▓▒░ ░▒▓███████▓▒░ ░▒▓██████▓▒░▒▓████████▓▒░
5 * ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░
6 * ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░
7 * ░▒▓███████▓▒░░▒▓██████▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓███████▓▒░░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░
8 * ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░
9 * ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░
10 * ░▒▓███████▓▒░░▒▓████████▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓███████▓▒░ ░▒▓██████▓▒░ ░▒▓█▓▒░
11 *
12 * ======================================================================================
13 */
14
19
20#pragma once
21
27
28namespace chess::game {
29
30using board::File;
31using board::Rank;
32using moves::Move;
33using pieces::Color;
35
40struct CastlingRights final {
42 bool kingside { true };
43
45 bool queenside { true };
46
48 [[nodiscard]] constexpr auto operator==(const CastlingRights&) const noexcept -> bool = default;
49
53 constexpr void king_moved() noexcept;
54
60 constexpr void rook_moved(bool isKingside) noexcept;
61
63 constexpr void our_move(Move move) noexcept;
64
70 template <Color Side>
71 constexpr void their_move(Move move) noexcept;
72
74 [[nodiscard]] constexpr auto either() const noexcept -> bool { return kingside or queenside; }
75
77 [[nodiscard]] constexpr auto neither() const noexcept -> bool { return not either(); }
78};
79
80/*
81 ___ ,--,
82 ,---, ,--.'|_ ,--, ,--.'|
83 ,---.'| | | :,' ,--.'| | | :
84 | | : : : ' : | |, : : ' .--.--.
85 | | | ,---. .;__,' / ,--.--. `--'_ | ' | / / '
86 ,--.__| | / \ | | | / \ ,' ,'| ' | | | : /`./
87 / ,' | / / |:__,'| : .--. .-. | ' | | | | : | : ;_
88. ' / |. ' / | ' : |__ \__\/: . . | | : ' : |__ \ \ `.
89' ; |: |' ; /| | | '.'| ," .--.; | ' : |__ | | '.'| `----. \
90| | '/ '' | / | ; : ;/ / ,. | | | '.'|; : ;/ /`--' /__ ___ ___
91| : :|| : | | , /; : .' \; : ;| , /'--'. / .\/ .\/ .\
92 \ \ / \ \ / ---`-' | , .-./| , / ---`-' `--'---'\ ; \ ; \ ; |
93 `----' `----' `--`---' ---`-' `--" `--" `--"
94
95 */
96
97constexpr void CastlingRights::king_moved() noexcept
98{
99 kingside = false;
100 queenside = false;
101}
102
103constexpr void CastlingRights::rook_moved(const bool isKingside) noexcept
104{
105 // if the rook wasn't already on its starting square, then the castling rights
106 // to that side would've already been lost, so we can tell if any rook move
107 // should lose castling rights to that side with a simple boolean indicating
108 // if the move's from square is on the kingside
109
110 kingside = kingside and not isKingside;
111 queenside = queenside and isKingside;
112}
113
114constexpr void CastlingRights::our_move(const Move move) noexcept
115{
116 switch (move.piece()) {
117 case PieceType::King : king_moved(); return;
118 case PieceType::Rook : rook_moved(move.from().is_kingside()); return;
119 case PieceType::Pawn : [[fallthrough]];
120 case PieceType::Knight: [[fallthrough]];
121 case PieceType::Bishop: [[fallthrough]];
122 case PieceType::Queen : [[fallthrough]];
123 default : return;
124 }
125}
126
127template <Color Side>
128constexpr void CastlingRights::their_move(const Move move) noexcept
129{
130 // we want to mark castling rights as lost when a rook is captured
131 // we simply test if the move's to square is the rook's starting position,
132 // since the rook either must've been there (in which case this move is a
133 // rook capture), or must've already moved (meaning the rights to that side
134 // must've already been lost)
135
136 static constexpr auto backRank = board::back_rank_for(Side);
137
138 const auto [file, rank] = move.to();
139
140 if (rank != backRank)
141 return;
142
143 kingside = kingside and (file != File::H);
144 queenside = queenside and (file != File::A);
145}
146
147} // namespace chess::game
constexpr auto back_rank_for(Color color) noexcept -> Rank
Definition Rank.hpp:132
pieces::Type PieceType
constexpr auto operator==(const CastlingRights &) const noexcept -> bool=default
constexpr void rook_moved(bool isKingside) noexcept
constexpr void their_move(Move move) noexcept
constexpr void our_move(Move move) noexcept
constexpr auto either() const noexcept -> bool
constexpr auto neither() const noexcept -> bool
constexpr void king_moved() noexcept