change code to not have a big choose_move function and add todos to try fix some problems in game
This commit is contained in:
+39
-26
@@ -5,7 +5,7 @@ class BetterMasterSnake(TemplateSnake):
|
|||||||
def __init__(self):
|
def __init__(self):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
self.name = "BetterMasterSnake"
|
self.name = "BetterMasterSnake"
|
||||||
self.disabled_find_near_by_food = True
|
self.disabled_check_if_food_is_near_by = True
|
||||||
# Definiere die möglichen Bewegungsrichtungen
|
# Definiere die möglichen Bewegungsrichtungen
|
||||||
self.min_safe_area = 2
|
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()]:
|
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
|
self.eat_the_snake_overwrite = True
|
||||||
return direction
|
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()]:
|
elif location in [{"x": v["x"], "y": v["y"]} for k, v in self.get_possible_moves(snake["head"]).items()]:
|
||||||
remove.append(direction)
|
remove.append(direction)
|
||||||
|
|
||||||
@@ -127,38 +128,50 @@ class BetterMasterSnake(TemplateSnake):
|
|||||||
|
|
||||||
self.find_safe_positions()
|
self.find_safe_positions()
|
||||||
if self.eat_the_snake_overwrite:
|
if self.eat_the_snake_overwrite:
|
||||||
if len(self.safe_positions) > 1:
|
move = self.overwrite_eat_the_other_snake(move, game_data["turn"])
|
||||||
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
|
|
||||||
|
|
||||||
if self.game_type == "constrictor":
|
if self.game_type == "constrictor":
|
||||||
move = self.ensure_escape_route(self.find_direction())
|
move = self.selected_move_constrictor()
|
||||||
self.add_calculations({"function": "find_direction", "my_head": self.my_head, "move": move})
|
|
||||||
else:
|
else:
|
||||||
# Finde den besten Weg zur Nahrung
|
move = self.selected_move_standard()
|
||||||
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})
|
|
||||||
|
|
||||||
self.add_to_history({"turn": game_data["turn"], "data": self.calculations})
|
self.add_to_history({"turn": game_data["turn"], "data": self.calculations})
|
||||||
return move if move else "up"
|
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):
|
def is_food_nearby(self):
|
||||||
for food in self.food_positions:
|
for food in self.food_positions:
|
||||||
if abs(self.my_head['x'] - food['x']) <= 1 and abs(self.my_head['y'] - food['y']) <= 1:
|
if abs(self.my_head['x'] - food['x']) <= 1 and abs(self.my_head['y'] - food['y']) <= 1:
|
||||||
|
|||||||
Reference in New Issue
Block a user