aboutsummaryrefslogtreecommitdiff
path: root/src/board/move_generation.rs
blob: 5a3650a5cb992d2a6f7474a398e4084c4add205e (plain)
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
use smallvec::SmallVec;
use crate::{moves::{Move, MoveKind}, board::{color::Color, piece::Piece, CastlingSide}, bitboard::BitboardFns, square::Square};

use super::Board;

pub type MoveList = SmallVec<[Move; 128]>;

impl Board {
    pub fn generate_pseudolegal_moves(&self) -> MoveList {
        let mut result = MoveList::new();
        result.append(&mut self.generate_moves_core(true));
        result.append(&mut self.generate_moves_core(false));
        result
    }

    pub fn generate_moves_core(&self, tactical_only: bool) -> MoveList {
        let color = self.color();
        let player_pieces = self.pieces_by_color(color);
        let mut moves = MoveList::new();
        let empty = self.empty();

        let targets = if tactical_only {
            self.color_occupancy(color.flip()) ^ match color {
                // Exclude opponent king because we can't capture it
                Color::White => self.piece_sets[Piece::KingBlack as usize],
                Color::Black => self.piece_sets[Piece::King as usize],
            }
        } else {
            empty
        };

        let kind = if tactical_only {
            MoveKind::Capture
        } else {
            MoveKind::Quiet
        };

        for (piece_id, piece) in player_pieces.iter().enumerate() {
            for source in piece.serialize() {
                let piece_type = Piece::from(piece_id);
                let move_targets = match piece_type {
                    Piece::Bishop => self.attacks.bishop(self.occupancy, source),
                    Piece::Rook => self.attacks.rook(self.occupancy, source),
                    Piece::Queen => self.attacks.queen(self.occupancy, source),
                    Piece::Knight => self.attacks.knight[source as usize],
                    Piece::King => self.attacks.king[source as usize],
                    Piece::Pawn => if tactical_only {
                        self.attacks.pawn[color as usize][source as usize]
                    } else {
                        self.attacks.pawn_pushes[color as usize][source as usize]
                    },
                    _ => panic!("Incorrect piece type"),
                };

                // Generalized attack/move pattern for all pieces
                for target in (move_targets & targets).serialize() {
                    moves.push(Move { source, target, kind });

                    // Promotions
                    if piece_type == Piece::Pawn {
                        if target.rank() == 7 {
                            for promo_type in [Piece::Bishop, Piece::Knight, Piece::Rook, Piece::Queen] {
                                moves.push(Move { source, target, kind: MoveKind::Promotion(promo_type)})
                            }
                        } else if target.rank() == 0 {
                            for promo_type in [Piece::BishopBlack, Piece::KnightBlack, Piece::RookBlack, Piece::QueenBlack] {
                                moves.push(Move { source, target, kind: MoveKind::Promotion(promo_type)})
                            }
                        }
                    }
                }

                // En Passant
                if piece_type == Piece::Pawn && tactical_only {
                    for target in (move_targets & self.ep_bitboard()).serialize() {
                        moves.push(Move { source, target, kind: MoveKind::EnPassant });
                    }
                }
            }
        }

        if !tactical_only {
            // Double Pushes
            // Make sure no blocking piece is standing in front of the pawns
            // that are potential double-push sources
            let able_to_double_push_mask = match color {
                Color::White => empty >> 8,
                Color::Black => empty << 8,
            };
            for source in (player_pieces[Piece::Pawn as usize] & able_to_double_push_mask).serialize() {
                for target in (self.attacks.pawn_double_pushes[color as usize][source as usize] & empty).serialize() {
                    moves.push(Move { source, target, kind: MoveKind::DoublePush });
                };
            }

            // Castling
            let king_home_position = match color {
                Color::White => Square::E1,
                Color::Black => Square::E8,
            };
            if player_pieces[Piece::King as usize].bitscan() == king_home_position {
                for rook_square in player_pieces[Piece::Rook as usize]
                    .serialize()
                    .filter(|rook_square| rook_square.rank() == king_home_position.rank())
                {
                    match rook_square.file() {
                        0 => {
                            let all_empty = [
                                king_home_position.west_one(),
                                king_home_position.west_one().west_one(),
                                king_home_position.west_one().west_one().west_one(),

                            ].iter().all(|square| empty & square.to_bitboard() > 0);

                            let any_checks = [
                                king_home_position,
                                king_home_position.west_one(),
                                king_home_position.west_one().west_one(),
                            ].iter().any(|square| self.is_square_attacked(*square, color.flip()));

                            if all_empty && !any_checks && self.castling_rights[color as usize][CastlingSide::Queen as usize] {
                                moves.push(Move {
                                    source: king_home_position,
                                    target: king_home_position.west_one().west_one(),
                                    kind: MoveKind::Castle,
                                })
                            }
                        },
                        7 => {
                            let all_empty = [
                                king_home_position.east_one(),
                                king_home_position.east_one().east_one(),

                            ].iter().all(|square| empty & square.to_bitboard() > 0);

                            let any_checks = [
                                king_home_position,
                                king_home_position.east_one(),
                                king_home_position.east_one().east_one(),
                            ].iter().any(|square| self.is_square_attacked(*square, color.flip()));

                            if all_empty && !any_checks && self.castling_rights[color as usize][CastlingSide::King as usize] {
                                moves.push(Move {
                                    source: king_home_position,
                                    target: king_home_position.east_one().east_one(),
                                    kind: MoveKind::Castle,
                                })
                            }
                        },
                        _ => {},
                    }
                }
            }
        }
        moves
    }
}

#[cfg(test)]
mod tests {
    use crate::board::{Board, io::IO};

    use super::*;

    #[test]
    fn generate_pseudolegal_moves_starting_position() {
        let mut board = Board::new();
        let moves = board.generate_pseudolegal_moves();
        let black_moves = board.generate_pseudolegal_moves();

        assert_eq!(moves.len(), 20);
        assert_eq!(black_moves.len(), 20);

        for mov in moves {
            mov.print();
        }
    }


    #[test]
    fn moved_king_castle() {
        let fen = String::from("4k2r/ppp1n3/8/4R1Pp/5P2/q1P5/P1P1BP2/1K1R4 b - - 2 22");
        let mut board = Board::from_FEN(fen);

        // Shuffle kings around, returning to the same position
        board.make_move(Move { source: Square::E8, target: Square::F8, kind: MoveKind::Quiet });
        board.make_move(Move { source: Square::B1, target: Square::A1, kind: MoveKind::Quiet });
        board.make_move(Move { source: Square::F8, target: Square::E8, kind: MoveKind::Quiet });
        board.make_move(Move { source: Square::A1, target: Square::B1, kind: MoveKind::Quiet });
        board.print();

        let moves = board.generate_pseudolegal_moves();

        let castle = moves.iter().find(|m| **m == Move { source: Square::E8, target: Square::G8, kind: MoveKind::Castle });
        println!("{:?}", board.castling_rights);
        assert!(castle.is_none(), "Castle should not be allowed after king has moved");
    }
}