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
impl Board
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a new empty board.
Consider using the Default trait to initialize the board.
Sourcepub fn side_to_move(&self) -> Color
pub fn side_to_move(&self) -> Color
Get the Color of the player who has to play.
Sourcepub fn castle_rights(&self, color: Color) -> CastleRights
pub fn castle_rights(&self, color: Color) -> CastleRights
Get the CastleRights for a given side.
Sourcepub fn en_passant(&self) -> Option<Square>
pub fn en_passant(&self) -> Option<Square>
Get the Square (if exist) of the En Passant.
Sourcepub fn is_valid(&self, m: ChessMove) -> bool
pub fn is_valid(&self, m: ChessMove) -> bool
Check if the ChessMove is valid. Legality is not verified.
Sourcepub fn remove_castle_rights(&mut self, color: Color, remove: CastleRights)
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);Sourcepub fn piece_on_is(&self, square: Square, piece: Piece) -> bool
pub fn piece_on_is(&self, square: Square, piece: Piece) -> bool
Sourcepub fn color_on_is(&self, square: Square, color: Color) -> bool
pub fn color_on_is(&self, square: Square, color: Color) -> bool
Sourcepub fn king_of(&self, color: Color) -> Square
pub fn king_of(&self, color: Color) -> Square
Get the Square of the Piece::King of the given Color.
Sourcepub fn is_empty(&self, square: Square) -> bool
pub fn is_empty(&self, square: Square) -> bool
Verify if the Square is empty (i.e. not occupied).
Sourcepub fn is_occupied(&self, square: Square) -> bool
pub fn is_occupied(&self, square: Square) -> bool
Verify if the Square is occupied.
Sourcepub fn is_check(&self) -> bool
pub fn is_check(&self) -> bool
Verify if the Piece::King is in check.
Sourcepub fn is_targeted(&self, target: Square, attacker: Color) -> bool
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.
Sourcepub fn is_not_targeted(&self, target: Square, attacker: Color) -> bool
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.
Sourcepub fn has_valid_move(&self, square: Square) -> bool
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.
Sourcepub fn has_legal_move(&self, square: Square) -> bool
pub fn has_legal_move(&self, square: Square) -> bool
Sourcepub fn has_any_move(&self) -> bool
pub fn has_any_move(&self) -> bool
Sourcepub fn get_valid_moves(&self, from: Square) -> Vec<Square>
pub fn get_valid_moves(&self, from: Square) -> Vec<Square>
Sourcepub fn get_legal_moves(&self, from: Square) -> Vec<Square>
pub fn get_legal_moves(&self, from: Square) -> Vec<Square>
Sourcepub fn get_line(&self, from: Square, direction: Direction) -> Vec<Square>
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§
impl Copy for Board
impl Eq for Board
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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