pub struct Board { /* fields omitted */ }
A representation of a chess board. That's why you're here, right?
Construct a board from a FEN string.
use chess::Board;
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());
Deprecating in 3.0.0:
please use the MoveGen structure instead. It is faster and more idiomatic.
Is this game Ongoing, is it Stalemate, or is it Checkmate?
use chess::{Board, BoardStatus, Square, Rank, File, ChessMove};
let mut board = Board::default();
assert_eq!(board.status(), BoardStatus::Ongoing);
board = board.make_move_new(ChessMove::new(Square::make_square(Rank::Second, File::E),
Square::make_square(Rank::Fourth, File::E),
None));
assert_eq!(board.status(), BoardStatus::Ongoing);
board = board.make_move_new(ChessMove::new(Square::make_square(Rank::Seventh, File::F),
Square::make_square(Rank::Sixth, File::F),
None));
assert_eq!(board.status(), BoardStatus::Ongoing);
board = board.make_move_new(ChessMove::new(Square::make_square(Rank::Second, File::D),
Square::make_square(Rank::Fourth, File::D),
None));
assert_eq!(board.status(), BoardStatus::Ongoing);
board = board.make_move_new(ChessMove::new(Square::make_square(Rank::Seventh, File::G),
Square::make_square(Rank::Fifth, File::G),
None));
assert_eq!(board.status(), BoardStatus::Ongoing);
board = board.make_move_new(ChessMove::new(Square::make_square(Rank::First, File::D),
Square::make_square(Rank::Fifth, File::H),
None));
assert_eq!(board.status(), BoardStatus::Checkmate);
Grab the "combined" BitBoard
. This is a BitBoard
with every piece.
use chess::{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);
Grab the "color combined" BitBoard
. This is a BitBoard
with every piece of a particular
color.
use chess::{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);
Give me the Square
the color
king is on.
use chess::{Board, Square, Color, Rank, File};
let board = Board::default();
assert_eq!(board.king_square(Color::White), Square::make_square(Rank::First, File::E));
assert_eq!(board.king_square(Color::Black), Square::make_square(Rank::Eighth, File::E));
Grab the "pieces" BitBoard
. This is a BitBoard
with every piece of a particular type.
use chess::{Board, BitBoard, Piece, Square, Rank, File};
let rooks = BitBoard::from_square(Square::make_square(Rank::First, File::A)) |
BitBoard::from_square(Square::make_square(Rank::First, File::H)) |
BitBoard::from_square(Square::make_square(Rank::Eighth, File::A)) |
BitBoard::from_square(Square::make_square(Rank::Eighth, File::H));
let board = Board::default();
assert_eq!(*board.pieces(Piece::Rook), rooks);
Grab the CastleRights
for a particular side.
use chess::{Board, Square, Rank, File, CastleRights, Color, ChessMove};
let move1 = ChessMove::new(Square::make_square(Rank::Second, File::A),
Square::make_square(Rank::Fourth, File::A),
None);
let move2 = ChessMove::new(Square::make_square(Rank::Seventh, File::E),
Square::make_square(Rank::Fifth, File::E),
None);
let move3 = ChessMove::new(Square::make_square(Rank::First, File::A),
Square::make_square(Rank::Second, File::A),
None);
let move4 = ChessMove::new(Square::make_square(Rank::Eighth, File::E),
Square::make_square(Rank::Seventh, File::E),
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);
Add castle rights for a particular side. Note: this can create an invalid position.
Remove castle rights for a particular side.
use chess::{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);
Who's turn is it?
use chess::{Board, Color};
let mut board = Board::default();
assert_eq!(board.side_to_move(), Color::White);
Grab my CastleRights
.
use chess::{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));
Add to my CastleRights
. Note: This can make the position invalid.
Remove some of my CastleRights
.
use chess::{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);
My opponents CastleRights
.
use chess::{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));
Add to my opponents CastleRights
. Note: This can make the position invalid.
Remove some of my opponents CastleRights
.
use chess::{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);
For a chess UI: set a piece on a particular square.
use chess::{Board, Piece, Color, Square, Rank, File};
let board = Board::default();
let new_board = board.set_piece(Piece::Queen,
Color::White,
Square::make_square(Rank::Fourth, File::E))
.expect("Valid Position");
assert_eq!(new_board.pieces(Piece::Queen).count(), 3);
For a chess UI: clear a particular square.
use chess::{Board, Square, Rank, File, Piece};
let board = Board::default();
let new_board = board.clear_square(Square::make_square(Rank::First, File::A))
.expect("Valid Position");
assert_eq!(new_board.pieces(Piece::Rook).count(), 3);
Switch the color of the player without actually making a move. Returns None if the current
player is in check.
use chess::{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);
Does this board "make sense"?
Do all the pieces make sense, do the bitboards combine correctly, etc?
This is for sanity checking.
use chess::{Board, Color, Piece, Square, Rank, File};
let board = Board::default();
assert_eq!(board.is_sane(), true);
let bad_board = board.clear_square(Square::make_square(Rank::First, File::E)).expect("Valid Position");
assert_eq!(bad_board.is_sane(), false);
Get a pawn hash of the board (a hash that only changes on color change and pawn moves).
What piece is on a particular Square
? Is there even one?
use chess::{Board, Piece, Square, Rank, File};
let board = Board::default();
let sq1 = Square::make_square(Rank::First, File::A);
let sq2 = Square::make_square(Rank::Fourth, File::D);
assert_eq!(board.piece_on(sq1), Some(Piece::Rook));
assert_eq!(board.piece_on(sq2), None);
What color piece is on a particular square?
Give me the en_passant square, if it exists.
use chess::{Board, ChessMove, Square, Rank, File};
let move1 = ChessMove::new(Square::make_square(Rank::Second, File::D),
Square::make_square(Rank::Fourth, File::D),
None);
let move2 = ChessMove::new(Square::make_square(Rank::Seventh, File::H),
Square::make_square(Rank::Fifth, File::H),
None);
let move3 = ChessMove::new(Square::make_square(Rank::Fourth, File::D),
Square::make_square(Rank::Fifth, File::D),
None);
let move4 = ChessMove::new(Square::make_square(Rank::Seventh, File::E),
Square::make_square(Rank::Fifth, File::E),
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::make_square(Rank::Fifth, File::E)));
Is a particular move legal? This function is very slow, but will work on unsanitized
input.
use chess::{Board, ChessMove, Square, Rank, File};
let move1 = ChessMove::new(Square::make_square(Rank::Second, File::E),
Square::make_square(Rank::Fourth, File::E),
None);
let move2 = ChessMove::new(Square::make_square(Rank::Second, File::E),
Square::make_square(Rank::Fifth, File::E),
None);
let board = Board::default();
assert_eq!(board.legal(move1), true);
assert_eq!(board.legal(move2), false);
Make a chess move onto a new board.
panic!() if king is captured.
use chess::{Board, ChessMove, Square, Rank, File, Color};
let m = ChessMove::new(Square::make_square(Rank::Second, File::D),
Square::make_square(Rank::Fourth, File::D),
None);
let board = Board::default();
assert_eq!(board.make_move_new(m).side_to_move(), Color::Black);
Make a chess move onto an already allocated Board
.
panic!() if king is captured.
use chess::{Board, ChessMove, Square, Rank, File, Color};
let m = ChessMove::new(Square::make_square(Rank::Second, File::D),
Square::make_square(Rank::Fourth, File::D),
None);
let board = Board::default();
let mut result = Board::default();
board.make_move(m, &mut result);
assert_eq!(result.side_to_move(), Color::Black);
Give me the BitBoard
of my pinned pieces.
Give me the Bitboard
of the pieces putting me in check.
Construct the initial position.
Returns the "default value" for a type. Read more
Performs copy-assignment from source
. Read more
This method tests for self
and other
values to be equal, and is used by ==
. Read more
This method tests for !=
.
Formats the value using the given formatter. Read more
type Owned = T
Creates owned data from borrowed data, usually by cloning. Read more
🔬 This is a nightly-only experimental API. (toowned_clone_into
)
recently added
Uses borrowed data to replace owned data, usually by cloning. Read more
type Error = !
🔬 This is a nightly-only experimental API. (try_from
)
The type returned in the event of a conversion error.
🔬 This is a nightly-only experimental API. (try_from
)
Immutably borrows from an owned value. Read more
type Error = <U as TryFrom<T>>::Error
🔬 This is a nightly-only experimental API. (try_from
)
The type returned in the event of a conversion error.
🔬 This is a nightly-only experimental API. (try_from
)
Mutably borrows from an owned value. Read more
🔬 This is a nightly-only experimental API. (get_type_id
)
this method will likely be replaced by an associated static