Skip to main content

StandardChess

Struct StandardChess 

Source
pub struct StandardChess { /* private fields */ }
Expand description

Standard Chess Variant This Variant is the most common chess variant played worldwide It is played on an 8x8 board with the following pieces for each player:

  • 8 Pawns
  • 2 Rooks
  • 2 Knights
  • 2 Bishops
  • 1 Queen
  • 1 King

The game is won by checkmating the opponent’s king The game is drawn by stalemate, threefold repetition, the fifty-move rule, or agreement The game is lost by resigning or losing on time

§Attributes

  • game - The game struct that holds the state of the game

Trait Implementations§

Source§

impl Clone for StandardChess

Source§

fn clone(&self) -> StandardChess

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 StandardChess

Source§

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

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

impl Default for StandardChess

Source§

fn default() -> StandardChess

Creates a new instance of the StandardChess Variant with default values

§Returns

A new instance of the StandardChess Variant

§Examples
let game = StandardChess::default();
Source§

impl Variant for StandardChess

Source§

fn move_piece(&mut self, move_str: &str) -> Result<GameStatus, MoveError>

Moves a Piece on the crate::logic::Board

§Arguments
  • move_str - A move string in algebraic notation.
§Returns

A Result<GameStatus, MoveError> object

  • Ok(GameStatus) - The status of the game after the move.
  • Err(MoveError) - An error occurred while moving the piece.
§Examples
let mut game = StandardChess::default();
let status = game.move_piece("e4");

assert_eq!(status, Ok(GameStatus::InProgress));
Source§

fn undo(&mut self)

Undoes the last Move

§Examples
let mut game = StandardChess::default();
game.move_piece("e4").unwrap();
game.undo();

assert_eq!(game.fen(), "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1");
Source§

fn redo(&mut self)

Redoes the last undone Move

§Examples
let mut game = StandardChess::default();
game.move_piece("e4").unwrap();
game.undo();
game.redo();

assert_eq!(game.fen(), "rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq - 0 1");
Source§

fn pgn(&self) -> String

Returns the PGN string of the Game

§Returns

The PGN string of the Game

§Examples
let mut game = StandardChess::default();
game.move_piece("e4").unwrap();
game.move_piece("e5").unwrap();
let pgn = game.pgn();

assert!(pgn.contains("1. e4 e5"));
Source§

fn fen(&self) -> String

Returns the FEN string of the game

§Returns

The FEN string of the game

§Examples
let game = StandardChess::default();
let fen = game.fen();

assert_eq!(fen, "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1");
Source§

fn get_piece_at(&self, pos: Position) -> Option<Piece>

Returns the piece at a given position

§Arguments
  • pos - The position to get the piece from
§Returns

The piece at the given position, if there is one

§Example
use chess_lab::core::Position;

let game = StandardChess::default();
let piece = game.get_piece_at(Position::from_string("e2").unwrap());
assert!(piece.is_some());
assert_eq!(piece.unwrap().to_string(), "P");

Returns the legal moves of a piece at a given position

§Arguments
  • pos - The position to get the legal moves for
§Returns

A vector of legal moves for the piece at the given position

§Example
use chess_lab::core::Position;

let game = StandardChess::default();
let legal_moves = game.get_legal_moves(Position::from_string("e2").unwrap());
assert!(legal_moves.iter().any(|m| m.to_string() == "e4"));
Source§

fn save(&self, path: &str, overwrite: bool) -> Result<(), Error>

Saves the Game to a file

§Arguments
  • path - The path to the file
  • overwrite - Whether to overwrite the file if it already exists
§Returns

A Result<(), std::io::Error> object

  • Ok(()) - The game was saved successfully
  • Err(std::io::Error) - An error occurred while saving the game
§Examples
let game = StandardChess::default();
let path = "data/standard/ex.pgn";

game.save(path, true).unwrap();
Source§

fn resign(&mut self, color: Color)

Resigns the Game for a player

§Arguments
  • color - The Color of the player who resigns
§Examples
use chess_lab::core::Color;
let mut game = StandardChess::default();
game.resign(Color::White);
Source§

fn draw(&mut self)

Sets the Game as a draw by agreement

§Examples
let mut game = StandardChess::default();
game.draw();
Source§

fn lost_on_time(&mut self, color: Color)

Sets the Game as lost in time for a player

§Arguments
  • color - The Color of the player who lost in time
§Examples
use chess_lab::core::Color;

let mut game = StandardChess::default();
game.lost_on_time(Color::Black);
Source§

fn get_minified_fen(&self) -> String

Returns the minified FEN string of the Game

§Returns

The minified FEN string of the Game

§Examples
let game = StandardChess::default();
let minified_fen = game.get_minified_fen();
Source§

fn get_last_move(&self) -> Option<Move>

Returns the last Move of the Game

§Returns

The last Move of the Game, if there is one

§Examples
let mut game = StandardChess::default();
game.move_piece("e4").unwrap();
let last_move = game.get_last_move();
assert_eq!(last_move.unwrap().to_string(), "e4");
Source§

fn is_white_turn(&self) -> bool

Returns whether it is white’s turn to Move

§Returns

Whether it is white’s turn to Move

§Examples
let game = StandardChess::default();
let color = game.is_white_turn();
Source§

fn get_halfmove_clock(&self) -> u32

Returns the halfmove clock of the Game

§Returns

The halfmove clock of the Game

§Examples
let game = StandardChess::default();
let halfmove_clock = game.get_halfmove_clock();
Source§

fn get_fullmove_number(&self) -> u32

Returns the fullmove number of the Game

§Returns

The fullmove number of the Game

§Examples
let game = StandardChess::default();
let fullmove_number = game.get_fullmove_number();
Source§

fn get_castling_rights(&self) -> String

Returns the castling rights of the Game

§Returns

The castling rights of the Game

§Examples
let game = StandardChess::default();
let castling_rights = game.get_castling_rights();
Source§

fn get_en_passant(&self) -> Option<Position>

Returns the en passant square of the Game

§Returns

The en passant square of the Game

§Examples
let game = StandardChess::default();
let en_passant = game.get_en_passant();
Source§

fn get_starting_fen(&self) -> String

Returns the starting FEN of the Game

§Returns

A copy of the starting FEN of the Game

§Examples
let game = StandardChess::default();
let starting_fen = game.get_starting_fen();
Source§

fn get_status(&self) -> GameStatus

Returns the status of the Game

§Returns

The status of the Game

§Examples
let game = StandardChess::default();
let status = game.get_status();
Source§

impl VariantBuilder for StandardChess

Source§

fn name() -> &'static str

Returns the name of the Variant

§Returns

The name of the Variant

§Examples
let name = StandardChess::name();
assert_eq!(name, "Standard");
Source§

fn new(game: Game) -> StandardChess

Creates a new instance of the StandardChess Variant

§Returns

A new instance of the StandardChess Variant

§Examples
use chess_lab::logic::Game;

let game = StandardChess::new(Game::default());
Source§

fn from_fen(fen: &str) -> Result<StandardChess, FenError>

Creates a new instance of the StandardChess Variant from a FEN string

§Arguments
  • fen - A FEN string
§Returns
  • Ok(StandardChess) - A new instance of the StandardChess Variant
  • Err(FenError) - An error occurred while parsing the FEN string
§Examples
let fen = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
let game = StandardChess::from_fen(fen).unwrap();
Source§

fn from_pgn(pgn: &str) -> Result<StandardChess, PGNError>

Creates a new instance of the StandardChess Variant from a PGN string

§Arguments
  • pgn - A PGN string
§Returns

A Result<StandardChess, PGNError> object

  • Ok(StandardChess) - A new instance of the StandardChess Variant
  • Err(PgnError) - An error occurred while parsing the PGN string
§Examples
let pgn = "1. e4 e5 2. Nf3 Nc6 3. Bb5 a6";

let game = StandardChess::from_pgn(pgn).unwrap();
Source§

fn load(path: &str) -> Result<StandardChess, PGNError>

Loads the Game from a file

§Arguments
  • path - The path to the file
§Returns

A Result<StandardChess, PGNError> object

  • Ok(StandardChess) - The Game was loaded successfully
  • Err(PgnError) - An error occurred while loading the Game
§Examples

let path = "data/standard/ex1.pgn";
let game = StandardChess::load(path).unwrap();
Source§

fn load_all(path: &str) -> Result<Vec<Self>, PGNError>

Loads multiple Games from a file

§Arguments
  • path - The path to the file
§Returns

A Result<Vec<StandardChess>, PGNError> object

  • Ok(Vec<StandardChess>) - The games were loaded successfully
  • Err(PgnError) - An error occurred while loading the games
§Examples
let path = "data/standard/ex3.pgn";
let games = StandardChess::load_all(path).unwrap();

Auto Trait Implementations§

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, 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