BenBot 1.7.5
A chess engine
Loading...
Searching...
No Matches
ben_bot::Position Struct Referencefinal

#include <libchess/game/Position.hpp>

Collaboration diagram for ben_bot::Position:

Public Types

using Hash = std::uint64_t

Public Member Functions

 Position ()
void flip ()
auto free () const noexcept -> Bitboard
template<Color Side>
auto get_backward_pawns () const noexcept -> Bitboard
template<Color Side>
auto get_passed_pawns () const noexcept -> Bitboard
auto is_black_to_move () const noexcept -> bool
auto is_capture (Move move) const noexcept -> bool
auto is_check () const noexcept -> bool
auto is_en_passant (Move move) const noexcept -> bool
auto is_illegal () const -> std::optional< std::string >
auto is_legal (Move move) const -> bool
auto is_quiet (Move move) const noexcept -> bool
auto is_white_to_move () const noexcept -> bool
void make_move (Move move)
void make_null_move ()
auto occupied () const noexcept -> Bitboard
auto operator== (const Position &other) const noexcept -> bool
void refresh_zobrist ()
Piece access
template<Color Side>
auto pieces_for () noexcept -> Pieces &
template<Color Side>
auto pieces_for () const noexcept -> const Pieces &
template<Color Side>
auto castling_rights_for () const noexcept -> CastlingRights
auto our_pieces () noexcept -> Pieces &
auto our_pieces () const noexcept -> const Pieces &
auto their_pieces () noexcept -> Pieces &
auto their_pieces () const noexcept -> const Pieces &
File queries
auto is_file_open (File file) const noexcept -> bool
auto get_open_files () const noexcept
auto is_file_half_open (File file) const noexcept -> bool
auto get_half_open_files () const noexcept
Game result queries
auto is_checkmate () const -> bool
auto is_stalemate () const -> bool
auto is_fifty_move_draw () const -> bool
auto is_threefold_repetition () const noexcept -> bool
auto is_draw_by_insufficient_material () const noexcept -> bool
auto is_draw () const -> bool
auto get_result () const -> std::optional< Result >

Static Public Member Functions

static auto empty () -> Position

Public Attributes

CastlingRights blackCastlingRights
Pieces blackPieces { Color::Black }
std::optional< SquareenPassantTargetSquare
std::uint_least64_t fullMoveCounter { UINT64_C(1) }
std::uint_least8_t halfmoveClock { UINT8_C(0) }
Hash hash { UINT64_C(0) }
Color sideToMove { Color::White }
CastlingRights whiteCastlingRights
Pieces whitePieces { Color::White }

(Note that these are not member symbols.)

auto after_move (const Position &starting, Move move) -> Position
auto after_null_move (const Position &starting) -> Position
auto flipped (const Position &starting) -> Position
auto print_ascii (const Position &position) -> std::string
auto print_utf8 (const Position &position) -> std::string

Detailed Description

This class models an instant in a game of chess.

It describes where all the pieces are placed, as well as some additional metadata such as castling rights, side to move, en passant possibility, etc.

Definition at line 74 of file Position.hpp.

Member Typedef Documentation

◆ Hash

using chess::game::Position::Hash = std::uint64_t

Unsigned 64-bit integer used for hash values.

Definition at line 130 of file Position.hpp.

Constructor & Destructor Documentation

◆ Position()

Creates a Position object representing the starting position.

Member Function Documentation

◆ castling_rights_for()

template<Color Side>
auto chess::game::Position::castling_rights_for ( ) const -> CastlingRights
inlinenodiscardnoexcept

Returns the castling rights for the given color.

Definition at line 170 of file Position.hpp.

◆ empty()

auto chess::game::Position::empty ( ) -> Position
inlinestaticnodiscard

Returns an empty position with none of the piece bitboards initialized. This is useful for tasks like parsing a FEN string, for example. After you've set up the position, don't forget to call whitePieces.refresh_occupied(), blackPieces.refresh_occupied(), and refresh_zobrist() to update all relevant cached state.

Definition at line 427 of file Position.hpp.

◆ flip()

Performs a color flip of this position.

◆ free()

auto chess::game::Position::free ( ) const -> Bitboard
inlinenodiscardnoexcept

Returns a bitboard that is the inverse of the occupied() board.

Definition at line 234 of file Position.hpp.

◆ get_backward_pawns()

template<Color Side>
auto chess::game::Position::get_backward_pawns ( ) const -> Bitboard
nodiscardnoexcept

Returns a bitboard containing the locations of backward pawns for the given side.

Definition at line 522 of file Position.hpp.

◆ get_half_open_files()

auto chess::game::Position::get_half_open_files ( ) const
inlinenodiscardnoexcept

Returns an iterable range of File enumeration values corresponding to all half-open files in this position.

See also
is_file_half_open()

Definition at line 501 of file Position.hpp.

◆ get_open_files()

auto chess::game::Position::get_open_files ( ) const
inlinenodiscardnoexcept

Returns an iterable range of File enumeration values corresponding to all open files in this position.

See also
is_file_open()

Definition at line 486 of file Position.hpp.

◆ get_passed_pawns()

template<Color Side>
auto chess::game::Position::get_passed_pawns ( ) const -> Bitboard
nodiscardnoexcept

Returns a bitboard containing the locations of passed pawns for the given side.

Definition at line 508 of file Position.hpp.

◆ get_result()

auto chess::game::Position::get_result ( ) const -> std::optional< Result >
nodiscard

If this position is a conclusive draw or checkmate, returns the appropriate Result enumeration. Returns nullopt if this position is not a conclusive one.

◆ is_black_to_move()

auto chess::game::Position::is_black_to_move ( ) const -> bool
inlinenodiscardnoexcept

Returns true if the side to move is Black.

Definition at line 223 of file Position.hpp.

◆ is_capture()

auto chess::game::Position::is_capture ( Move move) const -> bool
inlinenodiscardnoexcept

Returns true if the given move is a capture, including en passant.

Definition at line 471 of file Position.hpp.

◆ is_check()

auto chess::game::Position::is_check ( ) const -> bool
inlinenodiscardnoexcept

Returns true if the king of the side to move is in check.

Definition at line 266 of file Position.hpp.

◆ is_checkmate()

auto chess::game::Position::is_checkmate ( ) const -> bool
nodiscard

Returns true if the king is attacked and the side to move has no legal moves.

◆ is_draw()

auto chess::game::Position::is_draw ( ) const -> bool
nodiscard

Returns true if the game has concluded in a draw.

◆ is_draw_by_insufficient_material()

auto chess::game::Position::is_draw_by_insufficient_material ( ) const -> bool
nodiscardnoexcept

Returns true if this position is either lone kings or lone king vs. king and bishop or knight.

◆ is_en_passant()

auto chess::game::Position::is_en_passant ( Move move) const -> bool
inlinenodiscardnoexcept

Returns true if the given move is an en passant capture in the current position.

Definition at line 464 of file Position.hpp.

◆ is_fifty_move_draw()

auto chess::game::Position::is_fifty_move_draw ( ) const -> bool
nodiscard

Returns true if this position is a fifty-move draw, based on the halfmoveClock. Note that in order for the game to be drawn, the side to move must have at least 1 legal move.

◆ is_file_half_open()

auto chess::game::Position::is_file_half_open ( File file) const -> bool
inlinenodiscardnoexcept

Returns true if only one side has a pawn on the given file.

See also
get_half_open_files

Definition at line 492 of file Position.hpp.

◆ is_file_open()

auto chess::game::Position::is_file_open ( File file) const -> bool
inlinenodiscardnoexcept

Returns true if there are no pawns of either color on the given file.

See also
get_open_files()

Definition at line 481 of file Position.hpp.

◆ is_illegal()

auto chess::game::Position::is_illegal ( ) const -> std::optional< std::string >
nodiscard

Tests if the position is legal. If it is, returns nullopt; if the position is illegal, returns an explanatory string describing the error condition detecting. This function conducts basic checks about the number of each type of piece, but this is not an exhaustive validation that a position can definitively be reached from the starting position.

◆ is_legal()

auto chess::game::Position::is_legal ( Move move) const -> bool
inlinenodiscard

Returns true if the given move is legal (that is, the king is not left in check). This function does not verify piece movement mechanics or that a piece of the given type exists on the starting square; this function only verifies that making the move does not leave the side's king in check.

Definition at line 455 of file Position.hpp.

◆ is_quiet()

auto chess::game::Position::is_quiet ( Move move) const -> bool
inlinenodiscardnoexcept

Returns true if the given move is quiet - ie, does not alter material.

Definition at line 476 of file Position.hpp.

◆ is_stalemate()

auto chess::game::Position::is_stalemate ( ) const -> bool
nodiscard

Returns true if the side to move has no legal moves, but their king is not attacked.

◆ is_threefold_repetition()

auto chess::game::Position::is_threefold_repetition ( ) const -> bool
inlinenodiscardnoexcept

Returns true if this position is a draw by threefold repetition.

Definition at line 439 of file Position.hpp.

◆ is_white_to_move()

auto chess::game::Position::is_white_to_move ( ) const -> bool
inlinenodiscardnoexcept

Returns true if the side to move is White.

Definition at line 217 of file Position.hpp.

◆ make_move()

Makes a move to alter the position.

◆ make_null_move()

Makes a null move. This essentially represents the side to move "passing" their turn. This does update the Zobrist hash, halfmove clock and fullmove counter in the same way that make_move() does.

◆ occupied()

auto chess::game::Position::occupied ( ) const -> Bitboard
inlinenodiscardnoexcept

Returns a bitboard that is the union of all White and Black piece positions.

Definition at line 231 of file Position.hpp.

◆ operator==()

auto chess::game::Position::operator== ( const Position & other) const -> bool
inlinenodiscardnoexcept

Returns true if the two positions have the same Zobrist hash.

Definition at line 140 of file Position.hpp.

◆ our_pieces() [1/2]

auto chess::game::Position::our_pieces ( ) const -> const Pieces &
inlinenodiscardnoexcept

Returns the pieces belonging to the side to move.

Definition at line 188 of file Position.hpp.

◆ our_pieces() [2/2]

auto chess::game::Position::our_pieces ( ) -> Pieces &
inlinenodiscardnoexcept

Returns the pieces belonging to the side to move.

Definition at line 179 of file Position.hpp.

◆ pieces_for() [1/2]

template<Color Side>
auto chess::game::Position::pieces_for ( ) const -> const Pieces &
inlinenodiscardnoexcept

Returns the piece set representing the given color.

Definition at line 160 of file Position.hpp.

◆ pieces_for() [2/2]

template<Color Side>
auto chess::game::Position::pieces_for ( ) -> Pieces &
inlinenodiscardnoexcept

Returns the piece set representing the given color.

Definition at line 150 of file Position.hpp.

◆ refresh_zobrist()

Recalculates the Zobrist hash for this position.

◆ their_pieces() [1/2]

auto chess::game::Position::their_pieces ( ) const -> const Pieces &
inlinenodiscardnoexcept

Returns the pieces belonging to the side-to-move's opponent.

Definition at line 206 of file Position.hpp.

◆ their_pieces() [2/2]

auto chess::game::Position::their_pieces ( ) -> Pieces &
inlinenodiscardnoexcept

Returns the pieces belonging to the side-to-move's opponent.

Definition at line 197 of file Position.hpp.

Member Data Documentation

◆ blackCastlingRights

Castling rights for the Black player.

Definition at line 101 of file Position.hpp.

◆ blackPieces

The positions of the Black pieces.

Invariant
The bitboard indices of the bits set in blackPieces must not overlap with any of the indices of the bits set in whitePieces.

Definition at line 92 of file Position.hpp.

◆ enPassantTargetSquare

If the last move was a pawn double-push, then this holds the square that a pawn would land on after capturing en passant. This is always set to a non-null value if the last move was a pawn double-push, even if no enemy pawns are actually in position to perform the capture.

Definition at line 109 of file Position.hpp.

◆ fullMoveCounter

std::uint_least64_t chess::game::Position::fullMoveCounter { UINT64_C(1) }

The number of full moves in the game. This value is monotonically increasing throughout the game, and is incremented after every Black move.

Definition at line 127 of file Position.hpp.

◆ halfmoveClock

std::uint_least8_t chess::game::Position::halfmoveClock { UINT8_C(0) }

This is a ply counter that enforces the 50-move rule. The counter is incremented after every move and reset by captures and pawn moves; if the counter reaches 100 and the side to move has at least 1 legal move, then the game is drawn.

Invariant
This value will never be greater than 100.
See also
is_fifty_move_draw()

Definition at line 121 of file Position.hpp.

◆ hash

The Zobrist hash value of this position. This value is incrementally updated by the make_move() function. If you manually change attributes of the position, call the refresh_zobrist() function to recalculate it.

Definition at line 137 of file Position.hpp.

◆ sideToMove

Indicates whose move it is in this position.

Definition at line 95 of file Position.hpp.

◆ whiteCastlingRights

Castling rights for the White player.

Definition at line 98 of file Position.hpp.

◆ whitePieces

The positions of the White pieces.

Invariant
The bitboard indices of the bits set in whitePieces must not overlap with any of the indices of the bits set in blackPieces.

Definition at line 84 of file Position.hpp.


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