in source/BattlesnakeGym/battlesnake_gym/snake_gym.py [0:0]
def _get_ascii(self):
'''
Generate visualisation of the gym. Prints ascii representation of the gym.
Could be used as an input to initialise the gym.
Return:
-------
ascii_string: str
String visually depicting the gym
- The walls of the gym are labelled with "*"
- Food is labelled with "@"
- Snakes characters (head is the uppercase letter)
- The health of each snake is on the bottom of the gym
'''
FOOD_INDEX = 0
SNAKE_INDEXES = FOOD_INDEX + np.array(range(1, self.number_of_snakes + 1))
ascii_array = np.empty(shape=(self.map_size[0] + 2, self.map_size[1] + 2), dtype="object")
# Set the borders of the image
ascii_array[0, :] = "*"
ascii_array[-1, :] = "*"
ascii_array[:, 0] = "*"
ascii_array[:, -1] = "*"
# Populate food
for i in range(self.map_size[0]):
for j in range(self.map_size[1]):
if self.food.locations_map[i, j]:
ascii_array[i+1, j+1] = " @"
# Populate snakes
for idx, snake in enumerate(self.snakes.get_snakes()):
snake_character = string.ascii_lowercase[idx]
for snake_idx, location in enumerate(snake.locations):
snake_idx = len(snake.locations) - snake_idx - 1
ascii_array[location[0]+1, location[1]+1] = snake_character + str(snake_idx)
# convert to string
ascii_string = ""
for i in range(ascii_array.shape[0]):
for j in range(ascii_array.shape[1]):
if j == 0 and i == 0:
ascii_string += "* "
elif j == 0 and i == ascii_array.shape[0] - 1:
ascii_string += "* "
elif j == 0:
ascii_string += "*|"
elif ascii_array[i][j] == "*":
ascii_string += "\t - *"
elif ascii_array[i][j] is None:
ascii_string += "\t . |"
else:
ascii_string += ascii_array[i][j] + "\t . |"
ascii_string += "\n"
# Print turn count
ascii_string += "Turn = {}".format(self.turn_count) + "\n"
# Print snake health
for idx, snake in enumerate(self.snakes.get_snakes()):
ascii_string += "{} = {}".format(string.ascii_lowercase[idx], snake.health) + "\n"
return ascii_string