arday11ChessLibrary

Crates.ioarday11ChessLibrary
lib.rsarday11ChessLibrary
version0.5.0
sourcesrc
created_at2024-09-19 18:50:22.74649
updated_at2024-09-24 06:56:21.850582
descriptiontest
homepage
repository
max_upload_size
id1380595
size37,343
Arda (ArdaYil)

documentation

README

Chess Library

This is a chess library that does the management of chess games. It calculates all legal moves for any given position, and handles all events that might happen during a chess game

Start a game

This starts a new game session and returns a game struct

const game = ChessLibrary::Start();

Game struct

This struct holds all important information about the current game

struct Game {
    board: Board,
    status: Status,
    current_move: Move,
    white_castle_short: bool,
    white_castle_long: bool,
    black_castle_short: bool,
    black_castle_long: bool,
}

Status

This struct contain all important information about the current game status

pub enum Status {
    WHITE_TO_MOVE,
    BLACK_TO_MOVE,
    DRAW,
    WHITE_HAS_CHECKMATE,
    BLACK_HAS_CHECKMATE,
}

Get Moves

The first function gets all the legal moves for all pieces and maps all legal moves for each piece to its corresponding position on the board

The second function gets all legal moves for any given position (rank, file)

use std::collections::HashMap;

const all_legal_moves: HashMap<Position, Vec<Position>> = ChessLibrary::get_all_legal_moves(game);

const legal_moves = Vec<Position> = ChessLibrary::get_legal_moves_for_piece(position);

Make move

This function makes a move and modifies the game struct and returns a boolean on whether the operation was successful or not

let mut game = Game::new();

match make_move(&mut game, &Position::create(startRow, startFile), &Position::create(endRow, endFile)) {
    Ok(v) => println!("Success"),
    Err(e) => println!("{}", e)
    }
}
Commit count: 0

cargo fmt