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

/// Returns a collection of moves that are unobstructed pawn double-steps.
pub fn get_pawn_double_steps(board: Board, color: Color) -> Vec<Move> {
    let mut own_pawns = board.get_side(color).pawns;
    let mut all_pieces = board.white.pieces | board.black.pieces;

    // Normalize by flipping
    if color == Color::Black {
        own_pawns = own_pawns.rotate();
        all_pieces = all_pieces.rotate();
    }

    // Pawns on rank 2 that are unobstructed to rank 4
    let pawns = own_pawns
        & RANK_2
        & !BitBoard((all_pieces & RANK_3).0 >> 8)
        & !BitBoard((all_pieces & RANK_4).0 >> 16);

    // Convert to `Move`
    pawns
        .split()
        .into_iter()
        .map(|pawn| {
            let mut pawn = pawn;
            let mut destination = BitBoard(pawn.0 << 16); // Add 2 ranks
            if color == Color::Black {
                // De-normalize
                pawn = pawn.rotate();
                destination = destination.rotate();
            }
            Move(
                pawn.to_position(),
                destination.to_position(),
                Promotion::None,
            )
        })
        .collect()
}

/// Returns a collection of moves that are unobstructed pawn double-steps.
pub fn get_pawn_single_steps(board: Board, color: Color) -> Vec<Move> {
    let mut own_pawns = board.get_side(color).pawns;
    let mut all_pieces = board.white.pieces | board.black.pieces;

    // Normalize by flipping
    if color == Color::Black {
        own_pawns = own_pawns.rotate();
        all_pieces = all_pieces.rotate();
    }

    // Pawn single-step destinations with no obstruction
    let destinations = BitBoard(own_pawns.0 << 8) & !all_pieces;

    // Convert to `Move`
    destinations
        .split()
        .into_iter()
        .map(|destination| {
            let mut pawn = BitBoard(destination.0 >> 8); // Remove 1 rank
            let mut destination = destination;
            if color == Color::Black {
                // De-normalize
                pawn = pawn.rotate();
                destination = destination.rotate();
            }
            Move(
                pawn.to_position(),
                destination.to_position(),
                Promotion::None,
            )
        })
        .collect()
}

/// Returns a collection of moves that are unobstructed pawn attacks on the left side.
/// Each move is paired with the value of the piece taken.
pub fn get_pawn_left_attacks(board: Board, color: Color) -> Vec<(Move, u8)> {
    let mut own_pawns = board.get_side(color).pawns;
    let mut other_pieces = board.get_side(color.opposite()).pieces;

    // Normalize by flipping
    if color == Color::Black {
        own_pawns = own_pawns.rotate();
        other_pieces = other_pieces.rotate();
    }

    // Pawn single-step destinations with no obstruction
    let attacked = BitBoard((own_pawns & !FILE_A).0 << 7) & other_pieces;

    // Convert to `Move`
    attacked
        .split()
        .into_iter()
        .map(|destination| {
            let mut destination = destination;
            let mut pawn = BitBoard(destination.0 >> 7);
            if color == Color::Black {
                // De-normalize
                pawn = pawn.rotate();
                destination = destination.rotate();
            }
            let m = Move(
                pawn.to_position(),
                destination.to_position(),
                Promotion::None,
            );
            let value = board.get_piece_value(m.1);
            (m, value)
        })
        .collect()
}

/// Returns a collection of moves that are unobstructed pawn attacks on the right side.
/// Each move is paired with the value of the piece taken.
pub fn get_pawn_right_attacks(board: Board, color: Color) -> Vec<(Move, u8)> {
    let mut own_pawns = board.get_side(color).pawns;
    let mut other_pieces = board.get_side(color.opposite()).pieces;

    // Normalize by flipping
    if color == Color::Black {
        own_pawns = own_pawns.rotate();
        other_pieces = other_pieces.rotate();
    }

    // Pawn single-step destinations with no obstruction
    let attacked_squares = BitBoard((own_pawns & !FILE_H).0 << 9);
    let attacked = attacked_squares & other_pieces;

    // Convert to `Move`
    attacked
        .split()
        .into_iter()
        .map(|destination| {
            let mut destination = destination;
            let mut pawn = BitBoard(destination.0 >> 9);
            if color == Color::Black {
                // De-normalize
                pawn = pawn.rotate();
                destination = destination.rotate();
            }
            let m = Move(
                pawn.to_position(),
                destination.to_position(),
                Promotion::None,
            );
            let value = board.get_piece_value(m.1);
            (m, value)
        })
        .collect()
}

#[cfg(test)]
mod tests {
    use crate::game::pieces::Color;
    use crate::game::{Board, Move};

    #[test]
    fn test_pawn_double_step_white() {
        let board = Board::default();
        let double_steps = super::get_pawn_double_steps(board, Color::White);
        assert_eq!(
            double_steps,
            vec![
                ("a2", "a4").into(),
                ("b2", "b4").into(),
                ("c2", "c4").into(),
                ("d2", "d4").into(),
                ("e2", "e4").into(),
                ("f2", "f4").into(),
                ("g2", "g4").into(),
                ("h2", "h4").into(),
            ]
        );
    }

    #[test]
    fn test_pawn_double_step_black() {
        let board = Board::default();
        let double_steps = super::get_pawn_double_steps(board, Color::Black);
        assert_eq!(
            double_steps,
            vec![
                ("a7", "a5").into(),
                ("b7", "b5").into(),
                ("c7", "c5").into(),
                ("d7", "d5").into(),
                ("e7", "e5").into(),
                ("f7", "f5").into(),
                ("g7", "g5").into(),
                ("h7", "h5").into(),
            ]
            .into_iter()
            .rev()
            .collect::<Vec<Move>>()
        );
    }

    #[test]
    fn test_pawn_double_step_obstructed() {
        let board =
            Board::from_fen("rn2kbnr/pp2pppp/2p5/8/1b3P2/3qP1Pp/PPPP3P/RNBQKBNR w KQkq - 0 1")
                .unwrap();
        let double_steps = super::get_pawn_double_steps(board, Color::White);
        assert_eq!(
            double_steps,
            vec![("a2", "a4").into(), ("c2", "c4").into(),]
        );
    }
}