From 2137fbd565d9076a8bf5622746aa0749b8f3f4ac Mon Sep 17 00:00:00 2001 From: Joakim Hulthe Date: Tue, 31 Oct 2017 19:01:20 +0100 Subject: [PATCH] Fix self-nibbling --- src/maputil.rs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/maputil.rs b/src/maputil.rs index a81730a..211b1d7 100644 --- a/src/maputil.rs +++ b/src/maputil.rs @@ -96,7 +96,15 @@ impl Map { match tile { Tile::Empty { 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 } => { + snake.id != nibbler.id && snake.tailProtectedForGameTicks == 0 && coordinate == translate_position(*snake.positions.last().unwrap(), self.width) }, @@ -108,7 +116,9 @@ impl Map { let (xd,yd) = direction.as_movement_delta(); 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)]