add function when not eat food to remove snake tail
This commit is contained in:
@@ -72,6 +72,7 @@ class BetterMasterSnake(TemplateSnake):
|
|||||||
if len(self.food_positions) > 0:
|
if len(self.food_positions) > 0:
|
||||||
# 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.game_board.get_my_snake_head()['x']) + abs(food['y'] - self.game_board.get_my_snake_head()['y']))
|
closest_food = min(self.food_positions, key=lambda food: abs(food['x'] - self.game_board.get_my_snake_head()['x']) + abs(food['y'] - self.game_board.get_my_snake_head()['y']))
|
||||||
|
self.set_target_food(closest_food)
|
||||||
|
|
||||||
# Use A* to search for a safe path
|
# Use A* to search for a safe path
|
||||||
return self.a_star_search(self.game_board.get_my_snake_head(), closest_food, obstacles)
|
return self.a_star_search(self.game_board.get_my_snake_head(), closest_food, obstacles)
|
||||||
|
|||||||
+23
-2
@@ -4,6 +4,7 @@ import random
|
|||||||
class TemplateSnake:
|
class TemplateSnake:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.history = []
|
self.history = []
|
||||||
|
self.target_food = None
|
||||||
|
|
||||||
def clear_history(self):
|
def clear_history(self):
|
||||||
self.history = []
|
self.history = []
|
||||||
@@ -60,7 +61,7 @@ class TemplateSnake:
|
|||||||
snake.pop()
|
snake.pop()
|
||||||
return snake
|
return snake
|
||||||
|
|
||||||
def avoid_my_body(self, my_body:list[dict], safe_positions:dict[str, dict], add_to_calculations:bool=False) -> list:
|
def avoid_my_body(self, my_body:list[dict], my_head:dict, safe_positions:dict[str, dict], add_to_calculations:bool=False) -> 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} ]
|
||||||
@@ -70,6 +71,7 @@ class TemplateSnake:
|
|||||||
return: The list of remaining possible_moves, with the 'neck' direction removed
|
return: The list of remaining possible_moves, with the 'neck' direction removed
|
||||||
"""
|
"""
|
||||||
remove = []
|
remove = []
|
||||||
|
my_body = self.did_snake_eat_food(my_body, my_head, add_to_calculations)
|
||||||
for direction, location in safe_positions.items():
|
for direction, location in safe_positions.items():
|
||||||
if location in my_body:
|
if location in my_body:
|
||||||
remove.append(direction)
|
remove.append(direction)
|
||||||
@@ -154,7 +156,7 @@ class TemplateSnake:
|
|||||||
if add_to_calculations:
|
if add_to_calculations:
|
||||||
self.add_calculations({"function": "get_possible_moves", "safe_positions": safe_positions})
|
self.add_calculations({"function": "get_possible_moves", "safe_positions": safe_positions})
|
||||||
|
|
||||||
safe_positions = self.avoid_my_body(self.game_board.get_my_snake_body(), safe_positions, add_to_calculations)
|
safe_positions = self.avoid_my_body(self.game_board.get_my_snake_body(), self.game_board.get_my_snake_head(), safe_positions, add_to_calculations)
|
||||||
safe_positions = self.avoid_walls(safe_positions, add_to_calculations)
|
safe_positions = self.avoid_walls(safe_positions, add_to_calculations)
|
||||||
safe_positions = self.avoid_snakes(self.game_board.get_other_snakes(), safe_positions, add_to_calculations)
|
safe_positions = self.avoid_snakes(self.game_board.get_other_snakes(), safe_positions, add_to_calculations)
|
||||||
safe_positions = self.avoid_get_eaten_by_other_snakes(self.game_board.get_other_snakes(), safe_positions, add_to_calculations)
|
safe_positions = self.avoid_get_eaten_by_other_snakes(self.game_board.get_other_snakes(), safe_positions, add_to_calculations)
|
||||||
@@ -173,3 +175,22 @@ class TemplateSnake:
|
|||||||
|
|
||||||
return body
|
return body
|
||||||
return move
|
return move
|
||||||
|
|
||||||
|
def did_snake_eat_food(self, my_body:list[dict], my_head:dict, add_to_calculations:bool=False):
|
||||||
|
if self.target_food is None:
|
||||||
|
if add_to_calculations:
|
||||||
|
self.add_calculations({"function": "did_snake_eat_food", "my_body": my_body, "my_head": my_head, "target_food": self.target_food, "action": "No Target Food"})
|
||||||
|
return my_body
|
||||||
|
|
||||||
|
if self.target_food["x"] == my_head["x"] and self.target_food["y"] == my_head["y"]:
|
||||||
|
if add_to_calculations:
|
||||||
|
self.add_calculations({"function": "did_snake_eat_food", "my_body": my_body, "my_head": my_head, "target_food": self.target_food, "action": "Snake Eat no food"})
|
||||||
|
return my_body
|
||||||
|
|
||||||
|
if add_to_calculations:
|
||||||
|
self.add_calculations({"function": "did_snake_eat_food", "my_body": my_body[:-1], "my_head": my_head, "target_food": self.target_food, "action": "Remove Tail from Body"})
|
||||||
|
return my_body[:-1]
|
||||||
|
|
||||||
|
def set_target_food(self, target_food:dict):
|
||||||
|
self.target_food = target_food
|
||||||
|
return True
|
||||||
|
|||||||
Reference in New Issue
Block a user