Skip to content

Commit

Permalink
Work on replay functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
Rayman committed Nov 18, 2023
1 parent c3734d3 commit b9f62b6
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 11 deletions.
14 changes: 12 additions & 2 deletions replay.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,22 @@ def main(match, compare, seed):
print('Start replay')
history = GameHistory.deserialize(doc)
state = State(history.initial_snakes, history.grid_size, RoundType.TURNS, history.initial_candies)
print(state)

printer = Printer()
printer.print(state)
print('here!!!')
for moves in history.history:
for event in state.do_moves(moves.items()):
for id_to_move_value in history.history:
print(id_to_move_value)
print(state.snakes)
moves = []
for s in state.snakes:
try:
move_value = id_to_move_value[s.id]
moves.append(s, move_value)
except KeyError:
pass
for event in state.do_moves(moves):
print_event(event, 'TODO')
print()

Expand Down
31 changes: 22 additions & 9 deletions snakes/game.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from enum import Enum, auto
from io import StringIO
from math import floor, inf
from pprint import pformat
from random import sample
from time import time
from traceback import print_exception
Expand Down Expand Up @@ -34,11 +35,18 @@ def __init__(self, grid_size, snakes, candies, history=None):
self.grid_size = grid_size
self.initial_candies = deepcopy(candies)
self.initial_snakes = deepcopy(snakes)
self.history = history if history is not None else [] # type: List[dict[int,Move]]

def log_moves(self, moves: Dict[int, Move]):
assert isinstance(moves, dict)
self.history.append(moves)
# History items can be:
# - dict from snake index to move
# - candy tuple
self.history = history if history is not None else [] # type: List[dict[int,Move] | Tuple[int, int]]

def log_moves(self, moves: List[Tuple[Snake, Move]]):
assert isinstance(moves, List)
logged_moves = {}
for snake, move in moves:
index = next(i for i, s in enumerate(self.initial_snakes) if s.id == snake.id)
logged_moves[index] = move
self.history.append(logged_moves)

def log_candy_spawn(self, candy: Tuple[int, int]):
assert isinstance(candy, tuple)
Expand All @@ -60,7 +68,6 @@ def serialize(self) -> Dict[str, str]:
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)
print(data)
# assert '\n' not in data
return data

Expand All @@ -78,7 +85,7 @@ def deserialize(data):
return GameHistory(grid_size, snakes, candies, history)

def __repr__(self):
return f'{self.__class__.__name__}(grid_size={self.grid_size}, snakes={self.initial_snakes}, history={self.history})'
return f'{self.__class__.__name__}(grid_size={self.initial_snakes}, snakes={self.initial_snakes}, history={self.history})'


class GameEvent:
Expand Down Expand Up @@ -205,8 +212,11 @@ def players_turn(self) -> Iterator[Snake]:
yield self.snakes[self.turn]

def do_moves(self, moves: List[Tuple[Snake, Move]]) -> Iterator[GameEvent]:
assert set(self.players_turn()) == {s for s, _ in moves}
self.history.log_moves({snake.id: move for snake, move in moves})
if __debug__:
should_move = set(self.players_turn())
has_moves = set(s for s, _ in moves)
assert should_move == has_moves, f'{should_move} == {has_moves}'
self.history.log_moves(moves)

# first, move the snakes and record which candies have been eaten
remove_candies = set()
Expand Down Expand Up @@ -301,6 +311,9 @@ def do_moves(self, moves: List[Tuple[Snake, Move]]) -> Iterator[GameEvent]:
self.scores[snake.id] = score
yield Finished(self)

def __repr__(self):
return f'{self.__class__.__name__}({pformat(self.__dict__, indent=2)})'


class Game:
def __init__(self, agents: Dict[int, Type],
Expand Down

0 comments on commit b9f62b6

Please sign in to comment.