HooverChessUtils_PgnReader 0.9.0
Loading...
Searching...
No Matches
Public Member Functions | Private Member Functions | Static Private Member Functions | Private Attributes | List of all members
hoover_chess_utils::pgn_reader::ChessBoard Class Referencefinal

The chessboard. More...

#include <chessboard.h>

Collaboration diagram for hoover_chess_utils::pgn_reader::ChessBoard:
Collaboration graph
[legend]

Public Member Functions

 ChessBoard () noexcept
 Constructor (default)
 
 ChessBoard (const ChessBoard &) noexcept=default
 Constructor (copy)
 
 ChessBoard (ChessBoard &&) noexcept=default
 Constructor (move)
 
ChessBoardoperator= (const ChessBoard &) noexcept=default
 Assignment.
 
ChessBoardoperator= (ChessBoard &&) noexcept=default
 Move assignment.
 
 ~ChessBoard () noexcept=default
 Destructor.
 
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)
 
void getArrayBoard (ArrayBoard &out_board) const noexcept
 Extracts the current position as an ArrayBoard.
 
void setBoard (const BitBoard &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 SquareSets (bit board)
 
void loadFEN (std::string_view fen)
 Sets the board from FEN.
 
PositionStatus determineStatus () const noexcept
 Determines the status of the position.
 
void loadStartPos () noexcept
 Loads the starting position.
 
void printBoard () const
 Prints the current board to stdout. This is intended only for debugging purposes.
 
Piece getSquarePieceNoColor (Square sq) const noexcept
 Returns the piece on a specified square.
 
PieceAndColor getSquarePiece (Square sq) const noexcept
 Returns the piece and color on a specified square.
 
SquareSet getOccupancyMask () const noexcept
 Returns squares that are occupied by pieces (including pawns).
 
SquareSet getWhitePieces () const noexcept
 Returns squares occupied by white pieces (including pawns).
 
SquareSet getBlackPieces () const noexcept
 Returns squares occupied by white pieces (including pawns).
 
SquareSet getPiecesInTurn () const noexcept
 Returns squares that are occupied by pieces (including pawns) that belong to the side to move.
 
SquareSet getPawns () const noexcept
 Returns squares occupied by pawns.
 
SquareSet getKnights () const noexcept
 Returns squares occupied by knights.
 
SquareSet getBishops () const noexcept
 Returns squares occupied by bishops.
 
SquareSet getRooks () const noexcept
 Returns squares occupied by rooks.
 
SquareSet getQueens () const noexcept
 Returns squares occupied by queens.
 
SquareSet getBishopsAndQueens () const noexcept
 Returns squares occupied by bishops and queens (diagonal sliders)
 
SquareSet getRooksAndQueens () const noexcept
 Returns squares occupied by rooks and queens (horizontal/vertical sliders)
 
SquareSet getKings () const noexcept
 Returns squares occupied by kings.
 
Square getKingInTurn () const noexcept
 Returns the square of the king in turn.
 
Square getKingNotInTurn () const noexcept
 Returns the square of the king not in turn.
 
bool isInCheck () const noexcept
 Returns whether the king is in check.
 
SquareSet getCheckers () const noexcept
 Returns squares occupied by checkers.
 
SquareSet getPinnedPieces () const noexcept
 Returns squares occupied by absolutely pinned pieces.
 
Square getWhiteLongCastleRook () const noexcept
 Shorthand for getCastlingRook(Color::WHITE, false).
 
Square getWhiteShortCastleRook () const noexcept
 Shorthand for getCastlingRook(Color::WHITE, true).
 
Square getBlackLongCastleRook () const noexcept
 Shorthand for getCastlingRook(Color::BLACK, false).
 
Square getBlackShortCastleRook () const noexcept
 Shorthand for getCastlingRook(Color::BLACK, true).
 
Square getCastlingRook (Color c, bool shortCastling) const noexcept
 Returns castling rook square if castling rights apply or Square::NONE if no castling rights.
 
Square getEpSquare () const noexcept
 Returns the en-passant square or Square::NONE if en-passant capture is not possible.
 
std::uint_fast8_t getHalfMoveClock () const noexcept
 Current half-move clock.
 
std::uint_fast32_t getCurrentPlyNum () const noexcept
 Full move counter in plies.
 
Color getTurn () const noexcept
 Returns side to move.
 
Move generateSingleMoveForPawnAndDestNoCapture (SquareSet srcSqMask, Square dst) const noexcept
 Generates a legal non-promoting, non-capturing pawn move with a known destination square.
 
Move generateSingleMoveForPawnAndDestCapture (SquareSet srcSqMask, Square dst) const noexcept
 Generates a legal non-promoting, capturing pawn move with a known destination square.
 
Move generateSingleMoveForPawnAndDestPromoNoCapture (SquareSet srcSqMask, Square dst, Piece promo) const noexcept
 Generates a legal promoting, non-capturing pawn move with a known destination square.
 
Move generateSingleMoveForPawnAndDestPromoCapture (SquareSet srcSqMask, Square dst, Piece promo) const noexcept
 Generates a legal promoting, capturing pawn move with a known destination square.
 
Move generateSingleMoveForKnightAndDest (SquareSet srcSqMask, Square dst) const noexcept
 Generates a legal knight move with a known destination square. May be capturing.
 
Move generateSingleMoveForBishopAndDest (SquareSet srcSqMask, Square dst) const noexcept
 Generates a legal bishop move with a known destination square. May be capturing.
 
Move generateSingleMoveForRookAndDest (SquareSet srcSqMask, Square dst) const noexcept
 Generates a legal rook move with a known destination square. May be capturing.
 
Move generateSingleMoveForQueenAndDest (SquareSet srcSqMask, Square dst) const noexcept
 Generates a legal queen move with a known destination square. May be capturing.
 
Move generateSingleMoveForKingAndDest (SquareSet srcSqMask, Square dst) const noexcept
 Generates a legal king move with a known destination square. May be capturing.
 
Move generateSingleMoveForShortCastling () const noexcept
 Generates a legal short castling move.
 
Move generateSingleMoveForLongCastling () const noexcept
 Generates a legal long castling move.
 
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.
 
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.
 
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.
 
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.
 
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.
 
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.
 
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.
 
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.
 
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.
 
std::size_t generateMovesForShortCastling (ShortMoveList &moves) const noexcept
 Generates the short castling move in a list, if legal.
 
std::size_t generateMovesForLongCastling (ShortMoveList &moves) const noexcept
 Generates the long castling move in a list, if legal.
 
std::size_t generateMoves (MoveList &moves) const noexcept
 Generates a list of all legal moves for the current position.
 
std::size_t getNumberOfLegalMoves () const noexcept
 Returns the number of legal moves for the current position. This function is mostly useful for calculating the moves in perft leaf positions.
 
bool hasLegalMoves () const noexcept
 Determines whether any legal moves as available in the current position.
 
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 the generators. No legality checks are done by this function.
 
bool operator== (const ChessBoard &o) const noexcept
 Comparison operator (equality)
 

Private Member Functions

void updateCheckersAndPins () noexcept
 Determines checkers and pinned pieces.
 
void validateBoard ()
 Validates the board for items that are common for both setBoard() and loadFEN() and sets m_checkers.
 
void setCastlingRook (Color c, bool shortCastling, Square sq) noexcept
 Sets/resets castling rights and the associated rook.
 
SquaregetCastlingRookRef (Color c, bool shortCastling) noexcept
 Gets a reference to the castling rights rook for direct read/write access.
 
void calculateMasks (const ArrayBoard &board) noexcept
 Calculates bitboard masks for an ArrayBoard object.
 

Static Private Member Functions

static constexpr std::size_t getCastlingRookIndex (Color c, bool shortCastling) noexcept
 Returns the array index for a castling rook.
 

Private Attributes

SquareSet m_occupancyMask { SquareSet::row(0U) | SquareSet::row(1U) | SquareSet::row(6U) | SquareSet::row(7U) }
 Bit board of occupied squares.
 
SquareSet m_turnColorMask { SquareSet::row(0U) | SquareSet::row(1U) }
 Occupied squares that have the same color piece as getTurn()
 
SquareSet m_pawns { SquareSet::row(1U) | SquareSet::row(6U) }
 Pawns.
 
SquareSet m_knights
 Knights.
 
SquareSet m_kings { SquareSet::square(Square::E1) | SquareSet::square(Square::E8) }
 Kings.
 
SquareSet m_bishops
 Bishops and queens.
 
SquareSet m_rooks
 Rooks and queens.
 
SquareSet m_checkers { }
 Squares where there is a checker.
 
SquareSet m_pinnedPieces { }
 Squares with pinned pieces.
 
const MoveGenFunctionsm_moveGenFns
 Move generator functions for this position.
 
std::uint32_t m_plyNum { }
 Current ply number.
 
std::uint8_t m_halfMoveClock { }
 Current half move clock.
 
Square m_kingSq { Square::E1 }
 King in turn.
 
Square m_oppKingSq { Square::E8 }
 King in not turn.
 
Square m_epSquare { Square::NONE }
 En-passant square.
 
std::array< Square, 4U > m_castlingRooks { Square::A1, Square::H1, Square::A8, Square::H8 }
 Castling rooks.
 

Detailed Description

The chessboard.

The following move generator variants are provided:

Family Description Functions
Full move list These generators produce all legal moves for a position. generateMoves()
getNumberOfLegalMoves()
Piece/destination move list These generators produce all legal moves for a specified piece and piece destination square. This family is useful to resolve the minimal SAN notation for a move, for instance. generateMovesForPawnAndDestNoCapture()
generateMovesForPawnAndDestCapture()
generateMovesForPawnAndDestPromoNoCapture()
generateMovesForPawnAndDestPromoCapture()
generateMovesForKnightAndDest()
generateMovesForBishopAndDest()
generateMovesForRookAndDest()
generateMovesForQueenAndDest()
generateMovesForKingAndDest()
generateMovesForLongCastling()
generateMovesForShortCastling()
Piece/destination/source single move These generators produce a single legal move for a piece, destination square, and allowed source square set. This family is useful for playing out moves in SAN notation in PGNs, for instance. generateSingleMoveForPawnAndDestNoCapture()
generateSingleMoveForPawnAndDestCapture()
generateSingleMoveForPawnAndDestPromoNoCapture()
generateSingleMoveForPawnAndDestPromoCapture()
generateSingleMoveForKnightAndDest()
generateSingleMoveForBishopAndDest()
generateSingleMoveForRookAndDest()
generateSingleMoveForQueenAndDest()
generateSingleMoveForKingAndDest()
generateSingleMoveForLongCastling()
generateSingleMoveForShortCastling()

Constructor & Destructor Documentation

◆ ChessBoard() [1/3]

hoover_chess_utils::pgn_reader::ChessBoard::ChessBoard ( )
noexcept

Constructor (default)

The default constructor sets the board to the standard starting position.

◆ ChessBoard() [2/3]

hoover_chess_utils::pgn_reader::ChessBoard::ChessBoard ( const ChessBoard )
defaultnoexcept

Constructor (copy)

◆ ChessBoard() [3/3]

hoover_chess_utils::pgn_reader::ChessBoard::ChessBoard ( ChessBoard &&  )
defaultnoexcept

Constructor (move)

◆ ~ChessBoard()

hoover_chess_utils::pgn_reader::ChessBoard::~ChessBoard ( )
defaultnoexcept

Destructor.

Member Function Documentation

◆ calculateMasks()

void hoover_chess_utils::pgn_reader::ChessBoard::calculateMasks ( const ArrayBoard board)
privatenoexcept

Calculates bitboard masks for an ArrayBoard object.

Parameters
[in]boardChess board in array (mailbox) format

◆ determineStatus()

PositionStatus hoover_chess_utils::pgn_reader::ChessBoard::determineStatus ( ) const
inlinenoexcept

Determines the status of the position.

Returns
Status of the position

This function determines whether there are any legal moves. Then, the status is returned as per the following table:

Legal moves getCheckers() Returned status
Yes SquareSet::none() PositionStatus::NORMAL
Yes Other PositionStatus::CHECK
No SquareSet::none() PositionStatus::STALEMATE
No Other PositionStatus::MATE

◆ doMove()

void hoover_chess_utils::pgn_reader::ChessBoard::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 the generators. No legality checks are done by this function.

Parameters
[in]mMove to apply

This function does the following:

◆ generateMoves()

std::size_t hoover_chess_utils::pgn_reader::ChessBoard::generateMoves ( MoveList moves) const
inlinenoexcept

Generates a list of all legal moves for the current position.

Parameters
[in]movesCaller-allocated move list
Returns
Number of moves added in the move list

◆ generateMovesForBishopAndDest()

std::size_t hoover_chess_utils::pgn_reader::ChessBoard::generateMovesForBishopAndDest ( ShortMoveList moves,
SquareSet  srcSqMask,
Square  dst 
) const
inlinenoexcept

Generates a list of legal bishop moves with a known destination square. May be capturing.

Parameters
[out]movesList of returned moves
[in]srcSqMaskAllowed source squares
[in]dstDestination square
Returns
Number of returned moves

◆ generateMovesForKingAndDest()

std::size_t hoover_chess_utils::pgn_reader::ChessBoard::generateMovesForKingAndDest ( ShortMoveList moves,
SquareSet  srcSqMask,
Square  dst 
) const
inlinenoexcept

Generates a list of legal king moves with a known destination square. May be capturing.

Parameters
[out]movesList of returned moves
[in]srcSqMaskAllowed source squares
[in]dstDestination square
Returns
Number of returned moves

◆ generateMovesForKnightAndDest()

std::size_t hoover_chess_utils::pgn_reader::ChessBoard::generateMovesForKnightAndDest ( ShortMoveList moves,
SquareSet  srcSqMask,
Square  dst 
) const
inlinenoexcept

Generates a list of legal knight moves with a known destination square. May be capturing.

Parameters
[out]movesList of returned moves
[in]srcSqMaskAllowed source squares
[in]dstDestination square
Returns
Number of returned moves

◆ generateMovesForLongCastling()

std::size_t hoover_chess_utils::pgn_reader::ChessBoard::generateMovesForLongCastling ( ShortMoveList moves) const
inlinenoexcept

Generates the long castling move in a list, if legal.

Parameters
[out]movesList of returned moves
Returns
Number of returned moves

◆ generateMovesForPawnAndDestCapture()

std::size_t hoover_chess_utils::pgn_reader::ChessBoard::generateMovesForPawnAndDestCapture ( ShortMoveList moves,
SquareSet  srcSqMask,
Square  dst 
) const
inlinenoexcept

Generates a list of legal non-promoting, capturing pawn moves with a known destination square.

Parameters
[out]movesList of returned moves
[in]srcSqMaskAllowed source squares
[in]dstDestination square
Returns
Number of returned moves

◆ generateMovesForPawnAndDestNoCapture()

std::size_t hoover_chess_utils::pgn_reader::ChessBoard::generateMovesForPawnAndDestNoCapture ( ShortMoveList moves,
SquareSet  srcSqMask,
Square  dst 
) const
inlinenoexcept

Generates a list of legal non-promoting, non-capturing pawn moves with a known destination square.

Parameters
[out]movesList of returned moves
[in]srcSqMaskAllowed source squares
[in]dstDestination square
Returns
Number of returned moves

◆ generateMovesForPawnAndDestPromoCapture()

std::size_t hoover_chess_utils::pgn_reader::ChessBoard::generateMovesForPawnAndDestPromoCapture ( ShortMoveList moves,
SquareSet  srcSqMask,
Square  dst,
Piece  promo 
) const
inlinenoexcept

Generates a list of legal promoting, capturing pawn moves with a known destination square.

Parameters
[out]movesList of returned moves
[in]srcSqMaskAllowed source squares
[in]dstDestination square
[in]promoPromotion piece
Returns
Number of returned moves

◆ generateMovesForPawnAndDestPromoNoCapture()

std::size_t hoover_chess_utils::pgn_reader::ChessBoard::generateMovesForPawnAndDestPromoNoCapture ( ShortMoveList moves,
SquareSet  srcSqMask,
Square  dst,
Piece  promo 
) const
inlinenoexcept

Generates a list of legal promoting, non-capturing pawn moves with a known destination square.

Parameters
[out]movesList of returned moves
[in]srcSqMaskAllowed source squares
[in]dstDestination square
[in]promoPromotion piece
Returns
Number of returned moves

◆ generateMovesForQueenAndDest()

std::size_t hoover_chess_utils::pgn_reader::ChessBoard::generateMovesForQueenAndDest ( ShortMoveList moves,
SquareSet  srcSqMask,
Square  dst 
) const
inlinenoexcept

Generates a list of legal queen moves with a known destination square. May be capturing.

Parameters
[out]movesList of returned moves
[in]srcSqMaskAllowed source squares
[in]dstDestination square
Returns
Number of returned moves

◆ generateMovesForRookAndDest()

std::size_t hoover_chess_utils::pgn_reader::ChessBoard::generateMovesForRookAndDest ( ShortMoveList moves,
SquareSet  srcSqMask,
Square  dst 
) const
inlinenoexcept

Generates a list of legal rook moves with a known destination square. May be capturing.

Parameters
[out]movesList of returned moves
[in]srcSqMaskAllowed source squares
[in]dstDestination square
Returns
Number of returned moves

◆ generateMovesForShortCastling()

std::size_t hoover_chess_utils::pgn_reader::ChessBoard::generateMovesForShortCastling ( ShortMoveList moves) const
inlinenoexcept

Generates the short castling move in a list, if legal.

Parameters
[out]movesList of returned moves
Returns
Number of returned moves

◆ generateSingleMoveForBishopAndDest()

Move hoover_chess_utils::pgn_reader::ChessBoard::generateSingleMoveForBishopAndDest ( SquareSet  srcSqMask,
Square  dst 
) const
inlinenoexcept

Generates a legal bishop move with a known destination square. May be capturing.

Parameters
[in]srcSqMaskAllowed source squares
[in]dstDestination square
Returns
Move. In case a move was not found or it was ambiguous, Move::isIllegal() returns true.

◆ generateSingleMoveForKingAndDest()

Move hoover_chess_utils::pgn_reader::ChessBoard::generateSingleMoveForKingAndDest ( SquareSet  srcSqMask,
Square  dst 
) const
inlinenoexcept

Generates a legal king move with a known destination square. May be capturing.

Parameters
[in]srcSqMaskAllowed source squares
[in]dstDestination square
Returns
Move. In case a move was not found or it was ambiguous, Move::isIllegal() returns true.

◆ generateSingleMoveForKnightAndDest()

Move hoover_chess_utils::pgn_reader::ChessBoard::generateSingleMoveForKnightAndDest ( SquareSet  srcSqMask,
Square  dst 
) const
inlinenoexcept

Generates a legal knight move with a known destination square. May be capturing.

Parameters
[in]srcSqMaskAllowed source squares
[in]dstDestination square
Returns
Move. In case a move was not found or it was ambiguous, Move::isIllegal() returns true.

◆ generateSingleMoveForLongCastling()

Move hoover_chess_utils::pgn_reader::ChessBoard::generateSingleMoveForLongCastling ( ) const
inlinenoexcept

Generates a legal long castling move.

Returns
Move. In case a move was not found or it was ambiguous, Move::isIllegal() returns true.

◆ generateSingleMoveForPawnAndDestCapture()

Move hoover_chess_utils::pgn_reader::ChessBoard::generateSingleMoveForPawnAndDestCapture ( SquareSet  srcSqMask,
Square  dst 
) const
inlinenoexcept

Generates a legal non-promoting, capturing pawn move with a known destination square.

Parameters
[in]srcSqMaskAllowed source squares
[in]dstDestination square
Returns
Move. In case a move was not found or it was ambiguous, Move::isIllegal() returns true.

◆ generateSingleMoveForPawnAndDestNoCapture()

Move hoover_chess_utils::pgn_reader::ChessBoard::generateSingleMoveForPawnAndDestNoCapture ( SquareSet  srcSqMask,
Square  dst 
) const
inlinenoexcept

Generates a legal non-promoting, non-capturing pawn move with a known destination square.

Parameters
[in]srcSqMaskAllowed source squares
[in]dstDestination square
Returns
Move. In case a move was not found or it was ambiguous, Move::isIllegal() returns true.

◆ generateSingleMoveForPawnAndDestPromoCapture()

Move hoover_chess_utils::pgn_reader::ChessBoard::generateSingleMoveForPawnAndDestPromoCapture ( SquareSet  srcSqMask,
Square  dst,
Piece  promo 
) const
inlinenoexcept

Generates a legal promoting, capturing pawn move with a known destination square.

Parameters
[in]srcSqMaskAllowed source squares
[in]dstDestination square
[in]promoPromotion piece
Returns
Move. In case a move was not found or it was ambiguous, Move::isIllegal() returns true.

◆ generateSingleMoveForPawnAndDestPromoNoCapture()

Move hoover_chess_utils::pgn_reader::ChessBoard::generateSingleMoveForPawnAndDestPromoNoCapture ( SquareSet  srcSqMask,
Square  dst,
Piece  promo 
) const
inlinenoexcept

Generates a legal promoting, non-capturing pawn move with a known destination square.

Parameters
[in]srcSqMaskAllowed source squares
[in]dstDestination square
[in]promoPromotion piece
Returns
Move. In case a move was not found or it was ambiguous, Move::isIllegal() returns true.

◆ generateSingleMoveForQueenAndDest()

Move hoover_chess_utils::pgn_reader::ChessBoard::generateSingleMoveForQueenAndDest ( SquareSet  srcSqMask,
Square  dst 
) const
inlinenoexcept

Generates a legal queen move with a known destination square. May be capturing.

Parameters
[in]srcSqMaskAllowed source squares
[in]dstDestination square
Returns
Move. In case a move was not found or it was ambiguous, Move::isIllegal() returns true.

◆ generateSingleMoveForRookAndDest()

Move hoover_chess_utils::pgn_reader::ChessBoard::generateSingleMoveForRookAndDest ( SquareSet  srcSqMask,
Square  dst 
) const
inlinenoexcept

Generates a legal rook move with a known destination square. May be capturing.

Parameters
[in]srcSqMaskAllowed source squares
[in]dstDestination square
Returns
Move. In case a move was not found or it was ambiguous, Move::isIllegal() returns true.

◆ generateSingleMoveForShortCastling()

Move hoover_chess_utils::pgn_reader::ChessBoard::generateSingleMoveForShortCastling ( ) const
inlinenoexcept

Generates a legal short castling move.

Returns
Move. In case a move was not found or it was ambiguous, Move::isIllegal() returns true.

◆ getArrayBoard()

void hoover_chess_utils::pgn_reader::ChessBoard::getArrayBoard ( ArrayBoard out_board) const
noexcept

Extracts the current position as an ArrayBoard.

Parameters
[out]out_boardCurrent position
See also
setBoard()

◆ getBishops()

SquareSet hoover_chess_utils::pgn_reader::ChessBoard::getBishops ( ) const
inlinenoexcept

Returns squares occupied by bishops.

Returns
Bishops

◆ getBishopsAndQueens()

SquareSet hoover_chess_utils::pgn_reader::ChessBoard::getBishopsAndQueens ( ) const
inlinenoexcept

Returns squares occupied by bishops and queens (diagonal sliders)

Returns
Bishops and queens

◆ getBlackLongCastleRook()

Square hoover_chess_utils::pgn_reader::ChessBoard::getBlackLongCastleRook ( ) const
inlinenoexcept

Shorthand for getCastlingRook(Color::BLACK, false).

Returns
Rook square if black can castle long; or Square::NONE

◆ getBlackPieces()

SquareSet hoover_chess_utils::pgn_reader::ChessBoard::getBlackPieces ( ) const
inlinenoexcept

Returns squares occupied by white pieces (including pawns).

Returns
Occupied squares

◆ getBlackShortCastleRook()

Square hoover_chess_utils::pgn_reader::ChessBoard::getBlackShortCastleRook ( ) const
inlinenoexcept

Shorthand for getCastlingRook(Color::BLACK, true).

Returns
Rook square if black can castle short; or Square::NONE

◆ getCastlingRook()

Square hoover_chess_utils::pgn_reader::ChessBoard::getCastlingRook ( Color  c,
bool  shortCastling 
) const
inlinenoexcept

Returns castling rook square if castling rights apply or Square::NONE if no castling rights.

Parameters
[in]cSide to move
[in]shortCastlingWhether short castling is in question
Returns
Square of the rook or Square::NONE if no castling rights.

◆ getCastlingRookIndex()

static constexpr std::size_t hoover_chess_utils::pgn_reader::ChessBoard::getCastlingRookIndex ( Color  c,
bool  shortCastling 
)
inlinestaticconstexprprivatenoexcept

Returns the array index for a castling rook.

Parameters
[in]cSide
[in]shortCastlingWhether short castling is in question
Returns
Array index for accessing m_castlingRooks.
See also
getCastlingRook()

◆ getCastlingRookRef()

Square & hoover_chess_utils::pgn_reader::ChessBoard::getCastlingRookRef ( Color  c,
bool  shortCastling 
)
inlineprivatenoexcept

Gets a reference to the castling rights rook for direct read/write access.

Parameters
[in]cSide
[in]shortCastlingWhether short castling rights are accessed
Returns
Square for the castling rook

◆ getCheckers()

SquareSet hoover_chess_utils::pgn_reader::ChessBoard::getCheckers ( ) const
inlinenoexcept

Returns squares occupied by checkers.

Returns
Checkers

◆ getCurrentPlyNum()

std::uint_fast32_t hoover_chess_utils::pgn_reader::ChessBoard::getCurrentPlyNum ( ) const
inlinenoexcept

Full move counter in plies.

Ply number 0 means white's first move, and ply number 1 means black's first move.

See also
moveNumOfPly(), colorOfPly()

◆ getEpSquare()

Square hoover_chess_utils::pgn_reader::ChessBoard::getEpSquare ( ) const
inlinenoexcept

Returns the en-passant square or Square::NONE if en-passant capture is not possible.

Returns
En-passant square or Square::NONE if EP-capture is not legal.
Remarks
When EP square is other than Square::NONE, at least one EP capture is guaranteed to be legal.
When the king is in check, EP capture may be legal only when the checker is the EP-capturable pawn. When this is not the case, this function returns Square::NONE.
See getPinnedPieces() for a discussion on pin handling related to EP captures.

◆ getHalfMoveClock()

std::uint_fast8_t hoover_chess_utils::pgn_reader::ChessBoard::getHalfMoveClock ( ) const
inlinenoexcept

Current half-move clock.

Half-move clock is used to determine 50-move rule and similar draw termination rules.

◆ getKingInTurn()

Square hoover_chess_utils::pgn_reader::ChessBoard::getKingInTurn ( ) const
inlinenoexcept

Returns the square of the king in turn.

Returns
King square

◆ getKingNotInTurn()

Square hoover_chess_utils::pgn_reader::ChessBoard::getKingNotInTurn ( ) const
inlinenoexcept

Returns the square of the king not in turn.

Returns
King square

◆ getKings()

SquareSet hoover_chess_utils::pgn_reader::ChessBoard::getKings ( ) const
inlinenoexcept

Returns squares occupied by kings.

Returns
Kings

◆ getKnights()

SquareSet hoover_chess_utils::pgn_reader::ChessBoard::getKnights ( ) const
inlinenoexcept

Returns squares occupied by knights.

Returns
Knights

◆ getNumberOfLegalMoves()

std::size_t hoover_chess_utils::pgn_reader::ChessBoard::getNumberOfLegalMoves ( ) const
inlinenoexcept

Returns the number of legal moves for the current position. This function is mostly useful for calculating the moves in perft leaf positions.

Returns
Number of legal moves
See also
https://www.chessprogramming.org/Perft

◆ getOccupancyMask()

SquareSet hoover_chess_utils::pgn_reader::ChessBoard::getOccupancyMask ( ) const
inlinenoexcept

Returns squares that are occupied by pieces (including pawns).

Returns
Occupied squares

◆ getPawns()

SquareSet hoover_chess_utils::pgn_reader::ChessBoard::getPawns ( ) const
inlinenoexcept

Returns squares occupied by pawns.

Returns
Pawns

◆ getPiecesInTurn()

SquareSet hoover_chess_utils::pgn_reader::ChessBoard::getPiecesInTurn ( ) const
inlinenoexcept

Returns squares that are occupied by pieces (including pawns) that belong to the side to move.

Returns
Occupied squares, pieces belonging to the side to move

◆ getPinnedPieces()

SquareSet hoover_chess_utils::pgn_reader::ChessBoard::getPinnedPieces ( ) const
inlinenoexcept

Returns squares occupied by absolutely pinned pieces.

Returns
Pinned pieces
Remarks
The en passant capturable pawn is never marked as pinned. There are three cases to consider:
  • A diagonal pin. In this case, the EP-capturable pawn can never be EP-captured. getEpSquare() returns Square::NONE.
  • A vertical false pin. In this case, the EP-capturing pawn blocks the exposed check when capturing, hence, EP capture is allowed—provided that the EP-capture is otherwise legal.
  • A horizontal pin. In this case, both the EP-capturing pawn and the EP-capturable pawn leave the rank, revealing a check. Again, in this case, getEpSquare() returns Square::NONE to signal that EP-capture is not allowed.
    Three cases of pins related to EP-capturing
    Case Example
    (1) Diagonally pinned EP-capturable pawn
    bB
    (ep)
    bPwP
    wK
    (2) Vertically pinned EP-capturable pawn
    bR
    (ep)
    bPwP
    wK
    (3) Horizontally pinned EP-capturable pawn
    (ep)
    wKbPwPbR
See also
getEpSquare()

◆ getQueens()

SquareSet hoover_chess_utils::pgn_reader::ChessBoard::getQueens ( ) const
inlinenoexcept

Returns squares occupied by queens.

Returns
Queens

◆ getRooks()

SquareSet hoover_chess_utils::pgn_reader::ChessBoard::getRooks ( ) const
inlinenoexcept

Returns squares occupied by rooks.

Returns
Rooks

◆ getRooksAndQueens()

SquareSet hoover_chess_utils::pgn_reader::ChessBoard::getRooksAndQueens ( ) const
inlinenoexcept

Returns squares occupied by rooks and queens (horizontal/vertical sliders)

Returns
Rooks and queens

◆ getSquarePiece()

PieceAndColor hoover_chess_utils::pgn_reader::ChessBoard::getSquarePiece ( Square  sq) const
noexcept

Returns the piece and color on a specified square.

Parameters
[in]sqSquare to query
Returns
Piece and color on sq or PieceAndColor::NONE.
Note
This function may be slower than expected, since it decodes the piece from bit masks.

◆ getSquarePieceNoColor()

Piece hoover_chess_utils::pgn_reader::ChessBoard::getSquarePieceNoColor ( Square  sq) const
noexcept

Returns the piece on a specified square.

Parameters
[in]sqSquare to query
Returns
Piece on sq or Piece::NONE.
Note
This function may be slower than expected, since it decodes the piece from bit masks.

◆ getTurn()

Color hoover_chess_utils::pgn_reader::ChessBoard::getTurn ( ) const
inlinenoexcept

Returns side to move.

Returns
Side to move

◆ getWhiteLongCastleRook()

Square hoover_chess_utils::pgn_reader::ChessBoard::getWhiteLongCastleRook ( ) const
inlinenoexcept

Shorthand for getCastlingRook(Color::WHITE, false).

Returns
Rook square if white can castle long; or Square::NONE

◆ getWhitePieces()

SquareSet hoover_chess_utils::pgn_reader::ChessBoard::getWhitePieces ( ) const
inlinenoexcept

Returns squares occupied by white pieces (including pawns).

Returns
Occupied squares

◆ getWhiteShortCastleRook()

Square hoover_chess_utils::pgn_reader::ChessBoard::getWhiteShortCastleRook ( ) const
inlinenoexcept

Shorthand for getCastlingRook(Color::WHITE, true).

Returns
Rook square if white can castle short; or Square::NONE

◆ hasLegalMoves()

bool hoover_chess_utils::pgn_reader::ChessBoard::hasLegalMoves ( ) const
inlinenoexcept

Determines whether any legal moves as available in the current position.

Returns
Legal moves available.

◆ isInCheck()

bool hoover_chess_utils::pgn_reader::ChessBoard::isInCheck ( ) const
inlinenoexcept

Returns whether the king is in check.

Returns
Whether the king is in check

◆ loadFEN()

void hoover_chess_utils::pgn_reader::ChessBoard::loadFEN ( std::string_view  fen)

Sets the board from FEN.

Parameters
[in]fenPosition described in Forsyth–Edwards Notation (FEN)
Exceptions
PgnError(PgnErrorCode::BAD_FEN)Malformed FEN (e.g., too many rows or columns; bad character; bad castling indicator)
PgnError(PgnErrorCode::BAD_FEN)Pawns on 1st/8th rank
PgnError(PgnErrorCode::BAD_FEN)EP square on wrong rank or not empty; no EP pawn to capture.
PgnError(PgnErrorCode::BAD_FEN)Wrong number of kings
PgnError(PgnErrorCode::BAD_FEN)Opponent king is in check

The castling indicator may be in FEN, Shredded-FEN, or X-FEN format.

EP capture does not have to be a legal move as long as the EP square is empty and there is a pawn to capture. If case there is no legal EP capture moves (but the other requirements are met), this function resets the EP square.

Note
In case an exception is thrown, the board may be left in a bad state.
See also
setBoard()
https://en.wikipedia.org/wiki/Forsyth%E2%80%93Edwards_Notation

◆ loadStartPos()

void hoover_chess_utils::pgn_reader::ChessBoard::loadStartPos ( )
inlinenoexcept

Loads the starting position.

Resets the board to the regular starting position. Resets also half move clock and ply count.

This is equivalent to assigning a default-constructed ChessBoard.

◆ operator=() [1/2]

ChessBoard & hoover_chess_utils::pgn_reader::ChessBoard::operator= ( ChessBoard &&  )
defaultnoexcept

Move assignment.

◆ operator=() [2/2]

ChessBoard & hoover_chess_utils::pgn_reader::ChessBoard::operator= ( const ChessBoard )
defaultnoexcept

Assignment.

◆ operator==()

bool hoover_chess_utils::pgn_reader::ChessBoard::operator== ( const ChessBoard o) const
noexcept

Comparison operator (equality)

Parameters
[in]oAnother chessboard
Returns
Comparison result

◆ printBoard()

void hoover_chess_utils::pgn_reader::ChessBoard::printBoard ( ) const

Prints the current board to stdout. This is intended only for debugging purposes.

◆ setBoard() [1/2]

void hoover_chess_utils::pgn_reader::ChessBoard::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)

Parameters
[in]boardArray board containing pieces for squares
[in]whiteLongCastleRookWhite long castling rook or Square::NONE if no long castling rights.
[in]whiteShortCastleRookWhite short castling rook or Square::NONE if no short castling rights.
[in]blackLongCastleRookBlack long castling rook or Square::NONE if no long castling rights.
[in]blackShortCastleRookBlack short castling rook or Square::NONE if no short castling rights.
[in]epSquareEn-passant square or Square::NONE if en-passant capture is not possible.
[in]halfMoveClockHalf-move clock
[in]plyNumPly num. Use makePlyNum() if necessary.
Exceptions
PgnError(PgnErrorCode::BAD_FEN)Invalid input parameter, e.g., bad Square value
PgnError(PgnErrorCode::BAD_FEN)Inconsistent castling rights, e.g., castling rook not found, king not on 1st/8th rank, castling rook on the wrong side
PgnError(PgnErrorCode::BAD_FEN)Pawns on 1st/8th rank
PgnError(PgnErrorCode::BAD_FEN)EP square on wrong rank or not empty; no EP pawn to capture.
PgnError(PgnErrorCode::BAD_FEN)Wrong number of kings
PgnError(PgnErrorCode::BAD_FEN)Opponent king is in check
PgnError(PgnErrorCode::BAD_FEN)Move number overflow (max: 99999)

EP capture does not have to be a legal move as long as the EP square is empty and there is a pawn to capture. If case there is no legal EP capture moves (but the other requirements are met), this function resets the EP square.

Note
In case an exception is thrown, the board may be left in a bad state.
See also
loadFEN()

◆ setBoard() [2/2]

void hoover_chess_utils::pgn_reader::ChessBoard::setBoard ( const BitBoard 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 SquareSets (bit board)

Parameters
[in]boardArray board containing pieces for squares
[in]whiteLongCastleRookWhite long castling rook or Square::NONE if no long castling rights.
[in]whiteShortCastleRookWhite short castling rook or Square::NONE if no short castling rights.
[in]blackLongCastleRookBlack long castling rook or Square::NONE if no long castling rights.
[in]blackShortCastleRookBlack short castling rook or Square::NONE if no short castling rights.
[in]epSquareEn-passant square or Square::NONE if en-passant capture is not possible.
[in]halfMoveClockHalf-move clock
[in]plyNumPly num. Use makePlyNum() if necessary.
Exceptions
PgnError(PgnErrorCode::BAD_FEN)Invalid input parameter, e.g., bad Square value
PgnError(PgnErrorCode::BAD_FEN)Inconsistent castling rights, e.g., castling rook not found, king not on 1st/8th rank, castling rook on the wrong side
PgnError(PgnErrorCode::BAD_FEN)Pawns on 1st/8th rank
PgnError(PgnErrorCode::BAD_FEN)EP square on wrong rank or not empty; no EP pawn to capture.
PgnError(PgnErrorCode::BAD_FEN)Wrong number of kings
PgnError(PgnErrorCode::BAD_FEN)Opponent king is in check
PgnError(PgnErrorCode::BAD_FEN)Move number overflow (max: 99999)
PgnError(PgnErrorCode::BAD_FEN)More than one piece occupying a square (intersection of per-piece SquareSets is not empty)

EP capture does not have to be a legal move as long as the EP square is empty and there is a pawn to capture.

Note
In case an exception is thrown, the board may be left in a bad state.
See also
loadFEN()

◆ setCastlingRook()

void hoover_chess_utils::pgn_reader::ChessBoard::setCastlingRook ( Color  c,
bool  shortCastling,
Square  sq 
)
inlineprivatenoexcept

Sets/resets castling rights and the associated rook.

Parameters
[in]cSide
[in]shortCastlingWhether short castling rights are set/reset
[in]sqCastling rook or Square::NONE for no castling rights

◆ updateCheckersAndPins()

void hoover_chess_utils::pgn_reader::ChessBoard::updateCheckersAndPins ( )
privatenoexcept

Determines checkers and pinned pieces.

This function sets m_checkers and m_pinnedPieces.

◆ validateBoard()

void hoover_chess_utils::pgn_reader::ChessBoard::validateBoard ( )
private

Validates the board for items that are common for both setBoard() and loadFEN() and sets m_checkers.

Exceptions
PgnError(PgnErrorCode::BAD_FEN)Validation failed

Member Data Documentation

◆ m_bishops

SquareSet hoover_chess_utils::pgn_reader::ChessBoard::m_bishops
private
Initial value:

Bishops and queens.

See also
getBishopsAndQueens()

◆ m_castlingRooks

std::array<Square, 4U> hoover_chess_utils::pgn_reader::ChessBoard::m_castlingRooks { Square::A1, Square::H1, Square::A8, Square::H8 }
private

Castling rooks.

The castling rooks are in order:

  • white long castling rook
  • white short castling rook
  • black long castling rook
  • black short castling rook

Rook square Square::NONE signifies no castling right.

See also
getCastlingRookIndex()

◆ m_checkers

SquareSet hoover_chess_utils::pgn_reader::ChessBoard::m_checkers { }
private

Squares where there is a checker.

Remarks
Set by updateCheckersAndPins()

◆ m_epSquare

Square hoover_chess_utils::pgn_reader::ChessBoard::m_epSquare { Square::NONE }
private

En-passant square.

See also
getEpSquare()

◆ m_halfMoveClock

std::uint8_t hoover_chess_utils::pgn_reader::ChessBoard::m_halfMoveClock { }
private

Current half move clock.

See also
getHalfMoveClock()

◆ m_kings

SquareSet hoover_chess_utils::pgn_reader::ChessBoard::m_kings { SquareSet::square(Square::E1) | SquareSet::square(Square::E8) }
private

Kings.

See also
getKings()

◆ m_kingSq

Square hoover_chess_utils::pgn_reader::ChessBoard::m_kingSq { Square::E1 }
private

King in turn.

See also
m_kings, m_turnColorMask

◆ m_knights

SquareSet hoover_chess_utils::pgn_reader::ChessBoard::m_knights
private

◆ m_moveGenFns

const MoveGenFunctions* hoover_chess_utils::pgn_reader::ChessBoard::m_moveGenFns
private

Move generator functions for this position.

See also
MoveGenType

◆ m_occupancyMask

SquareSet hoover_chess_utils::pgn_reader::ChessBoard::m_occupancyMask { SquareSet::row(0U) | SquareSet::row(1U) | SquareSet::row(6U) | SquareSet::row(7U) }
private

Bit board of occupied squares.

See also
getOccupancyMask()

◆ m_oppKingSq

Square hoover_chess_utils::pgn_reader::ChessBoard::m_oppKingSq { Square::E8 }
private

King in not turn.

See also
m_kings, m_turnColorMask

◆ m_pawns

SquareSet hoover_chess_utils::pgn_reader::ChessBoard::m_pawns { SquareSet::row(1U) | SquareSet::row(6U) }
private

Pawns.

See also
getPawns()

◆ m_pinnedPieces

SquareSet hoover_chess_utils::pgn_reader::ChessBoard::m_pinnedPieces { }
private

Squares with pinned pieces.

Remarks
Set by updateCheckersAndPins()

◆ m_plyNum

std::uint32_t hoover_chess_utils::pgn_reader::ChessBoard::m_plyNum { }
private

Current ply number.

See also
getCurrentPlyNum()

◆ m_rooks

SquareSet hoover_chess_utils::pgn_reader::ChessBoard::m_rooks
private

◆ m_turnColorMask

SquareSet hoover_chess_utils::pgn_reader::ChessBoard::m_turnColorMask { SquareSet::row(0U) | SquareSet::row(1U) }
private

Occupied squares that have the same color piece as getTurn()

See also
getPiecesInTurn(), getWhitePieces(), getBlackPieces()

The documentation for this class was generated from the following file: