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
sourceimpl 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 pinned(&self) -> Vec<Square>ⓘNotable traits for Vec<u8, A>impl<A> Write for Vec<u8, A>where
A: Allocator,
pub fn pinned(&self) -> Vec<Square>ⓘNotable traits for Vec<u8, A>impl<A> Write for Vec<u8, A>where
A: Allocator,
A: Allocator,
Get the piece pinned for the current side.
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>ⓘNotable traits for Vec<u8, A>impl<A> Write for Vec<u8, A>where
A: Allocator,
pub fn get_valid_moves(&self, from: Square) -> Vec<Square>ⓘNotable traits for Vec<u8, A>impl<A> Write for Vec<u8, A>where
A: Allocator,
A: Allocator,
sourcepub fn get_legal_moves(&self, from: Square) -> Vec<Square>ⓘNotable traits for Vec<u8, A>impl<A> Write for Vec<u8, A>where
A: Allocator,
pub fn get_legal_moves(&self, from: Square) -> Vec<Square>ⓘNotable traits for Vec<u8, A>impl<A> Write for Vec<u8, A>where
A: Allocator,
A: Allocator,
sourcepub fn get_line(&self, from: Square, direction: Direction) -> Vec<Square>ⓘNotable traits for Vec<u8, A>impl<A> Write for Vec<u8, A>where
A: Allocator,
pub fn get_line(&self, from: Square, direction: Direction) -> Vec<Square>ⓘNotable traits for Vec<u8, A>impl<A> Write for Vec<u8, A>where
A: Allocator,
A: Allocator,
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]
)