Skip to main content

BoardBuilder

Struct BoardBuilder 

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

Represents a chess position that has not been validated for legality.

This structure is useful in the following cases:

  • You are trying to build a chess board manually in code.
  • The Board structure will try to keep the position fully legal, which will prevent you from placing pieces arbitrarily. This structure will not.
  • You want to display the chess position in a UI.
  • You want to convert between formats like FEN.
use chess::{BoardBuilder, Board, Square, Color, Piece};
use std::convert::TryFrom;
let mut position = BoardBuilder::new();
position.piece(Square::A1, Piece::King, Color::White);
position.piece(Square::A8, Piece::Rook, Color::Black);
position.piece(Square::D1, Piece::King, Color::Black);

// You can index the position by the square:
assert_eq!(position[Square::A1], Some((Piece::King, Color::White)));

// White is in check, but that's ok, it's white's turn to move.
assert!(Board::try_from(&position).is_ok());

// Now White is in check, but Black is ready to move.  This position is invalid.
position.side_to_move(Color::Black);
assert!(Board::try_from(position).is_err());

// One liners are possible with the builder pattern.
use std::convert::TryInto;

let res: Result<Board, _> = BoardBuilder::new()
                       .piece(Square::A1, Piece::King, Color::White)
                       .piece(Square::A8, Piece::King, Color::Black)
                       .try_into();
assert!(res.is_ok());

Implementations§

Source§

impl BoardBuilder

Source

pub fn new() -> BoardBuilder

Construct a new, empty, BoardBuilder.

  • No pieces are on the board
  • CastleRights are empty for both sides
  • en_passant is not set
  • side_to_move is Color::White
use chess::{BoardBuilder, Board, Square, Color, Piece};
use std::convert::TryInto;

let board: Board = BoardBuilder::new()
    .piece(Square::A1, Piece::King, Color::White)
    .piece(Square::A8, Piece::King, Color::Black)
    .try_into()?;
Source

pub fn setup<'a>( pieces: impl IntoIterator<Item = &'a (Square, Piece, Color)>, side_to_move: Color, white_castle_rights: CastleRights, black_castle_rights: CastleRights, en_passant: Option<File>, ) -> BoardBuilder

Set up a board with everything pre-loaded.

use chess::{BoardBuilder, Board, Square, Color, Piece, CastleRights};
use std::convert::TryInto;

let board: Board = BoardBuilder::setup(
        &[
            (Square::A1, Piece::King, Color::White),
            (Square::H8, Piece::King, Color::Black)
        ],
        Color::Black,
        CastleRights::NoRights,
        CastleRights::NoRights,
        None)
    .try_into()?;
Source

pub fn get_side_to_move(&self) -> Color

Get the current player

use chess::{BoardBuilder, Board, Color};

let bb: BoardBuilder = Board::default().into();
assert_eq!(bb.get_side_to_move(), Color::White);
Source

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

Get the castle rights for a player

use chess::{BoardBuilder, Board, CastleRights, Color};

let bb: BoardBuilder = Board::default().into();
assert_eq!(bb.get_castle_rights(Color::White), CastleRights::Both);
Source

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

Get the current en_passant square

use chess::{BoardBuilder, Board, Square, ChessMove};

let board = Board::default()
    .make_move_new(ChessMove::new(Square::E2, Square::E4, None))
    .make_move_new(ChessMove::new(Square::H7, Square::H6, None))
    .make_move_new(ChessMove::new(Square::E4, Square::E5, None))
    .make_move_new(ChessMove::new(Square::D7, Square::D5, None));
let bb: BoardBuilder = board.into();
assert_eq!(bb.get_en_passant(), Some(Square::D5));
Source

pub fn side_to_move(&mut self, color: Color) -> &mut Self

Set the side to move on the position

This function can be used on self directly or in a builder pattern.

use chess::{BoardBuilder, Color};
BoardBuilder::new()
             .side_to_move(Color::Black);      

let mut bb = BoardBuilder::new();
bb.side_to_move(Color::Black);
Source

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

Set the castle rights for a particular color on the position

This function can be used on self directly or in a builder pattern.

use chess::{BoardBuilder, Color, CastleRights};
BoardBuilder::new()
             .castle_rights(Color::White, CastleRights::NoRights);

let mut bb = BoardBuilder::new();
bb.castle_rights(Color::Black, CastleRights::Both);
Source

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

Set a piece on a square.

Note that this can and will overwrite another piece on the square if need.

Note also that this will not update your castle rights.

This function can be used on self directly or in a builder pattern.

use chess::{BoardBuilder, Color, Square, Piece};

BoardBuilder::new()
             .piece(Square::A1, Piece::Rook, Color::White);

let mut bb = BoardBuilder::new();
bb.piece(Square::A8, Piece::Rook, Color::Black);
Source

pub fn clear_square(&mut self, square: Square) -> &mut Self

Clear a square on the board.

Note that this will not update your castle rights.

This function can be used on self directly or in a builder pattern.

use chess::{BoardBuilder, Square, Board};

let mut bb: BoardBuilder = Board::default().into();
bb.clear_square(Square::A1);
Source

pub fn en_passant(&mut self, file: Option<File>) -> &mut Self

Set or clear the en_passant File.

This function can be used directly or in a builder pattern.

use chess::{BoardBuilder, Square, Board, File, Color, Piece};

BoardBuilder::new()
             .piece(Square::E4, Piece::Pawn, Color::White)
             .en_passant(Some(File::E));

Trait Implementations§

Source§

impl Clone for BoardBuilder

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

Source§

impl Default for BoardBuilder

Source§

fn default() -> BoardBuilder

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

impl Display for BoardBuilder

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 BoardBuilder

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 Index<Square> for BoardBuilder

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 BoardBuilder

Source§

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

Performs the mutable indexing (container[index]) operation. Read more
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.

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 = !

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.