use new game board functions
This commit is contained in:
+13
-23
@@ -1,3 +1,4 @@
|
||||
from server.GameBoard import GameBoard
|
||||
import random
|
||||
|
||||
class TemplateSnake:
|
||||
@@ -16,22 +17,11 @@ class TemplateSnake:
|
||||
def add_calculations(self, calculations:dict):
|
||||
self.calculations.append(calculations)
|
||||
|
||||
def choose_move(self, game_data:dict):
|
||||
def choose_move(self, game_data:GameBoard):
|
||||
self.game_board = game_data
|
||||
self.calculations = []
|
||||
self.eat_the_snake_overwrite = False
|
||||
|
||||
self.board_width = game_data['board']['width']
|
||||
self.board_height = game_data['board']['height']
|
||||
|
||||
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_body = self.my_snake["body"]
|
||||
|
||||
self.food_positions = game_data['board']['food']
|
||||
self.game_type = game_data['game']["ruleset"]["name"]
|
||||
|
||||
self.safe_positions = self.find_safe_positions(add_to_calculations=True)
|
||||
moves = list(self.safe_positions.keys())
|
||||
if len(moves) > 0:
|
||||
@@ -40,7 +30,7 @@ class TemplateSnake:
|
||||
print("No safe positions left - Going to Die")
|
||||
move = None
|
||||
|
||||
self.add_to_history({"turn": game_data["turn"], "data": self.calculations})
|
||||
self.add_to_history({"turn": game_data.get_turn(), "data": self.calculations})
|
||||
return move if move else "up"
|
||||
|
||||
def get_possible_moves(self, snake_head):
|
||||
@@ -95,8 +85,8 @@ class TemplateSnake:
|
||||
def avoid_walls(self, safe_positions:dict[str, dict], add_to_calculations:bool=False):
|
||||
remove = []
|
||||
for direction, location in list(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)
|
||||
x_out_range = (location["x"] < 0 or location["x"] == self.game_board.get_width())
|
||||
y_out_range = (location["y"] < 0 or location["y"] == self.game_board.get_height())
|
||||
if x_out_range or y_out_range:
|
||||
remove.append(direction)
|
||||
|
||||
@@ -104,7 +94,7 @@ class TemplateSnake:
|
||||
del safe_positions[direction]
|
||||
|
||||
if add_to_calculations:
|
||||
self.add_calculations({"function": "avoid_walls", "board_width": self.board_width, "board_height": self.board_height, "safe_positions": safe_positions})
|
||||
self.add_calculations({"function": "avoid_walls", "board_width": self.game_board.get_width(), "board_height": self.game_board.get_height(), "safe_positions": safe_positions})
|
||||
|
||||
return safe_positions
|
||||
|
||||
@@ -160,22 +150,22 @@ class TemplateSnake:
|
||||
return safe_positions
|
||||
|
||||
def find_safe_positions(self, add_to_calculations:bool=False):
|
||||
safe_positions = self.get_possible_moves(self.my_head)
|
||||
safe_positions = self.get_possible_moves(self.game_board.get_my_snake_head())
|
||||
if add_to_calculations:
|
||||
self.add_calculations({"function": "get_possible_moves", "safe_positions": safe_positions})
|
||||
|
||||
safe_positions = self.avoid_my_body(self.my_body, safe_positions, add_to_calculations)
|
||||
safe_positions = self.avoid_my_body(self.game_board.get_my_snake_body(), safe_positions, add_to_calculations)
|
||||
safe_positions = self.avoid_walls(safe_positions, add_to_calculations)
|
||||
safe_positions = self.avoid_snakes(self.other_snakes, safe_positions, add_to_calculations)
|
||||
safe_positions = self.avoid_get_eaten_by_other_snakes(self.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)
|
||||
return safe_positions
|
||||
|
||||
def calculate_new_body_position(self, move:str=None, with_tail:bool=False):
|
||||
if move:
|
||||
head = self.get_possible_moves(self.my_head)[move]
|
||||
head = self.get_possible_moves(self.game_board.get_my_snake_head())[move]
|
||||
|
||||
body = [head]
|
||||
body.extend(self.my_body)
|
||||
body.extend(self.game_board.get_my_snake_body())
|
||||
body.pop()
|
||||
|
||||
if not with_tail:
|
||||
|
||||
Reference in New Issue
Block a user