From ea36d60b4da1a781440f1758816f2532ab3fd9c9 Mon Sep 17 00:00:00 2001 From: Daniel Dolezal Date: Wed, 17 Apr 2024 09:33:35 +0200 Subject: [PATCH] change code to not have a big choose_move function and add todos to try fix some problems in game --- snakes/BetterMasterSnake.py | 65 ++++++++++++++++++++++--------------- 1 file changed, 39 insertions(+), 26 deletions(-) diff --git a/snakes/BetterMasterSnake.py b/snakes/BetterMasterSnake.py index 7631759..d79528e 100644 --- a/snakes/BetterMasterSnake.py +++ b/snakes/BetterMasterSnake.py @@ -5,7 +5,7 @@ class BetterMasterSnake(TemplateSnake): def __init__(self): super().__init__() self.name = "BetterMasterSnake" - self.disabled_find_near_by_food = True + self.disabled_check_if_food_is_near_by = True # Definiere die möglichen Bewegungsrichtungen self.min_safe_area = 2 @@ -90,6 +90,7 @@ class BetterMasterSnake(TemplateSnake): 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.eat_the_snake_overwrite = True return direction + #TODO: Check if snake on the way to the bood 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()]: remove.append(direction) @@ -127,38 +128,50 @@ class BetterMasterSnake(TemplateSnake): self.find_safe_positions() if self.eat_the_snake_overwrite: - if len(self.safe_positions) > 1: - first_key = list(self.safe_positions.keys())[0] - self.add_calculations({"function": "eat_the_snake_overwrite", "my_head": self.my_head, "move": move, "safe_positions": self.safe_positions, "selected_move": self.safe_positions[first_key]}) - self.add_to_history({"turn": game_data["turn"], "data": self.calculations}) - return self.safe_positions[first_key] - - self.add_calculations({"function": "eat_the_snake_overwrite", "my_head": self.my_head, "move": move, "safe_positions": self.safe_positions}) - self.add_to_history({"turn": game_data["turn"], "data": self.calculations}) - return self.safe_positions + move = self.overwrite_eat_the_other_snake(move, game_data["turn"]) if self.game_type == "constrictor": - move = self.ensure_escape_route(self.find_direction()) - self.add_calculations({"function": "find_direction", "my_head": self.my_head, "move": move}) + move = self.selected_move_constrictor() else: - # Finde den besten Weg zur Nahrung - if self.is_food_nearby() or self.disabled_find_near_by_food: - path_to_food = self.find_path_to_food() - if path_to_food: - move = self.move_towards(path_to_food[0]) - self.add_calculations({"function": "move_towards", "my_head": self.my_head, "path_to_food": path_to_food, "move": move}) - - if not move: - move = self.move_close_to_body() - self.add_calculations({"function": "move_close_to_body", "my_head": self.my_head, "move": move}) - - # Überprfe, ob der Zug einen Ausweg lässt - move = self.ensure_escape_route(move) - self.add_calculations({"function": "ensure_escape_route", "my_head": self.my_head, "move": move, "safe_positions": self.safe_positions}) + move = self.selected_move_standard() self.add_to_history({"turn": game_data["turn"], "data": self.calculations}) return move if move else "up" + def overwrite_eat_the_other_snake(self, move:str, turn:int): + if len(self.safe_positions) > 1: + first_key = list(self.safe_positions.keys())[0] + self.add_calculations({"function": "eat_the_snake_overwrite", "my_head": self.my_head, "move": move, "safe_positions": self.safe_positions, "selected_move": self.safe_positions[first_key]}) + self.add_to_history({"turn": turn, "data": self.calculations}) + return self.safe_positions[first_key] + + self.add_calculations({"function": "eat_the_snake_overwrite", "my_head": self.my_head, "move": move, "safe_positions": self.safe_positions}) + self.add_to_history({"turn": turn, "data": self.calculations}) + return self.safe_positions + + #TODO: How to Fill the Gameboard best? + def selected_move_constrictor(self): + move = self.ensure_escape_route(self.find_direction()) + self.add_calculations({"function": "find_direction", "my_head": self.my_head, "move": move}) + return move + + def selected_move_standard(self): + # Finde den besten Weg zur Nahrung + if self.is_food_nearby() or self.disabled_check_if_food_is_near_by: + path_to_food = self.find_path_to_food() + if path_to_food: + move = self.move_towards(path_to_food[0]) + self.add_calculations({"function": "move_towards", "my_head": self.my_head, "path_to_food": path_to_food, "move": move}) + + if not move: + move = self.move_close_to_body() + self.add_calculations({"function": "move_close_to_body", "my_head": self.my_head, "move": move}) + + # Überprfe, ob der Zug einen Ausweg lässt + move = self.ensure_escape_route(move) + self.add_calculations({"function": "ensure_escape_route", "my_head": self.my_head, "move": move, "safe_positions": self.safe_positions}) + return move + def is_food_nearby(self): for food in self.food_positions: if abs(self.my_head['x'] - food['x']) <= 1 and abs(self.my_head['y'] - food['y']) <= 1: