Skip to main content

Game

Struct Game 

Source
pub struct Game {
    pub board: Board,
    pub is_white_turn: bool,
    pub halfmove_clock: u32,
    pub fullmove_number: u32,
    pub en_passant: Option<Position>,
    pub castling_rights: u8,
    pub starting_fen: String,
    pub history: PGNTree<Move>,
    pub prev_positions: HashMap<String, u32>,
    pub status: GameStatus,
    /* private fields */
}
Expand description

Represents a game of chess It contains the board, the turn, the halfmove clock, the fullmove number, the en passant square, the castling rights, the start position, the history, a flag to indicate if the king needs to be captured, the previous positions and the game status

§Example

let game = Game::default();
assert_eq!(game.to_string(), "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1");

Fields§

§board: Board

Board state for the current game.

§is_white_turn: bool

True when it’s white to move.

§halfmove_clock: u32

Halfmove clock for the fifty-move rule.

§fullmove_number: u32

Fullmove number (starts at 1).

§en_passant: Option<Position>

En passant target square, if any.

§castling_rights: u8

Castling rights bitmask (KQkq).

§starting_fen: String

Starting FEN used to initialize the game.

§history: PGNTree<Move>

PGN tree storing move history and variations.

§prev_positions: HashMap<String, u32>

Reduced FEN positions for repetition tracking.

§status: GameStatus

Current game status.

Implementations§

Source§

impl Game

Source

pub fn new(fen: &str, capture_king: bool) -> Result<Game, FenError>

Creates a new game

§Arguments
  • fen: A string slice that holds the FEN representation of the game
  • capture_king: A boolean that indicates if the king needs to be captured
§Returns
  • Ok(Game): A new game
  • Err(FenError): An error if the FEN is invalid
§Example
let game = Game::new("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1", true).unwrap();
assert_eq!(game.to_string(), "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1");
Source

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

Creates a new game from a FEN string

§Arguments
  • fen: A string slice that holds the FEN representation of the game
§Returns
  • Ok(Game): A new game
  • Err(FenError): An error if the FEN is invalid
§Example
let game = Game::from_fen("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1").unwrap();
assert_eq!(game.to_string(), "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1");
Source

pub fn get_variant(&self) -> String

Returns the variant of the game

§Returns

A string that holds the variant of the game

§Example
let game = Game::default();
assert_eq!(game.get_variant(), "Standard");
Source

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

Moves a piece on the board

§Arguments
  • move_str: A string slice that holds the move
§Returns
  • Ok(GameStatus): The status of the game after the move
  • Err(MoveError): An error if the move is invalid
§Example
let mut game = Game::default();
game.move_piece("e4").unwrap();
assert_eq!(game.to_string(), "rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq - 0 1");
Source

pub fn fen(&self) -> String

Returns the FEN representation of the game

§Returns

A string that holds the FEN representation of the game

§Example
use chess_lab::logic::Game;

let game = Game::default();
assert_eq!(game.fen(), "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1");
Source

pub fn undo(&mut self)

Undoes the last move

§Example
use chess_lab::logic::Game;

let mut game = Game::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

pub fn redo(&mut self)

Redoes the last undone move

§Example
use chess_lab::logic::Game;

let mut game = Game::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

pub fn redo_nth(&mut self, n: u32)

Redoes the nth variation of the last undone move

§Arguments
  • n - The number of the variation to redo
§Example
use chess_lab::logic::Game;

let mut game = Game::default();
game.move_piece("e4").unwrap();
game.undo();
game.move_piece("d4").unwrap();
game.undo();
game.redo_nth(1);
assert_eq!(
    game.fen(),
    "rnbqkbnr/pppppppp/8/8/3P4/8/PPP1PPPP/RNBQKBNR b KQkq - 0 1"
);
Source

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

Returns the last move made in the game

§Returns

An Option<Move> containing the last move made in the game, or None if no moves have been made

§Example
use chess_lab::logic::Game;

let mut game = Game::default();
game.move_piece("e4").unwrap();

let last_move = game.get_last_move().unwrap();
assert_eq!(last_move.to_string(), "e4");
Source

pub 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

An Option<Piece> containing the piece at the given position, or None if the position is empty

§Example
use chess_lab::core::{PieceType, Position};
use chess_lab::logic::Game;

let game = Game::default();
let piece = game.get_piece_at(Position::from_string("e2").unwrap()).unwrap();
assert_eq!(piece.piece_type, PieceType::Pawn);
Source

pub fn start(&mut self)

Undoes all moves until the starting position

§Example
use chess_lab::logic::Game;

let mut game = Game::default();
game.move_piece("e4").unwrap();
game.move_piece("e5").unwrap();
game.start();

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

pub fn end(&mut self)

Redoes all moves until the last move

§Example
use chess_lab::logic::Game;

let mut game = Game::default();
game.move_piece("e4").unwrap();
game.move_piece("e5").unwrap();

game.start();
game.end();

assert_eq!(game.fen(), "rnbqkbnr/pppp1ppp/8/4p3/4P3/8/PPPP1PPP/RNBQKBNR w KQkq - 0 2");
Source

pub fn pgn(&self) -> String

Returns the PGN of the game

§Returns

A string containing the PGN of the game

§Example
use chess_lab::logic::Game;

let mut game = Game::default();
game.move_piece("e4").unwrap();
game.move_piece("e5").unwrap();
println!("{}", game.pgn());
Source

pub fn parse_move( &self, move_str: &str, ) -> Result<(PieceType, (Option<u8>, Option<u8>), Position, MoveType), MoveError>

Parse a move string and return the start and end positions

§Arguments
  • move_str: A string slice that holds the move to be parsed
§Returns
  • Ok((PieceType, (Option<u8>, Option<u8>), Position, MoveType)): A tuple containing the piece type, the start position, the end position and the move type
  • Err(MoveError): An error if the move is invalid

Check if a move is legal

§Arguments
  • piece: The piece being moved
  • start_pos: The starting position of the piece
  • end_pos: The ending position of the piece
  • move_type: The type of move being made
§Returns

Whether the move is legal

Returns all the legal moves for a piece at a given position

§Arguments
  • pos: The position of the piece to get the legal moves for
§Returns

A vector containing all the legal moves for the piece at the given position

§Example
use chess_lab::core::Position;
use chess_lab::logic::Game;

let game = Game::default();
let legal_moves = game.get_legal_moves(Position::from_string("e2").unwrap());
assert_eq!(legal_moves.len(), 2);
assert_eq!(legal_moves[0].to_string(), "e3");
assert_eq!(legal_moves[1].to_string(), "e4");
Source

pub fn get_castle_rook_pos(&self, side: CastleType) -> Option<Position>

Returns the position of the rook involved in a castle move

§Arguments
  • side: The side of the castle move
§Returns

An Option<Position> containing the position of the rook involved in the castle move

§Example
use chess_lab::logic::Game;
use chess_lab::core::CastleType;

let game = Game::default();
let rook_pos = game.get_castle_rook_pos(CastleType::KingSide).unwrap();
assert_eq!(rook_pos.to_string(), "h1");
Source

pub fn check(&self) -> bool

Returns whether the king is in check

§Returns

Whether the king is in check

§Example
use chess_lab::logic::Game;

let mut game = Game::default();

game.move_piece("c4").unwrap();
game.move_piece("d6").unwrap();
game.move_piece("Qa4+").unwrap();

assert!(game.check());
Source

pub fn checkmate(&self) -> bool

Returns whether the king is in checkmate

§Returns

Whether the king is in checkmate

§Example
use chess_lab::logic::Game;

let mut game = Game::default();

game.move_piece("e4").unwrap();
game.move_piece("e5").unwrap();
game.move_piece("Qh5").unwrap();
game.move_piece("Nc6").unwrap();
game.move_piece("Bc4").unwrap();
game.move_piece("Nf6").unwrap();
game.move_piece("Qxf7#").unwrap();

assert!(game.checkmate());
Source

pub fn stalemate(&self) -> bool

Returns whether the game is in stalemate

§Returns

Whether the game is in stalemate

§Example
use chess_lab::logic::Game;

let game = Game::from_fen("8/8/8/8/8/4KQ2/8/4k3 b - - 0 1").unwrap();

assert!(game.stalemate());
Source

pub fn insufficient_material(&self) -> bool

Returns whether the game is in insufficient material

§Returns

Whether the game is in insufficient material

§Example
use chess_lab::logic::Game;

let game = Game::from_fen("8/8/8/8/8/4K3/8/4kB2 w - - 0 1").unwrap();
assert!(game.insufficient_material());
Source

pub fn resign(&mut self, color: Color)

§Arguments
  • color: The color of the player that resigned
§Example
use chess_lab::logic::{Game};
use chess_lab::core::{Color, GameStatus, WinReason};

let mut game = Game::default();
game.resign(Color::White);

assert_eq!(game.status, GameStatus::BlackWins(WinReason::Resignation));
Source

pub fn lost_on_time(&mut self, color: Color)

Ends the game and sets the winner to the opposite of the color that lost on time

§Arguments
  • color: The color of the player that lost on time
§Example
use chess_lab::core::{Color, GameStatus, WinReason};
use chess_lab::logic::Game;

let mut game = Game::default();
game.lost_on_time(Color::White);

assert_eq!(game.status, GameStatus::BlackWins(WinReason::Time));
Source

pub fn draw_by_agreement(&mut self)

Ends the game by a draw due to agreement

§Example
use chess_lab::core::{GameStatus, DrawReason};
use chess_lab::logic::Game;

let mut game = Game::default();
game.draw_by_agreement();

assert_eq!(game.status, GameStatus::Draw(DrawReason::Agreement));

Trait Implementations§

Source§

impl Clone for Game

Source§

fn clone(&self) -> Game

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 Game

Source§

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

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

impl Default for Game

Source§

fn default() -> Game

Creates a new game with the default values

§Example
let game = Game::default();
assert_eq!(game.to_string(), "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1");
Source§

impl Display for Game

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Freeze for Game

§

impl !RefUnwindSafe for Game

§

impl !Send for Game

§

impl !Sync for Game

§

impl Unpin for Game

§

impl UnsafeUnpin for Game

§

impl !UnwindSafe for Game

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