simple_chess

Struct ChessGame

Source
pub struct ChessGame { /* private fields */ }

Implementations§

Source§

impl ChessGame

Source

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();
Source

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

Source

pub fn get_board(&self) -> &Board<ChessPiece>

Get board

§Returns

Board<ChessPiece>: The board in its current state

Source

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 moves
Source

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::White or Color::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);
Source

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.

Source

pub fn get_turn_number(&self) -> usize

Returns the current turn number.

§Returns

usize: The current turn number in the game. This method returns the total number of turns that have been taken in the game so far.

§Examples
use simple_chess::ChessGame;
let chess_game = ChessGame::new();
assert_eq!(chess_game.get_turn_number(), 1);
Source

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());
Source

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

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

pub fn make_move(&mut self, chess_move: ChessMoveType) -> GameState

Executes a given move on the simple_chess board.

§Arguments
  • chess_move - An instance of ChessMoveType representing 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.
Source

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.

Source

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.

Trait Implementations§

Source§

impl Default for ChessGame

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

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