Crate schachmatt

Source
Expand description

§Schachmatt

Crates.io Crates.io License

Schachmatt is a chess library written in rust. This library can be used to run chess games, generate legal moves or create and work with standardized chess data formats.

This crate is not written with speed in mind. The goal is not to provide the fastes chess library in rust. Therefore I would strongly advise to not write a chess engine using this library.

§Examples

The following example starts a game of chess and plays random moves until the game is over

use schachmatt::{Game, GameResult, PlayerColor};
use rand::seq::IndexedRandom;
use rand::rng;

/// Starts a game of chess and plays random moves until the game is over.
/// Afterwards the game result is printed.
fn main() {

    let mut game = Game::default();
    let mut rng = rng();

    while game.get_game_result().is_none() {
        let possible_moves = game.get_possible_turns();
        let turn_to_play = possible_moves.choose(&mut rng).unwrap();

        game.execute_turn(*turn_to_play);
    }

    let game_result = match game.get_game_result().unwrap() {
        GameResult::Draw => "Draw",
        GameResult::Decisive(player_color) => match player_color {
            PlayerColor::Black => "Black won",
            PlayerColor::White => "White won",
        }
    };

    println!("{}", game_result);
}

See more examples:

§Rulesets

Rulesets can be used to change behaviour of a game of chess. Currently, only the classical chess ruleset is implemented.

User can implement their own rulesets by using the Ruleset-module.

§Data format support

Schachmatt support multiple data interchange formats standardized for usage in chess. Below you find the currently supported format and if you follow the links and explanaition of the different formats. Schachmatt can import and export data in the following data formats:

  • Forsyth-Edwards Notation (FEN): Import and export single positions
    • https://www.chessprogramming.org/Forsyth-Edwards_Notation
  • Long algebraic notation (LAN): Import and export single moves
    • https://www.chessprogramming.org/Algebraic_Chess_Notation#Long_Algebraic_Notation_.28LAN.29
  • Standard algebraic notation (SAN): Import and export single moves
    • https://www.chessprogramming.org/Algebraic_Chess_Notation#Standard_Algebraic_Notation_.28SAN.29
  • Portable game notation (PGN): Import and export whole games
    • https://www.chessprogramming.org/Portable_Game_Notation

Modules§

Columns
Fields
Metadata
Rows

Structs§

CastlingRights
Stores information on whether a player is allowed to castle kingside or queenside.
Fen
Forsyth-Edwards-Notation Standardized notation to represent chess positions.
Field
Represents a board location in a chess game.
Game
Represents a game of chess. Manages the game state and history. The games behaviour is altered by the ruleset. Additionally, contains some amount of metadata.
Lan
Long Algebraic Notation. Standardized notation to represent chess moves.
Pgn
Portable Game Notation. Standardized notation for chess games
Piece
Position
A Position is defined as a state in a chess game and contains all information to unambiguously identify a position.
Ruleset
Contains functions which describe the rules of a chess game Can be used to alter the rules of the game (e.g. for variants like Chess960) Each Ruleset has to implement specific functions.
San
Standard algebraic notation Standardized notation for chess moves.
Turn
A Turn is defined as a combination of two fields and an optional promotion piece.

Enums§

FenParserError
GameResult
Represents the result of a chess game. An ongoing game is not considered a result. The game can end in a draw or with one of the players winning.
PgnParserError
PieceType
Different types of pieces in chess.
PlayerColor
Defines the two player colors of a chess game Black and White.
PositionError
SanParserError

Constants§

CLASSIC_RULESET
The classic ruleset for chess This ruleset is used by default and implements the standard rules of chess.
DEFAULT_BOARD_SETUP
The default board setup in Forsyth-Edwards-Notation.