Struct Board

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

A representation of a chess board. That’s why you’re here, right?

Implementations§

Source§

impl Board

Source

pub fn from_fen(fen: String) -> Option<Board>

👎Deprecated since 3.1.0: please use Board::from_str(fen)? instead

Construct a board from a FEN string.

use candidate::Board;
use std::str::FromStr;


// This is no longer supported
let init_position = Board::from_fen("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1".to_owned()).expect("Valid FEN");
assert_eq!(init_position, Board::default());

// This is the new way
let init_position_2 = Board::from_str("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1")?;
assert_eq!(init_position_2, Board::default());
Source

pub fn enumerate_moves(&self, moves: &mut [ChessMove; 256]) -> usize

👎Deprecated since 3.0.0: please use the MoveGen structure instead. It is faster and more idiomatic.
Source

pub fn status(&self) -> BoardStatus

Is this game Ongoing, is it Stalemate, or is it Checkmate?

use candidate::{Board, BoardStatus, Square, ChessMove};

let mut board = Board::default();

assert_eq!(board.status(), BoardStatus::Ongoing);

board = board.make_move_new(ChessMove::new(Square::E2,
                                           Square::E4,
                                           None));

assert_eq!(board.status(), BoardStatus::Ongoing);

board = board.make_move_new(ChessMove::new(Square::F7,
                                           Square::F6,
                                           None));

assert_eq!(board.status(), BoardStatus::Ongoing);

board = board.make_move_new(ChessMove::new(Square::D2,
                                           Square::D4,
                                           None));

assert_eq!(board.status(), BoardStatus::Ongoing);

board = board.make_move_new(ChessMove::new(Square::G7,
                                           Square::G5,
                                           None));

assert_eq!(board.status(), BoardStatus::Ongoing);

board = board.make_move_new(ChessMove::new(Square::D1,
                                           Square::H5,
                                           None));

assert_eq!(board.status(), BoardStatus::Checkmate);
Source

pub fn combined(&self) -> &BitBoard

Grab the “combined” BitBoard. This is a BitBoard with every piece.

use candidate::{Board, BitBoard, Rank, get_rank};

let board = Board::default();

let combined_should_be = get_rank(Rank::First) |
                         get_rank(Rank::Second) |
                         get_rank(Rank::Seventh) |
                         get_rank(Rank::Eighth);

assert_eq!(*board.combined(), combined_should_be);
Source

pub fn color_combined(&self, color: Color) -> &BitBoard

Grab the “color combined” BitBoard. This is a BitBoard with every piece of a particular color.

use candidate::{Board, BitBoard, Rank, get_rank, Color};

let board = Board::default();

let white_pieces = get_rank(Rank::First) |
                   get_rank(Rank::Second);

let black_pieces = get_rank(Rank::Seventh) |
                   get_rank(Rank::Eighth);

assert_eq!(*board.color_combined(Color::White), white_pieces);
assert_eq!(*board.color_combined(Color::Black), black_pieces);
Source

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

Give me the Square the color king is on.

use candidate::{Board, Square, Color};

let board = Board::default();

assert_eq!(board.king_square(Color::White), Square::E1);
assert_eq!(board.king_square(Color::Black), Square::E8);
Source

pub fn pieces(&self, piece: Piece) -> &BitBoard

Grab the “pieces” BitBoard. This is a BitBoard with every piece of a particular type.

use candidate::{Board, BitBoard, Piece, Square};

// The rooks should be in each corner of the board
let rooks = BitBoard::from_square(Square::A1) |
            BitBoard::from_square(Square::H1) |
            BitBoard::from_square(Square::A8) |
            BitBoard::from_square(Square::H8);

let board = Board::default();

assert_eq!(*board.pieces(Piece::Rook), rooks);
Source

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

Grab the CastleRights for a particular side.

use candidate::{Board, Square, CastleRights, Color, ChessMove};

let move1 = ChessMove::new(Square::A2,
                           Square::A4,
                           None);

let move2 = ChessMove::new(Square::E7,
                           Square::E5,
                           None);

let move3 = ChessMove::new(Square::A1,
                           Square::A2,
                           None);

let move4 = ChessMove::new(Square::E8,
                           Square::E7,
                           None);

let mut board = Board::default();

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

board = board.make_move_new(move1)
             .make_move_new(move2)
             .make_move_new(move3)
             .make_move_new(move4);

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

pub fn add_castle_rights(&mut self, color: Color, add: CastleRights)

👎Deprecated since 3.1.0: When doing board setup, use the BoardBuilder structure. It ensures you don’t end up with an invalid position.

Add castle rights for a particular side. Note: this can create an invalid position.

Source

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

👎Deprecated since 3.1.0: When doing board setup, use the BoardBuilder structure. It ensures you don’t end up with an invalid position.

Remove castle rights for a particular side.

use candidate::{Board, CastleRights, Color};

let mut board = Board::default();
assert_eq!(board.castle_rights(Color::White), CastleRights::Both);

board.remove_castle_rights(Color::White, CastleRights::KingSide);
assert_eq!(board.castle_rights(Color::White), CastleRights::QueenSide);
Source

pub fn side_to_move(&self) -> Color

Who’s turn is it?

use candidate::{Board, Color};

let mut board = Board::default();
assert_eq!(board.side_to_move(), Color::White);
Source

pub fn my_castle_rights(&self) -> CastleRights

Grab my CastleRights.

use candidate::{Board, Color, CastleRights};

let mut board = Board::default();
board.remove_castle_rights(Color::White, CastleRights::KingSide);
board.remove_castle_rights(Color::Black, CastleRights::QueenSide);

assert_eq!(board.my_castle_rights(), board.castle_rights(Color::White));
Source

pub fn add_my_castle_rights(&mut self, add: CastleRights)

👎Deprecated since 3.1.0: When doing board setup, use the BoardBuilder structure. It ensures you don’t end up with an invalid position.

Add to my CastleRights. Note: This can make the position invalid.

Source

pub fn remove_my_castle_rights(&mut self, remove: CastleRights)

👎Deprecated since 3.1.0: When doing board setup, use the BoardBuilder structure. It ensures you don’t end up with an invalid position.

Remove some of my CastleRights.

use candidate::{Board, CastleRights};

let mut board = Board::default();
assert_eq!(board.my_castle_rights(), CastleRights::Both);

board.remove_my_castle_rights(CastleRights::KingSide);
assert_eq!(board.my_castle_rights(), CastleRights::QueenSide);
Source

pub fn their_castle_rights(&self) -> CastleRights

My opponents CastleRights.

use candidate::{Board, Color, CastleRights};

let mut board = Board::default();
board.remove_castle_rights(Color::White, CastleRights::KingSide);
board.remove_castle_rights(Color::Black, CastleRights::QueenSide);

assert_eq!(board.their_castle_rights(), board.castle_rights(Color::Black));
Source

pub fn add_their_castle_rights(&mut self, add: CastleRights)

👎Deprecated since 3.1.0: When doing board setup, use the BoardBuilder structure. It ensures you don’t end up with an invalid position.

Add to my opponents CastleRights. Note: This can make the position invalid.

Source

pub fn remove_their_castle_rights(&mut self, remove: CastleRights)

👎Deprecated since 3.1.0: When doing board setup, use the BoardBuilder structure. It ensures you don’t end up with an invalid position.

Remove some of my opponents CastleRights.

use candidate::{Board, CastleRights};

let mut board = Board::default();
assert_eq!(board.their_castle_rights(), CastleRights::Both);

board.remove_their_castle_rights(CastleRights::KingSide);
assert_eq!(board.their_castle_rights(), CastleRights::QueenSide);
Source

pub fn set_piece( &self, piece: Piece, color: Color, square: Square, ) -> Option<Board>

👎Deprecated since 3.1.0: When doing board setup, use the BoardBuilder structure. It ensures you don’t end up with an invalid position.

For a chess UI: set a piece on a particular square.

use candidate::{Board, Piece, Color, Square};

let board = Board::default();

let new_board = board.set_piece(Piece::Queen,
                                Color::White,
                                Square::E4)
                     .expect("Valid Position");

assert_eq!(new_board.pieces(Piece::Queen).count(), 3);
Source

pub fn clear_square(&self, square: Square) -> Option<Board>

👎Deprecated since 3.1.0: When doing board setup, use the BoardBuilder structure. It ensures you don’t end up with an invalid position.

For a chess UI: clear a particular square.

use candidate::{Board, Square, Piece};

let board = Board::default();

let new_board = board.clear_square(Square::A1)
                     .expect("Valid Position");

assert_eq!(new_board.pieces(Piece::Rook).count(), 3);
Source

pub fn null_move(&self) -> Option<Board>

Switch the color of the player without actually making a move. Returns None if the current player is in check.

Note that this erases the en-passant information, so applying this function twice does not always give the same result back.

use candidate::{Board, Color};

let board = Board::default();

assert_eq!(board.side_to_move(), Color::White);

let new_board = board.null_move().expect("Valid Position");

assert_eq!(new_board.side_to_move(), Color::Black);
Source

pub fn is_sane(&self) -> bool

Does this board “make sense”? Do all the pieces make sense, do the bitboards combine correctly, etc? This is for sanity checking.

use candidate::{Board, Color, Piece, Square};

let board = Board::default();

assert_eq!(board.is_sane(), true);

// Remove the king
let bad_board = board.clear_square(Square::E1).expect("Valid Position");
assert_eq!(bad_board.is_sane(), false);
Source

pub fn get_hash(&self) -> u64

Get a hash of the board.

Source

pub fn get_pawn_hash(&self) -> u64

Get a pawn hash of the board (a hash that only changes on color change and pawn moves).

Currently not implemented…

Source

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

What piece is on a particular Square? Is there even one?

use candidate::{Board, Piece, Square};

let board = Board::default();

assert_eq!(board.piece_on(Square::A1), Some(Piece::Rook));
assert_eq!(board.piece_on(Square::D4), None);
Source

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

What color piece is on a particular square?

Source

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

Give me the en_passant square, if it exists.

use candidate::{Board, ChessMove, Square};

let move1 = ChessMove::new(Square::D2,
                           Square::D4,
                           None);

let move2 = ChessMove::new(Square::H7,
                           Square::H5,
                           None);

let move3 = ChessMove::new(Square::D4,
                           Square::D5,
                           None);

let move4 = ChessMove::new(Square::E7,
                           Square::E5,
                           None);

let board = Board::default().make_move_new(move1)
                            .make_move_new(move2)
                            .make_move_new(move3)
                            .make_move_new(move4);

assert_eq!(board.en_passant(), Some(Square::E5));
Source

pub fn en_passant_target(self) -> Option<Square>

Source

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

Is a particular move legal? This function is very slow, but will work on unsanitized input.

use candidate::{Board, ChessMove, Square, MoveGen};

let move1 = ChessMove::new(Square::E2,
                           Square::E4,
                           None);

let move2 = ChessMove::new(Square::E2,
                           Square::E5,
                           None);

let board = Board::default();

assert_eq!(board.legal(move1), true);
assert_eq!(board.legal(move2), false);
Source

pub fn legal_destinations_from(&self, square: Square) -> BitBoard

Source

pub fn make_move_new(&self, m: ChessMove) -> Board

Make a chess move onto a new board.

panic!() if king is captured.

use candidate::{Board, ChessMove, Square, Color};

let m = ChessMove::new(Square::D2,
                       Square::D4,
                       None);

let board = Board::default();
assert_eq!(board.make_move_new(m).side_to_move(), Color::Black);
Source

pub fn make_move(&self, m: ChessMove, result: &mut Board)

Make a chess move onto an already allocated Board.

panic!() if king is captured.

use candidate::{Board, ChessMove, Square, Color};

let m = ChessMove::new(Square::D2,
                       Square::D4,
                       None);

let board = Board::default();
let mut result = Board::default();
board.make_move(m, &mut result);
assert_eq!(result.side_to_move(), Color::Black);
Source

pub fn pinned(&self) -> &BitBoard

Give me the BitBoard of my pinned pieces.

Source

pub fn checkers(&self) -> &BitBoard

Give me the Bitboard of the pieces putting me in check.

Source

pub fn has_checkers(&self) -> bool

Trait Implementations§

Source§

impl Clone for Board

Source§

fn clone(&self) -> Board

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

const 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

Construct the initial position.

Source§

fn default() -> Board

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

impl Display for Board

Source§

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

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

impl From<&Board> for BoardBuilder

Source§

fn from(board: &Board) -> Self

Converts to this type from the input type.
Source§

impl From<Board> for BoardBuilder

Source§

fn from(board: Board) -> Self

Converts to this type from the input type.
Source§

impl FromStr for Board

Source§

type Err = Error

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

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

Parses a string s to return a value of this type. Read more
Source§

impl Hash for Board

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 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 TryFrom<&BoardBuilder> for Board

Source§

type Error = Error

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

fn try_from(fen: &BoardBuilder) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl TryFrom<&mut BoardBuilder> for Board

Source§

type Error = Error

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

fn try_from(fen: &mut BoardBuilder) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl TryFrom<BoardBuilder> for Board

Source§

type Error = Error

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

fn try_from(fen: BoardBuilder) -> Result<Self, Self::Error>

Performs the conversion.
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> 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.