atomic_movegen/lib.rs
1//! `atomic-movegen` — atomic chess move generation in Rust.
2//!
3//! This crate implements legal move generation, FEN parsing, and perft for
4//! [atomic chess](https://en.wikipedia.org/wiki/Atomic_chess).
5//!
6//! # Atomic chess rules implemented
7//!
8//! - **Blast on capture:** capturing (or en passant) destroys all non-pawn
9//! pieces in a 3×3 king-move blast zone centered on the capture square,
10//! including the capturer itself if it is not a pawn.
11//! - **Pawns are blast-immune:** pawns are never removed by a blast.
12//! - **COMMONER replaces KING:** pieces move like kings but are pseudo-royal.
13//! Losing all COMMONERs means loss. Adjacent COMMONERs (even own) are illegal
14//! (extinction pseudo-royal rule).
15//! - **No check/mate in the usual sense:** the game ends when a side has no
16//! COMMONERs left.
17//!
18//! # Example
19//!
20//! ```rust
21//! use atomic_movegen::board::Board;
22//! use atomic_movegen::perft;
23//!
24//! let mut board = Board::new();
25//! let nodes = perft(&mut board, 3);
26//! assert_eq!(nodes, 8902);
27//! ```
28
29pub mod attacks;
30pub mod bitboard;
31pub mod board;
32pub mod magic;
33pub mod movegen;
34pub mod pext;
35pub mod types;
36
37use crate::types::MoveList;
38
39/// Count the number of legal moves at each node to the given depth (perft).
40///
41/// This is the standard perft (performance test) function used to verify
42/// move-generator correctness against a reference engine.
43#[must_use]
44pub fn perft(board: &mut board::Board, depth: u32) -> u64 {
45 static INIT: std::sync::Once = std::sync::Once::new();
46 INIT.call_once(attacks::init);
47 if depth == 0 {
48 return 1;
49 }
50
51 let mut moves = MoveList::new();
52 movegen::generate_legal(board, &mut moves);
53
54 if depth == 1 {
55 return moves.len() as u64;
56 }
57
58 let mut total = 0u64;
59 let mut state = board::StateInfo::new();
60 for &m in moves.as_slice() {
61 board.do_move(m, &mut state);
62 total += perft(board, depth - 1);
63 board.undo_move(m, &state);
64 }
65 total
66}