| Crates.io | polyglot-book-rs |
| lib.rs | polyglot-book-rs |
| version | 0.1.0 |
| created_at | 2025-11-13 18:08:38.681914+00 |
| updated_at | 2025-11-13 18:08:38.681914+00 |
| description | A Rust library for reading and using Polyglot opening book format for chess engines |
| homepage | https://github.com/hedgeg0d/polyglot-rs |
| repository | https://github.com/hedgeg0d/polyglot-rs |
| max_upload_size | |
| id | 1931639 |
| size | 68,157 |
A Rust library for reading and using Polyglot opening book format for chess engines.
.bin format opening book filesBoardPosition traitAdd this to your Cargo.toml:
[dependencies]
polyglot-book-rs = "0.1.0"
use polyglot_book_rs::PolyglotBook;
// Load a book file
let book = PolyglotBook::load("Perfect2023.bin")?;
// Query by FEN string
let starting_position = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
if let Some(entry) = book.get_best_move_from_fen(starting_position) {
println!("Best move: {} (weight: {})", entry.move_string, entry.weight);
}
// Get all moves for a position
let all_moves = book.get_all_moves_from_fen(starting_position);
for entry in all_moves {
println!("Move: {} (weight: {})", entry.move_string, entry.weight);
}
Implement the BoardPosition trait for your board representation:
use polyglot_book_rs::{BoardPosition, Piece, PolyglotBook};
struct MyBoard {
// Your board representation
}
impl BoardPosition for MyBoard {
fn piece_at(&self, square: u8) -> Piece {
// Return the piece at the given square (0-63, a1=0, h8=63)
// Your implementation here
}
fn is_white_to_move(&self) -> bool {
// Return true if it's white's turn
// Your implementation here
}
fn castling_rights(&self) -> u8 {
// Return castling rights as bitmask:
// Bit 0: White kingside, Bit 1: White queenside
// Bit 2: Black kingside, Bit 3: Black queenside
// Your implementation here
}
fn en_passant_file(&self) -> Option<u8> {
// Return en passant file (0-7 for a-h) or None
// Your implementation here
}
}
// Now you can use your board with the book
let book = PolyglotBook::load("book.bin")?;
let my_board = MyBoard::new();
if let Some(entry) = book.get_best_move(&my_board) {
println!("Best move: {}", entry.move_string);
}
You can also compute Polyglot hashes directly:
use polyglot_book_rs::hash::polyglot_hash_from_fen;
let fen = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
let hash = polyglot_hash_from_fen(fen)?;
println!("Position hash: 0x{:016X}", hash);
// Output: Position hash: 0x463B96181691FC9C
PolyglotBookload(path: &str) - Load a book from fileget_best_move_from_fen(fen: &str) - Get best move from FENget_all_moves_from_fen(fen: &str) - Get all moves from FENget_best_move<B: BoardPosition>(board: &B) - Get best move from custom boardget_all_moves<B: BoardPosition>(board: &B) - Get all moves from custom boardhas_position_fen(fen: &str) - Check if position exists in bookentry_count() - Get number of entries in bookPolyglotEntrychess_move: PolyglotMove - The chess movemove_string: String - Move in algebraic notation (e.g., "e2e4")weight: u16 - Move weight/frequencyposition_hash: u64 - Polyglot hash of the positionPolyglotMovefrom: u8 - Source square (0-63)to: u8 - Target square (0-63)promotion: Option<Piece> - Promotion piece if anyto_algebraic() - Convert to algebraic notationThis library implements the official Polyglot opening book format:
The starting position hash is verified to be 0x463B96181691FC9C.
See the examples/ directory for more usage examples:
basic_usage.rs - Simple book queryingcustom_board.rs - Integration with custom board representationhash_computation.rs - Direct hash computationRun the test suite:
cargo test
The tests include verification of:
Licensed under either of
at your option.
Contributions are welcome! Please feel free to submit a Pull Request.