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 start_position: String,
    pub history: PgnTree<Move>,
    pub prev_positions: HashMap<String, u32>,
    pub game_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

use chess_lab::logic::Game;

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

Fields§

§board: Board§is_white_turn: bool§halfmove_clock: u32§fullmove_number: u32§en_passant: Option<Position>§castling_rights: u8§start_position: String§history: PgnTree<Move>§prev_positions: HashMap<String, u32>§game_status: GameStatus

Implementations§

Source§

impl Game

Source

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

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

A new game

§Panics

Panics if the FEN is invalid

§Example
use chess_lab::logic::Game;

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

pub fn from_fen(fen: &str) -> Game

Creates a new game from a FEN string

§Arguments
  • fen: A string slice that holds the FEN representation of the game
§Returns

A new game

§Panics

Panics if the FEN is invalid

§Example
use chess_lab::logic::Game;

let game = Game::from_fen("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1");
assert_eq!(game.to_string(), "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1");
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

The game status if the move was successful, otherwise an error

§Example
use chess_lab::logic::Game;

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 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();
assert_eq!(game.pgn(), "1. e4 e5");
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

A tuple containing the piece type, start position, end position and the move type If the move is invalid, a MoveError is returned

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

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

assert!(game.stalemate());
Source

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

Ends the game and sets the winner to the opposite of the color that resigned

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

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

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

pub fn set_lost_in_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::constants::{Color, GameStatus, WinReason};
use chess_lab::logic::Game;

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

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

pub fn set_draw_by_agreement(&mut self)

Ends the game by a draw due to agreement

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

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

assert_eq!(game.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
use chess_lab::logic::Game;

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

impl ToString for Game

Source§

fn to_string(&self) -> String

Convert the game to a FEN string

§Returns

The FEN string

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

Auto Trait Implementations§

§

impl Freeze for Game

§

impl !RefUnwindSafe for Game

§

impl !Send for Game

§

impl !Sync for Game

§

impl Unpin 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, 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.