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
impl Game
Sourcepub fn new(fen: &str, capture_king: bool) -> Game
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 gamecapture_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");
Sourcepub fn from_fen(fen: &str) -> Game
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");
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
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");
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 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>
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 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 resign(&mut self, color: Color)
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));
Sourcepub fn set_lost_in_time(&mut self, color: Color)
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));
Sourcepub fn set_draw_by_agreement(&mut self)
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));