-
Notifications
You must be signed in to change notification settings - Fork 0
/
chopsticks.py
412 lines (346 loc) · 13.5 KB
/
chopsticks.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
# -*- coding: utf-8 -*-
"""
Created on Tue Sep 15 13:28:56 2015
@author: Jonti
"""
import sys
import time
import serial
class Solver:
myLeftHand = 0
myRightHand = 0
theirLeftHand = 0
theirRightHand = 0
def __init__(self, myLeft, myRight, theirLeft, theirRight):
global myLeftHand
global myRightHand
global theirLeftHand
global theirRightHand
myLeftHand = myLeft
myRightHand = myRight
theirLeftHand = theirLeft
theirRightHand = theirRight
ser=serial.Serial(port='\\.\COM3', baudrate=9600, bytesize=serial.EIGHTBITS, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, timeout=1)
time.sleep(3)
serialOut = str(myLeft) + str(myRight) + str(6)
ser.write(serialOut)
ser.close()
time.sleep(2)
def normalMove(self, state):
global myLeftHand
global myRightHand
global theirLeftHand
global theirRightHand
print "pre start"
sys.stdout.flush()
print myLeftHand
sys.stdout.flush()
print "middle"
sys.stdout.flush()
print myRightHand
sys.stdout.flush()
print "state"
sys.stdout.flush()
print state
sys.stdout.flush()
print "end"
sys.stdout.flush()
if state == 7:
return
elif state == 2:
if myLeftHand > 0:
myLeftHand += theirRightHand
if myLeftHand > 4:
myLeftHand = 0
elif state == 3:
if myRightHand > 0:
myRightHand += theirRightHand
if myRightHand > 4:
myRightHand = 0
elif state == 4:
if myLeftHand > 0:
myLeftHand += theirLeftHand
if myLeftHand > 4:
myLeftHand = 0
elif state == 5:
if myRightHand > 0:
myRightHand += theirLeftHand
if myRightHand > 4:
myRightHand = 0
print "post start"
sys.stdout.flush()
print myLeftHand
sys.stdout.flush()
print "middle"
sys.stdout.flush()
print myRightHand
sys.stdout.flush()
print "end"
sys.stdout.flush()
ser=serial.Serial(port='\\.\COM3', baudrate=9600, bytesize=serial.EIGHTBITS, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, timeout=1)
time.sleep(2)
serialOut = str(myLeftHand) + str(myRightHand) + str(6)
ser.write(serialOut)
ser.close()
time.sleep(5)
def split(self, theirLeft,theirRight):
global theirLeftHand
global theirRightHand
print" split!!"
sys.stdout.flush()
if (theirLeft + theirRight) == (theirLeftHand + theirRightHand) :
theirLeftHand = theirLeft
theirRightHand = theirRight
ser=serial.Serial(port='\\.\COM3', baudrate=9600, bytesize=serial.EIGHTBITS, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, timeout=1)
time.sleep(2)
serialOut = str(myLeftHand) + str(myRightHand) + str(6)
ser.write(serialOut)
ser.close()
return True
else :
return False
def isValidMove(self, state):
global myLeftHand
global myRightHand
global theirLeftHand
global theirRightHand
#todo : check for valid splits
print "validity statecheck"
print state
if state == 3 and (myRightHand == 0 or theirRightHand == 0):
print "not allowed!!"
return False
elif state == 2 and (myLeftHand == 0 or theirRightHand == 0):
print "not allowed!!"
return False
elif state == 4 and (myLeftHand == 0 or theirLeftHand == 0):
print "not lol!!"
return False
elif state == 5 and (myRightHand == 0 or theirLeftHand == 0):
print "not allowed!!"
return False
else :
return True
def makeNextMove(self):
global myLeftHand
global myRightHand
global theirLeftHand
global theirRightHand
print "their left"
print theirLeftHand
print "their right"
print theirRightHand
outputState = 6
if myRightHand == 0 and myLeftHand == 0:
outputState = 8
elif theirRightHand == 0 and theirLeftHand == 0:
outputState = 7
elif theirLeftHand == 1 and theirRightHand ==1:
if (myLeftHand == 2 and myRightHand ==1 )or (myLeftHand == 1 and myRightHand ==2 ):
myLeftHand = 3
myRightHand = 0
outputState = 5
elif myLeftHand ==4 and myRightHand == 0:
myLeftHand = 3
myRightHand = 1
outputState = 5
elif myLeftHand == 4 and myRightHand == 1:
myLeftHand = 3
myRightHand = 2
outputState = 5
elif myLeftHand == 4 and myRightHand == 2 :
theirRightHand = 0
outputState = 2
elif myLeftHand == 3 and myRightHand == 3:
theirRightHand = 4
outputState = 3
elif (myLeftHand == 3 and myRightHand ==2 )or (myLeftHand == 2 and myRightHand ==3 ):
theirLeftHand = 4
if myLeftHand == 3:
outputState = 1
else:
outputState = 4
else:
print "error A "
elif (theirLeftHand ==2 and theirRightHand == 0) or (theirLeftHand ==0 and theirRightHand == 2) :
if myLeftHand == 1 and myRightHand == 1:
if theirLeftHand == 2:
theirLeftHand += myRightHand
outputState = 4
elif theirRightHand == 2:
theirRightHand += myLeftHand
outputState = 2
elif (myLeftHand == 3 and myRightHand == 0) or (myLeftHand == 0 and myRightHand == 3):
if myLeftHand == 3:
if theirLeftHand == 2:
theirLeftHand = 0
outputState = 1
else:
theirRightHand = 0
outputState = 2
else:
if theirLeftHand == 2:
theirLeftHand = 0
outputState = 4
else:
theirRightHand = 0
outputState = 3
else :
print "error Z"
elif (theirLeftHand == 3 and theirRightHand == 0) or (theirLeftHand ==0 and theirRightHand == 3):
if myLeftHand == 4:
if theirLeftHand == 3:
outputState == 1
else:
outputState == 2
elif myRightHand == 4:
if theirLeftHand == 3:
outputState == 4
theirLeftHand = 0
else:
outputState == 3
theirRightHand = 0
elif (myRightHand == 3 and myLeftHand == 0) or (myRightHand == 0 and myLeftHand == 3):
if theirLeftHand == 0:
theirRightHand = 0
outputState = 3
else:
theirLeftHand = 0
outputState = 4
else:
print "error Y"
elif (theirLeftHand == 2 and theirRightHand == 1) or (theirLeftHand == 1 and theirRightHand == 2):
if myLeftHand == 1 and myRightHand == 1 :
myLeftHand = 2
myRightHand = 0
outputState = 5
elif myLeftHand == 3 and myRightHand ==0:
if theirLeftHand == 2:
theirLeftHand = 0
outputState = 1
else:
theirRightHand = 0
outputState = 2
elif myLeftHand == 4 and myRightHand ==0:
myLeftHand = 2
myRightHand = 2
outputState = 5
elif (myLeftHand == 3 and myRightHand == 2) or (myLeftHand == 2 and myRightHand == 3):
if theirLeftHand == 2:
theirLeftHand == 0
if myLeftHand == 3:
outputState = 1
else:
outputState = 4
else:
theirRightHand = 0
if myLeftHand == 3:
outputState = 2
else:
outputState = 3
elif (myLeftHand == 4 and myRightHand == 2) or (myLeftHand == 2 and myRightHand == 4):
if theirLeftHand == 2:
theirLeftHand = 0
if myLeftHand == 4:
outputState = 1
else:
outputState = 4
else:
theirRightHand = 0
if myLeftHand == 4:
outputState = 2
else:
outputState = 3
elif myLeftHand == 3 and myRightHand == 3:
if theirLeftHand == 2:
theirLeftHand = 0
outputState = 4
else :
theirRightHand = 0
outputState = 3
else:
print "error D"
elif (theirRightHand == 4 and theirLeftHand == 0) or (theirRightHand == 0 and theirLeftHand == 4):
if myLeftHand > 0:
if theirRightHand == 4:
theirRightHand = 0
outputState = 2
else:
theirLeftHand = 0
outputState = 1
elif myRightHand > 0:
if theirRightHand == 4:
theirRightHand = 0
outputState = 3
else:
theirLeftHand = 0
outputState = 4
else:
print "error ast"
elif (theirLeftHand == 1 and theirRightHand == 0) or (theirLeftHand == 0 and theirRightHand == 1):
if myLeftHand == 4 :
if theirRightHand == 1:
outputState = 2
theirRightHand = 0
else:
outputState = 1
theirLeftHand = 0
elif myRightHand == 4 :
if theirRightHand == 1:
outputState = 3
theirRightHand = 0
else:
outputState = 4
theirLeftHand = 0
elif (myLeftHand ==2 and myRightHand ==1) or (myLeftHand ==1 and myRightHand ==2):
myRightHand =0
myLeftHand =3
outputState = 5
elif myLeftHand == 3 and myRightHand == 3:
myLeftHand = 4
myRightHand = 2
outputState = 5
elif myRightHand == 2 and myLeftHand == 0:
myLeftHand = 1
myRightHand = 1
outputState = 5
elif myLeftHand == 4 and myRightHand == 3 :
if theirLeftHand == 1:
theirLeftHand = 0
outputState = 1
else:
theirRightHand = 0
outputState = 2
else:
print "error Q"
elif (theirRightHand == 4 and theirLeftHand == 1) or (theirRightHand == 1 and theirLeftHand == 4):
if (myLeftHand == 3 and myRightHand == 0) or (myLeftHand == 0 and myRightHand == 3):
if theirRightHand == 1:
theirLeftHand = 0
if myLeftHand == 3:
outputState = 1
else:
outputState = 4
else:
theirRightHand = 0
if myLeftHand == 3:
outputState = 2
else:
outputState = 3
elif (myLeftHand == 4 and myRightHand ==3) or (myLeftHand == 3 and myRightHand == 4):
theirRightHand = 0
if myLeftHand == 4:
outputState = 2
else:
outputState = 3
else :
print "error T"
elif (theirRightHand ==3 and theirLeftHand == 2 )or (theirRightHand ==3 and theirLeftHand == 2 ):
if myLeftHand == 3 and myRightHand == 3:
if theirRightHand == 2:
theirRightHand = 0
outputState = 2
else:
theirLeftHand = 0
outputState = 4
return myLeftHand, myRightHand, outputState