add code to not get eaten when head is by food

This commit is contained in:
2024-04-17 15:15:25 +02:00
parent 12ac257d19
commit 950351b407
+10 -4
View File
@@ -28,6 +28,8 @@ class TemplateSnake:
self.my_head = self.my_snake['head']
self.my_body = self.my_snake["body"]
self.food_positions = game_data['board']['food']
self.game_type = game_data['game']["ruleset"]["name"]
self.find_safe_positions()
@@ -120,15 +122,19 @@ class TemplateSnake:
def avoid_get_eaten_by_other_snakes(self):
remove = []
no_way_out = {}
self.other_snake_posible_moves = []
for snake in self.other_snakes:
for direction, location in self.safe_positions.items():
if len(self.safe_positions) > 1:
#TODO: Avoid Draw when heads are by the food
if snake["length"] < self.my_snake["length"] and location in [{"x": v["x"], "y": v["y"]} for k, v in self.get_possible_moves(snake["head"]).items()]:
self.other_snake_posible_moves = [{"x": v["x"], "y": v["y"]} for k, v in self.get_possible_moves(snake["head"]).items()]
if snake["length"] < self.my_snake["length"] and location in self.other_snake_posible_moves:
self.eat_the_snake_overwrite = True
self.kill_the_snake = direction
#TODO: Check if snake on the way to the food here and only remove this pos
elif location in [{"x": v["x"], "y": v["y"]} for k, v in self.get_possible_moves(snake["head"]).items()]:
#TODO: Testing - Check if snake on the way to the food here and only remove this pos
elif location in self.other_snake_posible_moves and location in [{"x": food["x"], "y": food["y"]} for food in self.food_positions]:
remove.append(direction)
elif location in self.other_snake_posible_moves:
no_way_out[direction] = location
remove.append(direction)