giga-chess 0.5.0

A rust chess library built for performance, handling game logic and legal/best move generation.
Documentation
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
use crate::engine::attack_table::AttackTable;
use crate::game::bit_board::BitBoard;
use crate::game::chess_board::ChessBoard;
use crate::game::chess_move::{ChessMove, ChessMoveType};
use crate::game::color::Color;
use crate::game::piece::Piece;
use crate::game::state::GameState;
use crate::game::status::GameStatus;
use std::sync::Arc;

pub mod attack_table;
pub mod magic_numbers;

pub struct Engine {
    pub attack_table: AttackTable,
}

// ToDo: Check for insufficient material, 3-fold repetition
impl Engine {
    pub fn initialize() -> Arc<Self> {
        Arc::new(Self {
            attack_table: AttackTable::build(),
        })
    }

    pub fn generate_moves(&self, game_state: &GameState) -> (Vec<ChessMove>, GameStatus) {
        if game_state.half_moves >= 50 {
            return (vec![], GameStatus::DrawFiftyMove);
        }

        let player_mask = game_state.board.get_color_bb(game_state.side_to_move);
        let opponent_mask = game_state
            .board
            .get_color_bb(game_state.side_to_move.opposite());
        let occupied_mask = player_mask | opponent_mask;

        let pseudo_legal =
            self.generate_pseudo_legal_moves(game_state, player_mask, opponent_mask, occupied_mask);

        let legal = pseudo_legal
            .into_iter()
            .filter(|&mv| self.is_legal_move(game_state, mv))
            .collect::<Vec<_>>();
        if legal.is_empty() {
            return if self.is_in_check(game_state.board, game_state.side_to_move) {
                (legal, GameStatus::Checkmate)
            } else {
                (legal, GameStatus::Stalemate)
            };
        }

        (legal, GameStatus::Running)
    }

    pub fn is_legal_move(&self, game_state: &GameState, chess_move: ChessMove) -> bool {
        let future_board = game_state.board.play_move(
            chess_move,
            game_state.side_to_move,
            game_state.en_passant_square,
        );
        if !self.is_in_check(future_board, game_state.side_to_move) {
            true
        } else {
            false
        }
    }

    pub fn is_in_check(&self, board: ChessBoard, color: Color) -> bool {
        let king_bb = board.get_piece_bb(Piece::King, color);
        let Some(king_square) = king_bb.get_lowest_set_bit() else {
            return false;
        };

        let opponent_color = color.opposite();
        self.is_square_attacked(board, king_square, opponent_color)
    }

    pub fn is_square_attacked(&self, board: ChessBoard, square: u8, opponent_color: Color) -> bool {
        let occupied = board.get_occupied_bb();

        let pawn_attacks = self
            .attack_table
            .get_pawn_king_attack(square, opponent_color);
        if !(pawn_attacks & board.get_piece_bb(Piece::Pawn, opponent_color)).is_empty() {
            return true;
        }

        let knight_attacks = self.attack_table.get_knight_attacks(square);
        if !(knight_attacks & board.get_piece_bb(Piece::Knight, opponent_color)).is_empty() {
            return true;
        }

        let bishop_attacks = self.attack_table.get_bishop_attacks(square, occupied);
        if !(bishop_attacks
            & (board.get_piece_bb(Piece::Bishop, opponent_color)
                | board.get_piece_bb(Piece::Queen, opponent_color)))
        .is_empty()
        {
            return true;
        }

        let rook_attacks = self.attack_table.get_rook_attacks(square, occupied);
        if !(rook_attacks
            & (board.get_piece_bb(Piece::Rook, opponent_color)
                | board.get_piece_bb(Piece::Queen, opponent_color)))
        .is_empty()
        {
            return true;
        }

        let king_attacks = self.attack_table.get_king_attacks(square);
        if !(king_attacks & board.get_piece_bb(Piece::King, opponent_color)).is_empty() {
            return true;
        }

        false
    }

    pub fn is_promotion(&self, game_state: &GameState, move_to: u8) -> bool {
        game_state.side_to_move == Color::White && move_to > 55
            || game_state.side_to_move == Color::Black && move_to < 8
    }

    pub fn get_pawn_double_push_target(
        &self,
        game_state: &GameState,
        from: u8,
        occupied_mask: BitBoard,
    ) -> Option<u8> {
        if game_state.side_to_move == Color::White && from > 7 && from < 16
            || game_state.side_to_move == Color::Black && from > 47 && from < 56
        {
            let target_square = match game_state.side_to_move {
                Color::White => from + 16,
                Color::Black => from - 16,
            };
            if !occupied_mask.get_bit(target_square) {
                Some(target_square)
            } else {
                None
            }
        } else {
            None
        }
    }

    fn generate_pseudo_legal_moves(
        &self,
        game_state: &GameState,
        player_mask: BitBoard,
        opponent_mask: BitBoard,
        occupied_mask: BitBoard,
    ) -> Vec<ChessMove> {
        let mut moves = Vec::new();
        self.generate_pawn_moves(&mut moves, game_state, opponent_mask, occupied_mask);
        self.generate_knight_moves(&mut moves, game_state, player_mask, opponent_mask);
        self.generate_bishop_moves(
            &mut moves,
            game_state,
            player_mask,
            opponent_mask,
            occupied_mask,
        );
        self.generate_rook_moves(
            &mut moves,
            game_state,
            player_mask,
            opponent_mask,
            occupied_mask,
        );
        self.generate_king_moves(&mut moves, game_state, player_mask, opponent_mask);
        self.generate_queen_moves(
            &mut moves,
            game_state,
            player_mask,
            opponent_mask,
            occupied_mask,
        );
        self.generate_possible_castling_moves(&mut moves, game_state, occupied_mask);
        moves
    }

    fn generate_pawn_moves(
        &self,
        moves: &mut Vec<ChessMove>,
        game_state: &GameState,
        opponent_mask: BitBoard,
        occupied_mask: BitBoard,
    ) {
        let pawn_bb = game_state
            .board
            .get_piece_bb(Piece::Pawn, game_state.side_to_move);

        for from in pawn_bb.iter_set_bits() {
            let mask = self
                .attack_table
                .get_pawn_mask(from, game_state.side_to_move)
                & !occupied_mask;
            let attack = self
                .attack_table
                .get_pawn_attacks(from, game_state.side_to_move);

            if let Some(move_to) = mask.get_lowest_set_bit() {
                if !self.is_promotion(game_state, move_to) {
                    moves.push(ChessMove::new(from, move_to, ChessMoveType::Quiet));

                    if let Some(double_push_target) =
                        self.get_pawn_double_push_target(game_state, from, occupied_mask)
                    {
                        moves.push(ChessMove::new(
                            from,
                            double_push_target,
                            ChessMoveType::DoublePawnPush,
                        ));
                    }
                } else {
                    moves.extend(ChessMove::all_promotions(from, move_to))
                }
            }

            for move_to in attack.iter_set_bits() {
                if Some(move_to) == game_state.en_passant_square {
                    moves.push(ChessMove::new(from, move_to, ChessMoveType::EnPassant));
                } else if opponent_mask.get_bit(move_to) {
                    if !self.is_promotion(game_state, move_to) {
                        moves.push(ChessMove::new(from, move_to, ChessMoveType::Capture));
                    } else {
                        moves.extend(ChessMove::all_promotions_capture(from, move_to))
                    }
                }
            }
        }
    }

    fn generate_knight_moves(
        &self,
        moves: &mut Vec<ChessMove>,
        game_state: &GameState,
        player_mask: BitBoard,
        opponent_mask: BitBoard,
    ) {
        let knight_bb = game_state
            .board
            .get_piece_bb(Piece::Knight, game_state.side_to_move);
        for from in knight_bb.iter_set_bits() {
            let attack = self.attack_table.get_knight_attacks(from) & !player_mask;
            self.push_quiet_or_capture_moves(moves, from, attack, opponent_mask);
        }
    }

    fn generate_bishop_moves(
        &self,
        moves: &mut Vec<ChessMove>,
        game_state: &GameState,
        player_mask: BitBoard,
        opponent_mask: BitBoard,
        occupied_mask: BitBoard,
    ) {
        let bishop_bb = game_state
            .board
            .get_piece_bb(Piece::Bishop, game_state.side_to_move);
        for from in bishop_bb.iter_set_bits() {
            let attack = self.attack_table.get_bishop_attacks(from, occupied_mask) & !player_mask;
            self.push_quiet_or_capture_moves(moves, from, attack, opponent_mask);
        }
    }

    fn generate_rook_moves(
        &self,
        moves: &mut Vec<ChessMove>,
        game_state: &GameState,
        player_mask: BitBoard,
        opponent_mask: BitBoard,
        occupied_mask: BitBoard,
    ) {
        let rook_bb = game_state
            .board
            .get_piece_bb(Piece::Rook, game_state.side_to_move);
        for from in rook_bb.iter_set_bits() {
            let attack = self.attack_table.get_rook_attacks(from, occupied_mask) & !player_mask;
            self.push_quiet_or_capture_moves(moves, from, attack, opponent_mask);
        }
    }

    fn generate_king_moves(
        &self,
        moves: &mut Vec<ChessMove>,
        game_state: &GameState,
        player_mask: BitBoard,
        opponent_mask: BitBoard,
    ) {
        let king_bb = game_state
            .board
            .get_piece_bb(Piece::King, game_state.side_to_move);
        for from in king_bb.iter_set_bits() {
            let attack = self.attack_table.get_king_attacks(from) & !player_mask;
            self.push_quiet_or_capture_moves(moves, from, attack, opponent_mask);
        }
    }

    fn generate_queen_moves(
        &self,
        moves: &mut Vec<ChessMove>,
        game_state: &GameState,
        player_mask: BitBoard,
        opponent_mask: BitBoard,
        occupied_mask: BitBoard,
    ) {
        let queen_bb = game_state
            .board
            .get_piece_bb(Piece::Queen, game_state.side_to_move);
        for from in queen_bb.iter_set_bits() {
            let attack = self.attack_table.get_queen_attacks(from, occupied_mask) & !player_mask;
            self.push_quiet_or_capture_moves(moves, from, attack, opponent_mask);
        }
    }

    fn generate_possible_castling_moves(
        &self,
        moves: &mut Vec<ChessMove>,
        game_state: &GameState,
        occupied_mask: BitBoard,
    ) {
        match game_state.side_to_move {
            Color::White => {
                if game_state.castling_rights.white_king_side
                    && (occupied_mask.get_value() & 0x60) == 0
                {
                    if !self.is_square_attacked(
                        game_state.board,
                        4,
                        game_state.side_to_move.opposite(),
                    ) && !self.is_square_attacked(
                        game_state.board,
                        5,
                        game_state.side_to_move.opposite(),
                    ) && !self.is_square_attacked(
                        game_state.board,
                        6,
                        game_state.side_to_move.opposite(),
                    ) {
                        moves.push(ChessMove::new(0, 0, ChessMoveType::KingCastle));
                    }
                }
                if game_state.castling_rights.white_queen_side
                    && (occupied_mask.get_value() & 0xE) == 0
                {
                    if !self.is_square_attacked(
                        game_state.board,
                        2,
                        game_state.side_to_move.opposite(),
                    ) && !self.is_square_attacked(
                        game_state.board,
                        3,
                        game_state.side_to_move.opposite(),
                    ) && !self.is_square_attacked(
                        game_state.board,
                        4,
                        game_state.side_to_move.opposite(),
                    ) {
                        moves.push(ChessMove::new(0, 0, ChessMoveType::QueenCastle))
                    };
                }
            }
            Color::Black => {
                if game_state.castling_rights.black_king_side
                    && (occupied_mask.get_value() & 0x6000000000000000) == 0
                {
                    if !self.is_square_attacked(
                        game_state.board,
                        60,
                        game_state.side_to_move.opposite(),
                    ) && !self.is_square_attacked(
                        game_state.board,
                        61,
                        game_state.side_to_move.opposite(),
                    ) && !self.is_square_attacked(
                        game_state.board,
                        62,
                        game_state.side_to_move.opposite(),
                    ) {
                        moves.push(ChessMove::new(0, 0, ChessMoveType::KingCastle));
                    }
                }
                if game_state.castling_rights.black_queen_side
                    && (occupied_mask.get_value() & 0xE00000000000000) == 0
                {
                    if !self.is_square_attacked(
                        game_state.board,
                        58,
                        game_state.side_to_move.opposite(),
                    ) && !self.is_square_attacked(
                        game_state.board,
                        59,
                        game_state.side_to_move.opposite(),
                    ) && !self.is_square_attacked(
                        game_state.board,
                        60,
                        game_state.side_to_move.opposite(),
                    ) {
                        moves.push(ChessMove::new(0, 0, ChessMoveType::QueenCastle));
                    }
                }
            }
        };
    }

    fn push_quiet_or_capture_moves(
        &self,
        moves: &mut Vec<ChessMove>,
        from: u8,
        attack_mask: BitBoard,
        opponent_mask: BitBoard,
    ) {
        for move_to in attack_mask.iter_set_bits() {
            if opponent_mask.get_bit(move_to) {
                moves.push(ChessMove::new(from, move_to, ChessMoveType::Capture));
            } else {
                moves.push(ChessMove::new(from, move_to, ChessMoveType::Quiet));
            }
        }
    }
}