pub struct Board { /* private fields */ }Expand description
Board representation
The board is represented as a 120 square array, with the 64 squares of the board in the middle. The extra squares are used to simplify the implementation of move generation and attack detection.
Additionally, the board keeps track of the current state of the game, such as the active color, castling availability, en passant square, halfmove clock, fullmove count, and a log of moves made.
§Examples
use chessly::Board;
let board = Board::default();
assert_eq!(board.to_fen(), "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1");Implementations§
Source§impl Board
impl Board
Sourcepub fn from_fen(fen_string: &str) -> Result<Self, ParseError>
pub fn from_fen(fen_string: &str) -> Result<Self, ParseError>
Creates a new board from a FEN string
§Errors
Returns an error if the FEN string is invalid
§Examples
use chessly::Board;
let board = Board::from_fen("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1");
assert!(board.is_ok());
let board = Board::from_fen("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w");
assert!(board.is_err());
assert_eq!(board.unwrap_err().to_string(), "FEN parse error: Invalid FEN string");Source§impl Board
impl Board
Sourcepub fn to_fen(&self) -> String
pub fn to_fen(&self) -> String
Returns the FEN string representation of the board
§Examples
use chessly::Board;
let board = Board::default();
assert_eq!(board.to_fen(), "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1");Sourcepub fn get_legal_moves(&self) -> Vec<PieceMove>
pub fn get_legal_moves(&self) -> Vec<PieceMove>
Returns all legal moves in the current position
§Examples
use chessly::Board;
let board = Board::from_fen("8/6p1/4k3/8/4K3/8/4P3/8 w - - 0 1").unwrap();
let moves = board.get_legal_moves();
assert_eq!(moves.len(), 6);
assert_eq!(
moves
.into_iter()
.map(|m| m.to_string())
.collect::<Vec<_>>(),
vec!["e2e3", "e4d4", "e4e3", "e4f4", "e4f3", "e4d3"]
);Sourcepub fn get_legal_moves_for_square(
&self,
square: SquareCoordinates,
) -> Vec<PieceMove>
pub fn get_legal_moves_for_square( &self, square: SquareCoordinates, ) -> Vec<PieceMove>
Returns all legal moves for a given square in the current position
§Examples
use chessly::{Board, SquareCoordinates};
let board = Board::from_fen("8/2B2rp1/4k3/8/8/5K2/4P3/8 w - - 0 1").unwrap();
let square = SquareCoordinates::try_from("c7").unwrap();
let moves = board.get_legal_moves_for_square(square);
// Only one move is legal, since the king is in check
assert_eq!(moves.len(), 1);
assert_eq!(moves[0].to_string(), "c7f4");Sourcepub fn get_pseudo_legal_moves(&self) -> Vec<PieceMove>
pub fn get_pseudo_legal_moves(&self) -> Vec<PieceMove>
Returns all pseudo-legal moves in the current position
Pseudo-legal moves are moves that are valid according to the rules of chess, but may leave the king in check.
§Examples
use chessly::Board;
let board = Board::from_fen("8/6p1/4k3/8/4K3/8/4P3/8 w - - 0 1").unwrap();
let moves = board.get_pseudo_legal_moves();
assert_eq!(moves.len(), 9);
assert_eq!(
moves
.into_iter()
.map(|m| m.to_string())
.collect::<Vec<_>>(),
vec!["e2e3", "e4d4", "e4e3", "e4f4", "e4e5", "e4f3", "e4d3", "e4f5", "e4d5"]
);Sourcepub fn get_pseudo_legal_moves_for_square(
&self,
square: SquareCoordinates,
) -> Vec<PieceMove>
pub fn get_pseudo_legal_moves_for_square( &self, square: SquareCoordinates, ) -> Vec<PieceMove>
Returns all pseudo-legal moves for a given square in the current position
Pseudo-legal moves are moves that are valid according to the rules of chess, but may leave the king in check.
§Examples
use chessly::{Board, SquareCoordinates};
let board = Board::from_fen("8/2B2rp1/4k3/8/8/5K2/4P3/8 w - - 0 1").unwrap();
let square = SquareCoordinates::try_from("c7").unwrap();
let moves = board.get_pseudo_legal_moves_for_square(square);
assert_eq!(moves.len(), 9);
assert_eq!(
moves
.into_iter()
.map(|m| m.to_string())
.collect::<Vec<_>>(),
vec!["c7d6", "c7e5", "c7f4", "c7g3", "c7h2", "c7b6", "c7a5", "c7d8", "c7b8"]
);Sourcepub const fn get_piece(&self, square: SquareCoordinates) -> Option<Piece>
pub const fn get_piece(&self, square: SquareCoordinates) -> Option<Piece>
Returns the piece on a given square, if any exists
§Examples
use chessly::{Board, Color, Kind, Piece, SquareCoordinates};
let board = Board::default();
let square = SquareCoordinates::try_from("e2").unwrap();
assert_eq!(board.get_piece(square).unwrap(), Piece::new(Color::White, Kind::Pawn));
let square = SquareCoordinates::try_from("e3").unwrap();
assert!(board.get_piece(square).is_none());Sourcepub fn is_in_check(&self) -> bool
pub fn is_in_check(&self) -> bool
Determines if current player is in check
§Examples
use chessly::Board;
let board = Board::from_fen("8/3r3k/8/8/7P/3K3R/8/8 w - - 0 1").unwrap();
assert!(board.is_in_check());
let board = Board::from_fen("8/3r3k/8/8/7P/3K3R/8/8 b - - 0 1").unwrap();
assert!(!board.is_in_check());Sourcepub fn is_in_checkmate(&self) -> bool
pub fn is_in_checkmate(&self) -> bool
Determines if a game ended in checkmate
§Examples
use chessly::Board;
let board = Board::from_fen("8/1r5k/r7/8/7P/6BR/8/K7 w - - 0 1").unwrap();
assert!(board.is_in_checkmate());
let board = Board::from_fen("8/1r5k/r7/8/7P/7R/8/K7 w - - 0 1").unwrap();
assert!(!board.is_in_checkmate());Sourcepub fn is_draw(&self) -> bool
pub fn is_draw(&self) -> bool
Determines if a game ended in draw
The game can end in draw in the following situations:
- Stalemate
- Threefold repetition
- Fifty-move rule
- Insufficient material
Sourcepub fn is_in_stalemate(&self) -> bool
pub fn is_in_stalemate(&self) -> bool
Determines if a game ended in stalemate
§Examples
use chessly::Board;
let board = Board::from_fen("8/1r5k/7P/8/8/8/6r1/K7 w - - 0 1").unwrap();
assert!(board.is_in_stalemate());
// It's a mate, not a stalemate
let board = Board::from_fen("8/1r5k/r7/8/7P/6BR/8/K7 w - - 0 1").unwrap();
assert!(!board.is_in_stalemate());
let board = Board::from_fen("8/1r5k/r7/8/7P/7R/8/K7 w - - 0 1").unwrap();
assert!(!board.is_in_stalemate());Sourcepub fn is_threefold_repetition(&self) -> bool
pub fn is_threefold_repetition(&self) -> bool
Determines if a game ended in a draw by threefold repetition
§Examples
use chessly::{Board, PieceMove, SquareCoordinates};
let mut board = Board::from_fen("rnbqkbnr/pppp1ppp/4p3/8/8/4P3/PPPP1PPP/RNBQKBNR w KQkq - 0 2").unwrap();
assert!(!board.is_threefold_repetition());
let e1 = SquareCoordinates::try_from("e1").unwrap();
let e2 = SquareCoordinates::try_from("e2").unwrap();
let e8 = SquareCoordinates::try_from("e8").unwrap();
let e7 = SquareCoordinates::try_from("e7").unwrap();
board.make_move(PieceMove::new(e1, e2));
board.make_move(PieceMove::new(e8, e7));
board.make_move(PieceMove::new(e2, e1));
board.make_move(PieceMove::new(e7, e8));
board.make_move(PieceMove::new(e1, e2));
board.make_move(PieceMove::new(e8, e7));
board.make_move(PieceMove::new(e2, e1));
board.make_move(PieceMove::new(e7, e8));
board.make_move(PieceMove::new(e1, e2));
board.make_move(PieceMove::new(e8, e7));
assert!(board.is_threefold_repetition());Sourcepub const fn is_fifty_move_rule(&self) -> bool
pub const fn is_fifty_move_rule(&self) -> bool
Determines if a game ended in a draw by the fifty-move rule
§Examples
use chessly::Board;
let board = Board::from_fen("8/5kr1/8/8/8/4K3/4R3/8 w - - 100 123").unwrap();
assert!(board.is_fifty_move_rule());
let board = Board::from_fen("8/5kr1/8/8/8/4K3/4R3/8 w - - 99 101").unwrap();
assert!(!board.is_fifty_move_rule());pub const fn is_insufficient_material(&self) -> bool
Sourcepub const fn side_to_move(&self) -> Color
pub const fn side_to_move(&self) -> Color
Returns the current side to move
§Examples
use chessly::{Board, Color};
let board = Board::default();
assert_eq!(board.side_to_move(), Color::White);
let board = Board::from_fen("8/3r3k/8/8/7P/3K3R/8/8 b - - 0 1").unwrap();
assert_eq!(board.side_to_move(), Color::Black);Sourcepub fn make_move(&mut self, move_: PieceMove) -> Result<(), IllegalMoveError>
pub fn make_move(&mut self, move_: PieceMove) -> Result<(), IllegalMoveError>
Makes a move on the board
§Errors
Returns an error if the move is illegal
§Examples
use chessly::{Board, PieceMove, SquareCoordinates};
let mut board = Board::default();
let from = SquareCoordinates::try_from("e2").unwrap();
let to = SquareCoordinates::try_from("e4").unwrap();
let piece_move = PieceMove::new(from, to);
assert!(board.make_move(piece_move).is_ok());
assert_eq!(board.to_fen(), "rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq e3 0 1");Sourcepub fn undo_move(&mut self)
pub fn undo_move(&mut self)
Undoes the last move made on the board
§Examples
use chessly::{Board, PieceMove, SquareCoordinates};
let mut board = Board::default();
let from = SquareCoordinates::try_from("e2").unwrap();
let to = SquareCoordinates::try_from("e4").unwrap();
let piece_move = PieceMove::new(from, to);
board.make_move(piece_move).unwrap();
board.undo_move();
assert_eq!(board.to_fen(), "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1");