Struct Position

Source
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

Source

pub fn start_position() -> Self

Standard chess start position.

Source

pub fn pieces(&self) -> &PieceSets

Const getters.

Source

pub fn player(&self) -> &Color

Source

pub fn castling(&self) -> &Castling

Source

pub fn en_passant(&self) -> &Option<Square>

Source

pub fn halfmoves(&self) -> &MoveCount

Source

pub fn fullmoves(&self) -> &MoveCount

Source

pub fn moves_played(&self) -> MoveCount

Return the number of moves played in this game so far, from the fullmove counter.

Source

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.

Source

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.

Source

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.

Source

pub fn move_info(&self, move_: Move) -> MoveInfo

Generate a MoveInfo for this position from a given Move.

Source

pub fn cache(&self) -> Cache

Returns this position’s cached state.

Source

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.

Source

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.

Source

pub fn undo_move(&mut self, move_info: MoveInfo, cache: Cache)

Undo the application of a move, in place.

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.

Source

pub fn is_checkmate(&self) -> bool

Check if the current position is checkmated. Returns true if it is mate, false otherwise.

Source

pub fn is_stalemate(&self) -> bool

Check if the current position is stalemated. Returns true if it is stalemate, false otherwise.

Source

pub fn make_move(&self, move_: Move) -> Self

Generates a new Position from applying move on current Position.

Checks if given move is legal for current position.

Source

pub fn is_in_check(&self) -> bool

Returns true if active player’s king is in any check.

Source

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).

Source

pub fn num_active_king_checks(&self) -> u32

Source

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.

Source

pub fn is_attacked_by(&self, target: Square, attacking: Color) -> bool

Returns true if target square is attacked by any piece of attacking color.

Source

pub fn attacks(&self, attacking: Color, occupied: Bitboard) -> Bitboard

Returns bitboard with all squares attacked by a player’s pieces.

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 Clone for Position

Source§

fn clone(&self) -> Position

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Position

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Position

Defaults to standard chess start position.

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Display for Position

Displays pretty-printed chess board and Fen string representing Position.

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Fen for Position

Source§

fn parse_fen(s: &str) -> Result<Self, ParseFenError>

Attempt to parse a Fen string into implementing type.

Source§

fn to_fen(&self) -> String

Returns string representation of implementing type in Fen format.

Source§

fn parse_halfmove_clock(s: &str) -> Result<MoveCount, ParseFenError>

HalfMove Clock is any non-negative number.
Source§

fn parse_fullmove_number(s: &str) -> Result<MoveCount, ParseFenError>

FullMove Number starts at 1, and can increment infinitely.
Source§

impl<'a> From<&'a Position> for Key<'a>

Convert a Position reference into a Key.

Source§

fn from(pos_ref: &'a Position) -> Self

Converts to this type from the input type.
Source§

impl From<&Position> for Cache

Source§

fn from(position: &Position) -> Self

Converts to this type from the input type.
Source§

impl From<Position> for Game

Convert a position to a Game with no past moves.

Source§

fn from(position: Position) -> Self

Converts to this type from the input type.
Source§

impl PartialEq for Position

Source§

fn eq(&self, other: &Position) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Copy for Position

Source§

impl Eq for Position

Source§

impl StructuralPartialEq for Position

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V