chess_move_gen/
lib.rs

1//! # chess_move_gen
2//! Provides structs and methods for generating chess moves efficiently
3//!
4//! Example usage:
5//!
6//! ```
7//! use chess_move_gen::*;
8//! let mut list = MoveVec::new();
9//! let position = &Position::from_fen("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w QqKk - 0 1").unwrap();
10//! legal_moves::<MoveVec>(position, &mut list);
11//! assert_eq!(list.len(), 20);
12//! ```
13//!
14//! Ways to store moves:
15//!
16//! MoveVec: Wraps a vector of generated moves, useful if you need to access the actual moves being generated
17//! MoveCounter: Counts moves of each kind (captures, castles, promotions etc). Useful if you are making a perft function or need statistics about moves for a position, but don't care about the actual moves
18//! SortedMoveAdder + SortedMoveHeap: Stores genarated moves in a sorted binary heap, which are efficiently ordered as they are inserted based on a heuristic scoring and piece-square table that you provide. Use this if you want the moves to have a reasonably good initial ordering so moves that are checked first are more likely to lead to eg alpha-beta cutoffs and reduce the search tree size.
19
20#![feature(test)]
21#![feature(portable_simd)]
22#![feature(binary_heap_into_iter_sorted)]
23
24pub mod bb;
25mod board;
26mod cache;
27mod castle;
28mod castling_rights;
29mod generation;
30mod hash;
31mod integrity;
32mod mv;
33mod mv_list;
34mod perft;
35mod piece;
36mod position;
37mod side;
38mod square;
39mod util;
40
41#[cfg(target_feature = "sse3")]
42mod dbb;
43
44extern crate num_cpus;
45extern crate rand;
46extern crate threadpool;
47
48#[cfg(test)]
49extern crate test;
50#[cfg(test)]
51extern crate unindent;
52
53pub use crate::bb::BB;
54pub use crate::castle::{Castle, KING_SIDE, QUEEN_SIDE};
55pub use crate::castling_rights::{BLACK_KS, BLACK_QS, CastlingRights, WHITE_KS, WHITE_QS};
56pub use crate::generation::{
57    MoveGenPreprocessing, legal_moves, legal_moves_with_preprocessing, loud_legal_moves,
58    loud_legal_moves_with_preprocessing, movegen_preprocessing,
59};
60pub use crate::mv::{KING_SIDE_CASTLE, Move, MoveScore, NULL_MOVE, QUEEN_SIDE_CASTLE};
61pub use crate::mv_list::{
62    MoveAdder, MoveCounter, MoveVec, PieceSquareTable, SortedMoveAdder, SortedMoveHeap,
63    SortedMoveHeapItem,
64};
65pub use crate::piece::*;
66pub use crate::position::{Position, STARTING_POSITION_FEN, State};
67pub use crate::side::{BLACK, Side, WHITE};
68pub use crate::square::*;
69pub use board::Board;
70pub use perft::perft;
71pub use perft::perft_detailed;
72
73#[cfg(target_feature = "sse3")]
74pub use crate::dbb::*;
75
76#[test]
77fn basic_functionality() {
78    let mut counter = MoveCounter::new();
79    let position = Position::from_fen(STARTING_POSITION_FEN).unwrap();
80
81    let in_check = legal_moves(&position, &mut counter);
82    assert!(!in_check);
83
84    assert_eq!(counter.moves, 20);
85    assert_eq!(counter.captures, 0);
86    assert_eq!(counter.castles, 0);
87    assert_eq!(counter.ep_captures, 0);
88    assert_eq!(counter.promotions, 0);
89}