-
Notifications
You must be signed in to change notification settings - Fork 1
/
replay.py
executable file
·51 lines (41 loc) · 1.66 KB
/
replay.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/env python3
# Copyright 2023 Nobleo Technology B.V.
#
# SPDX-License-Identifier: Apache-2.0
from argparse import ArgumentParser, FileType
from typing import Tuple
import yaml
from snakes.game import RoundType, print_event, GameHistory, State
from snakes.utils import Printer
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)
printer = Printer()
printer.print(state)
for id_to_move_value in history.history:
if isinstance(id_to_move_value, Tuple):
state.spawn_candy(*id_to_move_value)
else:
moves = []
for s in state.snakes:
try:
move_value = id_to_move_value[s.id]
moves.append((s, move_value))
except KeyError as e:
pass
for event in state.do_moves(moves):
print_event(event, agent_names)
printer.print(state)
if __name__ == '__main__':
parser = ArgumentParser(description='Replay a match')
parser.add_argument('match', type=FileType('r'), help="Input match database")
parser.add_argument('--compare', help="Compare moves with another bot")
parser.add_argument('-s', '--seed', type=int, help='Random seed')
args = parser.parse_args()
try:
main(**vars(args))
except KeyboardInterrupt:
pass