Crates.io | chessframe |
lib.rs | chessframe |
version | 0.2.0 |
source | src |
created_at | 2025-02-02 13:05:43.378874 |
updated_at | 2025-02-05 17:57:18.686291 |
description | A Rust library for working with chess positions, generating psudo-legal moves, and interacting with the UCI protocol. |
homepage | |
repository | https://github.com/Zirconium419122/chessframe |
max_upload_size | |
id | 1539560 |
size | 181,646 |
chessframe is a Rust library for working with chess positions, generating psudo-legal moves, and interacting with the UCI protocol. It provides efficient tools for move generation and board manipulation.
Creating a Board
from a FEN string is easy with the Board::from_fen()
constructor. The Board::default()
method constructs the starting position using the standard starting FEN.
use chessframe::board::Board;
let fen = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
let board = Board::from_fen(fen);
This example demonstrates how to make a move on the chessboard. Since Board
does not maintain move history, you need to use board.make_move_new()
to create a new board instead of modifying the current one. However, in this example, we use board.make_move()
, which updates the board state but does not allow undoing the move.
use chessframe::{board::Board, chess_move::ChessMove, square::Square};
let mut board = Board::default();
let mv = ChessMove::new(Square::E2, Square::E4);
assert_eq!(board.make_move(&mv), Ok(()));
make_move_new
for Perft TestsThe following example implements a Perft test, which counts all possible positions after a given number of moves.
use chessframe::{board::Board, bitboard::EMPTY};
fn perft(board: &Board, depth: usize) -> usize {
let mut count = 0;
for mv in board.generate_moves_vec(!EMPTY) {
if let Ok(ref board) = board.make_move_new(&mv) {
let perft_results = if depth == 1 {
1
} else {
self.perft(board, depth - 1)
};
count += perft_results;
}
}
count
}
fn main() {
let fen = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
let board = Board::from_fen(fen);
let depth = 5;
println!("The number of nodes after {} moves is: {}", depth, perft(&board, depth));
}