-
Notifications
You must be signed in to change notification settings - Fork 0
/
step5.html
2340 lines (2139 loc) · 91.8 KB
/
step5.html
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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=index,viewport-fit=cover">
<title>Step 5 - Naive Solver</title>
<script type="text/javascript" src="./threejs/three.js"></script>
<script type="text/javascript" src="./threejs/controls/OrbitControls.js"></script>
<link rel="stylesheet" href="./reset.import.css">
<style type="text/css">
div#canvas-frame {
cursor: pointer;
width: 100%;
height: 100%;
background-color: #000;
}
.btn-list {
list-style-type: none;
margin: 0;
padding: 0;
position: fixed;
top: 0;
left: 0;
}
button {
font-size: 20px;
margin: 10px 0 0 10px;
border: 1px solid #d1d1d1;
background-color: #fff;
}
p {
color: #ff0000;
margin: 5px 0 0 5px;
}
</style>
</head>
<body onload="threeStart();">
<ul class="btn-list">
<li>
<button id="naiveSolverButton">Naive Solver</button>
</li>
<li>
<button id="shuffleButton">Shuffle</button>
</li>
<li>
<button id="step1Button">step1</button>
</li>
<li>
<button id="step2Button">step2</button>
</li>
<li>
<button id="step3Button">step3</button>
</li>
<li>
<button id="step4Button">step4</button>
</li>
<li>
<button id="step5Button">step5</button>
</li>
<li>
<button id="step6Button">step6</button>
</li>
<li>
<button id="step7Button">step7</button>
</li>
<li>
<button id="step8Button">step8</button>
</li>
<li>
<button id="UButton">U</button>
</li>
<li>
<button id="uButton">u</button>
</li>
<li>
<button id="FButton">F</button>
</li>
<li>
<button id="fButton">f</button>
</li>
<li>
<button id="LButton">L</button>
</li>
<li>
<button id="lButton">l</button>
</li>
<li>
<button id="DButton">D</button>
</li>
<li>
<button id="dButton">d</button>
</li>
<li>
<button id="RButton">R</button>
</li>
<li>
<button id="rButton">r</button>
</li>
<li>
<button id="BButton">B</button>
</li>
<li>
<button id="bButton">b</button>
</li>
<li>
<p class="error"></p>
</li>
</ul>
<div id="canvas-frame"></div>
<script>
let renderer;
let width;
let height;
let raycaster = new THREE.Raycaster(); // ray caster helps detect intersection.
let mouse = new THREE.Vector2();
let isRotating = false;
let intersect;
let normalize;
let startPoint;
let movePoint;
let initStatus = [];
let minCubeIndex;
let startFaceNo = 0;
let currentFaceNo = 0;
let endFaceNo = 3;
let isAutoSolver = false;
// Six possible rotation directions.
const XLine = new THREE.Vector3(1, 0, 0); // X
const XLineR = new THREE.Vector3(-1, 0, 0); // Anti-X
const YLine = new THREE.Vector3(0, 1, 0); // Y
const YLineR = new THREE.Vector3(0, -1, 0); // Anti-Y
const ZLine = new THREE.Vector3(0, 0, 1); // Z
const ZLineR = new THREE.Vector3(0, 0, -1); // Anti-Z
const cubeParams = { // cube arguments.
x: 0,
y: 0,
z: 0,
num: 3,
len: 50,
// [right, left, up, down, front, back]
colors: ['red', 'orange',
'yellow', 'white',
'blue', 'green'
]
// colors: ['rgba(255,193,37,1)', 'rgba(0,191,255,1)',
// 'rgba(50,205,50,1)', 'rgba(178,34,34,1)',
// 'rgba(255,255,0,1)', 'rgba(255,255,255,1)'
// ]
};
window.requestAnimFrame = (function() { // need requestAnimationFrame to refresh frame
return window.requestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.msRequestAnimationFrame ||
window.webkitRequestAnimationFrame;
})();
// Create Renderer with given width and height.
let $frame = document.getElementById('canvas-frame');
function initRenderer() {
width = window.innerWidth;
height = window.innerHeight;
renderer = new THREE.WebGLRenderer({
antialias: true
});
renderer.setSize(width, height);
renderer.setClearColor(0xFFFFFF, 1.0);
$frame.appendChild(renderer.domElement);
}
// Create camera. Set direction and position.
let camera;
let controller; // View Controller.
let viewCenter = new THREE.Vector3(0, 0, 0);
function initCamera() {
camera = new THREE.PerspectiveCamera(45, width / height, 1, 10000);
camera.position.set(0, 0, 600);
camera.up.set(0, 1, 0);
camera.lookAt(viewCenter);
}
// Create Scene.
let scene;
function initScene() {
scene = new THREE.Scene();
}
// Create ambient light.
let light;
function initLight() {
light = new THREE.AmbientLight(0xfefefe);
scene.add(light);
}
/**
* Simple Rubik's Cube.
* @param (x,y,z): the center point.
* @apram num: the level of the cube.
* @param len: the length of each atmoic cube component.
* @param colors: colors of six faces.
*/
function createRubiks(x, y, z, num, len, colors) {
// Upper left vertice position.
const UpLeftX = x - num / 2 * len;
const UpLeftY = y + num / 2 * len;
const UpLeftZ = z + num / 2 * len;
const gray = 'rgba(25,25,25,1)';
let cubes = [];
for (let Z = 0; Z < num; Z++) {
for (let Y = 0; Y < num; Y++) {
for (let X = 0; X < num; X++) {
let cubegeo = new THREE.BoxGeometry(len, len, len);
let materialArr = [];
for (let c = 0; c < colors.length; c++) {
let color = colors[c];
if (c == 0 && X != 2) color = gray;
if (c == 1 && X != 0) color = gray;
if (c == 2 && Y != 0) color = gray;
if (c == 3 && Y != 2) color = gray;
if (c == 4 && Z != 0) color = gray;
if (c == 5 && Z != 2) color = gray;
let texture = new THREE.Texture(createFace(color));
texture.needsUpdate = true;
let material = new THREE.MeshLambertMaterial({
map: texture
});
materialArr.push(material);
}
let cube = new THREE.Mesh(cubegeo, materialArr);
// Centers of atomic cube components.
cube.position.x = (UpLeftX + len / 2) + X * len;
cube.position.y = (UpLeftY - len / 2) - Y * len;
cube.position.z = (UpLeftZ - len / 2) - Z * len;
cubes.push(cube)
}
}
}
return cubes;
}
// Function to create texture for a face of the atmoic cube on canvas.
function createFace(rgbaColor) {
let canvas = document.createElement('canvas');
canvas.width = 256;
canvas.height = 256;
let context = canvas.getContext('2d');
if (context) {
// Background: 256*256 square.
context.fillStyle = 'rgba(0,0,0,1)';
context.fillRect(0, 0, 256, 256);
// Inner colored context: 224*224 rounded square with 16px padding.
context.rect(16, 16, 224, 224);
context.lineJoin = 'round';
context.lineWidth = 16;
context.fillStyle = rgbaColor;
context.strokeStyle = rgbaColor;
context.stroke();
context.fill();
} else {
alert('The browser does not support viewing this Canvas.\n');
}
return canvas;
}
// Create objects in scene.
let cubes
function initObject() {
// Create simple cube.
cubes = createRubiks(cubeParams.x, cubeParams.y, cubeParams.z, cubeParams.num, cubeParams.len, cubeParams.colors);
let ids = [];
for (let i = 0; i < cubes.length; i++) {
let item = cubes[i];
// console.log(item);
/**
* Since we are using item.id to select moving items, we need to set an extra dynamic cubeIndex.
* Update cubeIndex after rotation according to the init status.
*/
initStatus.push({
x: item.position.x,
y: item.position.y,
z: item.position.z,
cubeIndex: item.id
});
item.cubeIndex = item.id;
ids.push(item.id);
scene.add(cubes[i]);
}
minCubeIndex = min(ids).value;
// Create transprent square for raycasting.
let cubegeo = new THREE.BoxGeometry(150, 150, 150);
let hex = 0x000000;
for (let i = 0; i < cubegeo.faces.length; i++) {
cubegeo.faces[i].color.setHex(hex);
}
let cubemat = new THREE.MeshBasicMaterial({
vertexColors: THREE.FaceColors,
opacity: 0,
transparent: true
});
let cube = new THREE.Mesh(cubegeo, cubemat);
cube.cubeType = 'coverCube';
scene.add(cube);
}
// Render scene.
function render() {
renderer.clear();
renderer.render(scene, camera);
window.requestAnimFrame(render);
}
// Main function.
function threeStart() {
initRenderer();
initCamera();
initScene();
initLight();
initObject();
render();
// Mouse event listener.
renderer.domElement.addEventListener('mousedown', startCubeRotation, false);
renderer.domElement.addEventListener('mousemove', rotateCube, false);
renderer.domElement.addEventListener('mouseup', stopCubeRotation, false);
// View controller.
controller = new THREE.OrbitControls(camera, renderer.domElement);
controller.target = new THREE.Vector3(0, 0, 0); // Set controller point.
// Naive Solver.
let $naiveSolverButton = document.querySelector('#naiveSolverButton');
$naiveSolverButton.addEventListener('click', function() {
naiveSolver();
}, false);
// Shuffle.
let $shuffleButton = document.querySelector('#shuffleButton');
$shuffleButton.addEventListener('click', function() {
shuffle();
}, false);
// step 1 Button.
let $step1Button = document.querySelector('#step1Button');
$step1Button.addEventListener('click', function() {
stepCount = 0;
isAutoSolver = true;
let topCenter = getCubeByIndex(10);
topColor = getFaceColorByVector(topCenter, YLine);
buttomColor = getOppositeColor(topColor);
step1();
}, false);
// step 2 Button.
let $step2Button = document.querySelector('#step2Button');
$step2Button.addEventListener('click', function() {
step2();
}, false);
// step 3 Button.
let $step3Button = document.querySelector('#step3Button');
$step3Button.addEventListener('click', function() {
step3();
}, false);
// step 4 Button.
let $step4Button = document.querySelector('#step4Button');
$step4Button.addEventListener('click', function() {
step4();
}, false);
// step 5 Button.
let $step5Button = document.querySelector('#step5Button');
$step5Button.addEventListener('click', function() {
step5();
}, false);
// step 6 Button.
let $step6Button = document.querySelector('#step6Button');
$step6Button.addEventListener('click', function() {
step6();
}, false);
// step 7 Button.
let $step7Button = document.querySelector('#step7Button');
$step7Button.addEventListener('click', function() {
step7();
}, false);
// step 8 Button.
let $step8Button = document.querySelector('#step8Button');
$step8Button.addEventListener('click', function() {
step8();
}, false);
// U Button.
let $UButton = document.querySelector('#UButton');
$UButton.addEventListener('click', function() {
U();
}, false);
// u Button.
let $uButton = document.querySelector('#uButton');
$uButton.addEventListener('click', function() {
u();
}, false);
// F Button.
let $FButton = document.querySelector('#FButton');
$FButton.addEventListener('click', function() {
F();
}, false);
// f Button.
let $fButton = document.querySelector('#fButton');
$fButton.addEventListener('click', function() {
f();
}, false);
// L Button.
let $LButton = document.querySelector('#LButton');
$LButton.addEventListener('click', function() {
L();
}, false);
// l Button.
let $lButton = document.querySelector('#lButton');
$lButton.addEventListener('click', function() {
l();
}, false);
// D Button.
let $DButton = document.querySelector('#DButton');
$DButton.addEventListener('click', function() {
D();
}, false);
// d Button.
let $dButton = document.querySelector('#dButton');
$dButton.addEventListener('click', function() {
d();
}, false);
// R Button.
let $RButton = document.querySelector('#RButton');
$RButton.addEventListener('click', function() {
R();
}, false);
// r Button.
let $rButton = document.querySelector('#rButton');
$rButton.addEventListener('click', function() {
r();
}, false);
// B Button.
let $BButton = document.querySelector('#BButton');
$BButton.addEventListener('click', function() {
B();
}, false);
// b Button.
let $bButton = document.querySelector('#bButton');
$bButton.addEventListener('click', function() {
b();
}, false);
}
// Get intersection point and the face it belongs to. And the normal vector of this face.
function getIntersects(event) {
mouse.x = (event.clientX / width) * 2 - 1;
mouse.y = -(event.clientY / height) * 2 + 1;
raycaster.setFromCamera(mouse, camera);
// Use Raycaster to get the first element among intersected items.
let intersects = raycaster.intersectObjects(scene.children);
if (intersects.length) {
try {
if (intersects[0].object.cubeType === 'coverCube') {
intersect = intersects[1];
normalize = intersects[0].face.normal;
} else {
intersect = intersects[0];
normalize = intersects[1].face.normal;
}
} catch (err) {}
}
}
function startCubeRotation(event) {
getIntersects(event);
// Start cube rotation only when the cube is not rotating and we catch the intersect.
if (!isRotating && intersect) {
startPoint = intersect.point; // set starting point of rotation.
controller.enabled = false; // ignore the controller when we are rotating the cube.
} else {
controller.enabled = true;
}
}
// Cube rotation given mouse event.
function rotateCube(event) {
getIntersects(event);
if (intersect) {
if (!isRotating && startPoint) {
movePoint = intersect.point;
if (!movePoint.equals(startPoint)) { // Rotated.
let sub = movePoint.sub(startPoint); // get rotation vector.
rotateMove(intersect.object, sub);
}
}
}
event.preventDefault();
}
function rotateMove(target, vector, callback) {
isRotating = true;
let direction = getDirection(vector); // calculate rotation direction.
let elements = getCubesToRotate(target, direction);
window.requestAnimFrame(function(timestamp) {
rotateAnimation(elements, direction, timestamp, 0, null, callback);
});
}
function stopCubeRotation() {
intersect = null;
startPoint = null;
}
function rotateAroundWorldY(obj, rad) {
let x0 = obj.position.x;
let z0 = obj.position.z;
let q = new THREE.Quaternion();
q.setFromAxisAngle(new THREE.Vector3(0, 1, 0), rad);
obj.quaternion.premultiply(q);
// obj.rotateY(rad);
obj.position.x = Math.cos(rad) * x0 + Math.sin(rad) * z0;
obj.position.z = Math.cos(rad) * z0 - Math.sin(rad) * x0;
}
function rotateAroundWorldZ(obj, rad) {
let x0 = obj.position.x;
let y0 = obj.position.y;
let q = new THREE.Quaternion();
q.setFromAxisAngle(new THREE.Vector3(0, 0, 1), rad);
obj.quaternion.premultiply(q);
// obj.rotateZ(rad);
obj.position.x = Math.cos(rad) * x0 - Math.sin(rad) * y0;
obj.position.y = Math.cos(rad) * y0 + Math.sin(rad) * x0;
}
function rotateAroundWorldX(obj, rad) {
let y0 = obj.position.y;
let z0 = obj.position.z;
let q = new THREE.Quaternion();
q.setFromAxisAngle(new THREE.Vector3(1, 0, 0), rad);
obj.quaternion.premultiply(q);
// obj.rotateX(rad);
obj.position.y = Math.cos(rad) * y0 - Math.sin(rad) * z0;
obj.position.z = Math.cos(rad) * z0 + Math.sin(rad) * y0;
}
/**
* Function to animate rotation.
*/
function rotateAnimation(elements, direction, currentstamp, startstamp, laststamp, callback) {
let totalTime = 200;
let isLastRotateFrame = false;
if (startstamp === 0) {
startstamp = currentstamp;
laststamp = currentstamp;
}
if (currentstamp - startstamp >= totalTime) { // stop animation.
currentstamp = startstamp + totalTime;
isLastRotateFrame = true;
}
switch (direction) {
// Clockwise around X.
case 1:
for (let i = 0; i < elements.length; i++) {
rotateAroundWorldX(elements[i], 90 * Math.PI / 180 * (currentstamp - laststamp) / totalTime);
}
break;
// Counter-Clockwise around X.
case 2:
for (let i = 0; i < elements.length; i++) {
rotateAroundWorldX(elements[i], -90 * Math.PI / 180 * (currentstamp - laststamp) / totalTime);
}
break;
// Clockwise around Y.
case 3:
for (let i = 0; i < elements.length; i++) {
rotateAroundWorldY(elements[i], -90 * Math.PI / 180 * (currentstamp - laststamp) / totalTime);
}
break;
// Counter-Clockwise around Y.
case 4:
for (let i = 0; i < elements.length; i++) {
rotateAroundWorldY(elements[i], 90 * Math.PI / 180 * (currentstamp - laststamp) / totalTime);
}
break;
// Clockwise around Z.
case 5:
for (let i = 0; i < elements.length; i++) {
rotateAroundWorldZ(elements[i], -90 * Math.PI / 180 * (currentstamp - laststamp) / totalTime);
}
break;
// Counter-Clockwise around Z.
case 6:
for (let i = 0; i < elements.length; i++) {
rotateAroundWorldZ(elements[i], 90 * Math.PI / 180 * (currentstamp - laststamp) / totalTime);
}
break;
default:
break;
}
if (!isLastRotateFrame) {
window.requestAnimFrame(function(timestamp) {
rotateAnimation(elements, direction, timestamp, startstamp, currentstamp, callback);
});
} else {
isRotating = false;
startPoint = null;
updateCubeIndex(elements);
if (callback) {
callback();
} else {
if (isAutoSolver) {
switch (currentStep) {
case 1:
step1();
break;
case 2:
step2();
break;
case 3:
step3();
break;
case 4:
step4();
break;
case 5:
step5();
break;
case 6:
step6();
break;
case 7:
step7();
break;
case 8:
step8();
break;
default:
break;
}
}
}
}
}
function updateCubeIndex(elements) {
for (let i = 0; i < elements.length; i++) {
let temp1 = elements[i];
for (let j = 0; j < initStatus.length; j++) {
let temp2 = initStatus[j];
if (Math.abs(temp1.position.x - temp2.x) <= cubeParams.len / 2 &&
Math.abs(temp1.position.y - temp2.y) <= cubeParams.len / 2 &&
Math.abs(temp1.position.z - temp2.z) <= cubeParams.len / 2) {
temp1.cubeIndex = temp2.cubeIndex;
temp1.skipNext = false;
break;
}
}
}
}
// Function to get all atomic cubes that should be rotated
function getCubesToRotate(target, direction) {
let targetId = target.cubeIndex - minCubeIndex;
const Z_Offset = parseInt(targetId / 9);
const Y_Offset = parseInt((targetId % 9) / 3);
const X_Offset = parseInt((targetId % 9) % 3);
let boxs = [];
switch (direction) {
// Around X.
case 1:
case 2:
for (let i = 0; i < cubes.length; i++) {
let tempId = cubes[i].cubeIndex - minCubeIndex;
if (X_Offset === tempId % 9 % 3) {
boxs.push(cubes[i]);
}
}
break;
// Around Y.
case 3:
case 4:
for (let i = 0; i < cubes.length; i++) {
let tempId = cubes[i].cubeIndex - minCubeIndex;
if (Y_Offset === parseInt(tempId % 9 / 3)) {
boxs.push(cubes[i]);
}
}
break;
// Around Z.
case 5:
case 6:
for (let i = 0; i < cubes.length; i++) {
let tempId = cubes[i].cubeIndex - minCubeIndex;
if (Z_Offset === parseInt(tempId / 9)) {
boxs.push(cubes[i]);
}
}
break;
default:
break;
}
return boxs;
}
// Function to get rotation direction.
function getDirection(vector3) {
// calculate angle of vector to x,y,z-axis.
const XAngle = vector3.angleTo(XLine);
const XAngleR = vector3.angleTo(XLineR);
const YAngle = vector3.angleTo(YLine);
const YAngleR = vector3.angleTo(YLineR);
const ZAngle = vector3.angleTo(ZLine);
const ZAngleR = vector3.angleTo(ZLineR);
const minAngle = min([XAngle, XAngleR, YAngle, YAngleR, ZAngle, ZAngleR]).value;
let direction;
switch (minAngle) {
case XAngle: // Rotate 90 degrees to the positive X.
if (normalize.equals(YLine)) {
direction = 5; // Clockwise around Z.
} else if (normalize.equals(YLineR)) {
direction = 6; // Counter-Clockwise around Z.
} else if (normalize.equals(ZLine)) {
direction = 4; // Counter-Clockwise around Y.
} else {
direction = 3; // Clockwise around Y.
}
break;
case XAngleR: // Rotate 90 degrees to the negative X.
if (normalize.equals(YLine)) {
direction = 6; // Counter-Clockwise around Z.
} else if (normalize.equals(YLineR)) {
direction = 5; // Clockwise around Z.
} else if (normalize.equals(ZLine)) {
direction = 3; // Clockwise around Y.
} else {
direction = 4; // Counter-Clockwise around Y.
}
break;
case YAngle: // Rotate 90 degrees to the positive Y.
if (normalize.equals(ZLine)) {
direction = 2; // Counter-Clockwise around X.
} else if (normalize.equals(ZLineR)) {
direction = 1; // Clockwise around X.
} else if (normalize.equals(XLine)) {
direction = 6; // Counter-Clockwise around Z.
} else {
direction = 5; // Clockwise around Z.
}
break;
case YAngleR: // Rotate 90 degrees to the negative Y.
if (normalize.equals(ZLine)) {
direction = 1; // Clockwise around X.
} else if (normalize.equals(ZLineR)) {
direction = 2; // Counter-Clockwise around X.
} else if (normalize.equals(XLine)) {
direction = 5; // Clockwise around Z.
} else {
direction = 6; // Counter-Clockwise around Z.
}
break;
case ZAngle: // Rotate 90 degrees to the positive Z.
if (normalize.equals(YLine)) {
direction = 1; // Clockwise around X.
} else if (normalize.equals(YLineR)) {
direction = 2; // Counter-Clockwise around X.
} else if (normalize.equals(XLine)) {
direction = 3; // Clockwise around Y.
} else {
direction = 4; // Counter-Clockwise around Y.
}
break;
case ZAngleR: // Rotate 90 degrees to the negative Z.
if (normalize.equals(YLine)) {
direction = 2; // Counter-Clockwise around X.
} else if (normalize.equals(YLineR)) {
direction = 1; // Clockwise around X.
} else if (normalize.equals(XLine)) {
direction = 4; // Counter-Clockwise around Y.
} else {
direction = 3; // Clockwise around Y.
}
break;
default:
break;
}
// console.log(direction);
return direction;
}
// get min element in an array.
function min(arr) {
let min = arr[0];
let index = 0;
for (let i = 1; i < arr.length; i++) {
if (arr[i] < min) {
min = arr[i];
index = i;
}
}
return {
index: index,
value: min
};
}
/*** Rubiks Functions **/
// Function to shuffle the cube.
function shuffle() {
if (!isRotating && !isAutoSolver) {
const stepNum = parseInt(20 * Math.random()) + 1;
console.log('random rotate ' + stepNum);
let funcArr = [R, U, F, B, L, D, r, u, f, b, l, d];
let stepArr = [];
for (let i = 0; i < stepNum; i++) {
let num = parseInt(Math.random() * funcArr.length);
stepArr.push(funcArr[num]);
}
runMethodAtIndex(stepArr, 0, 0);
}
}
// Naive Solver
let currentStep = 1;
let topColor;
let buttomColor;
let startTime = 0;
let endTime = 0;
let stepCount = 0;
function naiveSolver() {
if (!checkStep1() && !isRotating) {
console.log('start naiveSolver');
startTime = window.performance.now();
console.log('start at:' + startTime);
stepCount = 0;
isAutoSolver = true;
let topCenter = getCubeByIndex(10);
topColor = getFaceColorByVector(topCenter, YLine);
buttomColor = getOppositeColor(topColor);
step1();
} else {
console.log('already reset');
}
}
/**
* Step 1 - Pre First Layer
**/
function checkStep1() {
const indices = [1, 9, 11, 19];
for (let i = 0; i < indices.length; i++) {
const cube = getCubeByIndex(indices[i]);
const color = getFaceColorByVector(cube, YLine); // get up face color
if (color != buttomColor) {
return false;
}
}
return true;
}
function step1() {
if (checkStep1()) {
console.log('step1 has finished');
currentStep = 2;
// step2();
return;
}
console.log('start step 1');
step1Case1(0);
step1Case1(1);
step1Case1(2);
step1Case1(3);
step1Case2(0);
step1Case2(1);
step1Case2(2);
step1Case2(3);
step1Case3(0);
step1Case3(1);
step1Case3(2);
step1Case3(3);
step1Case4(0);
step1Case4(1);
step1Case4(2);
step1Case4(3);
if (!isRotating) {
isAutoSolver = false;
console.log('something wrong in step1');
}
}
function step1Case1(rotateNum) {
if (!isRotating) {
const cube_3 = getCubeByIndex(3, rotateNum);
const cube_9 = getCubeByIndex(9, rotateNum);
const zLine = rotateAxisAroundY(ZLine, rotateNum);
if (getFaceColorByVector(cube_3, zLine) == buttomColor) {
if (getFaceColorByVector(cube_9, YLine) != buttomColor) {
l(rotateNum);
} else {
u(rotateNum);
}
}
}
}
function step1Case2(rotateNum) {
if (!isRotating) {
const cube_5 = getCubeByIndex(5, rotateNum);
const cube_11 = getCubeByIndex(11, rotateNum);
const zLine = rotateAxisAroundY(ZLine, rotateNum);
if (getFaceColorByVector(cube_5, zLine) == buttomColor) {
if (getFaceColorByVector(cube_11, YLine) != buttomColor) {
R(rotateNum);
} else {
u(rotateNum);
}
}
}
}
function step1Case3(rotateNum) {
if (!isRotating) {
const cube_15 = getCubeByIndex(15, rotateNum);
const cube_9 = getCubeByIndex(9, rotateNum);
const zLine = rotateAxisAroundY(ZLine, rotateNum);
if (getFaceColorByVector(cube_15, YLineR) == buttomColor) {
if (getFaceColorByVector(cube_9, YLine) != buttomColor) {
l(rotateNum);
} else {
u(rotateNum);
}
}
}
}
function step1Case4(rotateNum) {
if (!isRotating) {
const cube_1 = getCubeByIndex(1, rotateNum);
const cube_7 = getCubeByIndex(7, rotateNum);
const zLine = rotateAxisAroundY(ZLine, rotateNum);
if (getFaceColorByVector(cube_1, zLine) == buttomColor || getFaceColorByVector(cube_7, zLine) == buttomColor) { //105、106
if (getFaceColorByVector(cube_1, YLine) != buttomColor) {
F(rotateNum);
} else {
D(rotateNum)
}
}
}