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: BoardBoard state for the current game.
is_white_turn: boolTrue when it’s white to move.
halfmove_clock: u32Halfmove clock for the fifty-move rule.
fullmove_number: u32Fullmove number (starts at 1).
en_passant: Option<Position>En passant target square, if any.
castling_rights: u8Castling rights bitmask (KQkq).
starting_fen: StringStarting 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: GameStatusCurrent game status.
Implementations§
Source§impl Game
impl Game
Sourcepub fn new(fen: &str, capture_king: bool) -> Result<Game, FenError>
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 gamecapture_king: A boolean that indicates if the king needs to be captured
§Returns
Ok(Game): A new gameErr(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");Sourcepub fn from_fen(fen: &str) -> Result<Game, FenError>
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 gameErr(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");Sourcepub fn get_variant(&self) -> String
pub fn get_variant(&self) -> String
Sourcepub fn move_piece(&mut self, move_str: &str) -> Result<GameStatus, MoveError>
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 moveErr(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");Sourcepub fn undo(&mut self)
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");Sourcepub fn redo(&mut self)
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");Sourcepub fn redo_nth(&mut self, n: u32)
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"
);Sourcepub fn get_last_move(&self) -> Option<Move>
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");Sourcepub fn get_piece_at(&self, pos: Position) -> Option<Piece>
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);Sourcepub fn start(&mut self)
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");Sourcepub fn end(&mut self)
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");Sourcepub fn parse_move(
&self,
move_str: &str,
) -> Result<(PieceType, (Option<u8>, Option<u8>), Position, MoveType), MoveError>
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 typeErr(MoveError): An error if the move is invalid
Sourcepub fn is_legal(
&self,
piece: &Piece,
start_pos: &Position,
end_pos: &Position,
move_type: &MoveType,
) -> bool
pub fn is_legal( &self, piece: &Piece, start_pos: &Position, end_pos: &Position, move_type: &MoveType, ) -> bool
Sourcepub fn get_legal_moves(&self, pos: Position) -> Vec<Move>
pub fn get_legal_moves(&self, pos: Position) -> Vec<Move>
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");Sourcepub fn get_castle_rook_pos(&self, side: CastleType) -> Option<Position>
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");Sourcepub fn checkmate(&self) -> bool
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());Sourcepub fn insufficient_material(&self) -> bool
pub fn insufficient_material(&self) -> bool
Sourcepub fn lost_on_time(&mut self, color: Color)
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));Sourcepub fn draw_by_agreement(&mut self)
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));