BenBot 1.7.5
A chess engine
Loading...
Searching...
No Matches
Position.hpp
Go to the documentation of this file.
1/*
2 * ======================================================================================
3 *
4 * ░▒▓███████▓▒░░▒▓████████▓▒░▒▓███████▓▒░ ░▒▓███████▓▒░ ░▒▓██████▓▒░▒▓████████▓▒░
5 * ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░
6 * ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░
7 * ░▒▓███████▓▒░░▒▓██████▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓███████▓▒░░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░
8 * ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░
9 * ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░
10 * ░▒▓███████▓▒░░▒▓████████▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓███████▓▒░ ░▒▓██████▓▒░ ░▒▓█▓▒░
11 *
12 * ======================================================================================
13 */
14
20
25
26#pragma once
27
28#include <cstdint> // IWYU pragma: keep - for std::uint64_t
41#include <magic_enum/magic_enum.hpp>
42#include <optional>
43#include <ranges>
44#include <string>
45
46namespace chess::moves {
47struct Move;
48} // namespace chess::moves
49
55namespace chess::game {
56
57using board::Bitboard;
58using board::File;
59using board::Pieces;
60using board::Square;
61using moves::Move;
62using pieces::Color;
63
65
74struct [[nodiscard]] Position final {
77
84 Pieces whitePieces { Color::White };
85
92 Pieces blackPieces { Color::Black };
93
95 Color sideToMove { Color::White };
96
99
102
109 std::optional<Square> enPassantTargetSquare;
110
121 std::uint_least8_t halfmoveClock { UINT8_C(0) };
122
127 std::uint_least64_t fullMoveCounter { UINT64_C(1) };
128
130 using Hash = std::uint64_t;
131
137 Hash hash { UINT64_C(0) };
138
145 Hash pawnHash { UINT64_C(0) };
146
148 [[nodiscard]] auto operator==(const Position& other) const noexcept -> bool
149 {
150 return hash == other.hash;
151 }
152
155
157 template <Color Side>
158 [[nodiscard]] auto pieces_for() noexcept -> Pieces&
159 {
160 if constexpr (Side == Color::White)
161 return whitePieces;
162 else
163 return blackPieces;
164 }
165
167 template <Color Side>
168 [[nodiscard]] auto pieces_for() const noexcept -> const Pieces&
169 {
170 if constexpr (Side == Color::White)
171 return whitePieces;
172 else
173 return blackPieces;
174 }
175
177 template <Color Side>
178 [[nodiscard]] auto castling_rights_for() const noexcept -> CastlingRights
179 {
180 if constexpr (Side == Color::White)
181 return whiteCastlingRights;
182 else
183 return blackCastlingRights;
184 }
185
187 [[nodiscard]] auto our_pieces() noexcept -> Pieces&
188 {
189 if (is_white_to_move())
190 return whitePieces;
191
192 return blackPieces;
193 }
194
196 [[nodiscard]] auto our_pieces() const noexcept -> const Pieces&
197 {
198 if (is_white_to_move())
199 return whitePieces;
200
201 return blackPieces;
202 }
203
205 [[nodiscard]] auto their_pieces() noexcept -> Pieces&
206 {
207 if (is_white_to_move())
208 return blackPieces;
209
210 return whitePieces;
211 }
212
214 [[nodiscard]] auto their_pieces() const noexcept -> const Pieces&
215 {
216 if (is_white_to_move())
217 return blackPieces;
218
219 return whitePieces;
220 }
221
223
225 [[nodiscard]] auto is_white_to_move() const noexcept -> bool
226 {
227 return sideToMove == Color::White;
228 }
229
231 [[nodiscard]] auto is_black_to_move() const noexcept -> bool
232 {
233 return sideToMove == Color::Black;
234 }
235
239 [[nodiscard]] auto occupied() const noexcept -> Bitboard { return whitePieces.occupied | blackPieces.occupied; }
240
242 [[nodiscard]] auto free() const noexcept -> Bitboard { return occupied().inverse(); }
243
246
250 [[nodiscard]] auto is_file_open(File file) const noexcept -> bool;
251
257 [[nodiscard]] auto get_open_files() const noexcept;
258
262 [[nodiscard]] auto is_file_half_open(File file) const noexcept -> bool;
263
269 [[nodiscard]] auto get_half_open_files() const noexcept;
270
272
274 [[nodiscard]] auto is_check() const noexcept -> bool { return is_side_in_check(sideToMove); }
275
277 template <Color Side>
278 [[nodiscard]] auto get_passed_pawns() const noexcept -> Bitboard;
279
281 template <Color Side>
282 [[nodiscard]] auto get_backward_pawns() const noexcept -> Bitboard;
283
286
288 [[nodiscard]] auto is_checkmate() const -> bool;
289
291 [[nodiscard]] auto is_stalemate() const -> bool;
292
296 [[nodiscard]] auto is_fifty_move_draw() const -> bool;
297
299 [[nodiscard]] auto is_threefold_repetition() const noexcept -> bool;
300
302 [[nodiscard]] auto is_draw_by_insufficient_material() const noexcept -> bool;
303
305 [[nodiscard]] auto is_draw() const -> bool;
306
310 [[nodiscard]] auto get_result() const -> std::optional<Result>;
311
313
319 [[nodiscard]] auto is_legal(Move move) const -> bool;
320
322 [[nodiscard]] auto is_en_passant(Move move) const noexcept -> bool;
323
325 [[nodiscard]] auto is_capture(Move move) const noexcept -> bool;
326
328 [[nodiscard]] auto is_quiet(Move move) const noexcept -> bool;
329
331 void make_move(Move move);
332
339
341 void flip();
342
345
352 [[nodiscard]] auto is_illegal() const -> std::optional<std::string>;
353
358
365 [[nodiscard]] static auto empty() -> Position;
366
370 [[nodiscard]] constexpr auto get_repetition_count() const noexcept -> size_t
371 {
372 return threefoldChecker.repetition_count();
373 }
374
375private:
376 [[nodiscard]] auto is_side_in_check(Color side) const noexcept -> bool;
377
378 ThreefoldChecker threefoldChecker;
379};
380
389[[nodiscard, gnu::pure]] auto after_move(
390 const Position& starting, Move move)
391 -> Position;
392
399[[nodiscard, gnu::pure]] auto after_null_move(const Position& starting) -> Position;
400
407[[nodiscard, gnu::pure]] auto flipped(const Position& starting) -> Position;
408
418[[nodiscard]] auto print_utf8(const Position& position) -> std::string;
419
429[[nodiscard]] auto print_ascii(const Position& position) -> std::string;
430
431/*
432 ___ ,--,
433 ,---, ,--.'|_ ,--, ,--.'|
434 ,---.'| | | :,' ,--.'| | | :
435 | | : : : ' : | |, : : ' .--.--.
436 | | | ,---. .;__,' / ,--.--. `--'_ | ' | / / '
437 ,--.__| | / \ | | | / \ ,' ,'| ' | | | : /`./
438 / ,' | / / |:__,'| : .--. .-. | ' | | | | : | : ;_
439. ' / |. ' / | ' : |__ \__\/: . . | | : ' : |__ \ \ `.
440' ; |: |' ; /| | | '.'| ," .--.; | ' : |__ | | '.'| `----. \
441| | '/ '' | / | ; : ;/ / ,. | | | '.'|; : ;/ /`--' /__ ___ ___
442| : :|| : | | , /; : .' \; : ;| , /'--'. / .\/ .\/ .\
443 \ \ / \ \ / ---`-' | , .-./| , / ---`-' `--'---'\ ; \ ; \ ; |
444 `----' `----' `--`---' ---`-' `--" `--" `--"
445
446 */
447
448inline auto Position::empty() -> Position
449{
450 Position pos { };
451
452 pos.whitePieces = { };
453 pos.blackPieces = { };
454
455 pos.refresh_zobrist();
456
457 return pos;
458}
459
460inline auto Position::is_threefold_repetition() const noexcept -> bool
461{
462 return threefoldChecker.is_threefold();
463}
464
465inline auto Position::is_side_in_check(const Color side) const noexcept -> bool
466{
467 if (side == Color::White) {
469 blackPieces, whitePieces.king, whitePieces.occupied);
470 }
471
473 whitePieces, blackPieces.king, blackPieces.occupied);
474}
475
476inline auto Position::is_legal(const Move move) const -> bool
477{
478 auto copy { *this };
479
480 copy.make_move(move);
481
482 return not copy.is_side_in_check(sideToMove);
483}
484
485inline auto Position::is_en_passant(const Move move) const noexcept -> bool
486{
487 return move.piece() == PieceType::Pawn
488 and enPassantTargetSquare.has_value()
489 and move.to() == *enPassantTargetSquare;
490}
491
492inline auto Position::is_capture(const Move move) const noexcept -> bool
493{
494 return is_en_passant(move) or their_pieces().occupied.test(move.to());
495}
496
497inline auto Position::is_quiet(const Move move) const noexcept -> bool
498{
499 return not(move.is_promotion() or is_capture(move));
500}
501
502inline auto Position::is_file_open(const File file) const noexcept -> bool
503{
504 return whitePieces.is_file_half_open(file) and blackPieces.is_file_half_open(file);
505}
506
507inline auto Position::get_open_files() const noexcept
508{
509 return magic_enum::enum_values<File>()
510 | std::views::filter([this](const File file) { return is_file_open(file); });
511}
512
513inline auto Position::is_file_half_open(const File file) const noexcept -> bool
514{
515 const bool whiteOpen = whitePieces.is_file_half_open(file);
516 const bool blackOpen = blackPieces.is_file_half_open(file);
517
518 // boolean XOR
519 return whiteOpen != blackOpen;
520}
521
522inline auto Position::get_half_open_files() const noexcept
523{
524 return magic_enum::enum_values<File>()
525 | std::views::filter([this](const File file) { return is_file_half_open(file); });
526}
527
528template <Color Side>
529auto Position::get_passed_pawns() const noexcept -> Bitboard
530{
531 static constexpr auto OtherSide = pieces::other_side<Side>();
532
533 const auto friendlyPawns = pieces_for<Side>().pawns;
534 const auto enemyPawns = pieces_for<OtherSide>().pawns;
535
536 const auto mask = board::fills::pawn_front<OtherSide>(
537 enemyPawns | moves::patterns::pawn_attacks<OtherSide>(enemyPawns));
538
539 return friendlyPawns & mask.inverse();
540}
541
542template <Color Side>
544{
545 static constexpr auto OtherSide = pieces::other_side<Side>();
546
547 const auto friendlyPawns = pieces_for<Side>().pawns;
548 const auto enemyPawns = pieces_for<OtherSide>().pawns;
549
550 const auto ourAttackSpans = board::fills::pawn_front<Side>(
552
553 const auto theirAttacks = moves::patterns::pawn_attacks<OtherSide>(enemyPawns);
554
555 const auto backwardArea = board::fills::pawn_rear<Side>(
556 ourAttackSpans.inverse() & theirAttacks);
557
558 return backwardArea & friendlyPawns;
559}
560
561inline auto after_move(
562 const Position& starting, const Move move)
563 -> Position
564{
565 auto copy { starting };
566
567 copy.make_move(move);
568
569 return copy;
570}
571
572inline auto after_null_move(const Position& starting) -> Position
573{
574 auto copy { starting };
575
576 copy.make_null_move();
577
578 return copy;
579}
580
581inline auto flipped(const Position& starting) -> Position
582{
583 auto copy { starting };
584
585 copy.flip();
586
587 return copy;
588}
589
590} // namespace chess::game
constexpr auto pawn_rear(Bitboard starting) noexcept -> Bitboard
Definition Fills.hpp:243
constexpr auto pawn_front(Bitboard starting) noexcept -> Bitboard
Definition Fills.hpp:234
auto print_utf8(const Position &position) -> std::string
auto print_ascii(const Position &position) -> std::string
constexpr auto squares_attacked(const Pieces &pieces, Bitboard targetSquares, Bitboard enemyPieces) noexcept -> bool
Definition Attacks.hpp:81
constexpr auto pawn_attacks(Bitboard starting) noexcept -> Bitboard
Definition Patterns.hpp:120
constexpr auto other_side() noexcept -> Color
Definition Colors.hpp:40
auto flipped(const Position &starting) -> Position
Definition Position.hpp:581
auto after_null_move(const Position &starting) -> Position
Definition Position.hpp:572
auto after_move(const Position &starting, const Move move) -> Position
Definition Position.hpp:561
pieces::Type PieceType
auto is_en_passant(Move move) const noexcept -> bool
Definition Position.hpp:485
std::optional< Square > enPassantTargetSquare
Definition Position.hpp:109
auto is_checkmate() const -> bool
auto is_legal(Move move) const -> bool
Definition Position.hpp:476
auto is_fifty_move_draw() const -> bool
auto our_pieces() const noexcept -> const Pieces &
Definition Position.hpp:196
auto get_result() const -> std::optional< Result >
auto pieces_for() noexcept -> Pieces &
Definition Position.hpp:158
auto their_pieces() noexcept -> Pieces &
Definition Position.hpp:205
auto get_half_open_files() const noexcept
Definition Position.hpp:522
auto is_black_to_move() const noexcept -> bool
Definition Position.hpp:231
static auto empty() -> Position
Definition Position.hpp:448
auto is_file_open(File file) const noexcept -> bool
Definition Position.hpp:502
auto get_open_files() const noexcept
Definition Position.hpp:507
std::uint_least8_t halfmoveClock
Definition Position.hpp:121
auto our_pieces() noexcept -> Pieces &
Definition Position.hpp:187
auto get_backward_pawns() const noexcept -> Bitboard
Definition Position.hpp:543
auto occupied() const noexcept -> Bitboard
Definition Position.hpp:239
auto their_pieces() const noexcept -> const Pieces &
Definition Position.hpp:214
auto get_passed_pawns() const noexcept -> Bitboard
Definition Position.hpp:529
std::uint_least64_t fullMoveCounter
Definition Position.hpp:127
auto pieces_for() const noexcept -> const Pieces &
Definition Position.hpp:168
auto is_illegal() const -> std::optional< std::string >
std::uint64_t Hash
Definition Position.hpp:130
auto is_capture(Move move) const noexcept -> bool
Definition Position.hpp:492
void make_move(Move move)
CastlingRights blackCastlingRights
Definition Position.hpp:101
auto castling_rights_for() const noexcept -> CastlingRights
Definition Position.hpp:178
auto is_quiet(Move move) const noexcept -> bool
Definition Position.hpp:497
auto operator==(const Position &other) const noexcept -> bool
Definition Position.hpp:148
CastlingRights whiteCastlingRights
Definition Position.hpp:98
auto is_stalemate() const -> bool
auto is_draw() const -> bool
auto free() const noexcept -> Bitboard
Definition Position.hpp:242
auto is_check() const noexcept -> bool
Definition Position.hpp:274
auto is_draw_by_insufficient_material() const noexcept -> bool
auto is_threefold_repetition() const noexcept -> bool
Definition Position.hpp:460
auto is_white_to_move() const noexcept -> bool
Definition Position.hpp:225
constexpr auto get_repetition_count() const noexcept -> size_t
Definition Position.hpp:370
auto is_file_half_open(File file) const noexcept -> bool
Definition Position.hpp:513