Skip to main content

Position

Struct Position 

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

Piece occupancy for all 64 squares.

The internal array uses canonical A1-to-H8 indexing. Use Position::piece_at and Position::set_piece when the square is known by file and rank.

Realtime board updates carry positions through BoardEvent::PositionChanged.

§Examples

use chessnut_move::protocol::{
    Color, File, Piece, PieceKind, Position, Rank, SQUARE_COUNT, Square,
};

let mut position = Position::new([None; SQUARE_COUNT]);
let e4 = Square::new(File::E, Rank::Four);
position.set_piece(
    e4,
    Some(Piece {
        color: Color::White,
        kind: PieceKind::Pawn,
    }),
);
assert!(position.piece_at(e4).is_some());

Implementations§

Source§

impl Position

Source

pub fn new(squares: [Option<Piece>; 64]) -> Self

Creates a position from squares in canonical A1-to-H8 order.

Index zero represents A1 and index 63 represents H8.

§Examples
use chessnut_move::protocol::{
    Color, File, Piece, PieceKind, Position, Rank, SQUARE_COUNT, Square,
};

let white_king = Piece {
    color: Color::White,
    kind: PieceKind::King,
};
let e1 = Square::new(File::E, Rank::One);
let mut squares = [None; SQUARE_COUNT];
squares[e1.index()] = Some(white_king);

let position = Position::new(squares);
assert_eq!(position.piece_at(e1), Some(white_king));
Source

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

Returns the piece occupying a square, or None when it is empty.

§Examples
use chessnut_move::protocol::{
    BoardEvent, File, PieceKind, Rank, Square,
};

fn pawn_reached_e4(event: BoardEvent) -> bool {
    let BoardEvent::PositionChanged(position) = event else {
        return false;
    };

    position
        .piece_at(Square::new(File::E, Rank::Four))
        .is_some_and(|piece| piece.kind == PieceKind::Pawn)
}
Source

pub fn set_piece(&mut self, square: Square, piece: Option<Piece>)

Replaces the piece occupying a square.

Pass None to empty the square.

§Examples
use chessnut_move::protocol::{
    AutoMoveMode, Command, File, Position, Rank, Square,
};

fn move_e2_to_e4(mut observed: Position) -> Command {
    let e2 = Square::new(File::E, Rank::Two);
    let e4 = Square::new(File::E, Rank::Four);
    let pawn = observed.piece_at(e2);

    observed.set_piece(e2, None);
    observed.set_piece(e4, pawn);
    Command::auto_move(observed, AutoMoveMode::Normal)
}

Trait Implementations§

Source§

impl Clone for Position

Source§

fn clone(&self) -> Position

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

Source§

impl Debug for Position

Source§

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

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

impl Eq for Position

Source§

impl Hash for Position

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for Position

Source§

fn eq(&self, other: &Position) -> 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> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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 = 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<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more