simplify safety check

This commit is contained in:
Max Känner 2024-09-02 19:59:48 +02:00
parent 290b793b94
commit 0b02efc752

View File

@ -20,26 +20,12 @@ impl Battlesnake {
/// Check if moving the snake in `direction` is safe. /// Check if moving the snake in `direction` is safe.
#[must_use] #[must_use]
pub fn is_direction_safe(&self, direction: Direction, game: &Game, board: &Board) -> bool { pub fn is_direction_safe(&self, direction: Direction, game: &Game, board: &Board) -> bool {
if self.is_direction_death(direction, game, board) {
return false;
}
let target = self.head.move_to(direction); let target = self.head.move_to(direction);
// check if target is out of bounds
if !((0..board.width).contains(&target.x) && (0..board.height).contains(&target.y)) {
return false;
}
// check if target is inside a snake
if board
.snakes
.iter()
.filter(|snake| {
!(game.ruleset.settings.squad.allow_body_collisions && self.squad == snake.squad)
})
.flat_map(|snake| snake.body.iter())
.any(|&coord| target == coord)
{
return false;
}
// check if a bigger snake could move to this square // check if a bigger snake could move to this square
if board if board
.snakes .snakes