Struct minorhacks_chess::BoardBuilder [−][src]
pub struct BoardBuilder { /* fields omitted */ }
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 minorhacks_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
impl BoardBuilder
[src]
impl BoardBuilder
[src]pub fn new() -> BoardBuilder
[src]
pub fn new() -> BoardBuilder
[src]Construct a new, empty, BoardBuilder.
- No pieces are on the board
CastleRights
are empty for both sidesen_passant
is not setside_to_move
is Color::White
use minorhacks_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()?;
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
[src]
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
[src]Set up a board with everything pre-loaded.
use minorhacks_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()?;
pub fn get_side_to_move(&self) -> Color
[src]
pub fn get_side_to_move(&self) -> Color
[src]Get the current player
use minorhacks_chess::{BoardBuilder, Board, Color}; let bb: BoardBuilder = Board::default().into(); assert_eq!(bb.get_side_to_move(), Color::White);
pub fn get_castle_rights(&self, color: Color) -> CastleRights
[src]
pub fn get_castle_rights(&self, color: Color) -> CastleRights
[src]Get the castle rights for a player
use minorhacks_chess::{BoardBuilder, Board, CastleRights, Color}; let bb: BoardBuilder = Board::default().into(); assert_eq!(bb.get_castle_rights(Color::White), CastleRights::Both);
pub fn get_en_passant(&self) -> Option<Square>
[src]
pub fn get_en_passant(&self) -> Option<Square>
[src]Get the current en_passant square
use minorhacks_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));
pub fn side_to_move(&mut self, color: Color) -> &mut Self
[src]
pub fn side_to_move(&mut self, color: Color) -> &mut Self
[src]Set the side to move on the position
This function can be used on self directly or in a builder pattern.
use minorhacks_chess::{BoardBuilder, Color}; BoardBuilder::new() .side_to_move(Color::Black); let mut bb = BoardBuilder::new(); bb.side_to_move(Color::Black);
pub fn castle_rights(
&mut self,
color: Color,
castle_rights: CastleRights
) -> &mut Self
[src]
pub fn castle_rights(
&mut self,
color: Color,
castle_rights: CastleRights
) -> &mut Self
[src]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 minorhacks_chess::{BoardBuilder, Color, CastleRights}; BoardBuilder::new() .castle_rights(Color::White, CastleRights::NoRights); let mut bb = BoardBuilder::new(); bb.castle_rights(Color::Black, CastleRights::Both);
pub fn piece(&mut self, square: Square, piece: Piece, color: Color) -> &mut Self
[src]
pub fn piece(&mut self, square: Square, piece: Piece, color: Color) -> &mut Self
[src]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 minorhacks_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);
pub fn clear_square(&mut self, square: Square) -> &mut Self
[src]
pub fn clear_square(&mut self, square: Square) -> &mut Self
[src]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 minorhacks_chess::{BoardBuilder, Square, Board}; let mut bb: BoardBuilder = Board::default().into(); bb.clear_square(Square::A1);
pub fn en_passant(&mut self, file: Option<File>) -> &mut Self
[src]
pub fn en_passant(&mut self, file: Option<File>) -> &mut Self
[src]Set or clear the en_passant File
.
This function can be used directly or in a builder pattern.
use minorhacks_chess::{BoardBuilder, Square, Board, File, Color, Piece}; BoardBuilder::new() .piece(Square::E4, Piece::Pawn, Color::White) .en_passant(Some(File::E));
Trait Implementations
impl Clone for BoardBuilder
[src]
impl Clone for BoardBuilder
[src]fn clone(&self) -> BoardBuilder
[src]
fn clone(&self) -> BoardBuilder
[src]Returns a copy of the value. Read more
fn clone_from(&mut self, source: &Self)
1.0.0[src]
fn clone_from(&mut self, source: &Self)
1.0.0[src]Performs copy-assignment from source
. Read more
impl Default for BoardBuilder
[src]
impl Default for BoardBuilder
[src]fn default() -> BoardBuilder
[src]
fn default() -> BoardBuilder
[src]Returns the “default value” for a type. Read more
impl Display for BoardBuilder
[src]
impl Display for BoardBuilder
[src]impl From<&'_ Board> for BoardBuilder
[src]
impl From<&'_ Board> for BoardBuilder
[src]impl FromStr for BoardBuilder
[src]
impl FromStr for BoardBuilder
[src]impl Index<Square> for BoardBuilder
[src]
impl Index<Square> for BoardBuilder
[src]impl IndexMut<Square> for BoardBuilder
[src]
impl IndexMut<Square> for BoardBuilder
[src]impl TryFrom<&'_ BoardBuilder> for Board
[src]
impl TryFrom<&'_ BoardBuilder> for Board
[src]impl TryFrom<&'_ mut BoardBuilder> for Board
[src]
impl TryFrom<&'_ mut BoardBuilder> for Board
[src]impl TryFrom<BoardBuilder> for Board
[src]
impl TryFrom<BoardBuilder> for Board
[src]impl Copy for BoardBuilder
[src]
Auto Trait Implementations
impl RefUnwindSafe for BoardBuilder
impl Send for BoardBuilder
impl Sync for BoardBuilder
impl Unpin for BoardBuilder
impl UnwindSafe for BoardBuilder
Blanket Implementations
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]pub fn borrow_mut(&mut self) -> &mut T
[src]
pub fn borrow_mut(&mut self) -> &mut T
[src]Mutably borrows from an owned value. Read more
impl<T> ToOwned for T where
T: Clone,
[src]
impl<T> ToOwned for T where
T: Clone,
[src]type Owned = T
type Owned = T
The resulting type after obtaining ownership.
pub fn to_owned(&self) -> T
[src]
pub fn to_owned(&self) -> T
[src]Creates owned data from borrowed data, usually by cloning. Read more
pub fn clone_into(&self, target: &mut T)
[src]
pub fn clone_into(&self, target: &mut T)
[src]🔬 This is a nightly-only experimental API. (toowned_clone_into
)
recently added
Uses borrowed data to replace owned data, usually by cloning. Read more