28 releases (4 breaking)

0.5.0 Sep 24, 2024
0.4.2 Sep 23, 2024
0.3.10 Sep 23, 2024
0.2.3 Sep 20, 2024
0.1.8 Sep 20, 2024

#126 in Games

Download history 537/week @ 2024-09-14 1150/week @ 2024-09-21 110/week @ 2024-09-28 11/week @ 2024-10-05 21/week @ 2024-10-12 3/week @ 2024-10-19 142/week @ 2024-11-02

1,020 downloads per month

MIT license

35KB
699 lines

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)
    }
}

Dependencies

~0.3–1MB
~21K SLoC