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
use crate::bitboard::{BitBoard, EMPTY};
use crate::game::pieces::{Color, FILE_A, FILE_H, RANK_2, RANK_3, RANK_4, RANK_5, RANK_6, RANK_7};
use crate::game::Board;

/// Generates a bitboard with the moves that can be performed by the pawns in the given bitboard.
///
/// Note that multiple pawns can be passed in the bitboard; to get the moves for individual pawns,
/// iterate over the pawns and call this function with the singular bitboard for each pawn.
pub fn get_pawn_moves_and_attacks(board: &Board, color: Color, origin: &BitBoard) -> BitBoard {
    let mut result = EMPTY;

    let all_pieces = board.white.pieces | board.black.pieces;
    let other_side = board.get_side(color.opposite());
    let capturable_pieces = other_side.pieces | other_side.en_passant_target;

    if color.is_white() {
        // Single steps (lshift by 8 bits for: rank +1)
        result |= (origin << 8) & !all_pieces;

        // Right-side attacks (lshift by 9 bits for: rank +1, file +1)
        // 'H' file is excluded to prevent overflow
        // Also checks possibility of en-passant
        result |= (origin & !FILE_H) << 9 & capturable_pieces;

        // Left-side attacks (lshift by 7 bits for: rank +1, file -1).
        // 'A' file is excluded to prevent underflow
        // Also checks possibility of en-passant
        result |= (origin & !FILE_A) << 7 & capturable_pieces;

        // Double steps
        // 1. Only include pawns in rank 2
        // 2. Ensure there are no pieces in rank 3
        // 3. Ensure there are no pieces in rank 4
        // 4. Add 2 ranks to the original position
        result |=
            (origin & RANK_2 & !(all_pieces & RANK_3) >> 8 & !(all_pieces & RANK_4) >> 16) << 16;
    } else {
        // Single steps (rshift by 8 bits for: rank -1)
        result |= (origin >> 8) & !all_pieces;

        // Right-side attacks (rshift by 7 bits for: rank -1, file +1)
        // 'H' file is excluded to prevent overflow
        // Also checks possibility of en-passant
        result |= (origin & !FILE_H) >> 7 & capturable_pieces;

        // Left-side attacks (rshift by 9 bits for: rank -1, file -1).
        // 'A' file is excluded to prevent underflow
        // Also checks possibility of en-passant
        result |= (origin & !FILE_A) >> 9 & capturable_pieces;

        // Double steps
        // 1. Only include pawns in rank 7
        // 2. Ensure there are no pieces in rank 6
        // 3. Ensure there are no pieces in rank 5
        // 4. Remove 2 ranks to the original position
        result |=
            (origin & RANK_7 & !(all_pieces & RANK_6) << 8 & !(all_pieces & RANK_5) << 16) >> 16;
    }

    result
}

#[cfg(test)]
mod tests {
    use crate::bitboard::BitBoard;
    use crate::game::pieces::Color;
    use crate::game::position::Position;
    use crate::game::Board;

    #[test]
    fn test_get_pawn_moves_and_attacks_white() {
        let board =
            Board::from_fen("rn2k2r/pp2pp1p/8/2pPb1p1/1b3P1n/3qP1Pp/PP1P3P/RNBQKBNR w KQkq c6 0 1")
                .unwrap();

        // a2 can do single-step or double-step
        assert_eq!(
            super::get_pawn_moves_and_attacks(&board, Color::White, &BitBoard::from_position("a2"))
                .collect::<Vec<Position>>(),
            vec![Position::from("a3"), Position::from("a4")]
        );

        // b2 can do single-step only
        assert_eq!(
            super::get_pawn_moves_and_attacks(&board, Color::White, &BitBoard::from_position("b2"))
                .collect::<Vec<Position>>(),
            vec![Position::from("b3")]
        );

        // d2 cannot move
        assert_eq!(
            super::get_pawn_moves_and_attacks(&board, Color::White, &BitBoard::from_position("d2"))
                .collect::<Vec<Position>>(),
            vec![]
        );

        // d5 can do en-passant or single-step
        assert_eq!(
            super::get_pawn_moves_and_attacks(&board, Color::White, &BitBoard::from_position("d5"))
                .collect::<Vec<Position>>(),
            vec![Position::from("c6"), Position::from("d6")]
        );

        // e3 can do single-step
        assert_eq!(
            super::get_pawn_moves_and_attacks(&board, Color::White, &BitBoard::from_position("e3"))
                .collect::<Vec<Position>>(),
            vec![Position::from("e4")]
        );

        // f4 can capture left, right, or single-step
        assert_eq!(
            super::get_pawn_moves_and_attacks(&board, Color::White, &BitBoard::from_position("f4"))
                .collect::<Vec<Position>>(),
            vec![
                Position::from("e5"),
                Position::from("f5"),
                Position::from("g5")
            ]
        );

        // g3 can capture right, or single-step
        assert_eq!(
            super::get_pawn_moves_and_attacks(&board, Color::White, &BitBoard::from_position("g3"))
                .collect::<Vec<Position>>(),
            vec![Position::from("g4"), Position::from("h4")]
        );

        // test all pawn moves and attacks at once
        assert_eq!(
            super::get_pawn_moves_and_attacks(&board, Color::White, &board.white.pawns)
                .collect::<Vec<Position>>(),
            vec![
                Position::from("a3"),
                Position::from("b3"),
                Position::from("a4"),
                Position::from("e4"),
                Position::from("g4"),
                Position::from("h4"),
                Position::from("e5"),
                Position::from("f5"),
                Position::from("g5"),
                Position::from("c6"),
                Position::from("d6"),
            ]
        );
    }

    #[test]
    fn test_get_pawn_moves_and_attacks_black() {
        let board =
            Board::from_fen("rn2k2r/p4p1p/3P1b2/b7/5Ppn/pppqP1Pp/PP1P3P/RNBQKBNR b KQkq f3 0 1")
                .unwrap();

        // h8 can do single-step or double-step
        assert_eq!(
            super::get_pawn_moves_and_attacks(&board, Color::Black, &BitBoard::from_position("h7"))
                .collect::<Vec<Position>>(),
            vec![Position::from("h5"), Position::from("h6")]
        );

        // h3 cannot move
        assert_eq!(
            super::get_pawn_moves_and_attacks(&board, Color::Black, &BitBoard::from_position("h3"))
                .collect::<Vec<Position>>(),
            vec![]
        );

        // g4 can only do en-passant
        assert_eq!(
            super::get_pawn_moves_and_attacks(&board, Color::Black, &BitBoard::from_position("g4"))
                .collect::<Vec<Position>>(),
            vec![Position::from("f3")]
        );

        // f7 cannot move
        assert_eq!(
            super::get_pawn_moves_and_attacks(&board, Color::Black, &BitBoard::from_position("f7"))
                .collect::<Vec<Position>>(),
            vec![]
        );

        // c3 can capture left, right, or single-step
        assert_eq!(
            super::get_pawn_moves_and_attacks(&board, Color::Black, &BitBoard::from_position("c3"))
                .collect::<Vec<Position>>(),
            vec![
                Position::from("b2"),
                Position::from("c2"),
                Position::from("d2")
            ]
        );

        // b3 can capture left only
        assert_eq!(
            super::get_pawn_moves_and_attacks(&board, Color::Black, &BitBoard::from_position("b3"))
                .collect::<Vec<Position>>(),
            vec![Position::from("a2")]
        );

        // a3 can capture right only
        assert_eq!(
            super::get_pawn_moves_and_attacks(&board, Color::Black, &BitBoard::from_position("a3"))
                .collect::<Vec<Position>>(),
            vec![Position::from("b2")]
        );

        // test all pawn moves and attacks at once
        assert_eq!(
            super::get_pawn_moves_and_attacks(&board, Color::Black, &board.black.pawns)
                .collect::<Vec<Position>>(),
            vec![
                Position::from("a2"),
                Position::from("b2"),
                Position::from("c2"),
                Position::from("d2"),
                Position::from("f3"),
                Position::from("h5"),
                Position::from("a6"),
                Position::from("h6"),
            ]
        );
    }
}