-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame_engine.py
311 lines (264 loc) · 11.8 KB
/
game_engine.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
import asyncio
import copy
import cozmo
import time
from cozmo.util import degrees, distance_mm
import threading
from constants import (
START_CUBE,
TAP_CUBE,
COOP_CUBE,
DEFECT_CUBE,
RED_LIGHT,
GREEN_LIGHT,
SEA_LIGHT,
YELLOW_LIGHT,
BLUE_LIGHT,
#Score conditions
P_R,
P_O,
O_R,
O_O,
X_X,
COZMO_DEFECT,
COZMO_COOP,
PLAYER_DEFECT,
PLAYER_COOP)
from random import randint
thread_lock = threading.Condition()
class SpeedTapEngine:
MAX_INDEX = 5
def __init__(self, game_robot):
self.robot = game_robot
self.robot_cube = None
self.player_coop_cube = None
self.player_defect_cube = None
self.deal_hand_no = 0
self.tap_registry = []
self.result_track = []
self.robot_score = 0
self.player_score = 0
self.score_plan = None
self.strategy = None
self.robot_next_move = 0
self.player_move = None
def reset_deals(self):
self.deal_hand_no = 0
self.tap_registry = []
self.result_track = []
self.robot_score = 0
self.player_score = 0
def cozmo_setup_game(self, score_plan):
light_cube_list = [cozmo.objects.LightCube1Id, cozmo.objects.LightCube2Id ,cozmo.objects.LightCube3Id ]
self.score_plan = score_plan
try:
self.robot.set_head_angle(degrees(0)).wait_for_completed()
self.robot.move_lift(-3)
cube = self.robot.world.wait_for_observed_light_cube(timeout=60)
except asyncio.TimeoutError:
print("Didn't find a cube :-(")
return
# cozmo taps to select cube
try:
cube.start_light_chaser(START_CUBE)
self.robot.move_lift(3)
self.robot.go_to_object(cube, distance_mm(35.0)).wait_for_completed()
############################
#needs refining
self.robot.move_lift(-2)
time.sleep(.20)
self.robot.move_lift(2)
time.sleep(.20)
tapped_event = self.robot.world.wait_for(cozmo.objects.EvtObjectTapped,timeout=0.1)
##############################
except asyncio.TimeoutError:
pass
finally:
cube.stop_light_chaser()
cube.set_lights_off()
cube.set_lights(cozmo.lights.green_light)
self.robot_cube = cube
# Now player has to select their cube
other_cube_list = []
other_idx = 0
for lightcube_id in light_cube_list:
other_cube = self.robot.world.light_cubes.get(lightcube_id)
if other_cube.object_id == self.robot_cube.object_id:
# the robot's cube should not be lit
continue
if other_idx == 0:
other_cube.start_light_chaser(COOP_CUBE)
self.player_coop_cube = other_cube
other_idx += 1
else:
other_cube.start_light_chaser(DEFECT_CUBE)
self.player_defect_cube = other_cube
other_cube_list.append(other_cube)
try:
while len(other_cube_list) > 0:
tapped_event = self.robot.world.wait_for(cozmo.objects.EvtObjectTapped,timeout=120)
if tapped_event:
player_cube = tapped_event.obj
if player_cube.object_id == self.robot_cube.object_id:
print("Cannot take Cozmo's cube!!!")
elif player_cube not in other_cube_list:
continue
else:
player_cube.stop_light_chaser()
player_cube.set_lights_off()
other_cube_list.remove(player_cube)
#print("Player selected Cube: %d" % self.player_cube.object_id)
except asyncio.TimeoutError:
print("No-one tapped our cube :-(")
finally:
self.robot_cube.set_lights_off()
for other_cube in other_cube_list:
other_cube.stop_light_chaser()
other_cube.set_lights_off()
print(self.robot_cube)
print(self.player_coop_cube)
print(self.player_defect_cube)
return self.robot_cube, self.player_coop_cube, self.player_defect_cube
def deal_hand(self):
# Flash green three times and then show coop/defect colour
# cubes to player indicate ready for player to tap their choice
for i in range(0,3):
self.player_coop_cube.start_hand(GREEN_LIGHT)
self.player_defect_cube.start_hand(GREEN_LIGHT)
self.robot_cube.start_hand(GREEN_LIGHT)
time.sleep(0.5)
self.player_coop_cube.set_lights_off()
self.player_defect_cube.set_lights_off()
self.robot_cube.set_lights_off()
time.sleep(0.5)
self.player_coop_cube.start_hand(YELLOW_LIGHT)
self.player_defect_cube.start_hand(BLUE_LIGHT)
#Robot cube will change to red for Cozmo to set the colour
self.robot_cube.start_hand(RED_LIGHT)
#Enter empty score card to register taps
self.tap_registry.append([])
def register_tap(self, tap_type):
tap_registered = False
register_for = None
# lock tap_registry to prevent overwriting
try:
thread_lock.acquire()
self.tap_registry[self.deal_hand_no].append(tap_type)
tap_registered = True
except IndexError:
# Tap registered outside active life
pass
finally:
thread_lock.notify_all()
thread_lock.release()
if tap_registered:
if tap_type == PLAYER_DEFECT:
# Once player tap is registered both player cubes blink
# Out of action
cozmo.logger.info("PD : Player tap registered grab")
self.player_coop_cube.start_light_chaser(DEFECT_CUBE)
self.player_defect_cube.start_light_chaser(DEFECT_CUBE)
self.player_move = PLAYER_DEFECT
#self.player_coop_cube.start_light_chaser(TAP_CUBE)
#self.player_defect_cube.start_light_chaser(TAP_CUBE)
elif tap_type == PLAYER_COOP:
# Once player tap is registered both player cubes blink
# Out of action
cozmo.logger.info("PD : Player tap registered share")
self.player_coop_cube.start_light_chaser(COOP_CUBE)
self.player_defect_cube.start_light_chaser(COOP_CUBE)
self.player_move = PLAYER_COOP
#self.player_coop_cube.start_light_chaser(TAP_CUBE)
#self.player_defect_cube.start_light_chaser(TAP_CUBE)
elif tap_type == COZMO_DEFECT:
# Cozmo cube out of action
cozmo.logger.info("PD : Cozmo tap registered grab")
self.robot_cube.start_light_chaser(TAP_CUBE)
elif tap_type == COZMO_COOP:
# Cozmo cube out of action
cozmo.logger.info("PD : Cozmo tap registered share")
self.robot_cube.start_light_chaser(TAP_CUBE)
return tap_registered
def deactivate_current_deal(self):
try:
if self.player_move:
self.robot_next_move = self.player_move + 2
self.player_coop_cube.stop_light_chaser()
self.player_defect_cube.stop_light_chaser()
self.robot_cube.stop_light_chaser()
self.player_coop_cube.set_lights_off()
self.player_defect_cube.set_lights_off()
self.robot_cube.set_lights_off()
deal_no = self.deal_hand_no
self.deal_hand_no += 1
# Only tapped cubes will show red light chase
# for 2 seconds
if PLAYER_COOP in self.tap_registry[deal_no]:
self.player_coop_cube.start_light_chaser(COOP_CUBE)
self.player_defect_cube.start_light_chaser(COOP_CUBE)
elif PLAYER_DEFECT in self.tap_registry[deal_no]:
self.player_coop_cube.start_light_chaser(DEFECT_CUBE)
self.player_defect_cube.start_light_chaser(DEFECT_CUBE)
if COZMO_COOP in self.tap_registry[deal_no]:
self.robot_cube.start_light_chaser(COOP_CUBE)
else:
self.robot_cube.start_light_chaser(DEFECT_CUBE)
time.sleep(2)
# Switch to blue score light
#self.player_cube.start_hand(SEA_LIGHT)
#self.robot_cube.start_hand(SEA_LIGHT)
except IndexError:
# current deal is already inactive
pass
def score_last_deal(self, refresh = False):
"""
@param refresh: set to True for not having a running total
"""
deal_no = self.deal_hand_no - 1
if refresh:
self.robot_score = 0
self.player_score = 0
if PLAYER_DEFECT in self.tap_registry[deal_no]:
if COZMO_DEFECT in self.tap_registry[deal_no]:
self.result_track.append(P_R)
self.robot_score += self.score_plan[P_R][1]
self.player_score += self.score_plan[P_R][0]
else:
self.result_track.append(P_O)
self.robot_score += self.score_plan[P_O][1]
self.player_score += self.score_plan[P_O][0]
elif PLAYER_COOP in self.tap_registry[deal_no]:
if COZMO_DEFECT in self.tap_registry[deal_no]:
self.result_track.append(O_R)
self.robot_score += self.score_plan[O_R][1]
self.player_score += self.score_plan[O_R][0]
else:
self.result_track.append(O_O)
self.robot_score += self.score_plan[O_O][1]
self.player_score += self.score_plan[O_O][0]
else:
self.result_track.append(X_X)
cozmo.logger.info("PD : Missing data at deal %d" % self.deal_hand_no)
# Update screen on score
# A thread-safe implementation of Singleton pattern
# To be used as mixin or base class
class GoalStatements(object):
# use special name mangling for private class-level lock
# we don't want a global lock for all the classes that use Singleton
# each class should have its own lock to reduce locking contention
__lock = threading.Lock()
# private class instance may not necessarily need name-mangling
__instance = None
@classmethod
def instance(cls, *args):
if not cls.__instance:
with cls.__lock:
if not cls.__instance:
cls.__instance = cls(*args)
return cls.__instance
def __init__(self, score_plan):
self.statements = ["Goal : Try to get %s points in this game" % score_plan[0][0],
"Goal : Try to get %s points in this game" % score_plan[1][0],
"Goal : Try to get %s points in this game" % score_plan[2][0],
"Goal : Try to get %s points in this game" % score_plan[3][0],
"Ready for next one? Remember to tap on time!"]