add function when not eat food to remove snake tail
This commit is contained in:
+23
-2
@@ -4,6 +4,7 @@ import random
|
||||
class TemplateSnake:
|
||||
def __init__(self):
|
||||
self.history = []
|
||||
self.target_food = None
|
||||
|
||||
def clear_history(self):
|
||||
self.history = []
|
||||
@@ -60,7 +61,7 @@ class TemplateSnake:
|
||||
snake.pop()
|
||||
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.
|
||||
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
|
||||
"""
|
||||
remove = []
|
||||
my_body = self.did_snake_eat_food(my_body, my_head, add_to_calculations)
|
||||
for direction, location in safe_positions.items():
|
||||
if location in my_body:
|
||||
remove.append(direction)
|
||||
@@ -154,7 +156,7 @@ class TemplateSnake:
|
||||
if add_to_calculations:
|
||||
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_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 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