-
Notifications
You must be signed in to change notification settings - Fork 0
/
StateHistory.py
43 lines (36 loc) · 1.39 KB
/
StateHistory.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
from Utils import *
from GameState import GameState
from collections import deque
from copy import deepcopy
class StateHistory:
# Keeps a record of previous level states so that you can navigate
# back and forth (undoing and redoing moves)
# Also tracks where player is in the history so that player
# can undo a sequence of moves and branch off without losing
# history before that point.
def __init__(self, memory_length):
self.memory = deque([], memory_length)
self.active = 0
def add(self, state):
while self.active > 0:
# Memory forward in time from the point we were at should be deleted
self.memory.popleft()
self.active -= 1
# print(self.to_string())
self.memory.appendleft(deepcopy(state))
def back(self):
if self.active < len(self.memory) - 1:
self.active += 1
# print(self.to_string())
return deepcopy(self.memory[self.active])
def forward(self):
if self.active > 0:
self.active -= 1
# print(self.to_string())
return deepcopy(self.memory[self.active])
def to_string(self):
mem_strings = map(lambda x: str(x.player_position)+','+str(x.tape_end_position), self.memory)
return ' : '.join(mem_strings)
def forget_last_state(self):
# forget last state
self.memory.popleft()