Skip to content

Commit

Permalink
Work on replay mode
Browse files Browse the repository at this point in the history
  • Loading branch information
Rayman committed Nov 18, 2023
1 parent b9f62b6 commit 55f5edb
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 13 deletions.
11 changes: 6 additions & 5 deletions replay.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,19 @@ def main(match, compare, seed):
printer.print(state)
print('here!!!')
for id_to_move_value in history.history:
print(id_to_move_value)
print(state.snakes)
print(f'id_to_move_value={id_to_move_value}')
print(f'snakes={state.snakes}')
moves = []
for s in state.snakes:
try:
move_value = id_to_move_value[s.id]
moves.append(s, move_value)
except KeyError:
moves.append((s, move_value))
except KeyError as e:
pass
print(f'moves={moves}')
for event in state.do_moves(moves):
print_event(event, 'TODO')
print()
printer.print(state)


if __name__ == '__main__':
Expand Down
35 changes: 27 additions & 8 deletions snakes/game.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,20 +65,20 @@ def serialize(self) -> Dict[str, str]:
io.write(' ')

data = {}
data['i'] = serialize(self.grid_size, self.initial_candies, 0, self.initial_snakes)
data['m'] = io.getvalue()
data = yaml.safe_dump(data, default_flow_style=True, width=inf)
data['initial'] = serialize(self.grid_size, self.initial_candies, 0, self.initial_snakes)
data['moves'] = io.getvalue()

# assert '\n' not in data
return data

@staticmethod
def deserialize(data):
grid_size, candies, turn, snakes = deserialize(data['i'])
grid_size, candies, turn, snakes = deserialize(data['initial'])
history = []
for moves_string in data['m'].split(' '):
for moves_string in data['moves'].split(' '):
moves = {}
for match in re.finditer(r'(\d+)([udlr])', moves_string):
id = match.group(1)
id = int(match.group(1))
move = MOVES['udlr'.index(match.group(2))]
moves[id] = move
history.append(moves)
Expand Down Expand Up @@ -438,8 +438,27 @@ def finished(self):
# if every snake has a score, the game is finished
return len(self.scores) == len(self.agents)

def _snake_to_string(self, snake: Snake) -> str:
return f'{self.agents[snake.id].name} ({snake.id})'
def save_replay(self) -> str:
data = self.state.history.serialize()

# add the name of the agents
agents = [None] * len(self.agents)
for id, agent in self.agents.items():
index = next(i for i, s in enumerate(self.state.history.initial_snakes) if s.id == id)
print(index)
print(agent)
agents[index] = agent.name
data['agents'] = agents

# add final ranking if needed
if self.finished():
ranking = [None] * len(self.state.history.initial_snakes)
for id, rank in self.rank().items():
index = next(i for i, s in enumerate(self.state.history.initial_snakes) if s.id == id)
ranking[index] = rank
data['rank'] = ranking

return yaml.safe_dump(data, default_flow_style=True, width=inf)


def direction_to_move_value(direction):
Expand Down

0 comments on commit 55f5edb

Please sign in to comment.