BenBot 1.7.5
A chess engine
Loading...
Searching...
No Matches
AttackInfo.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
29
30namespace chess::moves {
31
32using board::Bitboard;
33using game::Position;
34using pieces::Color;
35
45struct AttackInfo final {
47 struct Side final {
54
57
62
64 [[nodiscard]] constexpr auto operator==(const Side&) const noexcept -> bool = default;
65 };
66
69
72
74 template <Color C>
75 [[nodiscard]] constexpr auto side() const noexcept -> const Side&
76 {
77 if constexpr (C == Color::White)
78 return white;
79 else
80 return black;
81 }
82
84 [[nodiscard]] constexpr auto side(const Color color) const noexcept -> const Side&
85 {
86 return color == Color::White ? white : black;
87 }
88
90 [[nodiscard]] constexpr auto operator==(const AttackInfo&) const noexcept -> bool = default;
91};
92
104template <typename Callback>
105[[nodiscard]] auto compute_attacks(
106 const Position& position, Callback&& onPieceAttacks)
107 -> AttackInfo;
108
113[[nodiscard]] inline auto compute_attacks(
114 const Position& position)
115 -> AttackInfo;
116
117/*
118 ___ ,--,
119 ,---, ,--.'|_ ,--, ,--.'|
120 ,---.'| | | :,' ,--.'| | | :
121 | | : : : ' : | |, : : ' .--.--.
122 | | | ,---. .;__,' / ,--.--. `--'_ | ' | / / '
123 ,--.__| | / \ | | | / \ ,' ,'| ' | | | : /`./
124 / ,' | / / |:__,'| : .--. .-. | ' | | | | : | : ;_
125. ' / |. ' / | ' : |__ \__\/: . . | | : ' : |__ \ \ `.
126' ; |: |' ; /| | | '.'| ," .--.; | ' : |__ | | '.'| `----. \
127| | '/ '' | / | ; : ;/ / ,. | | | '.'|; : ;/ /`--' /__ ___ ___
128| : :|| : | | , /; : .' \; : ;| , /'--'. / .\/ .\/ .\
129 \ \ / \ \ / ---`-' | , .-./| , / ---`-' `--'---'\ ; \ ; \ ; |
130 `----' `----' `--`---' ---`-' `--" `--" `--"
131
132 */
133
134#ifndef DOXYGEN
135namespace detail {
136
137 template <Color Side, typename Callback>
138 void compute_piece_attacks(
139 const Position& position, const AttackInfo& info,
140 AttackInfo::Side& result, Callback& onPieceAttacks)
141 {
142 using PieceType = pieces::Type;
143
144 const auto& pieces = position.pieces_for<Side>();
145
146 const auto occupied = position.occupied();
147
148 // each piece's attacks are added to the running totals one at a time,
149 // so that two pieces of the same type attacking a square show up in twice
150 auto add_attacks = [&result](const Bitboard attacks) noexcept {
151 result.twice |= result.all & attacks;
152 result.all |= attacks;
153 };
154
155 for (const auto square : pieces.knights.squares()) {
156 const auto attacks = patterns::knight(Bitboard::from_square(square));
157 add_attacks(attacks);
158 result.knights |= attacks;
159 onPieceAttacks(Side, PieceType::Knight, attacks, info);
160 }
161
162 for (const auto square : pieces.bishops.squares()) {
163 const auto attacks = magics::bishop(square, occupied, { });
164 add_attacks(attacks);
165 result.bishops |= attacks;
166 onPieceAttacks(Side, PieceType::Bishop, attacks, info);
167 }
168
169 for (const auto square : pieces.rooks.squares()) {
170 const auto attacks = magics::rook(square, occupied, { });
171 add_attacks(attacks);
172 result.rooks |= attacks;
173 onPieceAttacks(Side, PieceType::Rook, attacks, info);
174 }
175
176 for (const auto square : pieces.queens.squares()) {
177 const auto attacks = magics::queen(square, occupied, { });
178 add_attacks(attacks);
179 result.queens |= attacks;
180 onPieceAttacks(Side, PieceType::Queen, attacks, info);
181 }
182 }
183
184} // namespace detail
185#endif
186
187template <typename Callback>
189 const Position& position, Callback&& onPieceAttacks)
190 -> AttackInfo
191{
192 AttackInfo info;
193
194 info.white.pawns = patterns::pawn_attacks<Color::White>(position.whitePieces.pawns);
195 info.black.pawns = patterns::pawn_attacks<Color::Black>(position.blackPieces.pawns);
196
197 info.white.king = patterns::king(position.whitePieces.king);
198 info.black.king = patterns::king(position.blackPieces.king);
199
200 // squares attacked by two pawns (one capturing east & one capturing west), or by a pawn & the king
201 info.white.twice = board::shifts::pawn_capture_east<Color::White>(position.whitePieces.pawns)
202 & board::shifts::pawn_capture_west<Color::White>(position.whitePieces.pawns);
203 info.black.twice = board::shifts::pawn_capture_east<Color::Black>(position.blackPieces.pawns)
204 & board::shifts::pawn_capture_west<Color::Black>(position.blackPieces.pawns);
205
206 info.white.twice |= info.white.pawns & info.white.king;
207 info.black.twice |= info.black.pawns & info.black.king;
208
209 info.white.all = info.white.pawns | info.white.king;
210 info.black.all = info.black.pawns | info.black.king;
211
212 detail::compute_piece_attacks<Color::White>(position, info, info.white, onPieceAttacks);
213 detail::compute_piece_attacks<Color::Black>(position, info, info.black, onPieceAttacks);
214
215 return info;
216}
217
218inline auto compute_attacks(
219 const Position& position)
220 -> AttackInfo
221{
222 return compute_attacks(
223 position,
224 [](Color, pieces::Type, Bitboard, const AttackInfo&) noexcept { });
225}
226
227} // namespace chess::moves
constexpr auto pawn_capture_west(Bitboard board) noexcept -> Bitboard
Definition Shifts.hpp:198
constexpr auto pawn_capture_east(Bitboard board) noexcept -> Bitboard
Definition Shifts.hpp:180
auto queen(Square queenPos, Bitboard occupiedSquares, Bitboard friendlyPieces) -> Bitboard
constexpr auto pawn_attacks(Bitboard starting) noexcept -> Bitboard
Definition Patterns.hpp:120
constexpr auto knight(Bitboard starting) noexcept -> Bitboard
Definition Patterns.hpp:128
auto compute_attacks(const Position &position, Callback &&onPieceAttacks) -> AttackInfo
auto bishop(Square bishopPos, Bitboard occupiedSquares, Bitboard friendlyPieces) -> Bitboard
constexpr auto king(Bitboard starting) noexcept -> Bitboard
Definition Patterns.hpp:181
auto rook(Square rookPos, Bitboard occupiedSquares, Bitboard friendlyPieces) -> Bitboard
pieces::Type PieceType
Definition Move.hpp:54
auto pieces_for() noexcept -> Pieces &
Definition Position.hpp:158
auto occupied() const noexcept -> Bitboard
Definition Position.hpp:239
Bitboard knights
Squares attacked by knights.
Bitboard king
Squares attacked by the king.
Bitboard queens
Squares attacked by queens.
Bitboard pawns
Squares attacked by pawns.
constexpr auto operator==(const Side &) const noexcept -> bool=default
Bitboard bishops
Squares attacked by bishops.
Bitboard rooks
Squares attacked by rooks.
constexpr auto side() const noexcept -> const Side &
constexpr auto side(const Color color) const noexcept -> const Side &
constexpr auto operator==(const AttackInfo &) const noexcept -> bool=default
static constexpr auto from_square(Square square) noexcept -> Bitboard
Definition Bitboard.hpp:279