def get_successors(matrix, board, battlesnake): successors = [] for i in range(board.height): for j in range(board.width): if matrix[i][j] == HEAD: if i > 0 and matrix[i-1][j] != BODY: head = Point( x=j, y=board.height - 1 - (i-1)) successors.append( Battlesnake( id=battlesnake.id, name=battlesnake.name, body=(head,) + battlesnake.body[:battlesnake.length - 1], head=head, health=battlesnake.health, length=battlesnake.length, shout=battlesnake.shout)) if i + 1 < board.height and matrix[i+1][j] != BODY: head = Point( x=j, y=board.height - 1 - (i+1)) successors.append( Battlesnake( id=battlesnake.id, name=battlesnake.name, body=(head,) + battlesnake.body[:battlesnake.length - 1], head=head, health=battlesnake.health, length=battlesnake.length, shout=battlesnake.shout)) if j > 0 and matrix[i][j-1] != BODY: head = Point( x=j-1, y=board.height - 1 - i) successors.append( Battlesnake( id=battlesnake.id, name=battlesnake.name, body=(head,) + battlesnake.body[:battlesnake.length - 1], head=head, health=battlesnake.health, length=battlesnake.length, shout=battlesnake.shout)) if j + 1 < board.width and matrix[i][j+1] != BODY: head = Point( x=j+1, y=board.height - 1 - i) successors.append( Battlesnake( id=battlesnake.id, name=battlesnake.name, body=(head,) + battlesnake.body[:battlesnake.length - 1], head=head, health=battlesnake.health, length=battlesnake.length, shout=battlesnake.shout)) return successors
def from_json_input(cls, json): game_id = json["game"]["id"] turn = json["turn"] board = Board.from_json_input(json) you = Battlesnake.from_json_input(json) return cls(game_id=game_id, turn=turn, board=board, you=you)
def one_food_board(): return Board(height=11, width=11, food=(Point(x=5, y=5), ), snakes=(Battlesnake(id="snake-508e96ac-94ad-11ea-bb37", name="My Snake", health=54, body=(Point(x=0, y=0), Point(x=1, y=0), Point(x=2, y=0)), head=Point(x=0, y=0), length=3, shout="why are we shouting??"), Battlesnake(id="snake-b67f4906-94ae-11ea-bb37", name="Another Snake", health=16, body=(Point(x=5, y=4), Point(x=5, y=3), Point(x=6, y=3), Point(x=6, y=2)), head=Point(x=5, y=4), length=4, shout="I'm not really sure...")))
def battlesnake(): return Battlesnake( id="snake-508e96ac-94ad-11ea-bb37", name="My Snake", health=54, body=( Point(x=0,y=0), Point(x=1,y=0), Point(x=2,y=0)), head=Point(x=0,y=0), length=3, shout="why are we shouting??")
def from_json_input(cls, json): height = json["board"]["height"] width = json["board"]["width"] food = tuple([ Point(x=food_json["x"], y=food_json["y"]) for food_json in json["board"]["food"] ]) snakes = tuple([ Battlesnake(id=snake_json["id"], name=snake_json["name"], health=snake_json["health"], body=tuple([ Point(x=body_json["x"], y=body_json["y"]) for body_json in snake_json["body"] ]), head=Point(x=snake_json["head"]["x"], y=snake_json["head"]["y"]), length=snake_json["length"], shout=snake_json["shout"]) for snake_json in json["board"]["snakes"] ]) return cls(height=height, width=width, food=food, snakes=snakes)
def test_from_json_input(self, json, battlesnake): assert (Battlesnake.from_json_input(json) == battlesnake)