move Server Logic to own Snake Class

This commit is contained in:
2024-04-01 03:51:57 +02:00
parent 0d7739df76
commit d06c05b7b3
3 changed files with 50 additions and 58 deletions
+2 -2
View File
@@ -1,3 +1,3 @@
from snakes.MySnake import MySnake from snakes.LogicSnake import LogicSnake
SNAKE = MySnake() SNAKE = LogicSnake()
+12 -20
View File
@@ -1,15 +1,8 @@
import random import random
from scipy import spatial from scipy import spatial
""" class LogicSnake:
This file can be a nice home for your move logic, and to write helper functions. def avoid_my_body(self, my_body, possible_moves: dict) -> list:
We have started this for you, with a function to help remove the 'neck' direction
from the list of possible moves!
"""
def avoid_my_body(my_body, possible_moves: dict) -> 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} ]
@@ -28,8 +21,7 @@ def avoid_my_body(my_body, possible_moves: dict) -> list:
return possible_moves return possible_moves
def avoid_walls(self, board_width: int, board_height: int, possible_moves: dict):
def avoid_walls(board_width: int, board_height: int, possible_moves: dict):
remove = [] remove = []
for direction, location in possible_moves.items(): for direction, location in possible_moves.items():
x_out_range = (location["x"] < 0 or location["x"] == board_width) x_out_range = (location["x"] < 0 or location["x"] == board_width)
@@ -42,7 +34,7 @@ def avoid_walls(board_width: int, board_height: int, possible_moves: dict):
return possible_moves return possible_moves
def avoid_snakes(snakes: list, possible_moves: dict): def avoid_snakes(self, snakes: list, possible_moves: dict):
remove = [] remove = []
for snake in snakes: for snake in snakes:
for direction, location in possible_moves.items(): for direction, location in possible_moves.items():
@@ -55,7 +47,7 @@ def avoid_snakes(snakes: list, possible_moves: dict):
return possible_moves return possible_moves
def get_rarget_close(foods: list, my_head: dict): def get_rarget_close(self, foods: list, my_head: dict):
coordinates = [] coordinates = []
if len(foods) == 0: if len(foods) == 0:
@@ -69,7 +61,7 @@ def get_rarget_close(foods: list, my_head: dict):
return foods[results[0]] return foods[results[0]]
def move_target(possible_moves: list, my_head: dict, target:dict): def move_target(self, possible_moves: list, my_head: dict, target:dict):
distance_x = abs(my_head["x"] - target["x"]) distance_x = abs(my_head["x"] - target["x"])
distance_y = abs(my_head["y"] - target["y"]) distance_y = abs(my_head["y"] - target["y"])
@@ -82,7 +74,7 @@ def move_target(possible_moves: list, my_head: dict, target:dict):
return list(possible_moves.keys())[0] return list(possible_moves.keys())[0]
def choose_move(data: dict) -> str: def choose_move(self, data: dict) -> str:
""" """
data: Dictionary of all Game Board data as received from the Battlesnake Engine. data: Dictionary of all Game Board data as received from the Battlesnake Engine.
For a full example of 'data', see https://docs.battlesnake.com/references/api/sample-move-request For a full example of 'data', see https://docs.battlesnake.com/references/api/sample-move-request
@@ -129,16 +121,16 @@ def choose_move(data: dict) -> str:
} }
# Don't allow your Battlesnake to move back in on it's own neck # Don't allow your Battlesnake to move back in on it's own neck
possible_moves = avoid_my_body(my_body, possible_moves) possible_moves = self.avoid_my_body(my_body, possible_moves)
possible_moves = avoid_walls(board_width, board_height, possible_moves) possible_moves = self.avoid_walls(board_width, board_height, possible_moves)
possible_moves = avoid_snakes(snakes, possible_moves) possible_moves = self.avoid_snakes(snakes, possible_moves)
target = get_rarget_close(foods, my_head) target = self.get_rarget_close(foods, my_head)
# TODO: Explore new strategies for picking a move that are better than random # TODO: Explore new strategies for picking a move that are better than random
if len(possible_moves) > 0: if len(possible_moves) > 0:
if target is not None: if target is not None:
move = move_target(possible_moves, my_head, target) move = self.move_target(possible_moves, my_head, target)
else: else:
possible_moves = list(possible_moves.keys()) possible_moves = list(possible_moves.keys())
move = random.choice(possible_moves) move = random.choice(possible_moves)
+1 -1
View File
@@ -12,7 +12,7 @@ in the folder where this file exists:
""" """
import unittest import unittest
from snakes.MySnake import avoid_my_neck from snakes.LogicSnake import avoid_my_neck
class AvoidNeckTest(unittest.TestCase): class AvoidNeckTest(unittest.TestCase):