Skip to main content

Position

Struct Position 

Source
pub struct Position { /* private fields */ }
Expand description

A shogi position with fast do/undo and an incrementally maintained Zobrist key.

The board state is stored redundantly (mailbox + bitboards); moves are assumed to be legal (generate them with Position::legal_moves). A side may have no king (as in tsume-shogi problems).

A Position is a plain value: it owns nothing on the heap, so cloning it copies and does not allocate.

Implementations§

Source§

impl Position

Source

pub fn generate_moves( &self, listener: impl FnMut(MoveSet) -> ControlFlow<()>, ) -> ControlFlow<()>

Calls listener with every group of legal moves for the side to move, including pawn-drop-mate exclusion.

Returning ControlFlow::Break from listener stops generation early — useful to answer “is there any legal move?” without building the rest — and is reported back as the return value, so the caller can tell a full walk from an interrupted one. No empty MoveSet is ever passed.

The opponent’s king square is never a destination: in illegal positions where that king could already be captured (unreachable through legal play, but constructible via Position::new), no king-capture move is generated — the rest of the result is unspecified there, but this function does not panic.

Source

pub fn legal_moves(&self) -> Vec<Move>

All legal moves for the side to move.

The compatibility wrapper over Position::generate_moves; prefer the callback form in hot code, which allocates nothing.

Whether the side to move has at least one legal move.

Stops at the first group found, so it is far cheaper than !legal_moves().is_empty().

Source

pub fn in_check(&self) -> bool

Whether the side to move is in check.

Source§

impl Position

Source

pub fn new(partial: PartialPosition) -> Self

Builds a Position from a PartialPosition.

Source

pub fn startpos() -> Self

The initial position.

Source

pub fn piece_at(&self, square: Square) -> Option<Piece>

The piece on square, if any.

Source

pub fn side_to_move(&self) -> Color

The player to move.

Source

pub fn ply(&self) -> u16

The current ply (1 at the start of a game).

Source

pub fn hand(&self, color: Color) -> Hand

The hand of color.

Source

pub fn king_square(&self, color: Color) -> Option<Square>

The square of color’s king, if it is on the board.

Source

pub fn key(&self) -> u64

The Zobrist key (board, hands and side to move).

Source

pub fn occupied(&self) -> Bitboard ⓘ

The squares occupied by pieces of either color.

Source

pub fn player_bb(&self, color: Color) -> Bitboard ⓘ

The squares occupied by color’s pieces.

Source

pub fn piece_kind_bb(&self, piece_kind: PieceKind) -> Bitboard ⓘ

The squares occupied by pieces of piece_kind, either color.

Source

pub fn do_move(&mut self, mv: Move) -> Undo

Makes mv on the board. mv must be legal in this position.

Returns what Position::undo_move needs to take it back. Moves are undone in the reverse of the order they were made, each with its own Undo.

Source

pub fn undo_move(&mut self, mv: Move, undo: Undo)

Undoes mv, the last move made with Position::do_move, using the Undo that call returned.

Trait Implementations§

Source§

impl Clone for Position

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · 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

Draws the board as seen by Black, with USI piece letters.

Source§

impl Default for Position

Source§

fn default() -> Self

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

impl Eq for Position

Source§

impl From<PartialPosition> for Position

Source§

fn from(partial: PartialPosition) -> Self

Converts to this type from the input type.
Source§

impl PartialEq for Position

Source§

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

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.