move Server Logic to own Snake Class
This commit is contained in:
@@ -1,3 +1,3 @@
|
||||
from snakes.MySnake import MySnake
|
||||
from snakes.LogicSnake import LogicSnake
|
||||
|
||||
SNAKE = MySnake()
|
||||
SNAKE = LogicSnake()
|
||||
|
||||
@@ -1,15 +1,8 @@
|
||||
import random
|
||||
from scipy import spatial
|
||||
|
||||
"""
|
||||
This file can be a nice home for your move logic, and to write helper functions.
|
||||
|
||||
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:
|
||||
class LogicSnake:
|
||||
def avoid_my_body(self, my_body, possible_moves: dict) -> 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} ]
|
||||
@@ -28,8 +21,7 @@ def avoid_my_body(my_body, possible_moves: dict) -> list:
|
||||
|
||||
return possible_moves
|
||||
|
||||
|
||||
def avoid_walls(board_width: int, board_height: int, possible_moves: dict):
|
||||
def avoid_walls(self, board_width: int, board_height: int, possible_moves: dict):
|
||||
remove = []
|
||||
for direction, location in possible_moves.items():
|
||||
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
|
||||
|
||||
def avoid_snakes(snakes: list, possible_moves: dict):
|
||||
def avoid_snakes(self, snakes: list, possible_moves: dict):
|
||||
remove = []
|
||||
for snake in snakes:
|
||||
for direction, location in possible_moves.items():
|
||||
@@ -55,7 +47,7 @@ def avoid_snakes(snakes: list, possible_moves: dict):
|
||||
|
||||
return possible_moves
|
||||
|
||||
def get_rarget_close(foods: list, my_head: dict):
|
||||
def get_rarget_close(self, foods: list, my_head: dict):
|
||||
coordinates = []
|
||||
|
||||
if len(foods) == 0:
|
||||
@@ -69,7 +61,7 @@ def get_rarget_close(foods: list, my_head: dict):
|
||||
|
||||
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_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]
|
||||
|
||||
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.
|
||||
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
|
||||
possible_moves = avoid_my_body(my_body, possible_moves)
|
||||
possible_moves = avoid_walls(board_width, board_height, possible_moves)
|
||||
possible_moves = avoid_snakes(snakes, possible_moves)
|
||||
possible_moves = self.avoid_my_body(my_body, possible_moves)
|
||||
possible_moves = self.avoid_walls(board_width, board_height, 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
|
||||
if len(possible_moves) > 0:
|
||||
if target is not None:
|
||||
move = move_target(possible_moves, my_head, target)
|
||||
move = self.move_target(possible_moves, my_head, target)
|
||||
else:
|
||||
possible_moves = list(possible_moves.keys())
|
||||
move = random.choice(possible_moves)
|
||||
+1
-1
@@ -12,7 +12,7 @@ in the folder where this file exists:
|
||||
"""
|
||||
import unittest
|
||||
|
||||
from snakes.MySnake import avoid_my_neck
|
||||
from snakes.LogicSnake import avoid_my_neck
|
||||
|
||||
|
||||
class AvoidNeckTest(unittest.TestCase):
|
||||
|
||||
Reference in New Issue
Block a user