-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathcubes.js
More file actions
executable file
·1804 lines (1326 loc) · 47.7 KB
/
cubes.js
File metadata and controls
executable file
·1804 lines (1326 loc) · 47.7 KB
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
/*
CUBES
A Cube is composed of 27 Cubelets (3x3x3 grid) numbered 0 through 26.
Cubelets are numbered beginning from the top-left-forward corner of the
Cube and proceeding left to right, top to bottom, forward to back:
-----------------------
/ 18 19 20 /|
/ / |
/ 9 10 11 / 20
/ / |
/ 0 1 2 / 11 |
----------------------- 23
| |2 |
| 0 1 2 | 14 |
| | 26
| |5 |
| 3 4 5 | 17 /
| | /
| |8 /
| 6 7 8 | /
| | /
-----------------------
Portions of the Cube are grouped (Groups):
this.core
this.centers
this.edges
this.corners
this.crosses
Portions of the Cube are grouped and rotatable (Slices):
Rotatable around the Z axis:
this.front
this.standing
this.back
Rotatable around the X axis:
this.left
this.middle
this.right
Rotatable around the Y axis:
this.up
this.equator
this.down
A Cube may be inspected through its Faces (see Slices for more
information on Faces vs Slices). From the browser's JavaScript console:
this.inspect()
This will reveal each Face's Cubelet indexes and colors using the Face's
compact inspection mode. The non-compact mode may be accessed by passing
a non-false value as an argument:
this.inspect( true )
*/
function Cube( preset ){
// Important for working around lexical closures in things like
// forEach() or setTimeout(), etc which change the scope of "this".
var cube = this
// Some important booleans.
this.isReady = true
this.isShuffling = false
this.isRotating = false
this.isSolving = false
// Every fire of this.loop() will attempt to complete our tasks
// which can only be run if this.isReady === true.
this.taskQueue = new Queue()
// We need the ability to gang up twist commands.
// Every fire of this.loop() will attempt to empty it.
this.twistQueue = new Queue( Twist.validate )
// How long should a Cube.twist() take?
this.twistDuration = SECOND
// If we shuffle, how shall we do it?
this.shuffleMethod = this.PRESERVE_LOGO
// Size matters? Cubelets will attempt to read these values.
this.size = 420
this.cubeletSize = 140
// We need to create and setup a new CSS3 Object
// to represent our Cube.
// THREE will take care of attaching it to the DOM, etc.
if( erno.renderMode === 'css' ){
this.domElement = document.createElement( 'div' )
this.domElement.classList.add( 'cube' )
this.threeObject = new THREE.CSS3DObject( this.domElement )
}
else if( erno.renderMode === 'svg' ){
this.threeObject = new THREE.Object3D()
}
this.threeObject.rotation.set(
( 25 ).degreesToRadians(),
( -30 ).degreesToRadians(),
0
)
scene.add( this.threeObject )
// If we enable Auto-Rotate then the cube will spin (not twist!) in space
// by adding the following values to the Three object on each frame.
this.rotationDeltaX = 0.1
this.rotationDeltaY = 0.15
this.rotationDeltaZ = 0
// Here's the first big map we've come across in the program so far.
// Imagine you're looking at the Cube straight on so you only see the front face.
// We're going to map that front face from left to right (3), and top to bottom (3):
// that's 3 x 3 = 9 Cubelets.
// But then behind the Front slice we also have a Standing slice (9) and Back slice (9),
// so that's going to be 27 Cubelets in total to create a Cube.
this.cubelets = []
;([
// Front slice
[ W, O, , , G, ], [ W, O, , , , ], [ W, O, B, , , ],// 0, 1, 2
[ W, , , , G, ], [ W, , , , , ], [ W, , B, , , ],// 3, 4, 5
[ W, , , R, G, ], [ W, , , R, , ], [ W, , B, R, , ],// 6, 7, 8
// Standing slice
[ , O, , , G, ], [ , O, , , , ], [ , O, B, , , ],// 9, 10, 11
[ , , , , G, ], [ , , , , , ], [ , , B, , , ],// 12, XX, 14
[ , , , R, G, ], [ , , , R, , ], [ , , B, R, , ],// 15, 16, 17
// Back slice
[ , O, , , G, Y ], [ , O, , , , Y ], [ , O, B, , , Y ],// 18, 19, 20
[ , , , , G, Y ], [ , , , , , Y ], [ , , B, , , Y ],// 21, 22, 23
[ , , , R, G, Y ], [ , , , R, , Y ], [ , , B, R, , Y ] // 24, 25, 26
]).forEach( function( cubeletColorMap, cubeletId ){
cube.cubelets.push( new Cubelet( cube, cubeletId, cubeletColorMap ))
})
// Mapping the Cube creates all the convenience shortcuts
// that we will need later. (Demonstrated immediately below!)
this.map()
// Now that we have mapped faces we can create faceLabels
if( erno.renderMode === 'css' ){
this.faces.forEach( function( face, i ){
var labelElement = document.createElement( 'div' )
labelElement.classList.add( 'faceLabel' )
labelElement.classList.add( 'face'+ face.face.capitalize() )
labelElement.innerHTML = face.face.toUpperCase()
cube.domElement.appendChild( labelElement )
})
}
// We need to map our folds separately from Cube.map()
// because we only want folds mapped at creation time.
// Remapping folds with each Cube.twist() would get weird...
this.folds = [
new Fold( this.front, this.right ),
new Fold( this.left, this.up ),
new Fold( this.down, this.back )
]
// Enable some "Hero" text for this Cube.
if( erno.renderMode === 'css' ){
this.setText( 'BEYONDRUBIKs CUBE', 0 )
this.setText( 'BEYONDRUBIKs CUBE', 1 )
this.setText( 'BEYONDRUBIKs CUBE', 2 )
}
// Shall we load some presets here?
preset = 'preset' + preset.capitalize()
if( this[ preset ] instanceof Function === false ) preset = 'presetBling'
this[ preset ]()
// Get ready for major loop-age.
// Our Cube checks these booleans at roughly 60fps.
setInterval( cube.loop, 16 )
// Enable key commands for our Cube.
$( document ).keypress( function( event ){
if( $( 'input:focus, textarea:focus' ).length === 0 ){
var key = String.fromCharCode( event.which )
if( 'XxRrMmLlYyUuEeDdZzFfSsBb'.indexOf( key ) >= 0 ) cube.twistQueue.add( key )
}
})
}
setupTasks = window.setupTasks || []
setupTasks.push( function(){
Cube.prototype = Object.create( Group.prototype )
Cube.prototype.constructor = Cube
forceAugment( Cube, {
// A Rubik's Cube is composed of 27 cubelets arranged 3 x 3 x 3.
// We need a map that relates these 27 locations to the 27 cubelets
// such that we can ask questions like:
// What colors are on the Front face of the cube? Etc.
map: function(){
var that = this, i
// Groups are simple collections of Cubelets.
// Their position and rotation is irrelevant.
this.core = new Group()
this.centers = new Group()
this.edges = new Group()
this.corners = new Group()
this.crosses = new Group()
this.cubelets.forEach( function( cubelet, index ){
if( cubelet.type === 'core' ) that.core.add( cubelet )
if( cubelet.type === 'center' ) that.centers.add( cubelet )
if( cubelet.type === 'edge' ) that.edges.add( cubelet )
if( cubelet.type === 'corner' ) that.corners.add( cubelet )
if( cubelet.type === 'center' || cubelet.type === 'edge' ) that.crosses.add( cubelet )
})
// Slices that can rotate about the X-axis:
this.left = new Slice(
this.cubelets[ 24 ], this.cubelets[ 21 ], this.cubelets[ 18 ],
this.cubelets[ 15 ], this.cubelets[ 12 ], this.cubelets[ 9 ],
this.cubelets[ 6 ], this.cubelets[ 3 ], this.cubelets[ 0 ]
)
this.left.name = 'left'
this.middle = new Slice(
this.cubelets[ 25 ], this.cubelets[ 22 ], this.cubelets[ 19 ],
this.cubelets[ 16 ], this.cubelets[ 13 ], this.cubelets[ 10 ],
this.cubelets[ 7 ], this.cubelets[ 4 ], this.cubelets[ 1 ]
)
this.middle.name = 'middle'
this.right = new Slice(
this.cubelets[ 2 ], this.cubelets[ 11 ], this.cubelets[ 20 ],
this.cubelets[ 5 ], this.cubelets[ 14 ], this.cubelets[ 23 ],
this.cubelets[ 8 ], this.cubelets[ 17 ], this.cubelets[ 26 ]
)
this.right.name = 'right'
// Slices that can rotate about the Y-axis:
this.up = new Slice(
this.cubelets[ 18 ], this.cubelets[ 19 ], this.cubelets[ 20 ],
this.cubelets[ 9 ], this.cubelets[ 10 ], this.cubelets[ 11 ],
this.cubelets[ 0 ], this.cubelets[ 1 ], this.cubelets[ 2 ]
)
this.up.name = 'up'
this.equator = new Slice(
this.cubelets[ 21 ], this.cubelets[ 22 ], this.cubelets[ 23 ],
this.cubelets[ 12 ], this.cubelets[ 13 ], this.cubelets[ 14 ],
this.cubelets[ 3 ], this.cubelets[ 4 ], this.cubelets[ 5 ]
)
this.equator.name = 'equator'
this.down = new Slice(
this.cubelets[ 8 ], this.cubelets[ 17 ], this.cubelets[ 26 ],
this.cubelets[ 7 ], this.cubelets[ 16 ], this.cubelets[ 25 ],
this.cubelets[ 6 ], this.cubelets[ 15 ], this.cubelets[ 24 ]
)
this.down.name = 'down'
// Slices are Groups with purpose; they are rotate-able!
// These are Slices that can rotate about the Z-axis:
this.front = new Slice(
this.cubelets[ 0 ], this.cubelets[ 1 ], this.cubelets[ 2 ],
this.cubelets[ 3 ], this.cubelets[ 4 ], this.cubelets[ 5 ],
this.cubelets[ 6 ], this.cubelets[ 7 ], this.cubelets[ 8 ]
)
this.front.name = 'front'
this.standing = new Slice(
this.cubelets[ 9 ], this.cubelets[ 10 ], this.cubelets[ 11 ],
this.cubelets[ 12 ], this.cubelets[ 13 ], this.cubelets[ 14 ],
this.cubelets[ 15 ], this.cubelets[ 16 ], this.cubelets[ 17 ]
)
this.standing.name = 'standing'
this.back = new Slice(
this.cubelets[ 26 ], this.cubelets[ 23 ], this.cubelets[ 20 ],
this.cubelets[ 25 ], this.cubelets[ 22 ], this.cubelets[ 19 ],
this.cubelets[ 24 ], this.cubelets[ 21 ], this.cubelets[ 18 ]
)
this.back.name = 'back'
// Faces .... special kind of Slice!
this.faces = [ this.front, this.up, this.right, this.down, this.left, this.back ]
// Good to let each Cubelet know where it exists
// in relationship to our full Cube.
for( i = 0; i < this.cubelets.length; i ++ ){
this.cubelets[ i ].setAddress( i )
}
},
// We can read and write text to the Cube.
// This is handled by Folds which are composed of two Faces.
getText: function( fold ){
if( fold === undefined ){
return [
this.folds[ 0 ].getText(),
this.folds[ 1 ].getText(),
this.folds[ 2 ].getText()
]
}
else if( isNumeric( fold ) && fold >= 0 && fold <= 2 ){
return this.folds[ fold ].getText()
}
},
setText: function( text, fold ){
if( fold === undefined ){
this.folds[ 0 ].setText( text )
this.folds[ 1 ].setText( text )
this.folds[ 2 ].setText( text )
}
else if( isNumeric( fold ) && fold >= 0 && fold <= 2 ){
this.folds[ fold ].setText( text )
}
},
// We'll inspect the Cube by specifically inspecting the Faces.
// Bear in mind this is merely one way to think about the Cube
// and does require some redundancy in terms of Cubelet indexes.
// Here we'll default to 'compact' mode in order to give the
// full Cube overview in the least amount of space.
inspect: function( compact, side ){
compact = !compact
this.front.inspect( compact, side )
this.up.inspect( compact, side )
this.right.inspect( compact, side )
this.down.inspect( compact, side )
this.left.inspect( compact, side )
this.back.inspect( compact, side )
},
solve: function(){
this.isSolving = true
},
isSolved: function(){
return (
this.front.isSolved( FRONT ) &&
this.up.isSolved( UP ) &&
this.right.isSolved( RIGHT ) &&
this.down.isSolved( DOWN ) &&
this.left.isSolved( LEFT ) &&
this.back.isSolved( BACK )
)
},
twist: function( twist ){
var onTwistComplete
if( twist instanceof Twist && !cube.isTweening() ){
command = twist.command
degrees = twist.degrees
if( erno.verbosity >= 0.8 ){
console.log(
'Executing a twist command to rotate the '+
twist.group +' '+ twist.wise +' by',
twist.degrees, 'degrees.'
)
}
// X-axis rotations
if( command === 'X' && !cube.isEngagedY() && !cube.isEngagedZ() ){
onTwistComplete = function( swap ){
cube.cubelets = [
swap[ 6 ], swap[ 7 ], swap[ 8 ],
swap[ 15 ], swap[ 16 ], swap[ 17 ],
swap[ 24 ], swap[ 25 ], swap[ 26 ],
swap[ 3 ], swap[ 4 ], swap[ 5 ],
swap[ 12 ], swap[ 13 ], swap[ 14 ],
swap[ 21 ], swap[ 22 ], swap[ 23 ],
swap[ 0 ], swap[ 1 ], swap[ 2 ],
swap[ 9 ], swap[ 10 ], swap[ 11 ],
swap[ 18 ], swap[ 19 ], swap[ 20 ]
]
}
if( degrees === undefined ) degrees = cube.getDistanceToPeg( 'X' )
cube.cubelets.forEach( function( cubelet, i ){
if( i === cube.cubelets.length - 1 ) cubelet.rotate( 'X', degrees, onTwistComplete )
else cubelet.rotate( 'X', degrees )
})
}
else if( command === 'x' && !cube.isEngagedY() && !cube.isEngagedZ() ){
onTwistComplete = function( swap ){
cube.cubelets = [
swap[ 18 ], swap[ 19 ], swap[ 20 ],
swap[ 9 ], swap[ 10 ], swap[ 11 ],
swap[ 0 ], swap[ 1 ], swap[ 2 ],
swap[ 21 ], swap[ 22 ], swap[ 23 ],
swap[ 12 ], swap[ 13 ], swap[ 14 ],
swap[ 3 ], swap[ 4 ], swap[ 5 ],
swap[ 24 ], swap[ 25 ], swap[ 26 ],
swap[ 15 ], swap[ 16 ], swap[ 17 ],
swap[ 6 ], swap[ 7 ], swap[ 8 ]
]
}
if( degrees === undefined ) degrees = cube.getDistanceToPeg( 'x' )
cube.cubelets.forEach( function( cubelet, i ){
if( i === cube.cubelets.length - 1 ) cubelet.rotate( 'x', degrees, onTwistComplete )
else cubelet.rotate( 'x', degrees )
})
}
else if( command === 'R' && !cube.right.isEngagedY() && !cube.right.isEngagedZ() ){
onTwistComplete = function( swap ){
cube.cubelets[ 2 ] = swap[ 8 ]
cube.cubelets[ 11 ] = swap[ 5 ]
cube.cubelets[ 20 ] = swap[ 2 ]
cube.cubelets[ 5 ] = swap[ 17 ]
cube.cubelets[ 23 ] = swap[ 11 ]
cube.cubelets[ 8 ] = swap[ 26 ]
cube.cubelets[ 17 ] = swap[ 23 ]
cube.cubelets[ 26 ] = swap[ 20 ]
}
if( degrees === undefined ) degrees = cube.right.getDistanceToPeg( 'X' )
cube.right.cubelets.forEach( function( cubelet, i ){
if( i === cube.right.cubelets.length - 1 ) cubelet.rotate( 'X', degrees, onTwistComplete )
else cubelet.rotate( 'X', degrees )
})
}
else if( command === 'r' && !cube.right.isEngagedY() && !cube.right.isEngagedZ() ){
onTwistComplete = function( swap ){
cube.cubelets[ 2 ] = swap[ 20 ]
cube.cubelets[ 11 ] = swap[ 23 ]
cube.cubelets[ 20 ] = swap[ 26 ]
cube.cubelets[ 5 ] = swap[ 11 ]
cube.cubelets[ 23 ] = swap[ 17 ]
cube.cubelets[ 8 ] = swap[ 2 ]
cube.cubelets[ 17 ] = swap[ 5 ]
cube.cubelets[ 26 ] = swap[ 8 ]
}
if( degrees === undefined ) degrees = cube.right.getDistanceToPeg( 'x' )
cube.right.cubelets.forEach( function( cubelet, i ){
if( i === cube.right.cubelets.length - 1 ) cubelet.rotate( 'x', degrees, onTwistComplete )
else cubelet.rotate( 'x', degrees )
})
}
else if( command === 'M' && !cube.middle.isEngagedY() && !cube.middle.isEngagedZ() ){
onTwistComplete = function( swap ){
cube.cubelets[ 1 ] = swap[ 19 ]
cube.cubelets[ 10 ] = swap[ 22 ]
cube.cubelets[ 19 ] = swap[ 25 ]
cube.cubelets[ 4 ] = swap[ 10 ]
cube.cubelets[ 22 ] = swap[ 16 ]
cube.cubelets[ 7 ] = swap[ 1 ]
cube.cubelets[ 16 ] = swap[ 4 ]
cube.cubelets[ 25 ] = swap[ 7 ]
}
if( degrees === undefined ) degrees = cube.middle.getDistanceToPeg( 'x' )
cube.middle.cubelets.forEach( function( cubelet, i ){
if( i === cube.middle.cubelets.length - 1 ) cubelet.rotate( 'x', degrees, onTwistComplete )
else cubelet.rotate( 'x', degrees )
})
}
else if( command === 'm' && !cube.middle.isEngagedY() && !cube.middle.isEngagedZ() ){
onTwistComplete = function( swap ){
cube.cubelets[ 1 ] = swap[ 7 ]
cube.cubelets[ 10 ] = swap[ 4 ]
cube.cubelets[ 19 ] = swap[ 1 ]
cube.cubelets[ 4 ] = swap[ 16 ]
cube.cubelets[ 22 ] = swap[ 10 ]
cube.cubelets[ 7 ] = swap[ 25 ]
cube.cubelets[ 16 ] = swap[ 22 ]
cube.cubelets[ 25 ] = swap[ 19 ]
}
if( degrees === undefined ) degrees = cube.middle.getDistanceToPeg( 'X' )
cube.middle.cubelets.forEach( function( cubelet, i ){
if( i === cube.middle.cubelets.length - 1 ) cubelet.rotate( 'X', degrees, onTwistComplete )
else cubelet.rotate( 'X', degrees, onTwistComplete )
})
}
else if( command === 'L' && !cube.left.isEngagedY() && !cube.left.isEngagedZ() ){
onTwistComplete = function( swap ){
cube.cubelets[ 18 ] = swap[ 24 ]
cube.cubelets[ 9 ] = swap[ 21 ]
cube.cubelets[ 0 ] = swap[ 18 ]
cube.cubelets[ 21 ] = swap[ 15 ]
cube.cubelets[ 3 ] = swap[ 9 ]
cube.cubelets[ 24 ] = swap[ 6 ]
cube.cubelets[ 15 ] = swap[ 3 ]
cube.cubelets[ 6 ] = swap[ 0 ]
}
if( degrees === undefined ) degrees = cube.left.getDistanceToPeg( 'x' )
cube.left.cubelets.forEach( function( cubelet, i ){
if( i === cube.left.cubelets.length - 1 ) cubelet.rotate( 'x', degrees, onTwistComplete )
else cubelet.rotate( 'x', degrees )
})
}
else if( command === 'l' && !cube.left.isEngagedY() && !cube.left.isEngagedZ() ){
onTwistComplete = function( swap ){
cube.cubelets[ 18 ] = swap[ 0 ]
cube.cubelets[ 9 ] = swap[ 3 ]
cube.cubelets[ 0 ] = swap[ 6 ]
cube.cubelets[ 21 ] = swap[ 9 ]
cube.cubelets[ 3 ] = swap[ 15 ]
cube.cubelets[ 24 ] = swap[ 18 ]
cube.cubelets[ 15 ] = swap[ 21 ]
cube.cubelets[ 6 ] = swap[ 24 ]
}
if( degrees === undefined ) degrees = cube.left.getDistanceToPeg( 'X' )
cube.left.cubelets.forEach( function( cubelet, i ){
if( i === cube.left.cubelets.length - 1 ) cubelet.rotate( 'X', degrees, onTwistComplete )
else cubelet.rotate( 'X', degrees )
})
}
// Y-axis rotations
if( command === 'Y' && !cube.isEngagedX() && !cube.isEngagedZ() ){
onTwistComplete = function( swap ){
cube.cubelets = [
swap[ 2 ], swap[ 11 ], swap[ 20 ],
swap[ 5 ], swap[ 14 ], swap[ 23 ],
swap[ 8 ], swap[ 17 ], swap[ 26 ],
swap[ 1 ], swap[ 10 ], swap[ 19 ],
swap[ 4 ], swap[ 13 ], swap[ 22 ],
swap[ 7 ], swap[ 16 ], swap[ 25 ],
swap[ 0 ], swap[ 9 ], swap[ 18 ],
swap[ 3 ], swap[ 12 ], swap[ 21 ],
swap[ 6 ], swap[ 15 ], swap[ 24 ]
]
}
if( degrees === undefined ) degrees = cube.getDistanceToPeg( 'Y' )
cube.cubelets.forEach( function( cubelet, i ){
if( i === cube.cubelets.length - 1 ) cubelet.rotate( 'Y', degrees, onTwistComplete )
else cubelet.rotate( 'Y', degrees )
})
}
else if( command === 'y' && !cube.isEngagedX() && !cube.isEngagedZ() ){
onTwistComplete = function( swap ){
cube.cubelets = [
swap[ 18 ], swap[ 9 ], swap[ 0 ],
swap[ 21 ], swap[ 12 ], swap[ 3 ],
swap[ 24 ], swap[ 15 ], swap[ 6 ],
swap[ 19 ], swap[ 10 ], swap[ 1 ],
swap[ 22 ], swap[ 13 ], swap[ 4 ],
swap[ 25 ], swap[ 16 ], swap[ 7 ],
swap[ 20 ], swap[ 11 ], swap[ 2 ],
swap[ 23 ], swap[ 14 ], swap[ 5 ],
swap[ 26 ], swap[ 17 ], swap[ 8 ]
]
}
if( degrees === undefined ) degrees = cube.getDistanceToPeg( 'y' )
cube.cubelets.forEach( function( cubelet, i ){
if( i === cube.cubelets.length - 1 ) cubelet.rotate( 'y', degrees, onTwistComplete )
else cubelet.rotate( 'y', degrees )
})
}
else if( command === 'U' && !cube.up.isEngagedX() && !cube.up.isEngagedZ() ){
onTwistComplete = function( swap ){
cube.cubelets[ 18 ] = swap[ 0 ]
cube.cubelets[ 19 ] = swap[ 9 ]
cube.cubelets[ 20 ] = swap[ 18 ]
cube.cubelets[ 9 ] = swap[ 1 ]
cube.cubelets[ 11 ] = swap[ 19 ]
cube.cubelets[ 0 ] = swap[ 2 ]
cube.cubelets[ 1 ] = swap[ 11 ]
cube.cubelets[ 2 ] = swap[ 20 ]
}
if( degrees === undefined ) degrees = cube.up.getDistanceToPeg( 'Y' )
cube.up.cubelets.forEach( function( cubelet, i ){
if( i === cube.up.cubelets.length - 1 ) cubelet.rotate( 'Y', degrees, onTwistComplete )
else cubelet.rotate( 'Y', degrees )
})
}
else if( command === 'u' && !cube.up.isEngagedX() & !cube.up.isEngagedZ() ){
onTwistComplete = function( swap ){
cube.cubelets[ 18 ] = swap[ 20 ]
cube.cubelets[ 19 ] = swap[ 11 ]
cube.cubelets[ 20 ] = swap[ 2 ]
cube.cubelets[ 9 ] = swap[ 19 ]
cube.cubelets[ 11 ] = swap[ 1 ]
cube.cubelets[ 0 ] = swap[ 18 ]
cube.cubelets[ 1 ] = swap[ 9 ]
cube.cubelets[ 2 ] = swap[ 0 ]
}
if( degrees === undefined ) degrees = cube.up.getDistanceToPeg( 'y' )
cube.up.cubelets.forEach( function( cubelet, i ){
if( i === cube.up.cubelets.length - 1 ) cubelet.rotate( 'y', degrees, onTwistComplete )
else cubelet.rotate( 'y', degrees )
})
}
else if( command === 'E' && !cube.equator.isEngagedX() && !cube.equator.isEngagedZ() ){
onTwistComplete = function( swap ){
cube.cubelets[ 21 ] = swap[ 23 ]
cube.cubelets[ 22 ] = swap[ 14 ]
cube.cubelets[ 23 ] = swap[ 5 ]
cube.cubelets[ 12 ] = swap[ 22 ]
cube.cubelets[ 14 ] = swap[ 4 ]
cube.cubelets[ 3 ] = swap[ 21 ]
cube.cubelets[ 4 ] = swap[ 12 ]
cube.cubelets[ 5 ] = swap[ 3 ]
}
if( degrees === undefined ) degrees = cube.equator.getDistanceToPeg( 'y' )
cube.equator.cubelets.forEach( function( cubelet, i ){
if( i === cube.equator.cubelets.length - 1 ) cubelet.rotate( 'y', degrees, onTwistComplete )
else cubelet.rotate( 'y', degrees )
})
}
else if( command === 'e' && !cube.equator.isEngagedX() && !cube.equator.isEngagedZ() ){
onTwistComplete = function( swap ){
cube.cubelets[ 21 ] = swap[ 3 ]
cube.cubelets[ 22 ] = swap[ 12 ]
cube.cubelets[ 23 ] = swap[ 21 ]
cube.cubelets[ 12 ] = swap[ 4 ]
cube.cubelets[ 14 ] = swap[ 22 ]
cube.cubelets[ 3 ] = swap[ 5 ]
cube.cubelets[ 4 ] = swap[ 14 ]
cube.cubelets[ 5 ] = swap[ 23 ]
}
if( degrees === undefined ) degrees = cube.equator.getDistanceToPeg( 'Y' )
cube.equator.cubelets.forEach( function( cubelet, i ){
if( i === cube.equator.cubelets.length - 1 ) cubelet.rotate( 'Y', degrees, onTwistComplete )
else cubelet.rotate( 'Y', degrees )
})
}
else if( command === 'D' && !cube.down.isEngagedX() && !cube.down.isEngagedZ() ){
onTwistComplete = function( swap ){
cube.cubelets[ 6 ] = swap[ 24 ]
cube.cubelets[ 7 ] = swap[ 15 ]
cube.cubelets[ 8 ] = swap[ 6 ]
cube.cubelets[ 15 ] = swap[ 25 ]
cube.cubelets[ 17 ] = swap[ 7 ]
cube.cubelets[ 24 ] = swap[ 26 ]
cube.cubelets[ 25 ] = swap[ 17 ]
cube.cubelets[ 26 ] = swap[ 8 ]
}
if( degrees === undefined ) degrees = cube.down.getDistanceToPeg( 'y' )
cube.down.cubelets.forEach( function( cubelet, i ){
if( i === cube.down.cubelets.length - 1 ) cubelet.rotate( 'y', degrees, onTwistComplete )
else cubelet.rotate( 'y', degrees )
})
}
else if( command === 'd' && !cube.down.isEngagedX() && !cube.down.isEngagedZ() ){
onTwistComplete = function( swap ){
cube.cubelets[ 6 ] = swap[ 8 ]
cube.cubelets[ 7 ] = swap[ 17 ]
cube.cubelets[ 8 ] = swap[ 26 ]
cube.cubelets[ 15 ] = swap[ 7 ]
cube.cubelets[ 17 ] = swap[ 25 ]
cube.cubelets[ 24 ] = swap[ 6 ]
cube.cubelets[ 25 ] = swap[ 15 ]
cube.cubelets[ 26 ] = swap[ 24 ]
}
if( degrees === undefined ) degrees = cube.down.getDistanceToPeg( 'Y' )
cube.down.cubelets.forEach( function( cubelet, i ){
if( i === cube.down.cubelets.length - 1 ) cubelet.rotate( 'Y', degrees, onTwistComplete )
else cubelet.rotate( 'Y', degrees )
})
}
// Z-axis rotations
if( command === 'Z' && !cube.isEngagedX() && !cube.isEngagedY() ){
onTwistComplete = function( swap ){
cube.cubelets = [
swap[ 6 ], swap[ 3 ], swap[ 0 ],
swap[ 7 ], swap[ 4 ], swap[ 1 ],
swap[ 8 ], swap[ 5 ], swap[ 2 ],
swap[ 15 ], swap[ 12 ], swap[ 9 ],
swap[ 16 ], swap[ 13 ], swap[ 10 ],
swap[ 17 ], swap[ 14 ], swap[ 11 ],
swap[ 24 ], swap[ 21 ], swap[ 18 ],
swap[ 25 ], swap[ 22 ], swap[ 19 ],
swap[ 26 ], swap[ 23 ], swap[ 20 ]
]
}
if( degrees === undefined ) degrees = cube.getDistanceToPeg( 'Z' )
cube.cubelets.forEach( function( cubelet, i ){
if( i === cube.cubelets.length - 1 ) cubelet.rotate( 'Z', degrees, onTwistComplete )
else cubelet.rotate( 'Z', degrees )
})
}
else if( command === 'z' && !cube.isEngagedX() && !cube.isEngagedY() ){
onTwistComplete = function( swap ){
cube.cubelets = [
swap[ 2 ], swap[ 5 ], swap[ 8 ],
swap[ 1 ], swap[ 4 ], swap[ 7 ],
swap[ 0 ], swap[ 3 ], swap[ 6 ],
swap[ 11 ], swap[ 14 ], swap[ 17 ],
swap[ 10 ], swap[ 13 ], swap[ 16 ],
swap[ 9 ], swap[ 12 ], swap[ 15 ],
swap[ 20 ], swap[ 23 ], swap[ 26 ],
swap[ 19 ], swap[ 22 ], swap[ 25 ],
swap[ 18 ], swap[ 21 ], swap[ 24 ]
]
}
if( degrees === undefined ) degrees = cube.getDistanceToPeg( 'z' )
cube.cubelets.forEach( function( cubelet, i ){
if( i === cube.cubelets.length - 1 ) cubelet.rotate( 'z', degrees, onTwistComplete )
else cubelet.rotate( 'z', degrees )
})
}
else if( command === 'F' && !cube.front.isEngagedX() && !cube.front.isEngagedY() ){
onTwistComplete = function( swap ){
cube.cubelets[ 0 ] = swap[ 6 ]
cube.cubelets[ 1 ] = swap[ 3 ]
cube.cubelets[ 2 ] = swap[ 0 ]
cube.cubelets[ 3 ] = swap[ 7 ]
cube.cubelets[ 5 ] = swap[ 1 ]
cube.cubelets[ 6 ] = swap[ 8 ]
cube.cubelets[ 7 ] = swap[ 5 ]
cube.cubelets[ 8 ] = swap[ 2 ]
}
if( degrees === undefined ) degrees = cube.front.getDistanceToPeg( 'Z' )
cube.front.cubelets.forEach( function( cubelet, i ){
if( i === cube.front.cubelets.length - 1 ) cubelet.rotate( 'Z', degrees, onTwistComplete )
else cubelet.rotate( 'Z', degrees )
})
}
else if( command === 'f' && !cube.front.isEngagedX() && !cube.front.isEngagedY() ){
onTwistComplete = function( swap ){
cube.cubelets[ 0 ] = swap[ 2 ]
cube.cubelets[ 1 ] = swap[ 5 ]
cube.cubelets[ 2 ] = swap[ 8 ]
cube.cubelets[ 3 ] = swap[ 1 ]
cube.cubelets[ 5 ] = swap[ 7 ]
cube.cubelets[ 6 ] = swap[ 0 ]
cube.cubelets[ 7 ] = swap[ 3 ]
cube.cubelets[ 8 ] = swap[ 6 ]
}
if( degrees === undefined ) degrees = cube.front.getDistanceToPeg( 'z' )
cube.front.cubelets.forEach( function( cubelet, i ){
if( i === cube.front.cubelets.length - 1 ) cubelet.rotate( 'z', degrees, onTwistComplete )
else cubelet.rotate( 'z', degrees )
})
}
else if( command === 'S' && !cube.standing.isEngagedX() && !cube.standing.isEngagedY() ){
onTwistComplete = function( swap ){
cube.cubelets[ 9 ] = swap[ 15 ]
cube.cubelets[ 10 ] = swap[ 12 ]
cube.cubelets[ 11 ] = swap[ 9 ]
cube.cubelets[ 12 ] = swap[ 16 ]
cube.cubelets[ 14 ] = swap[ 10 ]
cube.cubelets[ 15 ] = swap[ 17 ]
cube.cubelets[ 16 ] = swap[ 14 ]
cube.cubelets[ 17 ] = swap[ 11 ]
}
if( degrees === undefined ) degrees = cube.standing.getDistanceToPeg( 'Z' )
cube.standing.cubelets.forEach( function( cubelet, i ){
if( i === cube.standing.cubelets.length - 1 ) cubelet.rotate( 'Z', degrees, onTwistComplete )
else cubelet.rotate( 'Z', degrees )
})
}
else if( command === 's' && !cube.standing.isEngagedX() && !cube.standing.isEngagedY() ){
onTwistComplete = function( swap ){
cube.cubelets[ 9 ] = swap[ 11 ]
cube.cubelets[ 10 ] = swap[ 14 ]
cube.cubelets[ 11 ] = swap[ 17 ]
cube.cubelets[ 12 ] = swap[ 10 ]
cube.cubelets[ 14 ] = swap[ 16 ]
cube.cubelets[ 15 ] = swap[ 9 ]
cube.cubelets[ 16 ] = swap[ 12 ]
cube.cubelets[ 17 ] = swap[ 15 ]
}
if( degrees === undefined ) degrees = cube.standing.getDistanceToPeg( 'z' )
cube.standing.cubelets.forEach( function( cubelet, i ){
if( i === cube.standing.cubelets.length - 1 ) cubelet.rotate( 'z', degrees, onTwistComplete )
else cubelet.rotate( 'z', degrees )
})
}
else if( command === 'B' && !cube.back.isEngagedX() && !cube.back.isEngagedY() ){
onTwistComplete = function( swap ){
cube.cubelets[ 18 ] = swap[ 20 ]
cube.cubelets[ 19 ] = swap[ 23 ]
cube.cubelets[ 20 ] = swap[ 26 ]
cube.cubelets[ 21 ] = swap[ 19 ]
cube.cubelets[ 23 ] = swap[ 25 ]
cube.cubelets[ 24 ] = swap[ 18 ]
cube.cubelets[ 25 ] = swap[ 21 ]
cube.cubelets[ 26 ] = swap[ 24 ]
}
if( degrees === undefined ) degrees = cube.back.getDistanceToPeg( 'z' )
cube.back.cubelets.forEach( function( cubelet, i ){