cozy-chess

Rust Chess and Chess960 move generation library

crates.io

cozy-chess is a Chess and Chess960 (Fischer Random Chess) move generation library written in Rust that aims to provide competitive move generation performance. It is largely inspired by Jordan Bray's neat chess library. cozy-chess aims to be a safer alternative to chess that maintains correctness while providing similar performance.

Cozy Glow

Overview

Crate features

A note on CPU features and performance

By default, Rust binaries target a baseline CPU to ensure maximum compatibility at the cost of performance. cozy-chess benefits significantly from features present in modern CPUs. For maximum performance, the target CPU can instead be set to native to use features supported by the machine running the build. Alternatively, the target CPU can be set to x86-64-v3, which will produce binaries that run on most modern CPUs. The target CPU may be changed by adding -C target-cpu=<CPU> to RUSTFLAGS.

PEXT bitboards are a faster variant of the magic bitboard algorithm used by cozy-chess. PEXT bitboards rely on an intrinsic introduced in the BMI2 CPU extension. However, it is not enabled by default, as PEXT bitboards are slower on AMD CPUs prior to Zen 3, which implement PEXT with microcode. PEXT bitboards can be enabled through the pext feature.

Examples

Basic example

```rust

use cozy_chess::*;

// Start position let board = Board::default(); let mut movelist = Vec::new(); board.generatemoves(|moves| { // Unpack dense move set into move list movelist.extend(moves); false }); asserteq!(move_list.len(), 20); ```

Get capture moves in bulk

```rust

use cozy_chess::*;

// Parse position from FEN let board = "r3k2r/p1ppqpb1/bn2pnp1/3PN3/1p2P3/2N2Q1p/PPPBBPPP/R3K2R w KQkq - 0 1" .parse::() .unwrap();

let mut totalmoves = 0; let mut totalcaptures = 0;

let enemypieces = board.colors(!board.sidetomove()); board.generatemoves(|moves| { let mut captures = moves.clone(); // Bitmask to efficiently get all captures set-wise. // Excluding en passant square for convenience. captures.to &= enemy_pieces;

total_moves += moves.len();
total_captures += captures.len();
false

});

asserteq!(totalmoves, 48); asserteq!(totalcaptures, 8); ```

Perft example

A perft implementation exists in examples/perft.rs: text $ cargo run --release --example perft -- 7 Compiling cozy-chess v0.3.0 Finished release [optimized] target(s) in 6.37s Running `target\release\examples\perft.exe 7` 3195901860 nodes in 10.05s (318045465 nps)

Changelog

v0.3

Added

Changed (breaking)

Removed (breaking)

Fixed