remove pos where other snake head is to not go into a dead end or use the pos where the snake head could be to find new food

This commit is contained in:
2024-04-17 15:17:09 +02:00
parent 950351b407
commit 5ce12d70c1
+9 -2
View File
@@ -75,12 +75,19 @@ class BetterMasterSnake(TemplateSnake):
for part in snake['body']:
obstacles.add((part['x'], part['y']))
other_snakes_other_snake_posible_moves_set = {(d['x'], d['y']) for d in self.other_snake_posible_moves}
removed_elements_set = set([(elem['x'], elem['y']) for elem in self.food_positions if (elem['x'], elem['y']) in other_snakes_other_snake_posible_moves_set])
obstacles |= removed_elements_set
self.food_positions = [elem for elem in self.food_positions if (elem['x'], elem['y']) not in other_snakes_other_snake_posible_moves_set]
if len(self.food_positions) > 0:
# Choose the closest food source based on the heuristic
closest_food = min(self.food_positions, key=lambda food: abs(food['x'] - self.my_head['x']) + abs(food['y'] - self.my_head['y']))
# Use A* to search for a safe path
path = self.a_star_search(self.my_head, closest_food, obstacles)
return path
return self.a_star_search(self.my_head, closest_food, obstacles)
return None
def find_path_to_tail(self):
# Exclude other snake's body from obstacles