pub struct ChessGame { /* private fields */ }Implementations§
Source§impl ChessGame
impl ChessGame
Sourcepub fn new() -> ChessGame
pub fn new() -> ChessGame
Initialize a new simple_chess game.
This function sets up a ChessGame with a starting board configuration,
sets the current player’s turn to white, initializes the turn number and
fifty-move rule counter, and indicates that both sides may castle.
§Returns
ChessGame: A new instance of the ChessGame struct.
§Examples
use simple_chess::ChessGame;
let game = ChessGame::new();pub fn build( board: Board<ChessPiece>, current_players_turn: Color, turn_number: usize, fifty_move_rule_counter: usize, can_white_castle_short: bool, can_white_castle_long: bool, can_black_castle_short: bool, can_black_castle_long: bool, moves: Vec<ChessMoveType>, ) -> ChessGame
Sourcepub fn get_board(&self) -> &Board<ChessPiece>
pub fn get_board(&self) -> &Board<ChessPiece>
Sourcepub fn get_board_mut(&mut self) -> &mut Board<ChessPiece>
pub fn get_board_mut(&mut self) -> &mut Board<ChessPiece>
Get a mutable reference to the board
This method provides mutable access to the simple_chess board, allowing for modifications to be made directly to the board’s state. This can be useful when making moves or updating the board after certain actions during the game.
§Returns
&mut Board<ChessPiece>: A mutable reference to the board in its current state
§Examples
use simple_chess::ChessGame;
let mut chess_game = ChessGame::new();
let board = chess_game.get_board_mut();
// Modify the board or make movesSourcepub fn get_current_players_turn(&self) -> Color
pub fn get_current_players_turn(&self) -> Color
Returns the color of the player whose turn it is.
§Returns
Color: An enum value representing the current player’s turn.
- This can be either
Color::WhiteorColor::Black.
§Examples
use simple_chess::{ChessGame, Color};
let chess_game = ChessGame::new();
// Assuming the game starts with White's turn
assert_eq!(chess_game.get_current_players_turn(), Color::White);Sourcepub fn get_castling_rights(&self) -> (bool, bool, bool, bool)
pub fn get_castling_rights(&self) -> (bool, bool, bool, bool)
Get castling rights
Returns a tuple containing four booleans that indicate the castling rights. The booleans represent:
- (can_white_castle_long, can_white_castle_short, can_black_castle_long, can_black_castle_short)
§Returns
(bool, bool, bool, bool): A tuple representing the castling rights for white and black players.
Sourcepub fn get_turn_number(&self) -> usize
pub fn get_turn_number(&self) -> usize
Sourcepub fn get_moves(&self) -> &Vec<ChessMoveType>
pub fn get_moves(&self) -> &Vec<ChessMoveType>
Get the list of moves made so far.
§Returns
&Vec<ChessMoveType>: A reference to the vector containing all the moves made in the game so far.
§Examples
use simple_chess::{ChessGame, ChessMoveType};
let chess_game = ChessGame::new();
// Assuming no moves have been made yet
assert!(chess_game.get_moves().is_empty());Sourcepub fn get_last_move(&self) -> Option<&ChessMoveType>
pub fn get_last_move(&self) -> Option<&ChessMoveType>
Get the last move made in the game.
§Returns
Option<&ChessMoveType>: An optional reference to the last move made.
If no moves have been made, this method returns None.
§Examples
use simple_chess::{ChessGame, ChessMoveType};
let chess_game = ChessGame::new();
// Assuming no moves have been made yet
assert_eq!(chess_game.get_last_move(), None);Sourcepub fn get_50_move_rule_counter(&self) -> usize
pub fn get_50_move_rule_counter(&self) -> usize
Get the fifty-move rule counter
§Returns
usize: The current count of half-moves since the last capture or pawn move.
This counter is used to determine if the fifty-move rule has been reached,
which allows a player to claim a draw if fifty consecutive moves have been
made without any pawn movement or piece capture.
§Examples
use simple_chess::ChessGame;
let chess_game = ChessGame::new();
assert_eq!(chess_game.get_50_move_rule_counter(), 0);Sourcepub fn make_move(&mut self, chess_move: ChessMoveType) -> GameState
pub fn make_move(&mut self, chess_move: ChessMoveType) -> GameState
Executes a given move on the simple_chess board.
§Arguments
chess_move- An instance ofChessMoveTyperepresenting the move to be made on the board.
§Effects
- The move is applied to the internal board representation.
- The turn number is incremented if it was Black’s turn.
- Updates internal state for castling rights and the fifty-move rule counter.
- Alternates the current player’s turn.
- Adds the move to the move history and updates previous board states.
§Returns
GameState- The new state of the game after the move is applied, which includes checks for checks, checkmates, and draws.
Sourcepub fn get_game_state(&mut self) -> GameState
pub fn get_game_state(&mut self) -> GameState
Get the current state of the game.
§Returns
GameState: The current state of the game, which can be calculated
based on various factors like board configuration, move history, etc.
This method internally calls a function to determine the game state and returns the result.
Sourcepub fn can_claim_draw(&self) -> Option<DrawReason>
pub fn can_claim_draw(&self) -> Option<DrawReason>
Determines if a draw can be claimed in the game based on specific rules.
§Returns
Option<DrawReason>: An optional DrawReason indicating the reason a draw can be claimed.
Returns None if a draw cannot be claimed.
A draw can be claimed based on:
- The fifty-move rule: If fifty moves have been made without a pawn move or piece capture.
- Insufficient material: If the material left on the board is not enough for a checkmate.
- Repetition: If the same board state has been repeated three times.