Skip to content

Commit

Permalink
generate pawn moves p3
Browse files Browse the repository at this point in the history
  • Loading branch information
Kacperacy committed Jul 9, 2024
1 parent 17ff756 commit 9e55243
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions src/board.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,17 +221,26 @@ impl Board {
};
}

fn is_starting_position(&self, color: Color, position: usize) -> bool {
/// Check if pawn position is starting position
fn is_pawn_starting_position(&self, color: Color, position: usize) -> bool {
match color {
Color::White => (8..16).contains(&position),
Color::Black => (48..56).contains(&position),
}
}

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

fn is_square_enemy(&self, color: Color, position: usize) -> bool {
match color {
Color::White => self.black_occupancy.is_set(position),
Color::Black => self.white_occupancy.is_set(position),
}
}

/// Generate all possible moves at the specified square index
pub fn generate_moves(&self, index: usize) -> Vec<usize> {
let mut moves = Vec::new();
Expand All @@ -255,11 +264,20 @@ impl Board {
moves.push(single_forward as usize);
}

if self.is_starting_position(self.turn, position) {
if self.is_pawn_starting_position(self.turn, position) {
let double_forward = single_forward + direction;
if self.is_square_empty(double_forward as usize, occupancy) {
moves.push(double_forward as usize);
}
}

let left_capture = single_forward - 1;
let right_capture = single_forward + 1;
if self.is_square_enemy(self.turn, left_capture as usize) {
moves.push(left_capture as usize);
}
if self.is_square_enemy(self.turn, right_capture as usize) {
moves.push(right_capture as usize);
}
}
}

0 comments on commit 9e55243

Please sign in to comment.