-
Notifications
You must be signed in to change notification settings - Fork 74
/
cube.py
1275 lines (1088 loc) · 51.2 KB
/
cube.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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# cube.py
# Chris Barker
# CMU S13 15-112 Term Project
from Tkinter import *
from geometry import *
import heapq
import copy
import random
import solutions
import math
from math import sin, cos, pi
class Struct(object): pass
def loadObject(path, index):
with open(path) as file:
try: data = file.read()
except Exception as e:
print 'Error reading data!', e
return eval(data)[index]
def drawChevron(canvas, cx, cy, r):
coords = (cx - 0.3 * r, cy - 0.5 * r,
cx - 0.2 * r, cy - 0.5 * r,
cx + 0.3 * r, cy,
cx - 0.2 * r, cy + 0.5 * r,
cx - 0.3 * r, cy + 0.5 * r,
cx - 0.3 * r, cy + 0.4 * r,
cx + 0.2 * r, cy,
cx - 0.3 * r, cy - 0.4 * r)
canvas.create_polygon(*coords, fill='white', state='disabled')
def brief(L):
s = ''
for e in L:
s += str(e[0])
return s
def reversal(move):
if type(move) == tuple:
move = move[0]
if type(move) == str:
if "'" in move:
move = move[0]
else:
move = move + "'"
return move
def darken(color):
if color[0] != '#':
if color == 'white':
color = '#ffffff'
elif color == 'orange':
color = '#ffa500'
elif color == 'red':
color = '#ff0000'
elif color == 'blue':
color = '#0000ff'
elif color == 'green':
color = '#00ff00'
elif color == 'yellow':
color = '#ffff00'
else: return color
return darken(color)
else:
red = int(color[1:3], 16)
green = int(color[3:5], 16)
blue = int(color[5:7], 16)
red /= 2
green /= 2
blue /= 2
return '#%02x%02x%02x' % (red, green, blue)
class CenterPiece(object):
def __init__(self, vec, parent):
self.vec = vec
self.parent = parent
def callback(self, e):
self.parent.addMoves([self.vec], self.PLAYING)
class Cube(object):
directions = { I_HAT : 'green',
-I_HAT : 'blue',
J_HAT : 'red',
-J_HAT : 'orange',
K_HAT : 'yellow',
-K_HAT : 'white'}
helpStrings = {
'general': 'Welcome to Cubr!\nHover over a button below to view help for it.\n\n\
The Rubik\'s Cube, invented in 1974 by Erno Rubik, is one of the most popular toys of all time.\n\
It consists of six independently rotating faces, each with nine colored stickers.\n\
The goal is to arrange the cube so that each face contains only one color.\n\
In 1981 David Singmaster published his popular three-layer solution method, which is used in this program.\n\
With practice, most people could solve the cube in under a minute. Since then, speedcubing has taken off and the current record is held by \n\
Mats Valk, who solved the cube in 5.55 seconds. In 2010, Tomas Rokicki proved that any Rubik\'s cube can be solved in 20 face rotations or less.\n\n\
This program will interactively guide you through the three-layer solution algorithm.\n\
At each step of the solution, you will be given information describing the step you are completing.\n\
You may either work with a randomly generated Rubik\'s Cube, or use your webcam to input the current configuration of your own cube!\n\n\
Many people think of solving the cube as moving the 54 stickers into place. However, it is much more helpful to think about it as\n\
moving 20 "blocks" (12 edges, 8 corners) into place. The centers of each face always stay in the same orientation relative to each other,\n\
and the stickers on each block always stay in place relative to each other.\n\
Solving the first layer means getting four edges and four corners in place so that one face is all the same color.\n\
This is intuitive for many people, but by being conscious of the algorithms you use, you can improve your time and consistency.\n\
The second layer of blocks requires only one algorithm, and involves moving only four edge pieces into place.\n\
The third and final layer is the most complicated, and requires separate algorithms for orienting (getting the stickers facing the right way)\n\
and for permuting (getting the individual blocks into place). With enough practice, you can be an expert cube solver!\n\
',
'pause': 'During a guided solution, press this button to pause your progress.',
'play': 'During a guided solution, press this button to resume solving the cube.',
'reverse': 'During a guided solution, press this button to reverse the moves made so far.',
'back': 'Press this button to step one move backward.',
'step': 'Press this button to step one move forward.',
'speedUp': 'Press this button to increase the rotation speed during a guided solution.',
'slowDown': 'Press this button to decrease the rotation speed during a guided solution.',
'fromCamera': 'Press this button to start the camera and input the configuration of your Rubik\'s cube.\n\
Tip: When inputting your cube through the camera, tilt the cube up or down to reduce glare from the screen.\n\
More tips: If the program misrecognizes a color, press the spacebar anyway to record the colors. Then, click on the misrecognized\n\
color and select the correct color from the list of colors that will pop up. Make sure you copy the movement of the virtual cube when it\n\
rotates to the next face so that your cube will be interpreted accurately.',
'guide': 'guides through solution',
'guideFast': 'guides through solution more quickly',
'reset': 'resets the cube to a solved state',
'shuffle': 'shuffles the cube',
'solve': 'solves the cube',
'info': 'reopen this screen',
'stats': 'shows statistics'
}
faceColors = { }
@classmethod
def setFaceColors(cls):
cls.faceColors = {}
for z in xrange(3):
for y in xrange(3):
for x in xrange(3):
pieceId = z * 9 + y * 3 + x + 1
cls.faceColors[pieceId] = [ ]
(X, Y, Z) = (x - 1, y - 1, z - 1)
pos = Vector(X,Y,Z)
for vec in [Vector(0,0,1), Vector(0,1,0), Vector(1,0,0)]:
for direction in cls.directions:
if direction // vec:
if direction ** pos > 0:
cls.faceColors[pieceId].append(cls.directions[direction])
def __init__(self, canvas, controlPane, app, mode='solved'):
Cube.setFaceColors()
self.state = CubeState(mode)
self.faces = { }
self.size = 3
self.center = Vector(0,0,0)
self.app = app
(self.PAUSED, self.PLAYING, self.REVERSING, self.STEP, self.BACK) = (1,2,3,4,5)
self.status = self.PAUSED
(self.INGAME, self.SHOWINGINFO, self.SHOWINGSTATS) = range(3)
self.helpState = self.INGAME
self.statString = ''
self.helpIndex = 'general'
self.shuffling = False
self.delay = 100
self.direction = (0, 0)
self.after = 0
self.debug = False
self.message = ""
self.sol = ''
self.shuffleLen = 200
self.moveList = [ ]
self.moveIndex = -1
self.controlPane = controlPane
self.timeBetweenRotations = 0
self.timeUntilNextRotation = 0
self.rotating = False
self.rotationAxis = False
self.rotationDirection = False
self.rotationCount = 0
self.maxRot = 5
self.rotationQueue = [ ]
self.rotatingValues = [ ]
self.sensitivity = 0.04 # click and drag
self.showingPB = False
self.pbMin = 0
self.pbVal = 0
self.pbMax = 0
self.paused = False
self.configureControls(controlPane)
self.configureWindow(canvas)
self.showInWindow()
@property
def maxRot(self):
return self.maxRotationCount
@maxRot.setter
def maxRot(self, value):
self.maxRotationCount = value
self.rotationDTheta = math.pi / (2. * self.maxRotationCount)
@maxRot.deleter
def maxRot(self):
pass
def configureControls(self, pane):
pane.delete(ALL)
width = int(pane.cget('width'))
height = int(pane.cget('height'))
r = 16
#
# PAUSE
#
(cx, cy) = (width/2, height/2)
pauseButton = pane.create_oval(cx - r, cy - r, cx + r, cy + r,
fill='#0088ff', activefill='#00ffff',
outline='#ffffff', width=1, activewidth=3)
pane.tag_bind(pauseButton, '<Button-1>', self.pause)
pane.create_rectangle(cx - (r * 0.35), cy - (r * 0.5),
cx - (r * 0.10), cy + (r * 0.5), fill='#ffffff',
state='disabled')
pane.create_rectangle(cx + (r * 0.35), cy - (r * 0.5),
cx + (r * 0.10), cy + (r * 0.5), fill='#ffffff',
state='disabled')
pane.tag_bind(pauseButton, '<Enter>', lambda e: self.assignHelp('pause'))
pane.tag_bind(pauseButton, '<Leave>', lambda e: self.assignHelp('general'))
#
# PLAY
#
(cx, cy) = (width/2 + r*2.4, height/2)
playButton = pane.create_oval(cx - r, cy - r, cx + r, cy + r,
fill='#0088ff', activefill='#00ffff',
outline='#ffffff', width=1, activewidth=3)
pane.tag_bind(playButton, '<Button-1>', self.play)
pane.create_polygon(cx - r * 0.35, cy - r * 0.5,
cx + r * 0.55, cy,
cx - r * 0.35, cy + r * 0.5, fill='#ffffff',
state='disabled')
pane.tag_bind(playButton, '<Enter>', lambda e: self.assignHelp('play'))
pane.tag_bind(playButton, '<Leave>', lambda e: self.assignHelp('general'))
#
# REVERSE
#
(cx, cy) = (width/2 - r*2.4, height/2)
reverseButton = pane.create_oval(cx - r, cy - r, cx + r, cy + r,
fill='#0088ff', activefill='#00ffff',
outline='#ffffff', width=1, activewidth=3)
pane.tag_bind(reverseButton, '<Button-1>', self.reverse)
pane.create_polygon(cx + r * 0.35, cy - r * 0.5,
cx - r * 0.55, cy,
cx + r * 0.35, cy + r * 0.5, fill='#ffffff',
state='disabled')
pane.tag_bind(reverseButton, '<Enter>', lambda e: self.assignHelp('reverse'))
pane.tag_bind(reverseButton, '<Leave>', lambda e: self.assignHelp('general'))
#
# SPEED UP
#
(cx, cy) = (width/2 + r * 10.0, height/2)
speedUpButton = pane.create_rectangle(cx - r, cy - r, cx + r, cy + r,
fill='#0088ff', activefill='#00ffff',
outline='#ffffff', width=1, activewidth=3)
pane.tag_bind(speedUpButton, '<Button-1>', self.speedUp)
drawChevron(pane, cx, cy, r)
drawChevron(pane, cx - 0.3 * r, cy, r * 0.8)
drawChevron(pane, cx + 0.3 * r, cy, r * 1.2)
pane.tag_bind(speedUpButton, '<Enter>', lambda e: self.assignHelp('speedUp'))
pane.tag_bind(speedUpButton, '<Leave>', lambda e: self.assignHelp('general'))
#
# SLOW DOWN
#
(cx, cy) = (width/2 + r * 7.5, height/2)
slowDownButton = pane.create_rectangle(cx - r, cy - r, cx + r, cy + r,
fill='#0088ff', activefill='#00ffff',
outline='#ffffff', width=1, activewidth=3)
pane.tag_bind(slowDownButton, '<Button-1>', self.slowDown)
drawChevron(pane, cx - 0.3 * r, cy, r * 0.8)
drawChevron(pane, cx, cy, r)
pane.tag_bind(slowDownButton, '<Enter>', lambda e: self.assignHelp('slowDown'))
pane.tag_bind(slowDownButton, '<Leave>', lambda e: self.assignHelp('general'))
#
# SHUFFLE
#
(cx, cy) = (r * 1.5, height/2)
shuffleButton = pane.create_oval(cx - r, cy - r, cx + r, cy + r,
fill='#0088ff', activefill='#00ffff',
outline='#ffffff', width=1, activewidth=3)
pane.tag_bind(shuffleButton, '<Button-1>', self.shuffle)
coords = (cx - 0.6 * r, cy - 0.4 * r,
cx - 0.6 * r, cy - 0.2 * r,
cx - 0.2 * r, cy - 0.2 * r,
cx + 0.2 * r, cy + 0.4 * r,
cx + 0.6 * r, cy + 0.4 * r,
cx + 0.6 * r, cy + 0.6 * r,
cx + 0.8 * r, cy + 0.3 * r,
cx + 0.6 * r, cy - 0.0 * r,
cx + 0.6 * r, cy + 0.2 * r,
cx + 0.2 * r, cy + 0.2 * r,
cx - 0.2 * r, cy - 0.4 * r,
cx - 0.4 * r, cy - 0.4 * r)
pane.create_polygon(*coords, outline='#ffffff', fill='#0000ff', state='disabled')
coords = (cx - 0.6 * r, cy + 0.4 * r,
cx - 0.6 * r, cy + 0.2 * r,
cx - 0.2 * r, cy + 0.2 * r,
cx + 0.2 * r, cy - 0.4 * r,
cx + 0.6 * r, cy - 0.4 * r,
cx + 0.6 * r, cy - 0.6 * r,
cx + 0.8 * r, cy - 0.3 * r,
cx + 0.6 * r, cy - 0.0 * r,
cx + 0.6 * r, cy - 0.2 * r,
cx + 0.2 * r, cy - 0.2 * r,
cx - 0.2 * r, cy + 0.4 * r,
cx - 0.4 * r, cy + 0.4 * r)
pane.create_polygon(*coords, outline='#ffffff', fill='#0000ff', state='disabled')
pane.tag_bind(shuffleButton, '<Enter>', lambda e: self.assignHelp('shuffle'))
pane.tag_bind(shuffleButton, '<Leave>', lambda e: self.assignHelp('general'))
#
# SOLVE
#
(cx, cy) = (r * 4.0, height/2)
solveButton = pane.create_oval(cx - r, cy - r, cx + r, cy + r,
fill='#0088ff', activefill='#00ffff',
outline='#ffffff', width=1, activewidth=3)
pane.tag_bind(solveButton, '<Button-1>', self.solve)
pane.create_text(cx, cy, text='Solve', fill='white', state='disabled', font='Arial 10')
pane.tag_bind(solveButton, '<Enter>', lambda e: self.assignHelp('solve'))
pane.tag_bind(solveButton, '<Leave>', lambda e: self.assignHelp('general'))
#
# RESET
#
(cx, cy) = (r * 6.5, height/2)
resetButton = pane.create_oval(cx - r, cy - r, cx + r, cy + r,
fill='#0088ff', activefill='#00ffff',
outline='#ffffff', width=1, activewidth=3)
pane.tag_bind(resetButton, '<Button-1>', self.reset)
pane.create_text(cx, cy, text='Reset', fill='white', state='disabled', font='Arial 10')
pane.tag_bind(resetButton, '<Enter>', lambda e: self.assignHelp('reset'))
pane.tag_bind(resetButton, '<Leave>', lambda e: self.assignHelp('general'))
#
# FROM CAMERA
#
(cx, cy) = (r * 9.0, height/2)
fromcamButton = pane.create_oval(cx - r, cy - r, cx + r, cy + r,
fill='#0088ff', activefill='#00ffff',
outline='#ffffff', width=1, activewidth=3)
pane.tag_bind(fromcamButton, '<Button-1>', self.fromCamera)
pane.create_text(cx, cy-12, text='From', fill='white', state='disabled', font='Arial 9')
pane.create_text(cx, cy, text='Camera', fill='white', state='disabled', font='Arial 9')
pane.tag_bind(fromcamButton, '<Enter>', lambda e: self.assignHelp('fromCamera'))
pane.tag_bind(fromcamButton, '<Leave>', lambda e: self.assignHelp('general'))
#
# GUIDE
#
(cx, cy) = (r * 12.5, height/2)
guideButton = pane.create_rectangle(cx - 2*r, cy - r, cx + 2*r, cy + r,
fill='#0088ff', activefill='#00ffff',
outline='#ffffff', width=1, activewidth=3)
pane.tag_bind(guideButton, '<Button-1>', self.guideThrough)
pane.create_text(cx, cy-12, text='Guide Through', fill='white', state='disabled', font='Arial 8')
pane.create_text(cx, cy, text='Solution', fill='white', state='disabled', font='Arial 8')
pane.tag_bind(guideButton, '<Enter>', lambda e: self.assignHelp('guide'))
pane.tag_bind(guideButton, '<Leave>', lambda e: self.assignHelp('general'))
#
# GUIDE FASTER
#
(cx, cy) = (r * 17, height/2)
guideFastButton = pane.create_rectangle(cx - 2*r, cy - r, cx + 2*r, cy + r,
fill='#0088ff', activefill='#00ffff',
outline='#ffffff', width=1, activewidth=3)
pane.tag_bind(guideFastButton, '<Button-1>', self.guideFastThrough)
pane.create_text(cx, cy-12, text='Guide Through', fill='white', state='disabled', font='Arial 8')
pane.create_text(cx, cy, text='Solution (Faster)', fill='white', state='disabled', font='Arial 8')
pane.tag_bind(guideFastButton, '<Enter>', lambda e: self.assignHelp('guideFast'))
pane.tag_bind(guideFastButton, '<Leave>', lambda e: self.assignHelp('general'))
#
# BACK
#
r = 8
(cx, cy) = (width/2 - r*7.5, height/2)
backButton = pane.create_oval(cx - r, cy - r, cx + r, cy + r,
fill='#0088ff', activefill='#00ffff',
outline='#ffffff', width=1, activewidth=3)
pane.tag_bind(backButton, '<Button-1>', self.back)
pane.create_polygon(cx + r * 0.35, cy - r * 0.5,
cx - r * 0.55, cy,
cx + r * 0.35, cy + r * 0.5, fill='#ffffff',
state='disabled')
pane.tag_bind(backButton, '<Enter>', lambda e: self.assignHelp('back'))
pane.tag_bind(backButton, '<Leave>', lambda e: self.assignHelp('general'))
#
# FORWARD
#
(cx, cy) = (width/2 + r*7.5, height/2)
stepButton = pane.create_oval(cx - r, cy - r, cx + r, cy + r,
fill='#0088ff', activefill='#00ffff',
outline='#ffffff', width=1, activewidth=3)
pane.tag_bind(stepButton, '<Button-1>', self.step)
pane.create_polygon(cx - r * 0.35, cy - r * 0.5,
cx + r * 0.55, cy,
cx - r * 0.35, cy + r * 0.5, fill='#ffffff',
state='disabled')
pane.tag_bind(stepButton, '<Enter>', lambda e: self.assignHelp('step'))
pane.tag_bind(stepButton, '<Leave>', lambda e: self.assignHelp('general'))
#
# INFO
#
(cx, cy) = (width - r * 3.5, height/2)
helpButton = pane.create_rectangle(cx - 2*r, cy - r, cx + 2*r, cy + r,
fill='#0088ff', activefill='#00ffff',
outline='#ffffff', width=1, activewidth=3)
pane.tag_bind(helpButton, '<Button-1>', lambda e: self.assignHelpState(self.SHOWINGINFO))
pane.create_text(cx, cy, text='Help', fill='white', state='disabled')
pane.tag_bind(helpButton, '<Enter>', lambda e: self.assignHelp('info'))
pane.tag_bind(helpButton, '<Leave>', lambda e: self.assignHelp('general'))
#
# STATS
#
(cx, cy) = (width - r * 8.0, height/2)
statsButton = pane.create_rectangle(cx - 2*r, cy - r, cx + 2*r, cy + r,
fill='#0088ff', activefill='#00ffff',
outline='#ffffff', width=1, activewidth=3)
pane.tag_bind(statsButton, '<Button-1>', self.showStats)
pane.create_text(cx, cy, text='Stats', fill='white', state='disabled')
pane.tag_bind(statsButton, '<Enter>', lambda e: self.assignHelp('stats'))
pane.tag_bind(statsButton, '<Leave>', lambda e: self.assignHelp('general'))
def configureWindow(self, canvas):
if canvas == None:
self.root = Tk()
(self.width, self.height) = (450, 450)
self.canvas = Canvas(self.root, width=self.width, height=self.height, background='#333333')
self.needsLoop = True
else:
self.root = canvas._root()
self.canvas = canvas
(self.width, self.height) = (int(canvas.cget('width')), int(canvas.cget('height')))
self.needsLoop = False
self.dim = {'width': self.width, 'height': self.height}
def speedUp(self, e):
self.maxRot = max(1, self.maxRot - 1)
def slowDown(self, e):
self.maxRot += 1
def timer(self):
needsRedraw = self.move() or (not self.status == self.PAUSED)
if self.rotating:
self.rotationCount -= 1
if self.rotationCount <= 0:
self.rotating = False
self.rotatingValues = [ ]
self.state.rotate(self.rotationItem)
del self.rotationItem
needsRedraw = True
if self.timeUntilNextRotation > 0:
self.timeUntilNextRotation -= 1
if (not self.rotating) and (self.timeUntilNextRotation <= 0):
if (self.status == self.PLAYING) or (self.status == self.STEP):
if self.moveIndex >= (len(self.moveList) - 1):
self.status = self.PAUSED
self.updateMessage('')
self.shuffling = False
else:
self.moveIndex += 1
needsRedraw = self.makeMove(self.moveList[self.moveIndex],
animate = not self.shuffling,
render = not self.shuffling or (self.moveIndex % 20 == 0))
if (self.status == self.REVERSING) or (self.status == self.BACK):
if self.moveIndex < 0:
self.status = self.PAUSED
else:
needsRedraw = self.makeMove(reversal(self.moveList[self.moveIndex]))
self.moveIndex -= 1
if (self.status == self.STEP) or (self.status == self.BACK):
self.status = self.PAUSED
self.timeUntilNextRotation = self.timeBetweenRotations
if needsRedraw:
try:
self.redraw()
except:
self.updateMessage('Could not read cube.')
self.state.setSolved()
self.redraw()
def updateMessage(self, msg):
self.message = msg
def updateSol(self, msg):
self.sol = msg
def showInWindow(self):
self.canvas.pack()
self.camera = Camera(Vector(4,-6.5,-7), Vector(0,0,0), pi/5, self.dim)
self.amt = self.camera.sensitivity * self.camera.pos.dist(self.camera.origin)
self.redraw()
if self.needsLoop: root.mainloop()
def cleanup(self):
for pg in self.faces.values():
self.canvas.itemconfig(pg, state='hidden')
def move(self):
self.amt = self.camera.sensitivity * self.camera.pos.dist(self.camera.origin)
redraw = False
if self.direction != (0, 0):
self.camera.rotate(self.direction)
redraw = True
if self.app.resized:
self.app.dragVal = (0,0)
self.app.resized = False
redraw = True
elif self.app.dragVal != (0,0):
self.camera.rotate((-self.sensitivity * self.app.dragVal[0],
-self.sensitivity * self.app.dragVal[1]))
redraw = True
self.app.dragVal = (self.app.dragVal[0] * 0.7,
self.app.dragVal[1] * 0.7)
if self.app.dragVal[0] < 0.01 and self.app.dragVal[1] < 0.01:
self.app.dragVal = (0,0)
return redraw
@staticmethod
def corners(center, direction, *args):
if len(args) == 0:
if direction // Vector(0,1,0): # parallel
norm1 = Vector(1, 0, 0)
else: norm1 = Vector(0,1,0)
norm2 = 2 * direction * norm1
else: (norm1, norm2) = args
corners = [ ]
for coef1 in xrange(-1, 2, 2):
for coef2 in xrange(coef1, -2 * coef1, -2*coef1):
corner = center + (0.5 * norm1 * coef1 +
0.5 * norm2 * coef2)
corners.append(corner)
return corners
def pieceOffset(self, x, y, z):
z -= 1
y -= 1
x -= 1
return Vector(x,y,z)
def redraw(self):
self.canvas.delete(ALL)
# Top message
self.canvas.create_text(self.camera.width/2, 40, text=self.message, fill='white', font='Arial 13 bold')
# Bottom message
sol = self.sol
lineWidth = 100
margin = 15
y = self.camera.height - margin - 20
while len(sol) > 0:
self.canvas.create_text(self.camera.width/2,
y, text=sol[-lineWidth:], fill='white', font='Courier 12')
y -= margin
sol = sol[:-lineWidth]
# Progress bar
if self.showingPB:
w = (self.width * (self.moveIndex - self.pbMin + 1) /
(max(1, self.pbMax - self.pbMin)))
self.canvas.create_rectangle(0, self.height-20, w, self.height, fill='#00ff66')
toDraw = [ ]
for z in xrange(self.size):
for y in xrange(self.size):
for x in xrange(self.size):
try:
(pieceID, rotationKey) = self.state.state[z][y][x]
except:
pieceID = 1
rotationKey = 210
pieceCenter = self.center + self.pieceOffset(x, y, z)
outDirections = [d for d in Cube.directions if d**pieceCenter > 0]
sod = [ ] #sorted out directions
for od in outDirections:
if od // CubeState.keys[rotationKey / 100]:
sod.append(od)
for od in outDirections:
if od // CubeState.keys[(rotationKey / 10) % 10]:
sod.append(od)
for od in outDirections:
if od // CubeState.keys[rotationKey % 10]:
sod.append(od)
pieceRotation = Vector(0,0,0)
theta = 0.
if pieceID in self.rotatingValues:
oldCenter = pieceCenter
pieceOffset = pieceCenter - (pieceCenter > self.rotationAxis)
pieceRotation = self.rotationAxis * pieceOffset
theta = self.rotationDTheta * (self.maxRot - self.rotationCount)
if self.rotationDirection:
theta *= -1
pieceCenter = (pieceCenter > self.rotationAxis)
pieceCenter = pieceCenter + cos(theta) * pieceOffset
pieceCenter = pieceCenter + sin(theta) * pieceRotation
faceColors = Cube.faceColors[pieceID]
for direc, color in zip(sod, faceColors):
axes = ()
faceCenter = pieceCenter + (direc / 2)
if pieceID in self.rotatingValues:
if direc // self.rotationAxis:
faceCenter = pieceCenter + (direc / 2)
if self.rotationAxis // Vector(0,1,0):
axis0temp = Vector(1,0,0)
else:
axis0temp = Vector(0,1,0)
axis1temp = direc * axis0temp
axis0 = axis0temp * cos(theta) + axis1temp * sin(theta)
axis1 = axis0 * direc
axes = (axis0, axis1)
else:
perp = -1 * (direc * self.rotationAxis)
perp = perp ^ (direc.mag)
faceCenter = pieceCenter + (sin(theta) * (perp / 2) +
cos(theta) * (direc / 2))
axis0 = self.rotationAxis
axis1 = (faceCenter - pieceCenter) * self.rotationAxis * 2
axes = (axis0, axis1)
visible = (faceCenter - pieceCenter) ** (faceCenter - self.camera.pos) < 0
corners = self.corners(faceCenter, pieceCenter - faceCenter, *axes)
corners = [corner.flatten(self.camera) for corner in corners]
state = 'disabled' # if visible else 'hidden'
outline = '#888888' if visible else 'gray'
if not visible: color = 'gray'
a = 0 if visible else 1000
spec = (corners, color, state, outline)
toDraw.append(((pieceCenter-self.camera.pos).mag + a, spec))
#a = self.canvas.create_polygon(corners, fill=color,
# width=2, state=state, outline='#888888'
# #,activewidth=4, activefill=darken(color)
# )
if self.debug:
self.canvas.create_text(faceCenter.flatten(self.camera), text=str(pieceID))
#if pieceCenter.mag() == 1:
# b = CenterPiece(pieceCenter, self)
# self.canvas.tag_bind(a, '<Button-1>', b.callback)
"""
newCorners = ()
for corner in corners: newCorners += corner.flatten(self.camera)
if visible:
self.canvas.create_polygon(self.faces[(pieceID,color)], newCorners)
#self.canvas.itemconfig(self.faces[(pieceID,color)], state=state)
"""
toDraw.sort(lambda a,b: cmp(b,a))
for polygon in toDraw:
spec = polygon[1]
(corners, color, state, outline) = spec
self.canvas.create_polygon(corners, fill=color, width=2, state=state, outline=outline)
self.drawHelp()
def gatherStats(self):
self.statString = 'Unable to fetch solution logs.'
stats = None
try:
with open('solutionLogs.txt') as file:
stats = eval(file.read())
except: return
if stats is not None:
self.statString = ''
stats = [s.split(';') for s in stats]
moves = [stat[-1] for stat in stats] # Gets last element
moves = [mv[6:] for mv in moves] # Remove "Moves:"
moves = [int(mv) for mv in moves]
if len(moves) == 0:
self.statString += "No solutions generated yet."
return
self.statString += "%d solution%s logged.\n" % (len(moves), '' if len(moves)==1 else 's')
avgMoves = sum(moves)/len(moves)
self.statString += "Average number of 90 degree face rotations per solution: %d\n" % (avgMoves)
times = [stat[-2] for stat in stats] # gets 2nd to last element
times = [tm[6:-4] for tm in times] # removes "Time: " ... " sec"
times = [float(tm) for tm in times]
avgTime = sum(times)/(max(1, len(times)))
self.statString += "Average time needed to generate a solution: %0.4f seconds" % (avgTime)
def resetStats(self):
try:
with open('solutionLogs.txt', 'r+') as file:
file.seek(0) # beginning
file.truncate()
file.writelines(['[]'])
except: return
def showStats(self, *args):
self.gatherStats()
self.helpState = self.SHOWINGSTATS
def drawHelp(self):
## MAGIC NUMBERS EVERYWHERE
if self.helpState == self.SHOWINGINFO:
canvas = self.canvas
canvas.create_rectangle(100, 100, self.width-100, self.height-100,
fill='#888888', outline='#ccccff', width=4)
canvas.create_rectangle(110, 110, 140, 140, fill='#880000', activefill='#aa0000')
canvas.create_text(125, 125, text='X', fill='black', state='disabled')
canvas.create_rectangle(self.width/2-50, self.height-140,
self.width/2+50, self.height-110,
fill='#008800', activefill='#00aa00')
canvas.create_text(self.width/2, self.height-125, text='Start', fill='black', state='disabled')
canvas.create_text(self.width/2, 130, text="Welcome to Cubr!",
font='Arial 25 bold')
canvas.create_text(self.width/2, self.height/2, text=self.helpStrings[self.helpIndex], font ='Arial 8')
elif self.helpState == self.SHOWINGSTATS:
canvas = self.canvas
canvas.create_rectangle(100, 100, self.width-100, self.height-100,
fill='#888888', outline='#ccccff', width=4)
canvas.create_rectangle(110, 110, 140, 140, fill='#880000', activefill='#aa0000')
canvas.create_text(125, 125, text='X', fill='black', state='disabled')
canvas.create_rectangle(self.width/2-50, self.height-140,
self.width/2+50, self.height-110,
fill='#008800', activefill='#00aa00')
canvas.create_text(self.width/2, self.height-125, text='Back', fill='black', state='disabled')
canvas.create_rectangle(142, self.height-130, 168, self.height-115, fill='#aaffaa', activefill='#ffffff')
canvas.create_text(225, self.height-130, text="These statistics are generated dynamically.\nClick here to reset your data logs.", state='disabled', font = 'Arial 12')
canvas.create_text(self.width/2, self.height/2, text=self.statString, font='Arial 14 bold')
def click(self, event):
if self.helpState == self.SHOWINGINFO or self.helpState == self.SHOWINGSTATS:
if 110 < event.x < 140 and 110 < event.y < 140:
self.helpState = self.INGAME
self.redraw()
elif self.width/2-50 < event.x < self.width/2+50 and \
self.height-140 < event.y < self.height-110:
self.helpState = self.INGAME
self.redraw()
if self.helpState == self.SHOWINGSTATS:
if 147 < event.x < 178 and self.height-130 < event.y < self.height-115:
self.resetStats()
self.showStats()
self.redraw()
def assignHelp(self, key):
self.helpIndex = key
self.redraw()
def assignHelpState(self, state):
self.helpState = state
self.redraw()
def setConfig(self, config):
try:
self.state = CubeState('barebones')
if self.debug:
print self.state
# Modify the state to include [(color, direction), (color, direction), ...]
# And then parse pieceId and orientationKey out of that
def faceToAxis(face):
if self.debug:
print face
center = face[1][1]
axis = [vec for vec in Cube.directions if
Cube.directions[vec].lower() == center.lower()][0]
return axis
def setAxes(normal, known, dirString):
dirString = dirString.lower()
if dirString == 'up':
up = known
elif dirString == 'down':
up = known * -1
elif dirString == 'left':
up = (normal * known)
elif dirString == 'right':
up = (known * normal)
down = up * -1
left = (up * normal)
right = left * -1
return (up, down, left, right)
timesTouched = [[[0,0,0],[0,0,0],[0,0,0]],[[0,0,0],[0,0,0],[0,0,0]],[[0,0,0],[0,0,0],[0,0,0]]]
for faceInfo in config:
axis = faceToAxis(faceInfo.currentFace)
prevAxis = nextAxis = None
if faceInfo.prevFace:
prevAxis = faceToAxis(faceInfo.prevFace)
if faceInfo.nextFace:
nextAxis = faceToAxis(faceInfo.nextFace)
prevTurn = faceInfo.prevTurn
nextTurn = faceInfo.nextTurn
if self.debug:
print 'axis:', axis, Cube.directions[axis]
print 'prevAxis:', prevAxis,
if prevAxis:
print Cube.directions[prevAxis]
print 'nextAxis:', nextAxis,
if nextAxis:
print Cube.directions[nextAxis]
print 'prevTurn:', prevTurn
print 'nextTurn:', nextTurn
if prevTurn:
(up, down, left, right) = setAxes(axis, prevAxis, prevTurn)
elif nextTurn:
(up, down, left, right) = setAxes(axis, nextAxis * -1, nextTurn)
if self.debug:
print 'up:', up, Cube.directions[up]
print 'down:', down, Cube.directions[down]
print 'left:', left, Cube.directions[left]
print 'right:', right, Cube.directions[right]
for row in xrange(3):
for col in xrange(3):
pos = axis
pos = pos + (down * (row - 1))
pos = pos + (right * (col - 1))
(x, y, z) = pos.components
(x, y, z) = (int(x+1), int(y+1), int(z+1))
if self.debug:
print 'x,y,z', x, y, z,
print 'pos=', pos
timesTouched[z][y][x] += 1
cell = self.state.state[z][y][x]
if type(cell) == list:
cell.append((faceInfo.currentFace[row][col], axis))
if self.debug:
print 'state=', self.state
print 'times', timesTouched
# Cast each [ ] list to a ( ) tuple
# [(color,dir),(color,dir),(color,dir)] ----> (pieceId, orientationKey)
reverseZ = -1 if self.camera.view ** Vector(0,0,1) < 0 else 1
reverseY = -1 if self.camera.view ** Vector(0,1,0) < 0 else 1
reverseX = -1 if self.camera.view ** Vector(1,0,0) < 0 else 1
zRange = range(3)[::reverseZ]
yRange = range(3)[::reverseY]
xRange = range(3)[::reverseX]
for z in zRange:
for y in yRange:
for x in xRange:
cell = self.state.state[z][y][x]
if type(cell) == list:
pieceId = -1
colors = set()
for i in cell:
colors.add(i[0])
for key in Cube.faceColors:
if set(Cube.faceColors[key]) == colors:
pieceId = key
break
if pieceId >= 0:
desiredColorOrder = Cube.faceColors[pieceId]
currentOrder = [ ]
ori = 0
notAdded = set([0,1,2])
cell.sort(lambda a,b: cmp(desiredColorOrder.index(a[0]),
desiredColorOrder.index(b[0])))
for i in cell:
ori *= 10
if i[1] // Vector(0,0,1):
ori += 2
notAdded.discard(2)
elif i[1] // Vector(0,1,0):
ori += 1
notAdded.discard(1)
elif i[1] // Vector(1,0,0):
ori += 0
notAdded.discard(0)
while len(notAdded) > 0:
ori *= 10
ori += notAdded.pop()
orientationKey = ori
else:
raise ValueError('Invalid Cube')
if pieceId in (5, 11, 13, 14, 15, 17, 23):
raise ValueError('Invalid Cube') # Center piece
desired = Cube.faceColors[CubeState.solvedState[z][y][x][0]]
if self.debug:
print 'The piece with colors %s is at the position of %s' % (colors, desired)
print 'setting (%d,%d,%d) to (%s, %s)' % (z,y,x,pieceId,orientationKey)
self.state.state[z][y][x] = (pieceId, orientationKey)
except:
self.updateMessage('Unable to read camera input.')
self.state.setSolved()
self.redraw()
if self.debug:
print 'final state=', self.state
self.redraw()
def addMoves(self, moves, status=-1):
self.moveList[self.moveIndex+1:] = [ ]
self.moveList.extend(moves)
if status != -1:
self.status = status
def rotate(self, axis):
self.showingPB = False
self.addMoves([axis], self.PLAYING)
def makeMove(self, move, render=True, animate=True):
if type(move) == tuple:
self.updateMessage(move[1])
axis = move[0]
else:
axis = move
self.rotationItem = self.state.rotationInfo(axis)
if animate:
self.rotating = True