use VecDeque in intended direction
All checks were successful
Build / build (push) Successful in 2m2s

This commit is contained in:
Max Känner 2025-01-21 19:26:43 +01:00
parent 2f2b7ac11e
commit 5bb7476af6

View File

@ -60,7 +60,7 @@ impl From<&Request> for Board {
board.free.set(index, false);
}
let snake = Snake {
body: snake.body.iter().copied().collect(),
body: snake.body.iter().rev().copied().collect(),
id: u8::try_from(id).unwrap_or(u8::MAX),
health: snake.health,
};
@ -206,9 +206,7 @@ impl Board {
|(_, action)| *action,
);
let new_head = snake.head().wrapping_apply(action);
let snake = &mut self.snakes[i];
snake.body.push_front(new_head);
snake.body.pop_back();
self.snakes[i].advance(new_head);
}
}
@ -244,9 +242,7 @@ impl Board {
fn feed_snakes_standard(&mut self) {
if self.constrictor {
for snake in &mut self.snakes {
snake.health = 100;
let tail = snake.tail();
snake.body.push_back(tail);
snake.feed();
}
} else {
let mut eaten_food = vec![];
@ -256,10 +252,7 @@ impl Board {
let head_index = self.coord_to_linear(head);
if self.food[head_index] {
eaten_food.push(head_index);
let snake = &mut self.snakes[i];
snake.health = 100;
let tail = snake.tail();
snake.body.push_back(tail);
self.snakes[i].feed();
}
}
}
@ -380,16 +373,28 @@ struct Snake {
impl Snake {
pub fn head(&self) -> Coord {
self.body.front().copied().unwrap_or_else(|| {
self.body.back().copied().unwrap_or_else(|| {
error!("Snake without a head: {self:?}");
Coord { x: 0, y: 0 }
})
}
pub fn tail(&self) -> Coord {
self.body.back().copied().unwrap_or_else(|| {
self.body.front().copied().unwrap_or_else(|| {
error!("Snake without a tail: {self:?}");
Coord { x: 0, y: 0 }
})
}
pub fn advance(&mut self, head: Coord) {
self.body.push_back(head);
self.body.pop_front();
self.health = 100;
}
pub fn feed(&mut self) {
if let Some(tail) = self.body.front() {
self.body.push_front(*tail);
}
}
}