-
Notifications
You must be signed in to change notification settings - Fork 105
/
Copy pathrpsls.py
145 lines (103 loc) · 3 KB
/
rpsls.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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
from random import choice
import gc
import radio
from microbit import *
CLASSIC_STORY = """
Scissors cuts Paper
Paper covers Rock
Rock crushes Scissors
"""
SHELDON_COOPER_STORY = """
Scissors cuts Paper
Paper covers Rock
Rock crushes Lizard
Lizard poisons Spock
Spock smashes Scissors
Scissors decapitates Lizard
Lizard eats Paper
Paper disproves Spock
Spock vaporizes Rock
(and as it always has) Rock crushes Scissors
"""
STORY = SHELDON_COOPER_STORY
# @formatter:off
SKETCHES = {
'Scissors': '* :'
' * :'
' *:'
' * :'
'* :',
'Paper': '*****:'
'* *:'
'* *:'
'* *:'
'*****:',
'Rock': ' *** :'
'*****:'
'*****:'
'*****:'
' *** :',
'Lizard': '** :'
' * :'
' * :'
' * :'
' **:',
'Spock': ' *** :'
'*****:'
'** **:'
'** **:'
'** **:',
}
# @formatter:on
def sketch_to_image(sketch):
return Image(sketch.replace(' ', '0').replace('*', '9'))
IMAGES = {name: sketch_to_image(sketch) for name, sketch in SKETCHES.items()}
def parse_story(story):
gc.collect()
sentences = [line.split()[-3:] for line in story.split('\n') if line.strip()]
actors = {s for s, v, o in sentences}
victims = {actor: {o for s, v, o in sentences if s == actor} for actor in actors}
return actors, victims
ACTORS, VICTIMS = parse_story(STORY)
def random_outcome():
return choice(list(ACTORS))
def expired(time):
return abs(running_time() - time) > 2000
def beats(actor, victim):
return victim in VICTIMS.get(actor, [])
def wait_for_outcome():
start = running_time()
while not expired(start):
received = radio.receive()
if received:
return received
def main():
last_received = 0
other_outcome = None
radio.on()
while True:
received = radio.receive()
if received:
other_outcome = received
last_received = running_time()
if not accelerometer.was_gesture('face down'):
continue
my_outcome = random_outcome()
radio.send(my_outcome)
if not other_outcome or expired(last_received):
other_outcome = wait_for_outcome()
if other_outcome:
sleep(1000)
image = IMAGES.get(my_outcome, Image.SAD)
if beats(my_outcome, other_outcome):
to_show = [Image('00000:'
'00000:'
'00000:'
'00000:'
'00000:'), image, image, image] * 5
else:
to_show = [image] * 20
display.show(to_show, delay=200, clear=True, wait=False)
other_outcome = None
if __name__ == '__main__':
main()