in source/BattlesnakeGym/battlesnake_gym/snake.py [0:0]
def make_from_list(cls, locations, health, map_size):
'''
Class method to make a snake from a list of coordinates.
Parameters:
----------
locations: [(int, int)]
An ordered list of coordinates of the body (y, x)
health: int
The health of the snake
map_size: (int, int)
'''
tmp_locations = []
for i, j in locations[::-1]: # head is element n
tmp_locations.append(np.array([i, j]))
if len(tmp_locations) == 0:
head = None
else:
head = tmp_locations[-1]
cls = Snake(head, map_size)
cls.locations = tmp_locations
cls.health = health
if len(tmp_locations) == 0:
cls.kill_snake()
if len(cls.locations) > 1:
# Calculate the facing direction with the head and the next location
snake_head = cls.locations[-1]
snake_2nd_body = cls.locations[-2]
difference = (snake_head[0] - snake_2nd_body[0], snake_head[1] - snake_2nd_body[1])
if difference[0] == -1 and difference[1] == 0:
cls.facing_direction = Snake.UP
elif difference[0] == 1 and difference[1] == 0:
cls.facing_direction = Snake.DOWN
elif difference[0] == 0 and difference[1] == -1:
cls.facing_direction = Snake.LEFT
elif difference[0] == 0 and difference[1] == 1:
cls.facing_direction = Snake.RIGHT
return cls