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
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
//! Statically initialized lookup tables.
//!
//! Whenever a `Board` is created, these are also created as well. Calling `Hepler::new()` will
//! initialize the tables the first time it's called, and successive calls won't waste time
//! initializing the table.
//!
//! It is highly recommended to go through a `Helper` to access these tables, as the access will
//! guarantee that the tables are initialized in the first place.
//!
//! If you want the same functions, but can ensure the Tables are initalized, see [`helper::prelude`]
//! for those raw functions.
//!
//! [`helper::prelude`]: prelude/index.html

mod magic;
mod boards;
mod zobrist;
mod psqt;
pub mod prelude;


use {SQ,BitBoard,Player,File,Rank,Piece};
use core::score::{Score,Value};

/// Helper structure for accessing statically-initialized tables and other constants.
///
/// Guarantees that the tables will be initialized upon access through a `Helper`.
#[derive(Copy, Clone)]
pub struct Helper {}

unsafe impl Send for Helper {}

unsafe impl Sync for Helper {}

impl Default for Helper {
    fn default() -> Self {
        Helper::new()
    }
}

impl Helper {
    /// Creates a new `Helper` Object, automatically initializing all the needed tables.
    ///
    /// Calling this method multiple times does not waste time computing the static variables if
    /// already initialized. [`init_statics`] also does the same thing as well.
    ///
    /// [`init_statics`]: prelude/fn.init_statics.html
    pub fn new() -> Self {
        prelude::init_statics();
        Helper {}
    }

    /// Generate Bishop Moves `BitBoard` from a bishop square and all occupied squares on the board.
    /// This function will return captures to pieces on both sides. The resulting `BitBoard` must be
    /// AND'd with the inverse of the intending moving player's pieces.
    #[inline(always)]
    pub fn bishop_moves(&self, occupied: BitBoard, sq: SQ) -> BitBoard {
        prelude::bishop_moves(occupied,sq)
    }

    /// Generate Rook Moves `BitBoard` from a bishop square and all occupied squares on the board.
    /// This function will return captures to pieces on both sides. The resulting `BitBoard` must be
    /// AND'd with the inverse of the intending moving player's pieces.
    #[inline(always)]
    pub fn rook_moves(&self, occupied: BitBoard, sq: SQ) -> BitBoard {
        prelude::rook_moves(occupied,sq)
    }

    /// Generate Queen Moves `BitBoard` from a bishop square and all occupied squares on the board.
    /// This function will return captures to pieces on both sides. The resulting `BitBoard` must be
    /// AND'd with the inverse of the intending moving player's pieces.
    #[inline(always)]
    pub fn queen_moves(&self, occupied: BitBoard, sq: SQ) -> BitBoard {
        prelude::queen_moves(occupied,sq)
    }

    /// Generate Knight Moves `BitBoard` from a source square.
    #[inline(always)]
    pub fn knight_moves(&self, sq: SQ) -> BitBoard {
        prelude::knight_moves(sq)
    }

    /// Generate King moves `BitBoard` from a source square.
    #[inline(always)]
    pub fn king_moves(&self, sq: SQ) -> BitBoard {
        prelude::king_moves(sq)
    }

    /// Get the distance of two squares.
    #[inline(always)]
    pub fn distance_of_sqs(&self, sq_one: SQ, sq_two: SQ) -> u8 {
        prelude::distance_of_sqs(sq_one, sq_two)
    }

    /// Get the line (diagonal / file / rank) `BitBoard` that two squares both exist on, if it exists.
    #[inline(always)]
    pub fn line_bb(&self, sq_one: SQ, sq_two: SQ) -> BitBoard {
        prelude::line_bb(sq_one, sq_two)
    }

    /// Get the line (diagonal / file / rank) `BitBoard` between two squares, not including the squares, if it exists.
    #[inline(always)]
    pub fn between_bb(&self, sq_one: SQ, sq_two: SQ) -> BitBoard {
        prelude::between_bb(sq_one, sq_two)
    }

    /// Gets the adjacent files `BitBoard` of the square
    #[inline(always)]
    pub fn adjacent_sq_file(&self, sq: SQ) -> BitBoard {
        prelude::adjacent_sq_file(sq)
    }

    /// Gets the adjacent files `BitBoard` of the file
    #[inline(always)]
    pub fn adjacent_file(&self, f: File) -> BitBoard {
        prelude::adjacent_file(f)
    }

    /// Pawn attacks `BitBoard` from a given square, per player.
    /// Basically, given square x, returns the BitBoard of squares a pawn on x attacks.
    #[inline(always)]
    pub fn pawn_attacks_from(&self, sq: SQ, player: Player) -> BitBoard {
        prelude::pawn_attacks_from(sq,player)
    }

    /// Returns if three Squares are in the same diagonal, file, or rank.
    #[inline(always)]
    pub fn aligned(&self, s1: SQ, s2: SQ, s3: SQ) -> bool {
        prelude::aligned(s1,s2,s3)
    }

    /// Returns the ring of bits surrounding the square sq at a specified distance.
    ///
    /// # Safety
    ///
    /// distance must be less than 8, or else a panic will occur.
    #[inline(always)]
    pub fn ring_distance(&self, sq: SQ, distance: u8) -> BitBoard {
        prelude::ring_distance(sq,distance)
    }

    /// Returns the BitBoard of all squares in the rank in front of the given one.
    #[inline(always)]
    pub fn forward_rank_bb(&self, player: Player, rank: Rank) -> BitBoard {
        prelude::forward_rank_bb(player,rank)
    }

    /// Returns the `BitBoard` of all squares that can be attacked by a pawn
    /// of the same color when it moves along its file, starting from the
    /// given square. Basically, if the pawn progresses along the same file
    /// for the entire game, this bitboard would contain all possible forward squares
    /// it could attack
    ///
    /// # Safety
    ///
    /// The Square must be within normal bounds, or else a panic or undefined behvaior may occur.
    #[inline(always)]
    pub fn pawn_attacks_span(&self, player: Player, sq: SQ) -> BitBoard {
        prelude::pawn_attacks_span(player,sq)
    }

    /// Returns the BitBoard of all squares in the file in front of the given one.
    ///
    /// # Safety
    ///
    /// The Square must be within normal bounds, or else a panic or undefined behvaior may occur.
    #[inline(always)]
    pub fn forward_file_bb(&self, player: Player, sq: SQ) -> BitBoard {
        prelude::forward_file_bb(player,sq)
    }

    /// Returns a `BitBoard` allowing for testing of the a pawn being a
    /// "passed pawn".
    ///
    /// # Safety
    ///
    /// The Square must be within normal bounds, or else a panic or undefined behvaior may occur.
    #[inline(always)]
    pub fn passed_pawn_mask(&self, player: Player, sq: SQ) -> BitBoard {
        prelude::passed_pawn_mask(player, sq)
    }

    /// Returns the zobrist hash of a specific player's piece being at a particular square.
    #[inline(always)]
    pub fn z_square(&self, sq: SQ, piece: Piece) -> u64 {
        prelude::z_square(sq, piece)
    }

    /// Returns the zobrist hash of a given file having an en-passant square.
    #[inline(always)]
    pub fn z_ep(&self, sq: SQ) -> u64 {
        prelude::z_ep(sq)
    }

    /// Returns a zobrast hash of the castling rights, as defined by the Board.
    #[inline(always)]
    pub fn z_castle(&self, castle: u8) -> u64 {
        prelude::z_castle(castle)
    }

    /// Returns Zobrist Hash of flipping sides.
    #[inline(always)]
    pub fn z_side(&self) -> u64 {
        prelude::z_side()
    }

    /// Returns the score for a player's piece being at a particular square.
    #[inline(always)]
    pub fn psq(&self, piece: Piece,  sq: SQ) -> Score {
        prelude::psq(piece, sq)
    }

    /// Returns the value of a piece for a player. If `eg` is true, it returns the end game value. Otherwise,
    /// it'll return the midgame value.
    #[inline(always)]
    pub fn piece_value(&self, piece: Piece, eg: bool) -> Value {
        prelude::piece_value(piece, eg)
    }
}



#[cfg(test)]
mod tests {

    use super::*;

    #[test]
    fn init_helper() {
        Helper::new();
    }
}