1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
//! # chess_move_gen
//! Provides structs and methods for generating chess moves efficiently
//!
//! Example usage:
//!
//! ```
//! use chess_move_gen::*;
//! let mut list = MoveVec::new();
//! let position = &Position::from_fen("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w QqKk - 0 1").unwrap();
//! legal_moves::<MoveVec>(position, &mut list);
//! assert_eq!(list.len(), 20);
//! ```

#![feature(test)]
#![feature(cfg_target_feature)]
#![feature(platform_intrinsics)]
#![feature(const_fn)]

pub mod bb;
mod position;
mod castle;
mod castling_rights;
mod gen;
mod integrity;
mod mv;
mod mv_list;
mod piece;
mod side;
mod square;
mod util;
mod hash;
mod board;

#[cfg(target_feature = "sse3")]
mod dbb;

extern crate rand;
#[cfg(test)]
extern crate unindent;
#[cfg(test)]
extern crate test;

pub use position::{Position, State, STARTING_POSITION_FEN};
pub use board::Board;
pub use gen::legal_moves;
pub use castle::{Castle, KING_SIDE, QUEEN_SIDE};
pub use castling_rights::{CastlingRights, BLACK_QS, BLACK_KS, WHITE_QS, WHITE_KS};
pub use mv::{Move, NULL_MOVE};
pub use mv_list::{MoveList, MoveCounter, MoveVec};
pub use side::{Side, WHITE, BLACK};
pub use piece::*;
pub use square::*;
pub use bb::BB;

#[cfg(target_feature = "sse3")]
pub use dbb::*;

#[cfg(test)]
fn perft(position: &mut Position, depth: usize) -> usize {
    if depth == 0 {
        let mut counter = MoveCounter::new();
        legal_moves(&position, &mut counter);
        return counter.moves as usize;
    }

    let mut moves = MoveVec::new();
    legal_moves(&position, &mut moves);

    let state = position.state().clone();
    let key = position.hash_key();
    let mut count = 0;
    for &mv in moves.iter() {
        let capture = position.make(mv);

        count += perft(position, depth - 1);

        position.unmake(mv, capture, &state, key);
    }

    count
}

#[test]
fn perft_test() {
    let mut position = Position::from_fen(STARTING_POSITION_FEN).unwrap();

    assert_eq!(perft(&mut position, 3), 197281);
}

#[bench]
fn perft_bench_starting_position(b: &mut test::Bencher) {
    let mut position = Position::from_fen(STARTING_POSITION_FEN).unwrap();

    b.iter(|| -> usize { perft(&mut position, 2) });
}