Fix self-nibbling

This commit is contained in:
2017-10-31 19:01:20 +01:00
parent 366d39c0a7
commit 2137fbd565

View File

@ -96,7 +96,15 @@ impl Map {
match tile { match tile {
Tile::Empty { coordinate: _ } => true, Tile::Empty { coordinate: _ } => true,
Tile::Food { coordinate: _ } => true, Tile::Food { coordinate: _ } => true,
_ => false
}
}
pub fn tile_is_nibbleable_snake(&self, nibbler: &SnakeInfo, coordinate: (i32,i32)) -> bool {
let tile = self.get_tile_at(coordinate);
match tile {
Tile::SnakeBody { coordinate: _, snake } => { Tile::SnakeBody { coordinate: _, snake } => {
snake.id != nibbler.id &&
snake.tailProtectedForGameTicks == 0 && snake.tailProtectedForGameTicks == 0 &&
coordinate == translate_position(*snake.positions.last().unwrap(), self.width) coordinate == translate_position(*snake.positions.last().unwrap(), self.width)
}, },
@ -108,7 +116,9 @@ impl Map {
let (xd,yd) = direction.as_movement_delta(); let (xd,yd) = direction.as_movement_delta();
let (x,y) = util::translate_position(snake.positions[0], self.width); let (x,y) = util::translate_position(snake.positions[0], self.width);
self.is_tile_available_for_movement((x+xd,y+yd)) let tile_coord = (x+xd,y+yd);
self.is_tile_available_for_movement(tile_coord) ||
self.tile_is_nibbleable_snake(snake, (tile_coord))
} }
#[allow(dead_code)] #[allow(dead_code)]