[core, moves] Improve bounds checking of Square::neighbor

Remove the <n> argument from this method. I think it was a bad idea to begin with
but at the time I was looking for an expedient solution for getting neighbor
squares 2 squares away.

Overhaul bounds checking in this method so horizontal (west and east) bounds are
checked in addition to vertical (north and south) bounds. For the diagonal
directions in particular, it was easy to generate some bogus neighbor squares
because this method didn't check for wrapping when calculating horizontal
neighbors.
This commit is contained in:
Eryn Wells 2025-06-07 20:06:14 -07:00
parent a8d83ad81d
commit e2ce778247
3 changed files with 80 additions and 62 deletions

View file

@ -54,9 +54,11 @@ impl Iterator for KnightMoveGenerator {
if (target_bitboard & self.friends).is_populated() {
self.next()
} else if (target_bitboard & self.enemies).is_populated() {
Some(Move::capture(origin, target).into())
let ply = Move::capture(origin, target);
Some(ply.into())
} else {
Some(Move::quiet(origin, target).into())
let ply = Move::quiet(origin, target);
Some(ply.into())
}
} else {
self.current_origin = None;

View file

@ -118,34 +118,38 @@ impl PawnMoveGenerator {
fn calculate_origin_square(&self, target: Square) -> Option<Square> {
match self.move_type {
MoveType::SinglePushes => match self.color {
Color::White => target.neighbor(Direction::South, None),
Color::Black => target.neighbor(Direction::North, None),
Color::White => target.neighbor(Direction::South),
Color::Black => target.neighbor(Direction::North),
},
MoveType::DoublePushes => match self.color {
Color::White => target.neighbor(Direction::South, Some(2)),
Color::Black => target.neighbor(Direction::North, Some(2)),
Color::White => target
.neighbor(Direction::South)?
.neighbor(Direction::South),
Color::Black => target
.neighbor(Direction::North)?
.neighbor(Direction::North),
},
MoveType::LeftCaptures => match self.color {
Color::White => target.neighbor(Direction::SouthEast, None),
Color::Black => target.neighbor(Direction::NorthWest, None),
Color::White => target.neighbor(Direction::SouthEast),
Color::Black => target.neighbor(Direction::NorthWest),
},
MoveType::RightCaptures => match self.color {
Color::White => target.neighbor(Direction::SouthWest, None),
Color::Black => target.neighbor(Direction::NorthEast, None),
Color::White => target.neighbor(Direction::SouthWest),
Color::Black => target.neighbor(Direction::NorthEast),
},
MoveType::EnPassant => match self.color {
Color::White => {
if (self.en_passant & self.left_captures).is_populated() {
target.neighbor(Direction::SouthEast, None)
target.neighbor(Direction::SouthEast)
} else {
target.neighbor(Direction::SouthWest, None)
target.neighbor(Direction::SouthWest)
}
}
Color::Black => {
if (self.en_passant & self.left_captures).is_populated() {
target.neighbor(Direction::NorthWest, None)
target.neighbor(Direction::NorthWest)
} else {
target.neighbor(Direction::NorthEast, None)
target.neighbor(Direction::NorthEast)
}
}
},