Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Rayman committed Nov 24, 2023
1 parent 05e0121 commit f7a2b13
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 7 deletions.
3 changes: 2 additions & 1 deletion commandline.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ def main(snake1, snake2, rate, seed, start):
printer.print(game)
while True:
for event in game.update():
print_event(event, game.agents)
agent_names = {id: agent.name for id, agent in game.agents.items()}
print_event(event, agent_names)
printer.print(game)
if not isinf(rate):
sleep(1 / rate)
Expand Down
4 changes: 2 additions & 2 deletions replay.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ def main(match, compare, seed):
for doc in yaml.safe_load_all(match):
print('Start replay')
history = GameHistory.deserialize(doc)
agent_names = {i: name for i, name in enumerate(doc['agents'])}
state = State(history.initial_snakes, history.grid_size, RoundType.TURNS, history.initial_candies)
print(state)
print(history)

printer = Printer()
printer.print(state)
print('here!!!')
for id_to_move_value in history.history:
if isinstance(id_to_move_value, Tuple):
state.spawn_candy(*id_to_move_value)
Expand All @@ -36,7 +36,7 @@ def main(match, compare, seed):
except KeyError as e:
pass
for event in state.do_moves(moves):
print_event(event, 'TODO')
print_event(event, agent_names)
printer.print(state)


Expand Down
10 changes: 6 additions & 4 deletions snakes/game.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,14 @@ def __init__(self, state):
def snake_to_str(snake: Snake, agents: Dict[int, Bot]) -> str:
assert isinstance(snake, Snake), snake
assert isinstance(agents, dict), agents
assert isinstance(agents[snake.id], Bot), agents
return f'{agents[snake.id].name} ({snake.id})'
assert isinstance(agents[snake.id], str), agents
return f'{agents[snake.id]} ({snake.id})'


def print_event(event: GameEvent, agents: Dict[int, Bot]):
def print_event(event: GameEvent, agents: Dict[int, str]):
assert isinstance(event, GameEvent)
assert isinstance(agents, dict), agents
assert all(isinstance(a, Bot) for a in agents.values()), agents
assert all(isinstance(a, str) for a in agents.values()), agents

if isinstance(event, InvalidMove):
if isinstance(event.move_value, Exception):
Expand Down Expand Up @@ -506,6 +506,8 @@ def serialize(grid_size, candies, turn, snakes) -> str:

snakedata = []
for snake in snakes:
if snake is None:
continue
snakestr = f'{snake[0][0]},{snake[0][1]}'
for i in range(1, len(snake)):
direction = snake[i] - snake[i - 1]
Expand Down
2 changes: 2 additions & 0 deletions snakes/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ def print(self, game):
for candy in game.candies:
grid[candy[0], candy[1]] = '*'
for snake in game.snakes:
if snake is None:
continue
for pos in snake:
grid[pos[0], pos[1]] = number_to_circled(snake.id)

Expand Down

0 comments on commit f7a2b13

Please sign in to comment.