Skip to main content

Crate laura_core

Crate laura_core 

Source
Expand description

§
laura_core

Build Crates.io Version License

laura_core a fast and efficient move generator for chess engines.

§Features

  • Bitboards for efficient board representation.
  • Zobrist Hashing
  • Black Magic Bitboards for rapid move generation of rooks, bishops, and queens.
  • PEXT Bitboards as an alternative for efficient sliding piece move generation.
  • Supports full legal move generation or selective move filtering (quiet or tactical moves)
  • FEN support: Initialize the board from a FEN string.
  • Move execution to update the board state dynamically.
  • Null move support for search optimizations like null move pruning.
  • UCI move execution: Apply moves directly from a UCI-compliant string.
  • Fully #![no_std] compatible

§Compilation Recommendations

If your processor supports BMI2 (e.g., Intel Haswell (2013+) or AMD Zen 3 (Nov 2020+)), it is recommended to compile with RUSTFLAGS="-C target-cpu=native" and enable the bmi2 feature for better performance.

Laura-Core provides a feature called bmi2, which enables the use of the pext instruction for more efficient bit manipulation.

For older processors without BMI2 support, only RUSTFLAGS="-C target-cpu=native" should be used, as the bmi2 feature will not work on unsupported hardware.

§Usage

§Setting up the initial board

use laura_core::Board;

let board: Board = Board::default();
assert_eq!(board.to_fen(), "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1")

§Initialize a board from a FEN string

You can create a Board from a FEN (Forsyth-Edwards Notation) string using the FromStr trait:

use std::str::FromStr;
use laura_core::Board;

let fen: &str = "r3k2r/p1ppqpb1/bn2pnp1/3PN3/1p2P3/2N2Q1p/PPPBBPPP/R3K2R w KQkq - 0 1";
let board: Board = Board::from_str(fen).unwrap();
assert_eq!(board, Board::kiwipete());

To generate moves from a given position, use the gen_moves function along with one of the filtering move types

  • AllMoves: Generates all legal moves.
  • QuietMoves: Generates only quiet moves (non-capturing moves).
  • TacticalMoves: Generates tactical moves (captures and queen promotions).

Example: Generating all legal moves

This example starts from the default position and generate all legal moves.

use laura_core::{gen_moves, Board, MoveList, AllMoves};

let board: Board = Board::default();
let moves: MoveList = gen_moves::<AllMoves>(&board);
assert_eq!(moves.len(), 20);

Example: Generating only quiet moves

use laura_core::{gen_moves, Board, MoveList, QuietMoves};

let board: Board = Board::kiwipete();
let moves: MoveList = gen_moves::<QuietMoves>(&board);
assert_eq!(moves.len(), 40);

Example: Generating only tactical moves

use laura_core::{gen_moves, Board, MoveList, TacticalMoves};

let board: Board = Board::kiwipete();
let moves: MoveList = gen_moves::<TacticalMoves>(&board);
assert_eq!(moves.len(), 8);

§Execute moves

You can apply a move to the board using UCI (Universal Chess Interface) notation:

use laura_core::Board;

let board: Board = Board::default();
let new: Board = board.make_uci_move("e2e4").unwrap();
assert_eq!(new.to_fen(), "rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq e3 0 1");

This executes the move e2e4 (pawn to e4) and asserts the updated board position.

§Benchmarks

Test system: AMD Ryzen 5 5600G (3.9 GHz), 32 GB DDR4 3200 MHz, Windows 10

Position (Depth)Black MagicsBlack Magics + Native*BMI2BMI2 + Native*
Start Position (6)410 MN/s625 MN/s434 MN/s640 MN/s
Kiwipete (5)531 MN/s840 MN/s564 MN/s910 MN/s

* Compiled with RUSTFLAGS="-C target-cpu=native" for hardware-specific optimization.

§Changelog

All notable changes are documented in the CHANGELOG.

For details on the latest updates, bug fixes, and improvements, check the full changelog.

§License

This project is licensed under GPLv3. See the LICENSE file for details.

Macros§

legal_moves
Generates all legal moves for the given board position.
quiet_moves
Generates only quiet (non-capturing) moves for the given board position.
tactical_moves
Generates only tactical moves (captures and queen promotions) for the given board position.

Structs§

AllMoves
A move filter that includes all legal moves (quiet and tactical).
BitBoard
A BitBoard represents a 64-bit chessboard where each bit corresponds to a square. It is useful for efficiently representing and manipulating chess positions.
Board
Represents a chess board, with bitboards for tracking piece positions, castling rights, en passant squares, the fifty-move rule counter, and Zobrist hashing for fast state comparison.
CastleRights
CastleRights represents the castling rights of both players (White and Black) using a bitmask stored in a u8. It tracks the availability of kingside and queenside castling rights for both sides.
Move
Represents a single chess move as a compact 16-bit unsigned integer.
MoveList
A container for storing and managing a list of Moves in a chess position.
QuietMoves
A move filter that includes only quiet moves (non-captures and non-promotions).
SanBuffered
A wrapper that holds a move and the corresponding board state for SAN rendering.
TacticalMoves
A move filter that includes only tactical moves (captures and queen promotions).
Zobrist
Zobrist is a struct that stores a Zobrist hash value, which is a 64-bit integer used to uniquely identify a specific board position in chess. Zobrist hashing is efficient for calculating incremental changes in chess board positions during moves.

Enums§

BoardParseError
Errors that can occur while parsing a FEN string into a [Board].
CastleRightsParseError
Errors that can occur while parsing castling rights from a FEN string.
Color
Enum representing the color of a piece/player in a chess game. It can either be White or Black, corresponding to the two players in a chess game.
File
Enum representing the files (columns) on a chessboard. Files are labeled from ‘A’ to ‘H’, with ‘A’ being the leftmost column and ‘H’ the rightmost. The File enum is used to identify the specific column a piece resides on during the game.
MoveType
Enum representing the different types of moves in chess, including promotions and special moves.
Piece
Enum representing all possible chess pieces, combining both color and piece type. The first six are White pieces, and the last six are Black pieces.
PieceParseError
Errors that can occur when parsing a chess piece from a character.
PieceType
Enum representing the different types of chess pieces.
Rank
Enum representing the ranks (rows) on a chessboard. Ranks are numbered from ‘One’ (1) to ‘Eight’ (8).
Square
Enum representing each square on a chessboard, from A1 to H8. The squares are ordered by Rank (rows) and File (columns), with A1 as the bottom-left and H8 as the top-right.
SquareParseError
Errors that can occur when parsing a square from algebraic notation.

Traits§

MoveFilter
A trait for filtering move types during move generation.

Functions§

enumerate_legal_moves
Enumerates all legal moves for the given board and passes them to a handler function.
gen_moves
Generates a list of legal moves for the given board based on the specified move filter.
get_between
Retrieves the BitBoard representing all the squares between the source and destination squares, based on the precomputed between table for rooks, bishops, or queens.
get_bishop_attacks
Gets the attack bitboard for a bishop from a given square, considering the positions of blockers.
get_bishop_rays
Retrieves the BitBoard representing the rays a bishop can attack from a given square.
get_king_attacks
Retrieves the precomputed attack BitBoard for a king located on a specific square.
get_knight_attacks
Retrieves the precomputed attack BitBoard for a knight located on a specific square.
get_pawn_attacks
Retrieves the attack BitBoard for a pawn of the specified color from a given square.
get_rook_attacks
Gets the attack bitboard for a rook from a given square, considering the positions of blockers.
get_rook_rays
Retrieves the BitBoard representing the rays a rook can attack from a given square.
to_san
Converts a given move to its Standard Algebraic Notation (SAN) representation.