pub struct Position { /* private fields */ }
Expand description
struct Position A complete data set that can represent any chess position.
§Members:
- pieces - a piece-centric setwise container of all basic chess piece positions.
- player - Color of player whose turn it is. AKA: “side_to_move”.
- castling - Castling rights for both players.
- en_passant - Indicates if en passant is possible, and for which square.
- halfmoves - Tracker for 50 move draw rule. Resets after capture/pawn move.
- fullmoves - Starts at 1, increments after each black player’s move.
Implementations§
Source§impl Position
impl Position
Sourcepub fn start_position() -> Self
pub fn start_position() -> Self
Standard chess start position.
pub fn player(&self) -> &Color
pub fn castling(&self) -> &Castling
pub fn en_passant(&self) -> &Option<Square>
pub fn halfmoves(&self) -> &MoveCount
pub fn fullmoves(&self) -> &MoveCount
Sourcepub fn moves_played(&self) -> MoveCount
pub fn moves_played(&self) -> MoveCount
Return the number of moves played in this game so far, from the fullmove counter.
Sourcepub fn color_flip(&self) -> Self
pub fn color_flip(&self) -> Self
Create a new position where the relative position is the same for the active player, but the player gets switched. This is equivalent to a vertical flip and color swap for all pieces, along with castling rights, player and en-passant.
This is useful for checking that an evaluation function scores the same scenario presented to either player.
Sourcepub fn is_same_as(&self, other: &Self) -> bool
pub fn is_same_as(&self, other: &Self) -> bool
Returns true if the positions are the same, in context of FIDE laws for position repetition. They are the same if the player to move, piece kind and color per square, en passant, and castling rights are the same.
Sourcepub fn fifty_move_rule(&self, num_legal_moves: usize) -> bool
pub fn fifty_move_rule(&self, num_legal_moves: usize) -> bool
Returns true if the fifty-move rule has been reached by this position, indicating that it is drawn.
num_legal_moves
: number of legal moves for this position.
Sourcepub fn move_info(&self, move_: Move) -> MoveInfo
pub fn move_info(&self, move_: Move) -> MoveInfo
Generate a MoveInfo for this position from a given Move.
Sourcepub fn do_move(&mut self, move_: Move) -> MoveInfo
pub fn do_move(&mut self, move_: Move) -> MoveInfo
Apply a move to self, in place.
do_move
does not check if the move is legal or not,
it simply executes it while assuming legality.
Castling is described by moving king 2 squares, as defined in UCI protocol.
Assumptions:
There is an active player’s piece on from square.
There is no active player’s piece on to square.
A double king move from starting position is a castling.
Current behavior:
Removes from square from active player piece on that square.
Removes to square from all passive player pieces.
Panics if from square has no active player piece.
Sourcepub fn do_move_info(&mut self, move_info: MoveInfo)
pub fn do_move_info(&mut self, move_info: MoveInfo)
Incrementally apply a move to self, in place. This assumes the given move_info is legal.
Sourcepub fn undo_move(&mut self, move_info: MoveInfo, cache: Cache)
pub fn undo_move(&mut self, move_info: MoveInfo, cache: Cache)
Undo the application of a move, in place.
Sourcepub fn do_legal_move(&mut self, move_: Move) -> Option<MoveInfo>
pub fn do_legal_move(&mut self, move_: Move) -> Option<MoveInfo>
Checks if move is legal before applying it. If move is legal, the move is applied and returns the resulting MoveInfo. Otherwise, no action is taken and returns None. This is best used as a CLI function, not in the engine.
Sourcepub fn is_checkmate(&self) -> bool
pub fn is_checkmate(&self) -> bool
Check if the current position is checkmated. Returns true if it is mate, false otherwise.
Sourcepub fn is_stalemate(&self) -> bool
pub fn is_stalemate(&self) -> bool
Check if the current position is stalemated. Returns true if it is stalemate, false otherwise.
Sourcepub fn make_move(&self, move_: Move) -> Self
pub fn make_move(&self, move_: Move) -> Self
Generates a new Position from applying move on current Position.
Sourcepub fn is_legal_move(&self, move_: Move) -> bool
pub fn is_legal_move(&self, move_: Move) -> bool
Checks if given move is legal for current position.
Sourcepub fn is_in_check(&self) -> bool
pub fn is_in_check(&self) -> bool
Returns true if active player’s king is in any check.
Sourcepub fn active_king_checks(&self) -> (bool, bool)
pub fn active_king_checks(&self) -> (bool, bool)
Returns tuple representing if current player’s king is in single or double check. Tuple format: (is_in_single_check, is_in_double_check).
pub fn num_active_king_checks(&self) -> u32
Sourcepub fn attackers_to(&self, target: Square, attacking: Color) -> Bitboard
pub fn attackers_to(&self, target: Square, attacking: Color) -> Bitboard
Returns bitboard with positions of all pieces of a player attacking a square. Assumes there is no overlap for pieces of a color.
Sourcepub fn is_attacked_by(&self, target: Square, attacking: Color) -> bool
pub fn is_attacked_by(&self, target: Square, attacking: Color) -> bool
Returns true if target square is attacked by any piece of attacking color.
Sourcepub fn attacks(&self, attacking: Color, occupied: Bitboard) -> Bitboard
pub fn attacks(&self, attacking: Color, occupied: Bitboard) -> Bitboard
Returns bitboard with all squares attacked by a player’s pieces.
Sourcepub fn get_legal_moves(&self) -> MoveList
pub fn get_legal_moves(&self) -> MoveList
Returns a list of all legal moves for active player in current position. This operation is expensive. Notes: If En-Passant, need to check for sliding piece check discovery. If king is in check, number of moves are restricted. If king is pinned, number of moves are restricted.
Trait Implementations§
Source§impl Display for Position
Displays pretty-printed chess board and Fen string representing Position.
impl Display for Position
Displays pretty-printed chess board and Fen string representing Position.
Source§impl Fen for Position
impl Fen for Position
Source§fn parse_fen(s: &str) -> Result<Self, ParseFenError>
fn parse_fen(s: &str) -> Result<Self, ParseFenError>
Attempt to parse a Fen string into implementing type.