-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstate.py
More file actions
122 lines (100 loc) · 2.2 KB
/
state.py
File metadata and controls
122 lines (100 loc) · 2.2 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
StateMachine.
"""
from random import random
__all__ = [
]
__author__ = "syntaxval"
__copyright__ = "copyright (c) 2017, syntaxval"
__version__ = "0.0.1"
__license__ = "BSD 2-Clause license"
transitions = {
'A' : {
'push_left' : 'A',
'push_right' : 'B',
'push_up': 'A',
'push_down' : 'D'
},
'B' : {
'push_left' : 'A',
'push_right' : 'C',
'push_up' : 'B',
'push_down' : 'E'
},
'C' : {
'push_left' : 'B',
'push_right' : 'C',
'push_up' : 'C',
'push_down' : 'F'
},
'D' : {
'push_left' : 'D',
'push_right' : 'E',
'push_up' : 'A',
'push_down' : 'G'
},
'E' : {
'push_left' : 'D',
'push_right' : 'F',
'push_up' : 'B',
'push_down' : 'H'
},
'F' : {
'push_left' : 'E',
'push_right' : 'F',
'push_up' : 'C',
'push_down' : 'I'
},
'G' : {
'push_left' : 'G',
'push_right' : 'H',
'push_up' : 'D',
'push_down' : 'G'
},
'H' : {
'push_left' : 'G',
'push_right' : 'I',
'push_up' : 'E',
'push_down' : 'H'
},
'I' : {
'push_left' : 'H',
'push_right' : 'I',
'push_up' : 'F',
'push_down' : 'I'
}
}
def move_left():
print("moving left...")
def move_right():
print("moving right...")
def move_up():
print("moving up...")
def move_down():
print("moving down...")
actions = {
'push_left': move_left,
'push_right': move_right,
'push_up': move_up,
'push_down': move_down
}
def do_action(state):
return actions[state]()
def transition (state, event):
return transitions[state][event]
def get_events():
return sorted(actions.keys(), key=lambda _: random())
# ...
if __name__ == "__main__":
""" Example run. """
initial_state = 'E'
current_state = initial_state
events = get_events()
print("Starting at: " + current_state)
for event in events:
current_state = transition(current_state, event)
do_action(event)
print("Now at: " + current_state)
print("Finished.")