Created
December 17, 2019 18:57
-
-
Save chelseatroy/6df7dee91ff6b0968122a9136885345b to your computer and use it in GitHub Desktop.
Storing States as Dictionaries
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Initial state of traffic light | |
Init = { | |
'out1': 'G', | |
'out2': 'R', | |
'clock': 0, | |
'button': False, | |
'pc': 'G1' | |
} | |
# Define functions that determine state membership and state update. | |
# How to incorporate the button press? | |
G1 = lambda s: s['pc'] == 'G1' and ( | |
(s['clock'] < 30 and dict(s, clock=s['clock'] + 1)) | |
or (s['clock'] == 30 and dict(s, out1="Y", clock=0, pc="Y1")) | |
) | |
Y1 = lambda s: s['pc'] == 'Y1' and ( | |
(s['clock'] < 5 and dict(s, clock=s['clock'] + 1)) | |
or (s['clock'] == 5 and dict(s, out1='R', out2='G', clock=0, pc='G2')) | |
) | |
G2 = lambda s: s['pc'] == 'G2' and ( | |
((s['clock'] == 60 or (s['clock'] >= 30 and s['button'])) | |
and dict(s, out2="Y", clock=0, button=False, pc="Y2")) | |
or (s['clock'] < 60 and dict(s, clock=s['clock'] + 1)) | |
) | |
Y2 = lambda s: s['pc'] == 'Y2' and ( | |
(s['clock'] < 5 and dict(s, clock=s['clock'] + 1)) | |
or (s['clock'] == 5 and dict(s, out1='G', out2='R', clock=0, pc='G1')) | |
) | |
# Next state relationship | |
Next = lambda s: G1(s) or Y1(s) or G2(s) or Y2(s) | |
def run(): | |
s = Init | |
while s: | |
print(s) | |
s = Next(s) # State transition | |
# If here. There was no next state | |
print("DEADLOCK") | |
run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment