Skip to content

Commit 9e55243

Browse files
committed
generate pawn moves p3
1 parent 17ff756 commit 9e55243

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

src/board.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,17 +221,26 @@ impl Board {
221221
};
222222
}
223223

224-
fn is_starting_position(&self, color: Color, position: usize) -> bool {
224+
/// Check if pawn position is starting position
225+
fn is_pawn_starting_position(&self, color: Color, position: usize) -> bool {
225226
match color {
226227
Color::White => (8..16).contains(&position),
227228
Color::Black => (48..56).contains(&position),
228229
}
229230
}
230231

232+
/// Check is square is empty
231233
fn is_square_empty(&self, index: usize, occupancy: Bitboard) -> bool {
232234
!occupancy.is_set(index)
233235
}
234236

237+
fn is_square_enemy(&self, color: Color, position: usize) -> bool {
238+
match color {
239+
Color::White => self.black_occupancy.is_set(position),
240+
Color::Black => self.white_occupancy.is_set(position),
241+
}
242+
}
243+
235244
/// Generate all possible moves at the specified square index
236245
pub fn generate_moves(&self, index: usize) -> Vec<usize> {
237246
let mut moves = Vec::new();
@@ -255,11 +264,20 @@ impl Board {
255264
moves.push(single_forward as usize);
256265
}
257266

258-
if self.is_starting_position(self.turn, position) {
267+
if self.is_pawn_starting_position(self.turn, position) {
259268
let double_forward = single_forward + direction;
260269
if self.is_square_empty(double_forward as usize, occupancy) {
261270
moves.push(double_forward as usize);
262271
}
263272
}
273+
274+
let left_capture = single_forward - 1;
275+
let right_capture = single_forward + 1;
276+
if self.is_square_enemy(self.turn, left_capture as usize) {
277+
moves.push(left_capture as usize);
278+
}
279+
if self.is_square_enemy(self.turn, right_capture as usize) {
280+
moves.push(right_capture as usize);
281+
}
264282
}
265283
}

0 commit comments

Comments
 (0)