HooverChessUtils_PgnReader 0.9.0
Loading...
Searching...
No Matches
chessboard.h
Go to the documentation of this file.
1// Hoover Chess Utilities / PGN reader
2// Copyright (C) 2024-2025 Sami Kiminki
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program. If not, see <https://www.gnu.org/licenses/>.
16
17#ifndef HOOVER_CHESS_UTILS__PGN_READER__CHESSBOARD_H_INCLUDED
18#define HOOVER_CHESS_UTILS__PGN_READER__CHESSBOARD_H_INCLUDED
19
20#include "pgnreader-types.h"
21#include "chessboard-types.h"
23
24#include <array>
25#include <cassert>
26#include <cinttypes>
27#include <type_traits>
28
30{
31
32// forward declarations
33class ChessBoard;
34struct MoveGenFunctions;
35class MoveGenFunctionTables;
36
37
47using ArrayBoard = std::array<PieceAndColorCompact, 64U>;
48
79
81using MoveTypeAndPromotionUnderlyingType = std::uint_fast8_t;
82
91{
95
98
101
104
107
110
126 CASTLING_SHORT = 6U,
127
143 CASTLING_LONG = 7U,
144
146 PROMO_KNIGHT = 8U,
147
149 PROMO_BISHOP = 9U,
150
152 PROMO_ROOK = 10U,
153
155 PROMO_QUEEN = 11U,
156
169 EN_PASSANT = 12U,
170
179 ILLEGAL = 15U
180};
181
182class CompactMove;
183
197class Move
198{
199private:
214 std::uint_fast16_t m_encoded { };
215
216 static constexpr std::uint_fast16_t getMoveTypeAndPromotionMask(
218 {
219 assert(mask <= 0x0FU);
220 return std::uint_fast16_t(mask) << 6U;
221 }
222
223public:
225 constexpr Move() noexcept = default;
226
238 constexpr Move(Square src, Square dst, MoveTypeAndPromotion typeAndPromo) noexcept :
239 m_encoded(
240 (static_cast<std::uint16_t>(src)) |
241 (static_cast<std::uint16_t>(typeAndPromo) << 6) |
242 (static_cast<std::uint16_t>(dst) << 10U)
243 )
244 {
245 assert(isValidSquare(src));
246 assert(isValidSquare(dst));
247 assert((typeAndPromo <= MoveTypeAndPromotion::EN_PASSANT) ||
248 (typeAndPromo == MoveTypeAndPromotion::ILLEGAL));
249 }
250
252 constexpr Move(const Move &) noexcept = default;
253
255 constexpr Move(Move &&) noexcept = default;
256
258 Move &operator = (const Move &) noexcept = default;
259
261 Move &operator = (Move &&) noexcept = default;
262
266 constexpr inline Move(const CompactMove &m) noexcept;
267
269 ~Move() noexcept = default;
270
275 constexpr Square getSrc() const noexcept
276 {
277 return static_cast<Square>(m_encoded & 63U);
278 }
279
284 constexpr Square getDst() const noexcept
285 {
286 return static_cast<Square>(m_encoded >> 10U);
287 }
288
290 constexpr MoveTypeAndPromotion getTypeAndPromotion() const noexcept
291 {
292 return static_cast<MoveTypeAndPromotion>((m_encoded >> 6U) & 0xFU);
293 }
294
306 constexpr bool isRegularMove() const noexcept
307 {
314
315 constexpr auto fieldMask { getMoveTypeAndPromotionMask(0x0FU) };
316 return (m_encoded & fieldMask) <= getMoveTypeAndPromotionMask(0x05U);
317
318 //return getTypeAndPromotion() <= MoveTypeAndPromotion::REGULAR_KING_MOVE;
319 }
320
327 constexpr bool isEnPassantMove() const noexcept
328 {
330 }
331
341 constexpr bool isPromotionMove() const noexcept
342 {
347
348 constexpr auto fieldMask { getMoveTypeAndPromotionMask(0x0CU) };
349 return (m_encoded & fieldMask) == getMoveTypeAndPromotionMask(0x08U);
350 }
351
359 constexpr bool isCastlingMove() const noexcept
360 {
363
364 constexpr auto fieldMask { getMoveTypeAndPromotionMask(0x0EU) };
365 return (m_encoded & fieldMask) == getMoveTypeAndPromotionMask(0x06U);
366 }
367
369 constexpr Piece getPromotionPiece() const noexcept
370 {
371 assert(isPromotionMove());
372
373 using UnderlyingType = PieceUnderlyingType;
374 static_assert(std::is_same_v<UnderlyingType, PieceUnderlyingType>);
375
376 constexpr UnderlyingType bias =
377 static_cast<UnderlyingType>(MoveTypeAndPromotion::PROMO_KNIGHT) -
378 static_cast<UnderlyingType>(Piece::KNIGHT);
379
380 static_assert(
381 (static_cast<UnderlyingType>(Piece::KNIGHT) + bias) ==
382 static_cast<UnderlyingType>(MoveTypeAndPromotion::PROMO_KNIGHT));
383 static_assert(
384 (static_cast<UnderlyingType>(Piece::BISHOP) + bias) ==
385 static_cast<UnderlyingType>(MoveTypeAndPromotion::PROMO_BISHOP));
386 static_assert(
387 (static_cast<UnderlyingType>(Piece::ROOK) + bias) ==
388 static_cast<UnderlyingType>(MoveTypeAndPromotion::PROMO_ROOK));
389 static_assert(
390 (static_cast<UnderlyingType>(Piece::KNIGHT) + bias) ==
391 static_cast<UnderlyingType>(MoveTypeAndPromotion::PROMO_KNIGHT));
392
393 return Piece(static_cast<UnderlyingType>(getTypeAndPromotion()) - bias);
394 }
395
402 constexpr bool isIllegal() const noexcept
403 {
404 // this is slightly faster than extracting and comparing the
405 // MoveTypeAndPromotion field
406 return m_encoded >= 0xFFC0U;
407 }
408
412 constexpr std::uint_fast16_t getEncodedValue() const noexcept
413 {
414 return m_encoded;
415 }
416
421 constexpr bool operator == (const Move &o) const noexcept
422 {
423 return m_encoded == o.m_encoded;
424 }
425
429 static constexpr Move illegalNoMove() noexcept
430 {
432 }
433
437 static constexpr Move illegalAmbiguousMove() noexcept
438 {
440 }
441};
442
446{
447private:
462 std::uint16_t m_encoded { };
463
464public:
466 CompactMove() = default;
467
469 CompactMove(const CompactMove &) = default;
470
473
475 CompactMove &operator = (const CompactMove &) & = default;
476
479
481 ~CompactMove() = default;
482
484 constexpr CompactMove(const Move &m) noexcept :
485 m_encoded(m.getEncodedValue())
486 {
487 assert(m.getEncodedValue() <= 0xFFFFU);
488 }
489
493 constexpr std::uint16_t getEncodedValue() const noexcept
494 {
495 return m_encoded;
496 }
497};
498
499constexpr Move::Move(const CompactMove &m) noexcept :
500 m_encoded { m.getEncodedValue() }
501{
502}
503
508using MoveList = std::array<CompactMove, 256U>;
509
514using ShortMoveList = std::array<CompactMove, 8U>;
515
518enum class MoveGenType
519{
522 NO_CHECK = 0U,
523
528 CHECK,
529
533};
534
535
588class ChessBoard final
589{
590public:
591
596 ChessBoard() noexcept;
597
599 ChessBoard(const ChessBoard &) noexcept = default;
600
602 ChessBoard(ChessBoard &&) noexcept = default;
603
605 ChessBoard &operator = (const ChessBoard &) noexcept = default;
606
608 ChessBoard &operator = (ChessBoard &&) noexcept = default;
609
611 ~ChessBoard() noexcept = default;
612
649 const ArrayBoard &board,
650 Square whiteLongCastleRook, Square whiteShortCastleRook,
651 Square blackLongCastleRook, Square blackShortCastleRook,
652 Square epSquare,
653 std::uint_fast8_t halfMoveClock, std::uint_fast32_t plyNum);
654
660 void getArrayBoard(ArrayBoard &out_board) const noexcept;
661
698 const BitBoard &board,
699 Square whiteLongCastleRook, Square whiteShortCastleRook,
700 Square blackLongCastleRook, Square blackShortCastleRook,
701 Square epSquare,
702 std::uint_fast8_t halfMoveClock, std::uint_fast32_t plyNum);
703
727 void loadFEN(std::string_view fen);
728
763 inline PositionStatus determineStatus() const noexcept;
764
771 void loadStartPos() noexcept
772 {
773 *this = ChessBoard { };
774 }
775
778 void printBoard() const;
779
788
797
801 inline SquareSet getOccupancyMask() const noexcept
802 {
803 return m_occupancyMask;
804 }
805
809 inline SquareSet getWhitePieces() const noexcept
810 {
811 const std::uint_fast64_t flipBits { getTurn() == Color::WHITE ? UINT64_C(0) : ~UINT64_C(0) };
812
813 return m_occupancyMask & (m_turnColorMask ^ SquareSet { flipBits });
814 }
815
819 inline SquareSet getBlackPieces() const noexcept
820 {
821 const std::uint_fast64_t flipBits { getTurn() == Color::WHITE ? ~UINT64_C(0) : UINT64_C(0) };
822
823 return m_occupancyMask & (m_turnColorMask ^ SquareSet { flipBits });
824 }
825
830 inline SquareSet getPiecesInTurn() const noexcept
831 {
832 return m_turnColorMask;
833 }
834
838 inline SquareSet getPawns() const noexcept
839 {
840 return m_pawns;
841 }
842
846 inline SquareSet getKnights() const noexcept
847 {
848 return m_knights;
849 }
850
854 inline SquareSet getBishops() const noexcept
855 {
856 return m_bishops & ~m_rooks;
857 }
858
862 inline SquareSet getRooks() const noexcept
863 {
864 return m_rooks & ~m_bishops;
865 }
866
870 inline SquareSet getQueens() const noexcept
871 {
872 return m_rooks & m_bishops;
873 }
874
878 inline SquareSet getBishopsAndQueens() const noexcept
879 {
880 return m_bishops;
881 }
882
886 inline SquareSet getRooksAndQueens() const noexcept
887 {
888 return m_rooks;
889 }
890
894 inline SquareSet getKings() const noexcept
895 {
896 return m_kings;
897 }
898
902 inline Square getKingInTurn() const noexcept
903 {
904 return m_kingSq;
905 }
906
910 inline Square getKingNotInTurn() const noexcept
911 {
912 return m_oppKingSq;
913 }
914
918 inline bool isInCheck() const noexcept
919 {
920 return m_checkers != SquareSet::none();
921 }
922
926 inline SquareSet getCheckers() const noexcept
927 {
928 return m_checkers;
929 }
930
1001 inline SquareSet getPinnedPieces() const noexcept
1002 {
1003 return m_pinnedPieces;
1004 }
1005
1010 inline Square getWhiteLongCastleRook() const noexcept
1011 {
1012 return getCastlingRook(Color::WHITE, false);
1013 }
1014
1019 inline Square getWhiteShortCastleRook() const noexcept
1020 {
1021 return getCastlingRook(Color::WHITE, true);
1022 }
1023
1028 inline Square getBlackLongCastleRook() const noexcept
1029 {
1030 return getCastlingRook(Color::BLACK, false);
1031 }
1032
1037 inline Square getBlackShortCastleRook() const noexcept
1038 {
1039 return getCastlingRook(Color::BLACK, true);
1040 }
1041
1049 inline Square getCastlingRook(Color c, bool shortCastling) const noexcept
1050 {
1051 return m_castlingRooks[getCastlingRookIndex(c, shortCastling)];
1052 }
1053
1069 inline Square getEpSquare() const noexcept
1070 {
1071 return m_epSquare;
1072 }
1073
1078 inline std::uint_fast8_t getHalfMoveClock() const noexcept
1079 {
1080 return m_halfMoveClock;
1081 }
1082
1089 inline std::uint_fast32_t getCurrentPlyNum() const noexcept
1090 {
1091 return m_plyNum;
1092 }
1093
1097 inline Color getTurn() const noexcept
1098 {
1099 return Color { static_cast<std::uint_fast8_t>((m_plyNum & 1U) * 8U) };
1100 }
1101
1109 inline Move generateSingleMoveForPawnAndDestNoCapture(SquareSet srcSqMask, Square dst) const noexcept;
1110
1118 inline Move generateSingleMoveForPawnAndDestCapture(SquareSet srcSqMask, Square dst) const noexcept;
1119
1128 inline Move generateSingleMoveForPawnAndDestPromoNoCapture(SquareSet srcSqMask, Square dst, Piece promo) const noexcept;
1129
1138 inline Move generateSingleMoveForPawnAndDestPromoCapture(SquareSet srcSqMask, Square dst, Piece promo) const noexcept;
1139
1147 inline Move generateSingleMoveForKnightAndDest(SquareSet srcSqMask, Square dst) const noexcept;
1148
1156 inline Move generateSingleMoveForBishopAndDest(SquareSet srcSqMask, Square dst) const noexcept;
1157
1165 inline Move generateSingleMoveForRookAndDest(SquareSet srcSqMask, Square dst) const noexcept;
1166
1174 inline Move generateSingleMoveForQueenAndDest(SquareSet srcSqMask, Square dst) const noexcept;
1175
1183 inline Move generateSingleMoveForKingAndDest(SquareSet srcSqMask, Square dst) const noexcept;
1184
1189 inline Move generateSingleMoveForShortCastling() const noexcept;
1190
1195 inline Move generateSingleMoveForLongCastling() const noexcept;
1196
1204 inline std::size_t generateMovesForPawnAndDestNoCapture(ShortMoveList &moves, SquareSet srcSqMask, Square dst) const noexcept;
1205
1213 inline std::size_t generateMovesForPawnAndDestCapture(ShortMoveList &moves, SquareSet srcSqMask, Square dst) const noexcept;
1214
1223 inline std::size_t generateMovesForPawnAndDestPromoNoCapture(ShortMoveList &moves, SquareSet srcSqMask, Square dst, Piece promo) const noexcept;
1224
1233 inline std::size_t generateMovesForPawnAndDestPromoCapture(ShortMoveList &moves, SquareSet srcSqMask, Square dst, Piece promo) const noexcept;
1234
1242 inline std::size_t generateMovesForKnightAndDest(ShortMoveList &moves, SquareSet srcSqMask, Square dst) const noexcept;
1243
1251 inline std::size_t generateMovesForBishopAndDest(ShortMoveList &moves, SquareSet srcSqMask, Square dst) const noexcept;
1252
1260 inline std::size_t generateMovesForRookAndDest(ShortMoveList &moves, SquareSet srcSqMask, Square dst) const noexcept;
1261
1269 inline std::size_t generateMovesForQueenAndDest(ShortMoveList &moves, SquareSet srcSqMask, Square dst) const noexcept;
1270
1278 inline std::size_t generateMovesForKingAndDest(ShortMoveList &moves, SquareSet srcSqMask, Square dst) const noexcept;
1279
1284 inline std::size_t generateMovesForShortCastling(ShortMoveList &moves) const noexcept;
1285
1290 inline std::size_t generateMovesForLongCastling(ShortMoveList &moves) const noexcept;
1291
1296 inline std::size_t generateMoves(MoveList &moves) const noexcept;
1297
1305 inline std::size_t getNumberOfLegalMoves() const noexcept;
1306
1311 inline bool hasLegalMoves() const noexcept;
1312
1327 void doMove(Move m) noexcept;
1328
1333 bool operator == (const ChessBoard &o) const noexcept;
1334
1335private:
1340
1345
1350
1357
1362
1370
1378
1383
1388
1393
1397 std::uint32_t m_plyNum { };
1398
1402 std::uint8_t m_halfMoveClock { };
1403
1408
1413
1418
1431
1435 void updateCheckersAndPins() noexcept;
1436
1442
1450 static constexpr inline std::size_t getCastlingRookIndex(Color c, bool shortCastling) noexcept
1451 {
1452 static_assert(static_cast<std::size_t>(Color::WHITE) == 0U);
1453 static_assert(static_cast<std::size_t>(Color::BLACK) == 8U);
1454
1455 assert((c == Color::WHITE) || (c == Color::BLACK));
1456 [[assume((c == Color::WHITE) || (c == Color::BLACK))]];
1457
1458 return (static_cast<std::size_t>(c) / 4U) + static_cast<std::size_t>(shortCastling);
1459 }
1460
1466 inline void setCastlingRook(Color c, bool shortCastling, Square sq) noexcept
1467 {
1468 m_castlingRooks[getCastlingRookIndex(c, shortCastling)] = sq;
1469 }
1470
1476 inline Square &getCastlingRookRef(Color c, bool shortCastling) noexcept
1477 {
1478 return m_castlingRooks[getCastlingRookIndex(c, shortCastling)];
1479 }
1480
1484 void calculateMasks(const ArrayBoard &board) noexcept;
1485};
1486
1490{
1500
1510
1521
1531 Move (*generateSingleMoveForPawnAndDestPromoCapture)(const ChessBoard &board, SquareSet srcSqMask, Square dst, Piece promo) noexcept;
1532
1541 Move (*generateSingleMoveForKnightAndDest)(const ChessBoard &board, SquareSet srcSqMask, Square dst) noexcept;
1542
1551 Move (*generateSingleMoveForBishopAndDest)(const ChessBoard &board, SquareSet srcSqMask, Square dst) noexcept;
1552
1561 Move (*generateSingleMoveForRookAndDest)(const ChessBoard &board, SquareSet srcSqMask, Square dst) noexcept;
1562
1571 Move (*generateSingleMoveForQueenAndDest)(const ChessBoard &board, SquareSet srcSqMask, Square dst) noexcept;
1572
1581 Move (*generateSingleMoveForKingAndDest)(const ChessBoard &board, SquareSet srcSqMask, Square dst) noexcept;
1582
1588
1594
1603 std::size_t (*generateMovesForPawnAndDestNoCapture)(const ChessBoard &board, ShortMoveList &moves, SquareSet srcSqMask, Square dst) noexcept;
1604
1613 std::size_t (*generateMovesForPawnAndDestCapture)(const ChessBoard &board, ShortMoveList &moves, SquareSet srcSqMask, Square dst) noexcept;
1614
1624 std::size_t (*generateMovesForPawnAndDestPromoNoCapture)(const ChessBoard &board, ShortMoveList &moves, SquareSet srcSqMask, Square dst, Piece promo) noexcept;
1625
1635 std::size_t (*generateMovesForPawnAndDestPromoCapture)(const ChessBoard &board, ShortMoveList &moves, SquareSet srcSqMask, Square dst, Piece promo) noexcept;
1636
1645 std::size_t (*generateMovesForKnightAndDest)(const ChessBoard &board, ShortMoveList &moves, SquareSet srcSqMask, Square dst) noexcept;
1646
1655 std::size_t (*generateMovesForBishopAndDest)(const ChessBoard &board, ShortMoveList &moves, SquareSet srcSqMask, Square dst) noexcept;
1656
1665 std::size_t (*generateMovesForRookAndDest)(const ChessBoard &board, ShortMoveList &moves, SquareSet srcSqMask, Square dst) noexcept;
1666
1675 std::size_t (*generateMovesForQueenAndDest)(const ChessBoard &board, ShortMoveList &moves, SquareSet srcSqMask, Square dst) noexcept;
1676
1685 std::size_t (*generateMovesForKingAndDest)(const ChessBoard &board, ShortMoveList &moves, SquareSet srcSqMask, Square dst) noexcept;
1686
1692 std::size_t (*generateMovesForShortCastling)(const ChessBoard &board, ShortMoveList &moves) noexcept;
1693
1699 std::size_t (*generateMovesForLongCastling)(const ChessBoard &board, ShortMoveList &moves) noexcept;
1700
1705 std::size_t (*generateMoves)(const ChessBoard &board, MoveList &moves) noexcept;
1706
1715 std::size_t (*getNumberOfLegalMoves)(const ChessBoard &board) noexcept;
1716
1722 bool (*hasLegalMoves)(const ChessBoard &board) noexcept;
1723};
1724
1726{
1727 return m_moveGenFns->generateSingleMoveForPawnAndDestNoCapture(*this, srcSqMask, dst);
1728}
1729
1731{
1732 return m_moveGenFns->generateSingleMoveForPawnAndDestCapture(*this, srcSqMask, dst);
1733}
1734
1736{
1737 return m_moveGenFns->generateSingleMoveForPawnAndDestPromoNoCapture(*this, srcSqMask, dst, promo);
1738}
1739
1741{
1742 return m_moveGenFns->generateSingleMoveForPawnAndDestPromoCapture(*this, srcSqMask, dst, promo);
1743}
1744
1746{
1747 return m_moveGenFns->generateSingleMoveForKnightAndDest(*this, srcSqMask, dst);
1748}
1749
1751{
1752 return m_moveGenFns->generateSingleMoveForBishopAndDest(*this, srcSqMask, dst);
1753}
1754
1756{
1757 return m_moveGenFns->generateSingleMoveForRookAndDest(*this, srcSqMask, dst);
1758}
1759
1761{
1762 return m_moveGenFns->generateSingleMoveForQueenAndDest(*this, srcSqMask, dst);
1763}
1764
1766{
1767 return m_moveGenFns->generateSingleMoveForKingAndDest(*this, srcSqMask, dst);
1768}
1769
1774
1779
1780std::size_t ChessBoard::generateMovesForPawnAndDestNoCapture(ShortMoveList &moves, SquareSet srcSqMask, Square dst) const noexcept
1781{
1782 return m_moveGenFns->generateMovesForPawnAndDestNoCapture(*this, moves, srcSqMask, dst);
1783}
1784
1785std::size_t ChessBoard::generateMovesForPawnAndDestCapture(ShortMoveList &moves, SquareSet srcSqMask, Square dst) const noexcept
1786{
1787 return m_moveGenFns->generateMovesForPawnAndDestCapture(*this, moves, srcSqMask, dst);
1788}
1789
1790std::size_t ChessBoard::generateMovesForPawnAndDestPromoNoCapture(ShortMoveList &moves, SquareSet srcSqMask, Square dst, Piece promo) const noexcept
1791{
1792 return m_moveGenFns->generateMovesForPawnAndDestPromoNoCapture(*this, moves, srcSqMask, dst, promo);
1793}
1794
1795std::size_t ChessBoard::generateMovesForPawnAndDestPromoCapture(ShortMoveList &moves, SquareSet srcSqMask, Square dst, Piece promo) const noexcept
1796{
1797 return m_moveGenFns->generateMovesForPawnAndDestPromoCapture(*this, moves, srcSqMask, dst, promo);
1798}
1799
1800std::size_t ChessBoard::generateMovesForKnightAndDest(ShortMoveList &moves, SquareSet srcSqMask, Square dst) const noexcept
1801{
1802 return m_moveGenFns->generateMovesForKnightAndDest(*this, moves, srcSqMask, dst);
1803}
1804
1805std::size_t ChessBoard::generateMovesForBishopAndDest(ShortMoveList &moves, SquareSet srcSqMask, Square dst) const noexcept
1806{
1807 return m_moveGenFns->generateMovesForBishopAndDest(*this, moves, srcSqMask, dst);
1808}
1809
1810std::size_t ChessBoard::generateMovesForRookAndDest(ShortMoveList &moves, SquareSet srcSqMask, Square dst) const noexcept
1811{
1812 return m_moveGenFns->generateMovesForRookAndDest(*this, moves, srcSqMask, dst);
1813}
1814
1815std::size_t ChessBoard::generateMovesForQueenAndDest(ShortMoveList &moves, SquareSet srcSqMask, Square dst) const noexcept
1816{
1817 return m_moveGenFns->generateMovesForQueenAndDest(*this, moves, srcSqMask, dst);
1818}
1819
1820std::size_t ChessBoard::generateMovesForKingAndDest(ShortMoveList &moves, SquareSet srcSqMask, Square dst) const noexcept
1821{
1822 return m_moveGenFns->generateMovesForKingAndDest(*this, moves, srcSqMask, dst);
1823}
1824
1826{
1827 return m_moveGenFns->generateMovesForShortCastling(*this, moves);
1828}
1829
1831{
1832 return m_moveGenFns->generateMovesForLongCastling(*this, moves);
1833}
1834
1835std::size_t ChessBoard::generateMoves(MoveList &moves) const noexcept
1836{
1837 return m_moveGenFns->generateMoves(*this, moves);
1838}
1839
1840std::size_t ChessBoard::getNumberOfLegalMoves() const noexcept
1841{
1842 return m_moveGenFns->getNumberOfLegalMoves(*this);
1843}
1844
1845bool ChessBoard::hasLegalMoves() const noexcept
1846{
1847 return m_moveGenFns->hasLegalMoves(*this);
1848}
1849
1851{
1852 static_assert(PositionStatus::NORMAL == PositionStatus { 0U });
1853 static_assert(PositionStatus::CHECK == PositionStatus { 1U });
1854 static_assert(PositionStatus::STALEMATE == PositionStatus { 2U });
1855 static_assert(PositionStatus::MATE == PositionStatus { 3U });
1856
1857 const bool noLegalMoves { !m_moveGenFns->hasLegalMoves(*this) };
1858 const bool inCheck { m_checkers != SquareSet::none() };
1859
1860 return PositionStatus { static_cast<std::uint_fast8_t>(noLegalMoves * 2U + inCheck) };
1861}
1862
1863
1864static_assert(Move::illegalNoMove().isIllegal());
1866static_assert(Move::illegalAmbiguousMove().isIllegal());
1868
1869}
1870
1871#endif
The chessboard.
Definition chessboard.h:589
Square getCastlingRook(Color c, bool shortCastling) const noexcept
Returns castling rook square if castling rights apply or Square::NONE if no castling rights.
Definition chessboard.h:1049
bool isInCheck() const noexcept
Returns whether the king is in check.
Definition chessboard.h:918
SquareSet getBishopsAndQueens() const noexcept
Returns squares occupied by bishops and queens (diagonal sliders)
Definition chessboard.h:878
Move generateSingleMoveForShortCastling() const noexcept
Generates a legal short castling move.
Definition chessboard.h:1770
void printBoard() const
Prints the current board to stdout. This is intended only for debugging purposes.
Square getEpSquare() const noexcept
Returns the en-passant square or Square::NONE if en-passant capture is not possible.
Definition chessboard.h:1069
bool hasLegalMoves() const noexcept
Determines whether any legal moves as available in the current position.
Definition chessboard.h:1845
void loadFEN(std::string_view fen)
Sets the board from FEN.
std::size_t generateMovesForKnightAndDest(ShortMoveList &moves, SquareSet srcSqMask, Square dst) const noexcept
Generates a list of legal knight moves with a known destination square. May be capturing.
Definition chessboard.h:1800
SquareSet m_rooks
Rooks and queens.
Definition chessboard.h:1374
SquareSet getKnights() const noexcept
Returns squares occupied by knights.
Definition chessboard.h:846
std::size_t generateMovesForQueenAndDest(ShortMoveList &moves, SquareSet srcSqMask, Square dst) const noexcept
Generates a list of legal queen moves with a known destination square. May be capturing.
Definition chessboard.h:1815
SquareSet getBlackPieces() const noexcept
Returns squares occupied by white pieces (including pawns).
Definition chessboard.h:819
void doMove(Move m) noexcept
Applies a move on the current position. The move is assumed to be legal, and it must be from one of t...
Move generateSingleMoveForPawnAndDestNoCapture(SquareSet srcSqMask, Square dst) const noexcept
Generates a legal non-promoting, non-capturing pawn move with a known destination square.
Definition chessboard.h:1725
SquareSet m_bishops
Bishops and queens.
Definition chessboard.h:1366
std::size_t generateMovesForLongCastling(ShortMoveList &moves) const noexcept
Generates the long castling move in a list, if legal.
Definition chessboard.h:1830
Square getWhiteLongCastleRook() const noexcept
Shorthand for getCastlingRook(Color::WHITE, false).
Definition chessboard.h:1010
Move generateSingleMoveForKingAndDest(SquareSet srcSqMask, Square dst) const noexcept
Generates a legal king move with a known destination square. May be capturing.
Definition chessboard.h:1765
std::uint8_t m_halfMoveClock
Current half move clock.
Definition chessboard.h:1402
Square getWhiteShortCastleRook() const noexcept
Shorthand for getCastlingRook(Color::WHITE, true).
Definition chessboard.h:1019
void calculateMasks(const ArrayBoard &board) noexcept
Calculates bitboard masks for an ArrayBoard object.
Square m_oppKingSq
King in not turn.
Definition chessboard.h:1412
Square getKingInTurn() const noexcept
Returns the square of the king in turn.
Definition chessboard.h:902
void updateCheckersAndPins() noexcept
Determines checkers and pinned pieces.
std::size_t generateMovesForPawnAndDestCapture(ShortMoveList &moves, SquareSet srcSqMask, Square dst) const noexcept
Generates a list of legal non-promoting, capturing pawn moves with a known destination square.
Definition chessboard.h:1785
Move generateSingleMoveForRookAndDest(SquareSet srcSqMask, Square dst) const noexcept
Generates a legal rook move with a known destination square. May be capturing.
Definition chessboard.h:1755
SquareSet getPawns() const noexcept
Returns squares occupied by pawns.
Definition chessboard.h:838
SquareSet m_pawns
Pawns.
Definition chessboard.h:1349
SquareSet getBishops() const noexcept
Returns squares occupied by bishops.
Definition chessboard.h:854
SquareSet getKings() const noexcept
Returns squares occupied by kings.
Definition chessboard.h:894
SquareSet getPinnedPieces() const noexcept
Returns squares occupied by absolutely pinned pieces.
Definition chessboard.h:1001
const MoveGenFunctions * m_moveGenFns
Move generator functions for this position.
Definition chessboard.h:1392
Move generateSingleMoveForQueenAndDest(SquareSet srcSqMask, Square dst) const noexcept
Generates a legal queen move with a known destination square. May be capturing.
Definition chessboard.h:1760
std::size_t getNumberOfLegalMoves() const noexcept
Returns the number of legal moves for the current position. This function is mostly useful for calcul...
Definition chessboard.h:1840
SquareSet m_turnColorMask
Occupied squares that have the same color piece as getTurn()
Definition chessboard.h:1344
SquareSet getQueens() const noexcept
Returns squares occupied by queens.
Definition chessboard.h:870
void setBoard(const ArrayBoard &board, Square whiteLongCastleRook, Square whiteShortCastleRook, Square blackLongCastleRook, Square blackShortCastleRook, Square epSquare, std::uint_fast8_t halfMoveClock, std::uint_fast32_t plyNum)
Sets the board from an array board (aka Mailbox)
Square m_epSquare
En-passant square.
Definition chessboard.h:1417
SquareSet getRooksAndQueens() const noexcept
Returns squares occupied by rooks and queens (horizontal/vertical sliders)
Definition chessboard.h:886
std::size_t generateMovesForKingAndDest(ShortMoveList &moves, SquareSet srcSqMask, Square dst) const noexcept
Generates a list of legal king moves with a known destination square. May be capturing.
Definition chessboard.h:1820
static constexpr std::size_t getCastlingRookIndex(Color c, bool shortCastling) noexcept
Returns the array index for a castling rook.
Definition chessboard.h:1450
SquareSet getWhitePieces() const noexcept
Returns squares occupied by white pieces (including pawns).
Definition chessboard.h:809
std::size_t generateMovesForShortCastling(ShortMoveList &moves) const noexcept
Generates the short castling move in a list, if legal.
Definition chessboard.h:1825
Move generateSingleMoveForLongCastling() const noexcept
Generates a legal long castling move.
Definition chessboard.h:1775
std::uint_fast8_t getHalfMoveClock() const noexcept
Current half-move clock.
Definition chessboard.h:1078
std::size_t generateMovesForRookAndDest(ShortMoveList &moves, SquareSet srcSqMask, Square dst) const noexcept
Generates a list of legal rook moves with a known destination square. May be capturing.
Definition chessboard.h:1810
Square & getCastlingRookRef(Color c, bool shortCastling) noexcept
Gets a reference to the castling rights rook for direct read/write access.
Definition chessboard.h:1476
void validateBoard()
Validates the board for items that are common for both setBoard() and loadFEN() and sets m_checkers.
Move generateSingleMoveForKnightAndDest(SquareSet srcSqMask, Square dst) const noexcept
Generates a legal knight move with a known destination square. May be capturing.
Definition chessboard.h:1745
void setCastlingRook(Color c, bool shortCastling, Square sq) noexcept
Sets/resets castling rights and the associated rook.
Definition chessboard.h:1466
void getArrayBoard(ArrayBoard &out_board) const noexcept
Extracts the current position as an ArrayBoard.
std::size_t generateMovesForPawnAndDestNoCapture(ShortMoveList &moves, SquareSet srcSqMask, Square dst) const noexcept
Generates a list of legal non-promoting, non-capturing pawn moves with a known destination square.
Definition chessboard.h:1780
Square m_kingSq
King in turn.
Definition chessboard.h:1407
SquareSet getOccupancyMask() const noexcept
Returns squares that are occupied by pieces (including pawns).
Definition chessboard.h:801
Move generateSingleMoveForPawnAndDestCapture(SquareSet srcSqMask, Square dst) const noexcept
Generates a legal non-promoting, capturing pawn move with a known destination square.
Definition chessboard.h:1730
std::uint32_t m_plyNum
Current ply number.
Definition chessboard.h:1397
SquareSet getRooks() const noexcept
Returns squares occupied by rooks.
Definition chessboard.h:862
Move generateSingleMoveForBishopAndDest(SquareSet srcSqMask, Square dst) const noexcept
Generates a legal bishop move with a known destination square. May be capturing.
Definition chessboard.h:1750
SquareSet m_occupancyMask
Bit board of occupied squares.
Definition chessboard.h:1339
std::size_t generateMovesForPawnAndDestPromoNoCapture(ShortMoveList &moves, SquareSet srcSqMask, Square dst, Piece promo) const noexcept
Generates a list of legal promoting, non-capturing pawn moves with a known destination square.
Definition chessboard.h:1790
std::array< Square, 4U > m_castlingRooks
Castling rooks.
Definition chessboard.h:1430
PieceAndColor getSquarePiece(Square sq) const noexcept
Returns the piece and color on a specified square.
Move generateSingleMoveForPawnAndDestPromoNoCapture(SquareSet srcSqMask, Square dst, Piece promo) const noexcept
Generates a legal promoting, non-capturing pawn move with a known destination square.
Definition chessboard.h:1735
Color getTurn() const noexcept
Returns side to move.
Definition chessboard.h:1097
void loadStartPos() noexcept
Loads the starting position.
Definition chessboard.h:771
SquareSet getCheckers() const noexcept
Returns squares occupied by checkers.
Definition chessboard.h:926
Square getBlackShortCastleRook() const noexcept
Shorthand for getCastlingRook(Color::BLACK, true).
Definition chessboard.h:1037
std::uint_fast32_t getCurrentPlyNum() const noexcept
Full move counter in plies.
Definition chessboard.h:1089
PositionStatus determineStatus() const noexcept
Determines the status of the position.
Definition chessboard.h:1850
SquareSet m_checkers
Squares where there is a checker.
Definition chessboard.h:1382
std::size_t generateMovesForBishopAndDest(ShortMoveList &moves, SquareSet srcSqMask, Square dst) const noexcept
Generates a list of legal bishop moves with a known destination square. May be capturing.
Definition chessboard.h:1805
ChessBoard() noexcept
Constructor (default)
SquareSet m_knights
Knights.
Definition chessboard.h:1354
Piece getSquarePieceNoColor(Square sq) const noexcept
Returns the piece on a specified square.
Square getBlackLongCastleRook() const noexcept
Shorthand for getCastlingRook(Color::BLACK, false).
Definition chessboard.h:1028
Move generateSingleMoveForPawnAndDestPromoCapture(SquareSet srcSqMask, Square dst, Piece promo) const noexcept
Generates a legal promoting, capturing pawn move with a known destination square.
Definition chessboard.h:1740
SquareSet getPiecesInTurn() const noexcept
Returns squares that are occupied by pieces (including pawns) that belong to the side to move.
Definition chessboard.h:830
std::size_t generateMovesForPawnAndDestPromoCapture(ShortMoveList &moves, SquareSet srcSqMask, Square dst, Piece promo) const noexcept
Generates a list of legal promoting, capturing pawn moves with a known destination square.
Definition chessboard.h:1795
SquareSet m_kings
Kings.
Definition chessboard.h:1361
SquareSet m_pinnedPieces
Squares with pinned pieces.
Definition chessboard.h:1387
Square getKingNotInTurn() const noexcept
Returns the square of the king not in turn.
Definition chessboard.h:910
std::size_t generateMoves(MoveList &moves) const noexcept
Generates a list of all legal moves for the current position.
Definition chessboard.h:1835
Compact representation of Move
Definition chessboard.h:446
~CompactMove()=default
Default destructor.
CompactMove & operator=(const CompactMove &) &=default
Default copy assignment.
CompactMove(CompactMove &&)=default
Default move constructor.
std::uint16_t m_encoded
Encoded move.
Definition chessboard.h:462
constexpr std::uint16_t getEncodedValue() const noexcept
Returns raw encoded value. Usually only used in debugging.
Definition chessboard.h:493
CompactMove(const CompactMove &)=default
Default copy constructor.
constexpr CompactMove(const Move &m) noexcept
Constructs a compact move from Move
Definition chessboard.h:484
CompactMove()=default
Default constructor (null move)
A legal move. Important: see the note!
Definition chessboard.h:198
static constexpr Move illegalNoMove() noexcept
Token for illegal move: no moves generated.
Definition chessboard.h:429
constexpr Square getSrc() const noexcept
Returns move source square.
Definition chessboard.h:275
constexpr MoveTypeAndPromotion getTypeAndPromotion() const noexcept
Returns move type and promotion piece.
Definition chessboard.h:290
constexpr Move(const Move &) noexcept=default
Constructor (copy)
constexpr Piece getPromotionPiece() const noexcept
Returns promotion piece of a promotion move.
Definition chessboard.h:369
static constexpr Move illegalAmbiguousMove() noexcept
Token for illegal move: ambiguous move generation.
Definition chessboard.h:437
constexpr std::uint_fast16_t getEncodedValue() const noexcept
Returns raw encoded value. Usually only used in debugging.
Definition chessboard.h:412
constexpr bool operator==(const Move &o) const noexcept
Comparator (equality)
Definition chessboard.h:421
std::uint_fast16_t m_encoded
Encoded move.
Definition chessboard.h:214
constexpr bool isEnPassantMove() const noexcept
Checks whether the move type is en-passant pawn capture.
Definition chessboard.h:327
constexpr Move() noexcept=default
Default constructor (null move)
constexpr bool isRegularMove() const noexcept
Checks whether a move type is regular.
Definition chessboard.h:306
constexpr Square getDst() const noexcept
Returns move destination square.
Definition chessboard.h:284
constexpr bool isIllegal() const noexcept
Checks whether the move type is illegal.
Definition chessboard.h:402
constexpr Move(Move &&) noexcept=default
Constructor (move)
constexpr bool isCastlingMove() const noexcept
Checks whether a move type is a castling move.
Definition chessboard.h:359
static constexpr std::uint_fast16_t getMoveTypeAndPromotionMask(MoveTypeAndPromotionUnderlyingType mask) noexcept
Definition chessboard.h:216
constexpr bool isPromotionMove() const noexcept
Checks whether the move type is a pawn promotion.
Definition chessboard.h:341
Set of squares. Implemented using a bit-mask.
Definition chessboard-types-squareset.h:35
static constexpr SquareSet square(Square sq) noexcept
Returns a set of single square.
Definition chessboard-types-squareset.h:497
static constexpr SquareSet row(RowColumn row) noexcept
Returns a set of squares in row number row.
Definition chessboard-types-squareset.h:485
static constexpr SquareSet none() noexcept
Returns an empty set.
Definition chessboard-types-squareset.h:452
std::array< CompactMove, 256U > MoveList
Move list returned by ChessBoard::generateMoves().
Definition chessboard.h:508
MoveTypeAndPromotion
Move type (4 bits, range: 0..15)
Definition chessboard.h:91
constexpr bool isValidSquare(Square sq) noexcept
Checks whether a value is a square.
Definition chessboard-types.h:306
std::array< PieceAndColorCompact, 64U > ArrayBoard
Board representation using an array.
Definition chessboard.h:47
PieceAndColor
Named piece and color.
Definition chessboard-types.h:219
Piece
Named piece.
Definition chessboard-types.h:204
std::array< CompactMove, 8U > ShortMoveList
Short move list returned by move generators for writing a SAN move. That is, when the piece type and ...
Definition chessboard.h:514
Color
Color of a piece or side to move.
Definition chessboard-types.h:194
Square
Named square.
Definition chessboard-types.h:122
std::uint_fast8_t PieceUnderlyingType
Underlying type of Piece
Definition chessboard-types.h:56
PositionStatus
Status of a position.
Definition chessboard-types.h:276
@ REGULAR_PAWN_MOVE
Regular non-capturing or capturing move (no promotion or en passant)
@ BLACK
Black piece or black side to move.
@ WHITE
White piece or white side to move.
@ NORMAL
Regular position (not in check, mate, or stalemate)
@ CHECK
King is checked (but not mated)
MoveGenType
Move generator type.
Definition chessboard.h:519
@ NO_CHECK
Move generator for when the king is not in check. All moves are considered.
@ DOUBLE_CHECK
Move generator for when the king is in double check. Only king moves are considered.
Definition chessboard-types-squareset.h:30
std::uint_fast8_t MoveTypeAndPromotionUnderlyingType
Underlying type of MoveTypeAndPromotion
Definition chessboard.h:81
Board representation using SquareSets (bit boards)
Definition chessboard.h:57
SquareSet bishops
Bishop squares.
Definition chessboard.h:65
SquareSet rooks
Rook squares.
Definition chessboard.h:68
SquareSet queens
Queen squares.
Definition chessboard.h:71
SquareSet kings
King squares.
Definition chessboard.h:74
SquareSet whitePieces
Squares of white pieces. Square of black pieces are implied.
Definition chessboard.h:77
SquareSet knights
Knight squares.
Definition chessboard.h:62
SquareSet pawns
Pawn squares.
Definition chessboard.h:59
Move generator functions.
Definition chessboard.h:1490
std::size_t(* generateMoves)(const ChessBoard &board, MoveList &moves) noexcept
Generates a list of all legal moves for the current position.
Definition chessboard.h:1705
Move(* generateSingleMoveForPawnAndDestPromoCapture)(const ChessBoard &board, SquareSet srcSqMask, Square dst, Piece promo) noexcept
Generates a legal promoting, capturing pawn move with a known destination square.
Definition chessboard.h:1531
Move(* generateSingleMoveForRookAndDest)(const ChessBoard &board, SquareSet srcSqMask, Square dst) noexcept
Generates a legal rook move with a known destination square. May be capturing.
Definition chessboard.h:1561
std::size_t(* getNumberOfLegalMoves)(const ChessBoard &board) noexcept
Returns the number of legal moves for the current position. This function is mostly useful for calcul...
Definition chessboard.h:1715
std::size_t(* generateMovesForPawnAndDestCapture)(const ChessBoard &board, ShortMoveList &moves, SquareSet srcSqMask, Square dst) noexcept
Generates a list of legal non-promoting, capturing pawn moves with a known destination square.
Definition chessboard.h:1613
std::size_t(* generateMovesForPawnAndDestNoCapture)(const ChessBoard &board, ShortMoveList &moves, SquareSet srcSqMask, Square dst) noexcept
Generates a list of legal non-promoting, non-capturing pawn moves with a known destination square.
Definition chessboard.h:1603
Move(* generateSingleMoveForPawnAndDestCapture)(const ChessBoard &board, SquareSet srcSqMask, Square dst) noexcept
Generates a legal non-promoting, capturing pawn move with a known destination square.
Definition chessboard.h:1509
Move(* generateSingleMoveForBishopAndDest)(const ChessBoard &board, SquareSet srcSqMask, Square dst) noexcept
Generates a legal bishop move with a known destination square. May be capturing.
Definition chessboard.h:1551
std::size_t(* generateMovesForRookAndDest)(const ChessBoard &board, ShortMoveList &moves, SquareSet srcSqMask, Square dst) noexcept
Generates a list of legal rook moves with a known destination square. May be capturing.
Definition chessboard.h:1665
std::size_t(* generateMovesForKingAndDest)(const ChessBoard &board, ShortMoveList &moves, SquareSet srcSqMask, Square dst) noexcept
Generates a list of legal king moves with a known destination square. May be capturing.
Definition chessboard.h:1685
Move(* generateSingleMoveForShortCastling)(const ChessBoard &board) noexcept
Generates a legal short castling move.
Definition chessboard.h:1587
std::size_t(* generateMovesForBishopAndDest)(const ChessBoard &board, ShortMoveList &moves, SquareSet srcSqMask, Square dst) noexcept
Generates a list of legal bishop moves with a known destination square. May be capturing.
Definition chessboard.h:1655
std::size_t(* generateMovesForKnightAndDest)(const ChessBoard &board, ShortMoveList &moves, SquareSet srcSqMask, Square dst) noexcept
Generates a list of legal knight moves with a known destination square. May be capturing.
Definition chessboard.h:1645
bool(* hasLegalMoves)(const ChessBoard &board) noexcept
Determines whether there are any legal moves function is mostly useful for calculating the moves in p...
Definition chessboard.h:1722
Move(* generateSingleMoveForPawnAndDestNoCapture)(const ChessBoard &board, SquareSet srcSqMask, Square dst) noexcept
Generates a legal non-promoting, non-capturing pawn move with a known destination square.
Definition chessboard.h:1499
std::size_t(* generateMovesForLongCastling)(const ChessBoard &board, ShortMoveList &moves) noexcept
Generates the long castling move in a list, if legal.
Definition chessboard.h:1699
std::size_t(* generateMovesForShortCastling)(const ChessBoard &board, ShortMoveList &moves) noexcept
Generates the short castling move in a list, if legal.
Definition chessboard.h:1692
std::size_t(* generateMovesForPawnAndDestPromoCapture)(const ChessBoard &board, ShortMoveList &moves, SquareSet srcSqMask, Square dst, Piece promo) noexcept
Generates a list of legal promoting, capturing pawn moves with a known destination square.
Definition chessboard.h:1635
Move(* generateSingleMoveForKnightAndDest)(const ChessBoard &board, SquareSet srcSqMask, Square dst) noexcept
Generates a legal knight move with a known destination square. May be capturing.
Definition chessboard.h:1541
Move(* generateSingleMoveForPawnAndDestPromoNoCapture)(const ChessBoard &board, SquareSet srcSqMask, Square dst, Piece promo) noexcept
Generates a legal promoting, non-capturing pawn move with a known destination square.
Definition chessboard.h:1520
std::size_t(* generateMovesForPawnAndDestPromoNoCapture)(const ChessBoard &board, ShortMoveList &moves, SquareSet srcSqMask, Square dst, Piece promo) noexcept
Generates a list of legal promoting, non-capturing pawn moves with a known destination square.
Definition chessboard.h:1624
Move(* generateSingleMoveForQueenAndDest)(const ChessBoard &board, SquareSet srcSqMask, Square dst) noexcept
Generates a legal queen move with a known destination square. May be capturing.
Definition chessboard.h:1571
Move(* generateSingleMoveForLongCastling)(const ChessBoard &board) noexcept
Generates a legal long castling move.
Definition chessboard.h:1593
std::size_t(* generateMovesForQueenAndDest)(const ChessBoard &board, ShortMoveList &moves, SquareSet srcSqMask, Square dst) noexcept
Generates a list of legal queen moves with a known destination square. May be capturing.
Definition chessboard.h:1675
Move(* generateSingleMoveForKingAndDest)(const ChessBoard &board, SquareSet srcSqMask, Square dst) noexcept
Generates a legal king move with a known destination square. May be capturing.
Definition chessboard.h:1581