Board

Struct Board 

Source
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

Source

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

Source

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");

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"]
);

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");

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"]
);

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"]
);
Source

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());
Source

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());
Source

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());
Source

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
Source

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());
Source

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());
Source

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());
Source

pub const fn is_insufficient_material(&self) -> bool

Source

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);
Source

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");
Source

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");

Trait Implementations§

Source§

impl Clone for Board

Source§

fn clone(&self) -> Board

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Board

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Board

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Display for Board

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V