Board

Struct Board 

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

A representation of a chess board that implement FEN notation (Board::from_str).

§Examples

use chess::{Square, Board, Color, Piece, ChessMove};

let mut board = Board::default();
// 8 | r n b q k b n r
// 7 | p p p p p p p p
// 6 | . . . . . . . .
// 5 | . . . . . . . .
// 4 | . . . . . . . .
// 3 | . . . . . . . .
// 2 | P P P P P P P P
// 1 | R N B Q K B N R
//   +----------------
//     A B C D E F G H

assert_eq!(board.on(Square::E8), Some((Piece::King, Color::Black)));

// White move the pawn from E2 to E4
let m = ChessMove::new(Square::E2, Square::E4);
board.update(m);
// 8 | r n b q k b n r
// 7 | p p p p p p p p
// 6 | . . . . . . . .
// 5 | . . . . . . . .
// 4 | . . . . P . . .
// 3 | . . . . . . . .
// 2 | P P P P . P P P
// 1 | R N B Q K B N R
//   +----------------
//     A B C D E F G H

assert_eq!(board.on(Square::E4), Some((Piece::Pawn, Color::White)));
assert_eq!(board.side_to_move(), Color::Black);

Implementations§

Source§

impl Board

Source

pub fn new() -> Self

Create a new empty board.

Consider using the Default trait to initialize the board.

Source

pub fn side_to_move(&self) -> Color

Get the Color of the player who has to play.

Source

pub fn castle_rights(&self, color: Color) -> CastleRights

Get the CastleRights for a given side.

Source

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

Get the Square (if exist) of the En Passant.

Source

pub fn halfmoves(&self) -> u64

Get the halfmoves number.

Source

pub fn fullmoves(&self) -> u64

Get the fullmoves number.

Source

pub fn state(&self) -> GameState

Get the GameState of the Board.

Source

pub fn is_valid(&self, m: ChessMove) -> bool

Check if the ChessMove is valid. Legality is not verified.

Check if the ChessMove is legal.

Source

pub fn update(&mut self, m: ChessMove)

Update the chessboard according to the chess rules.

Assume that the ChessMove is legal.

§Examples
use chess::{Board, ChessMove, Square};

let mut board = Board::default();
let m = ChessMove::new(Square::E2, Square::E4);

board.update(m);
Source

pub fn remove_castle_rights(&mut self, color: Color, remove: CastleRights)

Remove CastleRights for a particular side.

§Examples
use chess::{Board, CastleRights, Color};
let mut board = Board::default();

assert_eq!(board.castle_rights(Color::White), CastleRights::Both);
assert_eq!(board.castle_rights(Color::Black), CastleRights::Both);

board.remove_castle_rights(Color::White, CastleRights::QueenSide);
board.remove_castle_rights(Color::Black, CastleRights::Both);

assert_eq!(board.castle_rights(Color::White), CastleRights::KingSide);
assert_eq!(board.castle_rights(Color::Black), CastleRights::NoRights);
Source

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

Get the Piece at a given Square.

Source

pub fn piece_on_is(&self, square: Square, piece: Piece) -> bool

Verify if the Square is occupied by the given Piece.

Source

pub fn color_on(&self, square: Square) -> Option<Color>

Get the Color at a given Square.

Source

pub fn color_on_is(&self, square: Square, color: Color) -> bool

Verify if the Square is occupied by the given Color.

Source

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

Get the Color at a given Square.

Source

pub fn on_is(&self, square: Square, (piece, color): (Piece, Color)) -> bool

Verify if the Square is occupied by the given Piece and Color.

Source

pub fn is_pinned(&self, square: Square) -> bool

Verify if the given Square is pinned for the current side.

This implementation returns true only if the Piece has more than one valid move, if not or if the Square is empty, then it returns false.

Source

pub fn pinned(&self) -> Vec<Square>

Get the piece pinned for the current side.

Source

pub fn king_of(&self, color: Color) -> Square

Get the Square of the Piece::King of the given Color.

Source

pub fn is_empty(&self, square: Square) -> bool

Verify if the Square is empty (i.e. not occupied).

Source

pub fn is_occupied(&self, square: Square) -> bool

Verify if the Square is occupied.

Source

pub fn is_check(&self) -> bool

Verify if the Piece::King is in check.

Source

pub fn is_targeted(&self, target: Square, attacker: Color) -> bool

Verify if a Square can be taken by the given Color in the current Board.

Reciprocal: see Board::is_not_targeted.

Source

pub fn is_not_targeted(&self, target: Square, attacker: Color) -> bool

Verify if a Square cannot be taken by the given Color in the current Board.

Reciprocal: see Board::is_targeted.

Source

pub fn has_valid_move(&self, square: Square) -> bool

Verify if the Piece on the Square has one or more valid moves.

If no Piece exist on the Square, then return false.

Note: The legality is not verify, if you want to: use has_legal_move.

Verify if the Piece on the Square has one or more legal moves.

If no Piece exist on the Square, then return false.

Source

pub fn has_any_move(&self) -> bool

Verify if the player has one or more legal moves in all the Board.

If no Piece exist on the Square, then return false.

Source

pub fn get_valid_moves(&self, from: Square) -> Vec<Square>

Compute and return all the valid moves for a Piece (if exist) at a given Square.

If no Piece exist on the Square, then return an empty Vec.

Note: The legality is not verify, if you want to: use get_legal_moves.

Compute and return all the legal moves for a Piece (if exist) at a given Square.

If no Piece exist on the Square, then return an empty Vec.

Source

pub fn get_line(&self, from: Square, direction: Direction) -> Vec<Square>

Construct a Vec of Square from a Square (exclusive) to the first Piece (inclusive) with a given direction.

§Examples
use chess::{Board, ChessMove, Direction, Square};

let mut board = Board::default();
board.update(ChessMove::new(Square::D2, Square::D3));
board.update(ChessMove::new(Square::G7, Square::G5));
// 8 | r n b q k b n r
// 7 | p p p p p p . p
// 6 | . . . . . . . .
// 5 | . . . . . . p .
// 4 | . . . . . . . .
// 3 | . . . P . . . .
// 2 | P P P . P P P P
// 1 | R N B Q K B N R
//   +----------------
//     A B C D E F G H

assert_eq!(
    board.get_line(Square::C1, Direction::UpRight),
    vec![Square::D2, Square::E3, Square::F4, Square::G5]
)

Trait Implementations§

Source§

impl Clone for Board

Source§

fn clone(&self) -> Board

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 Board

Source§

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

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

impl Default for Board

Source§

fn default() -> Self

Default board is his initial state at the beginning of a chess game.

8 | r n b q k b n r
7 | p p p p p p p p
6 | . . . . . . . .
5 | . . . . . . . .
4 | . . . . . . . .
3 | . . . . . . . .
2 | P P P P P P P P
1 | R N B Q K B N R
  +----------------
    A B C D E F G H
Source§

impl Display for Board

Source§

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

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

impl FromStr for Board

Source§

fn from_str(value: &str) -> Result<Self, Self::Err>

From Forsyth-Edwards Notation (FEN).

https://www.chess.com/terms/fen-chess

Source§

type Err = Error

The associated error which can be returned from parsing.
Source§

impl Index<Square> for Board

Source§

type Output = Option<(Piece, Color)>

The returned type after indexing.
Source§

fn index(&self, index: Square) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl IndexMut<Square> for Board

Source§

fn index_mut(&mut self, index: Square) -> &mut Self::Output

Performs the mutable indexing (container[index]) operation. Read more
Source§

impl PartialEq for Board

Source§

fn eq(&self, other: &Board) -> 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 Board

Source§

impl Eq for Board

Source§

impl StructuralPartialEq for Board

Auto Trait Implementations§

§

impl Freeze for Board

§

impl RefUnwindSafe for Board

§

impl Send for Board

§

impl Sync for Board

§

impl Unpin for Board

§

impl UnwindSafe for Board

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
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