diff --git a/snakes/BetterMasterSnake.py b/snakes/BetterMasterSnake.py index 0e7ed91..71989d6 100644 --- a/snakes/BetterMasterSnake.py +++ b/snakes/BetterMasterSnake.py @@ -8,105 +8,6 @@ class BetterMasterSnake(TemplateSnake): # Definiere die möglichen Bewegungsrichtungen self.min_safe_area = 2 - def get_possible_moves(self, my_head): - return { - "up": { - "x": my_head["x"], - "y": my_head["y"] + 1 - }, - "down": { - "x": my_head["x"], - "y": my_head["y"] - 1 - }, - "left": { - "x": my_head["x"] - 1, - "y": my_head["y"] - }, - "right": { - "x": my_head["x"] + 1, - "y": my_head["y"] - } - } - - def get_snake_body_without_snake_tail(self, snake:list[dict]): - if len(set((pos["x"], pos["y"]) for pos in snake)) < 3: - return snake - - snake.pop() - return snake - - def avoid_my_body(self) -> list: - """ - my_body: List of dictionaries of x/y coordinates for every segment of a Battlesnake. - e.g. [ {"x": 0, "y": 0}, {"x": 1, "y": 0}, {"x": 2, "y": 0} ] - possible_moves: List of strings. Moves to pick from. - e.g. ["up", "down", "left", "right"] - - return: The list of remaining possible_moves, with the 'neck' direction removed - """ - remove = [] - for direction, location in self.safe_positions.items(): - if location in self.my_body: - remove.append(direction) - - for direction in remove: - del self.safe_positions[direction] - self.add_calculations({"function": "avoid_my_body", "my_body": self.my_body, "safe_positions": self.safe_positions}) - - def avoid_walls(self): - remove = [] - for direction, location in list(self.safe_positions.items()): - x_out_range = (location["x"] < 0 or location["x"] == self.board_width) - y_out_range = (location["y"] < 0 or location["y"] == self.board_height) - if x_out_range or y_out_range: - remove.append(direction) - - for direction in remove: - del self.safe_positions[direction] - self.add_calculations({"function": "avoid_walls", "board_width": self.board_width, "board_height": self.board_height, "safe_positions": self.safe_positions}) - - def avoid_snakes(self): - remove = [] - for snake in self.other_snakes: - for direction, location in self.safe_positions.items(): - #if self.game_type == "constrictor": - if location in snake["body"]: - remove.append(direction) - #else: - # if location in self.get_snake_body_without_snake_tail(snake["body"]): - # remove.append(direction) - - remove = set(remove) - for direction in remove: - del self.safe_positions[direction] - self.add_calculations({"function": "avoid_snakes", "other_snakes": self.other_snakes, "safe_positions": self.safe_positions}) - - def avoid_get_eaten_by_other_snakes(self): - remove = [] - for snake in self.other_snakes: - for direction, location in self.safe_positions.items(): - if len(self.safe_positions) > 1: - 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) - - remove = set(remove) - for direction in remove: - del self.safe_positions[direction] - self.add_calculations({"function": "avoid_get_eaten_by_other_snakes", "other_snakes": self.other_snakes, "safe_positions": self.safe_positions}) - - def find_safe_positions(self): - self.safe_positions = self.get_possible_moves(self.my_head) - self.add_calculations({"function": "get_possible_moves", "safe_positions": self.safe_positions}) - - self.avoid_my_body() - self.avoid_walls() - self.avoid_snakes() - self.avoid_get_eaten_by_other_snakes() - def choose_move(self, game_data): move = None self.calculations = [] @@ -163,7 +64,7 @@ class BetterMasterSnake(TemplateSnake): 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: + if not move or self.would_eating_the_food_kill_the_snake(move): move = self.move_close_to_body(move_close_to_tail=True) self.add_calculations({"function": "move_close_to_body", "my_head": self.my_head, "move": move}) @@ -236,6 +137,9 @@ class BetterMasterSnake(TemplateSnake): best_move = direction return best_move if best_move else "up" # Standardbewegung, falls keine bessere gefunden wird + #TODO: Neat to Implement Function to check if eating the food would kill the snake? + def would_eating_the_food_kill_the_snake(self, move:str): + return False def ensure_escape_route(self, move): try: @@ -353,6 +257,3 @@ class BetterMasterSnake(TemplateSnake): if next_position in safe_positions_tuples: return direction return "up" # Standardbewegung, falls keine sichere Position gefunden wird - - def add_calculations(self, calculations:dict): - self.calculations.append(calculations)