-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatemachine.py
39 lines (32 loc) · 1.13 KB
/
statemachine.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
class StateMachine:
def __init__(self):
self.handlers = {}
self.startState = None
self.step_counter = 0
def add_state(self, name, handler):
self.handlers[name] = handler
def set_start(self, name):
self.startState = name
def run(self, cargo, num_steps):
self.step_counter = 0
try:
handler = self.handlers[self.startState]
except:
raise InitializationError("must call .set_start() before .run()")
visited = [self.startState]
while True:
(newState, cargo) = handler(cargo)
self.step_counter = self.step_counter + 1
visited.append(newState)
if self.step_counter == num_steps:
if newState == self.startState:
print("valid siteswap")
for state in visited:
print(state)
else:
print("invalid siteswap")
for state in visited:
print(state)
break
else:
handler = self.handlers[newState]