add more debug outputs and use all the self.args when neaded in a specific function, make own variable to only store other snakes
This commit is contained in:
+35
-35
@@ -4,7 +4,7 @@ from collections import deque
|
|||||||
class BetterMasterSnake(TemplateSnake):
|
class BetterMasterSnake(TemplateSnake):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
self.name = "CloseToBodySnake"
|
self.name = "BetterMasterSnake"
|
||||||
self.disabled_find_near_by_food = True
|
self.disabled_find_near_by_food = True
|
||||||
# Definiere die möglichen Bewegungsrichtungen
|
# Definiere die möglichen Bewegungsrichtungen
|
||||||
self.min_safe_area = 2
|
self.min_safe_area = 2
|
||||||
@@ -36,7 +36,7 @@ class BetterMasterSnake(TemplateSnake):
|
|||||||
snake.pop()
|
snake.pop()
|
||||||
return snake
|
return snake
|
||||||
|
|
||||||
def avoid_my_body(self, my_body:dict) -> list:
|
def avoid_my_body(self) -> list:
|
||||||
"""
|
"""
|
||||||
my_body: List of dictionaries of x/y coordinates for every segment of a Battlesnake.
|
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} ]
|
e.g. [ {"x": 0, "y": 0}, {"x": 1, "y": 0}, {"x": 2, "y": 0} ]
|
||||||
@@ -47,29 +47,28 @@ class BetterMasterSnake(TemplateSnake):
|
|||||||
"""
|
"""
|
||||||
remove = []
|
remove = []
|
||||||
for direction, location in self.safe_positions.items():
|
for direction, location in self.safe_positions.items():
|
||||||
if location in my_body:
|
if location in self.my_body:
|
||||||
remove.append(direction)
|
remove.append(direction)
|
||||||
|
|
||||||
for direction in remove:
|
for direction in remove:
|
||||||
del self.safe_positions[direction]
|
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, board_width:int, board_height:int):
|
def avoid_walls(self):
|
||||||
remove = []
|
remove = []
|
||||||
for direction, location in list(self.safe_positions.items()):
|
for direction, location in list(self.safe_positions.items()):
|
||||||
x_out_range = (location["x"] < 0 or location["x"] == board_width)
|
x_out_range = (location["x"] < 0 or location["x"] == self.board_width)
|
||||||
y_out_range = (location["y"] < 0 or location["y"] == board_height)
|
y_out_range = (location["y"] < 0 or location["y"] == self.board_height)
|
||||||
if x_out_range or y_out_range:
|
if x_out_range or y_out_range:
|
||||||
remove.append(direction)
|
remove.append(direction)
|
||||||
|
|
||||||
for direction in remove:
|
for direction in remove:
|
||||||
del self.safe_positions[direction]
|
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, snakes:list):
|
def avoid_snakes(self):
|
||||||
remove = []
|
remove = []
|
||||||
for snake in snakes:
|
for snake in self.other_snakes:
|
||||||
if snake["id"] == self.my_snake["id"]:
|
|
||||||
continue
|
|
||||||
|
|
||||||
for direction, location in self.safe_positions.items():
|
for direction, location in self.safe_positions.items():
|
||||||
if self.game_type == "constrictor":
|
if self.game_type == "constrictor":
|
||||||
if location in snake["body"]:
|
if location in snake["body"]:
|
||||||
@@ -81,13 +80,11 @@ class BetterMasterSnake(TemplateSnake):
|
|||||||
remove = set(remove)
|
remove = set(remove)
|
||||||
for direction in remove:
|
for direction in remove:
|
||||||
del self.safe_positions[direction]
|
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, snakes:list):
|
def avoid_get_eaten_by_other_snakes(self):
|
||||||
remove = []
|
remove = []
|
||||||
for snake in snakes:
|
for snake in self.other_snakes:
|
||||||
if snake["id"] == self.my_snake["id"]:
|
|
||||||
continue
|
|
||||||
|
|
||||||
for direction, location in self.safe_positions.items():
|
for direction, location in self.safe_positions.items():
|
||||||
if len(self.safe_positions) > 1:
|
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()]:
|
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()]:
|
||||||
@@ -99,23 +96,28 @@ class BetterMasterSnake(TemplateSnake):
|
|||||||
remove = set(remove)
|
remove = set(remove)
|
||||||
for direction in remove:
|
for direction in remove:
|
||||||
del self.safe_positions[direction]
|
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):
|
def find_safe_positions(self):
|
||||||
self.safe_positions = self.get_possible_moves(self.my_head)
|
self.safe_positions = self.get_possible_moves(self.my_head)
|
||||||
self.avoid_my_body(self.my_body)
|
self.add_calculations({"function": "get_possible_moves", "safe_positions": self.safe_positions})
|
||||||
self.avoid_walls(self.board_width, self.board_height)
|
|
||||||
self.avoid_snakes(self.snakes)
|
self.avoid_my_body()
|
||||||
self.avoid_get_eaten_by_other_snakes(self.snakes)
|
self.avoid_walls()
|
||||||
|
self.avoid_snakes()
|
||||||
|
self.avoid_get_eaten_by_other_snakes()
|
||||||
|
|
||||||
def choose_move(self, game_data):
|
def choose_move(self, game_data):
|
||||||
|
move = None
|
||||||
self.calculations = []
|
self.calculations = []
|
||||||
self.eat_the_snake_overwrite = False
|
self.eat_the_snake_overwrite = False
|
||||||
move = None
|
|
||||||
|
|
||||||
self.board_width = game_data['board']['width']
|
self.board_width = game_data['board']['width']
|
||||||
self.board_height = game_data['board']['height']
|
self.board_height = game_data['board']['height']
|
||||||
self.snakes = game_data['board']['snakes']
|
|
||||||
self.my_snake = game_data['you']
|
self.my_snake = game_data['you']
|
||||||
|
self.other_snakes = [ x for x in game_data['board']['snakes'] if x["id"] != self.my_snake["id"] ]
|
||||||
|
|
||||||
self.my_head = self.my_snake['head']
|
self.my_head = self.my_snake['head']
|
||||||
self.my_body = self.my_snake["body"]
|
self.my_body = self.my_snake["body"]
|
||||||
self.food_positions = game_data['board']['food']
|
self.food_positions = game_data['board']['food']
|
||||||
@@ -123,12 +125,12 @@ class BetterMasterSnake(TemplateSnake):
|
|||||||
|
|
||||||
self.find_safe_positions()
|
self.find_safe_positions()
|
||||||
if self.eat_the_snake_overwrite:
|
if self.eat_the_snake_overwrite:
|
||||||
self.add_calculations({"function": "eat_the_snake_overwrite", "my_head": self.my_head, "move": move, "safe_positions": self.safe_positions})
|
self.add_calculations({"function": "eat_the_snake_overwrite", "my_head": self.my_head, "move": move})
|
||||||
return self.safe_positions
|
return self.safe_positions
|
||||||
|
|
||||||
if self.game_type == "constrictor":
|
if self.game_type == "constrictor":
|
||||||
move = self.ensure_escape_route(self.find_direction())
|
move = self.ensure_escape_route(self.find_direction())
|
||||||
self.add_calculations({"function": "find_direction", "my_head": self.my_head, "move": move, "safe_positions": self.safe_positions})
|
self.add_calculations({"function": "find_direction", "my_head": self.my_head, "move": move})
|
||||||
else:
|
else:
|
||||||
# Finde den besten Weg zur Nahrung
|
# Finde den besten Weg zur Nahrung
|
||||||
if self.is_food_nearby() or self.disabled_find_near_by_food:
|
if self.is_food_nearby() or self.disabled_find_near_by_food:
|
||||||
@@ -139,11 +141,11 @@ class BetterMasterSnake(TemplateSnake):
|
|||||||
|
|
||||||
if not move:
|
if not move:
|
||||||
move = self.move_close_to_body()
|
move = self.move_close_to_body()
|
||||||
self.add_calculations({"function": "move_close_to_body", "my_head": self.my_head, "move": move, "safe_positions": self.safe_positions})
|
self.add_calculations({"function": "move_close_to_body", "my_head": self.my_head, "move": move})
|
||||||
|
|
||||||
# Überprfe, ob der Zug einen Ausweg lässt
|
# Überprfe, ob der Zug einen Ausweg lässt
|
||||||
move = self.ensure_escape_route(move)
|
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})
|
self.add_calculations({"function": "ensure_escape_route", "my_head": self.my_head, "move": move})
|
||||||
|
|
||||||
self.add_to_history(self.calculations)
|
self.add_to_history(self.calculations)
|
||||||
return move if move else "up"
|
return move if move else "up"
|
||||||
@@ -161,10 +163,9 @@ class BetterMasterSnake(TemplateSnake):
|
|||||||
# Exclude own snake's body from obstacles
|
# Exclude own snake's body from obstacles
|
||||||
obstacles = set((part['x'], part['y']) for part in self.my_body)
|
obstacles = set((part['x'], part['y']) for part in self.my_body)
|
||||||
|
|
||||||
for snake in self.snakes:
|
for snake in self.other_snakes:
|
||||||
if snake['id'] != self.my_snake['id']:
|
for part in snake['body']:
|
||||||
for part in snake['body']:
|
obstacles.add((part['x'], part['y']))
|
||||||
obstacles.add((part['x'], part['y']))
|
|
||||||
|
|
||||||
# Choose the closest food source based on the heuristic
|
# 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']))
|
closest_food = min(self.food_positions, key=lambda food: abs(food['x'] - self.my_head['x']) + abs(food['y'] - self.my_head['y']))
|
||||||
@@ -172,14 +173,13 @@ class BetterMasterSnake(TemplateSnake):
|
|||||||
# Use A* to search for a safe path
|
# Use A* to search for a safe path
|
||||||
path = self.a_star_search(self.my_head, closest_food, obstacles, self.board_width, self.board_height)
|
path = self.a_star_search(self.my_head, closest_food, obstacles, self.board_width, self.board_height)
|
||||||
return path
|
return path
|
||||||
|
|
||||||
def find_path_to_tail(self):
|
def find_path_to_tail(self):
|
||||||
# Exclude other snake's body from obstacles
|
# Exclude other snake's body from obstacles
|
||||||
obstacles = set()
|
obstacles = set()
|
||||||
for snake in self.snakes:
|
for snake in self.other_snakes:
|
||||||
if snake['id'] != self.my_snake['id']:
|
for part in snake['body']:
|
||||||
for part in snake['body']:
|
obstacles.add((part['x'], part['y']))
|
||||||
obstacles.add((part['x'], part['y']))
|
|
||||||
|
|
||||||
my_snake_tail = {"x": self.my_body[-1]['x'], "y": self.my_body[-1]['y']}
|
my_snake_tail = {"x": self.my_body[-1]['x'], "y": self.my_body[-1]['y']}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user