forked from newbieYoung/Threejs_rubik
-
Notifications
You must be signed in to change notification settings - Fork 0
/
step5.html
2255 lines (2150 loc) · 93.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=no,viewport-fit=cover">
<title>第五步:层先法自动还原魔方</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: #EEEEEE;
}
.btn-list{
list-style-type:none;
margin:0;
padding:0;
position: fixed;
top:0;
left:0;
}
button{
font-size:20px;
margin:5px 0 0 5px;
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="autoResetV1">自动还原</button></li>
<li><button id="randomRotate">随机旋转</button></li>
<li><p class="error"></p></li>
</ul>
<div id="canvas-frame"></div>
<script src="//cdn.bootcss.com/eruda/1.4.2/eruda.min.js"></script>
<script>eruda.init();</script>
<script>
var renderer;//渲染器
var width;//页面宽度
var height;//页面高度
var raycaster = new THREE.Raycaster();//光线碰撞检测器
var mouse = new THREE.Vector2();//存储鼠标坐标或者触摸坐标
var isRotating = false;//魔方是否正在转动
var intersect;//碰撞光线穿过的元素
var normalize;//触发平面法向量
var startPoint;//触发点
var movePoint;
var minCubeIndex;
var initStatus = [];//魔方初始状态
var startFaceNo = 0;
var currentFaceNo = 0;
var endFaceNo = 3;
//魔方转动的六个方向
const XLine = new THREE.Vector3( 1, 0, 0 );//X轴正方向
const XLineAd = new THREE.Vector3( -1, 0, 0 );//X轴负方向
const YLine = new THREE.Vector3( 0, 1, 0 );//Y轴正方向
const YLineAd = new THREE.Vector3( 0, -1, 0 );//Y轴负方向
const ZLine = new THREE.Vector3( 0, 0, 1 );//Z轴正方向
const ZLineAd = new THREE.Vector3( 0, 0, -1 );//Z轴负方向
const CubeParams = {//魔方参数
x:0,
y:0,
z:0,
num:3,
len:50,
//[右,左,上,下,前,后]
colors:['yellow','red','blue','green','white','pink']
};
//随机旋转,用于打乱魔方
function randomRotate(){
if(!isRotating&&!isAutoReset){
var stepNum = parseInt(20*Math.random())+1;//保证至少转动一步
//var stepNum = 0;
console.log('random rotate '+stepNum);
var funcArr = [R,U,F,B,L,D,r,u,f,b,l,d];
var stepArr = [];
for(var i=0;i<stepNum;i++){
var num = parseInt(Math.random()*funcArr.length);
stepArr.push(funcArr[num]);
}
runMethodAtNo(stepArr,0,0);
}
}
//自动还原第一版
var isAutoReset = false;
var currentStep = 1;
var bottomColor;
var topColor;
var startTime = 0;
var endTime = 0;
var stepCount = 0;
function autoResetV1(cubes){
if(!checkStep8()&&!isRotating){
console.log('start autoResetV1');
startTime = window.performance.now();
console.log('start at:'+startTime);
stepCount = 0;
isAutoReset = true;
var topCenter = getCubeByIndex(10);//获取上表面中心小方块
topColor = getFaceColorByVector(topCenter,YLine);//获取上表面颜色
bottomColor = getOppositeColor(topColor);//获取上表面颜色对应色
if(checkStep7()){
currentStep = 8;
console.log('start step8');
step8();
}else if(checkStep6()){
currentStep = 7;
console.log('start step7');
step7();
}else if(checkStep5()){
currentStep = 6;
console.log('start step6');
step6();
}else if(checkStep4()){
currentStep = 5;
console.log('start step5');
step5();
}else if(checkStep3()){
currentStep = 4;
startFaceNo = 0;
currentFaceNo = 0;
endFaceNo = 3;
console.log('start step4');
step4();
}else if(checkStep2()){
currentStep = 3;
console.log('start step3');
step3();
}else if(checkStep1()){
currentStep = 2;
console.log('start step2');
step2();
}else{
currentStep = 1;
console.log('start step1');
step1();
}
}else{
console.log('already reset');
}
}
//第八步 顶角归位
function step8(){
if(checkStep8()){
isAutoReset = false;
endTime = window.performance.now();
console.log('end at:'+endTime);
console.log('total times:'+(endTime-startTime));
console.log('total steps:'+stepCount);
console.log('end autoResetV1');
return;
}
step8Case1(0);
step8Case1(1);
step8Case1(2);
step8Case1(3);
step8Case2(0);
step8Case2(1);
step8Case2(2);
step8Case2(3);
step8Case3();
if(!isRotating){
isAutoReset = false;
console.log('something wrong in step8');
}
}
//判断顶角归位 是否完成
function checkStep8Item(indexs,line){
if(indexs.length>0){
var arr = getCubeByIndexs(indexs);
var color = getFaceColorByVector(arr[0],line)
for(var i=1;i<arr.length;i++){
if(getFaceColorByVector(arr[i],line)!=color){
return false;
}
}
}
return true;
}
function checkStep8(){
var result = true;
var indexs1 = [1,0,2,3,4,5,6,7,8];
result = checkStep8Item(indexs1,ZLine);
if(!result){
return result;
}
var indexs2 = [11,14,17,20,23,26,2,5,8]
result = checkStep8Item(indexs2,XLine);
if(!result){
return result;
}
var indexs3 = [19,18,21,22,24,25,20,23,26];
result = checkStep8Item(indexs3,ZLineAd);
if(!result){
return result;
}
var index4 = [9,0,3,6,12,15,18,21,24];
result = checkStep8Item(index4,XLineAd);
if(!result){
return result;
}
return result;
}
function rotate801(rotateNum){
//RRBBRFrBBRfR
var arr = [R,R,B,B,R,F,r,B,B,R,f,R];
runMethodAtNo(arr,0,rotateNum);
}
function rotate802(rotateNum){
//LLBBlfLBBlFl
var arr = [L,L,B,B,l,f,L,B,B,l,F,l];
runMethodAtNo(arr,0,rotateNum);
}
//顶角归位 第一种情况
function step8Case1(rotateNum){
if(!isRotating){
var cube2 = getCubeByIndex(2,rotateNum);
var cube20 = getCubeByIndex(20,rotateNum);
var cube11 = getCubeByIndex(11,rotateNum);
var xLine = rotateAxisByYLine(XLine,rotateNum);
if(getFaceColorByVector(cube2,xLine)==getFaceColorByVector(cube20,xLine)&&
getFaceColorByVector(cube2,xLine)!=getFaceColorByVector(cube11,xLine)){
rotate801(rotateNum);
}
}
}
//顶角归位 第二种情况
function step8Case2(rotateNum){
if(!isRotating){
var cube0 = getCubeByIndex(0,rotateNum);
var cube1 = getCubeByIndex(1,rotateNum);
var cube2 = getCubeByIndex(2,rotateNum);
var cube11 = getCubeByIndex(11,rotateNum);
var cube20 = getCubeByIndex(20,rotateNum);
var xLine = rotateAxisByYLine(XLine,rotateNum);
var zLine = rotateAxisByYLine(ZLine,rotateNum);
if(getFaceColorByVector(cube0,zLine)==getFaceColorByVector(cube20,xLine)&&
getFaceColorByVector(cube1,zLine)==getFaceColorByVector(cube2,zLine)&&
getFaceColorByVector(cube11,xLine)==getFaceColorByVector(cube20,xLine)&&
getFaceColorByVector(cube0,zLine)!=getFaceColorByVector(cube1,zLine)&&
getFaceColorByVector(cube20,xLine)!=getFaceColorByVector(cube20,xLine)){
rotate802(rotateNum);
}
}
}
//顶角归位 第三种情况
function step8Case3(){
if(!isRotating){
var cube0 = getCubeByIndex(0);
var cube2 = getCubeByIndex(2);
var cube20 = getCubeByIndex(20);
var cube18 = getCubeByIndex(18);
if(getFaceColorByVector(cube0,ZLine)!=getFaceColorByVector(cube2,ZLine)&&
getFaceColorByVector(cube2,XLine)!=getFaceColorByVector(cube20,XLine)&&
getFaceColorByVector(cube20,ZLineAd)!=getFaceColorByVector(cube18,ZLineAd)&&
getFaceColorByVector(cube18,XLineAd)!=getFaceColorByVector(cube0,XLineAd)){
rotate801(0);
}
}
}
//第七步 顶棱归位
function step7(){
if(checkStep7()){
console.log('start step8');
currentStep = 8;
step8();
return;
}
step7Case1(0);
step7Case1(1);
step7Case1(2);
step7Case1(3);
step7Case2(0);
step7Case2(1);
step7Case2(2);
step7Case2(3);
step7Case3();
if(!isRotating){
isAutoReset = false;
console.log('something wrong in step7');
}
}
//顶棱归位 第一种情况
function step7Case1(rotateNum){
if(!isRotating){
var cube11 = getCubeByIndex(11,rotateNum);
var cube4 = getCubeByIndex(4,rotateNum);
var cube1 = getCubeByIndex(1,rotateNum);
var cube14 = getCubeByIndex(14,rotateNum);
var zLine = rotateAxisByYLine(ZLine,rotateNum);
var xLine = rotateAxisByYLine(XLine,rotateNum);
var zLine4Color = getFaceColorByVector(cube4,zLine);
if(getFaceColorByVector(cube1,zLine)!=zLine4Color&&
zLine4Color==getFaceColorByVector(cube11,xLine)&&
zLine4Color!=getFaceColorByVector(cube14,xLine)){
F(rotateNum,function(){
F(rotateNum,function(){
U(rotateNum,function(){
r(rotateNum,function(){
L(rotateNum,function(){
F(rotateNum,function(){
F(rotateNum,function(){
R(rotateNum,function(){
l(rotateNum,function(){
U(rotateNum,function(){
F(rotateNum,function(){
F(rotateNum)
})
})
})
})
})
})
})
})
})
})
})
}
}
}
//顶棱归位 第二种情况
function step7Case2(rotateNum){
if(!isRotating){
var cube1 = getCubeByIndex(1,rotateNum);
var cube4 = getCubeByIndex(4,rotateNum);
var cube11 = getCubeByIndex(11,rotateNum);
var cube14 = getCubeByIndex(14,rotateNum);
var zLine = rotateAxisByYLine(ZLine,rotateNum);
var xLine = rotateAxisByYLine(XLine,rotateNum);
var zLine1Color = getFaceColorByVector(cube1,zLine);
if(zLine1Color!=getFaceColorByVector(cube4,zLine)&&
zLine1Color==getFaceColorByVector(cube14,xLine)&&
zLine1Color!=getFaceColorByVector(cube11,xLine)){
F(rotateNum,function(){
F(rotateNum,function(){
u(rotateNum,function(){
r(rotateNum,function(){
L(rotateNum,function(){
F(rotateNum,function(){
F(rotateNum,function(){
R(rotateNum,function(){
l(rotateNum,function(){
u(rotateNum,function(){
F(rotateNum,function(){
F(rotateNum)
})
})
})
})
})
})
})
})
})
})
})
}
}
}
//顶棱归位 第三种情况(有两个对应的面顶棱完成,另外两个对应的面顶棱没有)
function step7Case3(){
if(!isRotating&&!checkStep7()){
u(0);
}
}
//判断是否完成第七步 顶棱归位
function checkStep7(){
if(!checkStep6()){
return false;
}
var cube1 = getCubeByIndex(1);
var cube4 = getCubeByIndex(4);
var cube11 = getCubeByIndex(11);
var cube14 = getCubeByIndex(14);
var cube19 = getCubeByIndex(19);
var cube22 = getCubeByIndex(22);
var cube9 = getCubeByIndex(9);
var cube12 = getCubeByIndex(12);
if(getFaceColorByVector(cube1,ZLine)!=getFaceColorByVector(cube4,ZLine)||
getFaceColorByVector(cube11,XLine)!=getFaceColorByVector(cube14,XLine)||
getFaceColorByVector(cube19,ZLineAd)!=getFaceColorByVector(cube22,ZLineAd)||
getFaceColorByVector(cube9,XLineAd)!=getFaceColorByVector(cube12,XLineAd)){
return false;
}
return true;
}
//第六步 顶角面位
function step6(){
if(checkStep6()){
console.log('start step7');
currentStep = 7;
step7();
return;
}
step6Case1(0);
step6Case1(1);
step6Case1(2);
step6Case1(3);
if(!isRotating){
isAutoReset = false;
console.log('something wrong in step6');
}
}
function rotate601(rotateNum){
//rULuRUlu
var arr = [r,U,L,u,R,U,l,u];
runMethodAtNo(arr,0,rotateNum);
}
function rotate602(rotateNum){
//ULurUluR
var arr = [U,L,u,r,U,l,u,R];
runMethodAtNo(arr,0,rotateNum);
}
function rotate603(rotateNum){
//RUrURUUr
var arr = [R,U,r,U,R,U,U,r];
runMethodAtNo(arr,0,rotateNum);
}
//顶角面位 第一种、第二种和第三种情况
function step6Case1(rotateNum){
if(!isRotating){
var cube0 = getCubeByIndex(0,rotateNum);
var cube2 = getCubeByIndex(2,rotateNum);
var cube20 = getCubeByIndex(20,rotateNum);
var zLine = rotateAxisByYLine(ZLine,rotateNum);
var xLine = rotateAxisByYLine(XLine,rotateNum);
if(getFaceColorByVector(cube0,zLine)==topColor&&
getFaceColorByVector(cube2,xLine)==topColor){
rotate601(rotateNum);
}else if(getFaceColorByVector(cube2,zLine)==topColor&&
getFaceColorByVector(cube20,xLine)==topColor){
rotate602(rotateNum);
}else if(getFaceColorByVector(cube0,zLine)==topColor){
rotate603(rotateNum);
}
}
}
//判断是否完成第六步 顶角面位
function checkStep6(){
if(!checkStep5()){
return false;
}
var cube0 = getCubeByIndex(0);
var cube2 = getCubeByIndex(2);
var cube18 = getCubeByIndex(18);
var cube20 = getCubeByIndex(20);
if(getFaceColorByVector(cube0,YLine)!=topColor||
getFaceColorByVector(cube2,YLine)!=topColor||
getFaceColorByVector(cube18,YLine)!=topColor||
getFaceColorByVector(cube20,YLine)!=topColor){
return false;
}
return true;
}
//第五步 顶棱面位
function step5(){
if(checkStep5()){
console.log('start step6');
currentStep = 6;
step6();
return;
}
step5Case1(0);
step5Case1(1);
step5Case1(2);
step5Case1(3);
step5Case2(0);
step5Case2(1);
step5Case2(2);
step5Case2(3);
step5Case3(0);
step5Case3(1);
step5Case3(2);
step5Case3(3);
if(!isRotating){
isAutoReset = false;
console.log('something wrong in step5');
}
}
function rotate501(rotateNum,next){
//rufUFR
var arr = [r,u,f,U,F,R];
runMethodAtNo(arr,0,rotateNum,next);
}
function rotate502(rotateNum,next){
//rfuFUR
var arr = [r,f,u,F,U,R];
runMethodAtNo(arr,0,rotateNum,next);
}
//顶棱面位 第一种情况
function step5Case1(rotateNum){
if(!isRotating){
var cube1 = getCubeByIndex(1,rotateNum);
var cube11 = getCubeByIndex(11,rotateNum);
var cube9 = getCubeByIndex(9,rotateNum);
var cube19 = getCubeByIndex(19,rotateNum);
var cube10 = getCubeByIndex(10,rotateNum);
var zLine = rotateAxisByYLine(ZLine,rotateNum);
var xLine = rotateAxisByYLine(XLine,rotateNum);
if(getFaceColorByVector(cube10,YLine)==topColor&&
getFaceColorByVector(cube9,YLine)==topColor&&
getFaceColorByVector(cube19,YLine)==topColor&&
getFaceColorByVector(cube1,zLine)==topColor&&
getFaceColorByVector(cube11,xLine)==topColor){
rotate501(rotateNum);
}
}
}
//顶棱面位 第二种情况
function step5Case2(rotateNum){
if(!isRotating){
var cube1 = getCubeByIndex(1,rotateNum);
var cube11 = getCubeByIndex(11,rotateNum);
var cube19 = getCubeByIndex(19,rotateNum);
var cube10 = getCubeByIndex(10,rotateNum);
var xLine = rotateAxisByYLine(XLine,rotateNum);
if(getFaceColorByVector(cube10,YLine)==topColor&&
getFaceColorByVector(cube1,YLine)==topColor&&
getFaceColorByVector(cube19,YLine)==topColor&&
getFaceColorByVector(cube11,xLine)==topColor){
rotate501(rotateNum);
}
}
}
//顶棱面位 第三种情况
function step5Case3(rotateNum){
if(!isRotating){
var cube1 = getCubeByIndex(1,rotateNum);
var cube11 = getCubeByIndex(11,rotateNum);
var cube10 = getCubeByIndex(10,rotateNum);
var zLine = rotateAxisByYLine(ZLine,rotateNum);
var xLine = rotateAxisByYLine(XLine,rotateNum);
if(getFaceColorByVector(cube10,YLine)==topColor&&
getFaceColorByVector(cube1,zLine)==topColor&&
getFaceColorByVector(cube11,xLine)==topColor){
rotate501(rotateNum,function(){
U(rotateNum,function(){
rotate502(rotateNum);
})
});
}
}
}
//判断是否完成第五步 顶棱面位
function checkStep5(){
if(!checkStep4()){
return false;
}
var cube1 = getCubeByIndex(1);
var cube11 = getCubeByIndex(11);
var cube9 = getCubeByIndex(9);
var cube19 = getCubeByIndex(19);
var cube10 = getCubeByIndex(10);
if(getFaceColorByVector(cube10,YLine)!=topColor||
getFaceColorByVector(cube1,YLine)!=topColor||
getFaceColorByVector(cube11,YLine)!=topColor||
getFaceColorByVector(cube9,YLine)!=topColor||
getFaceColorByVector(cube19,YLine)!=topColor){
return false;
}
return true;
}
//第四步 中棱归位
function step4(){
if(checkStep4()){
console.log('start step5');
currentStep = 5;
step5();
return;
}
step4Face(currentFaceNo);
if(!isRotating){
isAutoReset = false;
console.log('something wrong in step4');
}
}
function rotate401(rotateNum,next){
if(rotateNum<0){
rotateNum = 4-Math.abs(rotateNum);
}
//rururURUR
var arr = [r,u,r,u,r,U,R,U,R];
runMethodAtNo(arr,0,rotateNum,next);
}
function rotate401Opposite(rotateNum,next){
if(rotateNum<0){
rotateNum = 4-Math.abs(rotateNum);
}
//ruruRURUR
var arr = [r,u,r,u,R,U,R,U,R];
runMethodAtNo(arr,0,rotateNum,next);
}
function rotate402(rotateNum,next){
if(rotateNum<0){
rotateNum = 4-Math.abs(rotateNum);
}
//FUFUFufuf
var arr = [F,U,F,U,F,u,f,u,f];
runMethodAtNo(arr,0,rotateNum,next);
}
function rotate402Opposite(rotateNum,next){
if(rotateNum<0){
rotateNum = 4-Math.abs(rotateNum);
}
//FUFUfufuf
var arr = [F,U,F,U,f,u,f,u,f];
runMethodAtNo(arr,0,rotateNum,next);
}
//中棱归位优先还原一个面
function step4Face(rotateNum){
if(!isRotating){
if(rotateNum>3){
rotateNum = rotateNum - 4;
}
currentFaceNo = rotateNum;
var cube3 = getCubeByIndex(3,rotateNum);
var cube4 = getCubeByIndex(4,rotateNum);
var cube5 = getCubeByIndex(5,rotateNum);
var cube6 = getCubeByIndex(6,rotateNum);
var cube9 = getCubeByIndex(9,rotateNum);
var cube19 = getCubeByIndex(19,rotateNum);
var cube11 = getCubeByIndex(11,rotateNum);
var cube14 = getCubeByIndex(14,rotateNum);
var cube1 = getCubeByIndex(1,rotateNum);
var cube21 = getCubeByIndex(21,rotateNum);
var cube23 = getCubeByIndex(23,rotateNum);
var xLine = rotateAxisByYLine(XLine,rotateNum);
var zLine = rotateAxisByYLine(ZLine,rotateNum);
var xLineAd = rotateAxisByYLine(XLineAd,rotateNum);
var zLineAd = rotateAxisByYLine(ZLineAd,rotateNum);
var zLine4Color = getFaceColorByVector(cube4,zLine);
var xLineAd6Color = getFaceColorByVector(cube6,xLineAd);
var xLine14Color = getFaceColorByVector(cube14,xLine);
if(getFaceColorByVector(cube3,zLine)!=zLine4Color){
if(getFaceColorByVector(cube9,YLine)==zLine4Color&&
(getFaceColorByVector(cube9,xLineAd)==xLineAd6Color||rotateNum==startFaceNo)){
rotate402(rotateNum-1);
return;
}else if(getFaceColorByVector(cube9,xLineAd)==zLine4Color&&
(getFaceColorByVector(cube9,YLine)==xLineAd6Color||rotateNum==startFaceNo)){
u(0,function(){
rotate401(rotateNum-1);
});
return;
}else if((getFaceColorByVector(cube19,YLine)==zLine4Color&&
(getFaceColorByVector(cube19,zLineAd)==xLineAd6Color||rotateNum==startFaceNo))||
(getFaceColorByVector(cube19,zLineAd)==zLine4Color&&
(getFaceColorByVector(cube19,YLine)==xLineAd6Color||rotateNum==startFaceNo))||
(getFaceColorByVector(cube11,YLine)==zLine4Color&&
(getFaceColorByVector(cube11,xLine)==xLineAd6Color||rotateNum==startFaceNo))||
(getFaceColorByVector(cube11,xLine)==zLine4Color&&
(getFaceColorByVector(cube11,YLine)==xLineAd6Color||rotateNum==startFaceNo))||
(getFaceColorByVector(cube1,YLine)==zLine4Color&&
(getFaceColorByVector(cube1,zLine)==xLineAd6Color||rotateNum==startFaceNo))||
(getFaceColorByVector(cube1,zLine)==zLine4Color&&
(getFaceColorByVector(cube1,YLine)==xLineAd6Color||rotateNum==startFaceNo))){
U(0);
return;
}else if(getFaceColorByVector(cube5,zLine)==zLine4Color&&
(getFaceColorByVector(cube5,xLine)==xLineAd6Color||rotateNum==startFaceNo)){
rotate401Opposite(rotateNum);
return;
}else if(getFaceColorByVector(cube3,xLineAd)==zLine4Color&&
(getFaceColorByVector(cube3,zLine)==xLineAd6Color||rotateNum==startFaceNo)){
var tempNum = rotateNum-1;
rotate402(tempNum,function(){
U(tempNum,function(){
rotate401(tempNum);
});
});
return;
}else if(getFaceColorByVector(cube23,xLine)==zLine4Color&&
(getFaceColorByVector(cube23,zLineAd)==xLineAd6Color||rotateNum==startFaceNo)){
rotate402Opposite(rotateNum-3);
return;
}else if(getFaceColorByVector(cube23,zLineAd)==zLine4Color&&
(getFaceColorByVector(cube23,xLine)==xLineAd6Color||rotateNum==startFaceNo)){
rotate402Opposite(rotateNum-3);
return;
}else if(getFaceColorByVector(cube5,xLine)==zLine4Color&&
(getFaceColorByVector(cube5,zLine)==xLineAd6Color||rotateNum==startFaceNo)){
rotate402Opposite(rotateNum);
return;
}else if((getFaceColorByVector(cube21,xLineAd)==zLine4Color||getFaceColorByVector(cube21,zLineAd)==zLine4Color)&&rotateNum<=0){
//除非是刚开始否则不能影响已经还原好的面
rotate402Opposite(rotateNum-2);
return;
}
}
if(getFaceColorByVector(cube5,zLine)!=zLine4Color){
if(getFaceColorByVector(cube11,YLine)==zLine4Color&&
(getFaceColorByVector(cube11,xLine)==xLine14Color||rotateNum!=endFaceNo)){
rotate401(rotateNum);
return;
}else if(getFaceColorByVector(cube11,xLine)==zLine4Color&&
(getFaceColorByVector(cube11,YLine)==xLine14Color||rotateNum!=endFaceNo)){
U(0,function(){
rotate402(rotateNum);
});
return;
}else if((getFaceColorByVector(cube1,YLine)==zLine4Color&&
(getFaceColorByVector(cube1,zLine)==xLine14Color||rotateNum!=endFaceNo))||
(getFaceColorByVector(cube1,zLine)==zLine4Color&&
(getFaceColorByVector(cube1,YLine)==xLine14Color||rotateNum!=endFaceNo))||
(getFaceColorByVector(cube9,YLine)==zLine4Color&&
(getFaceColorByVector(cube9,xLineAd)==xLine14Color||rotateNum!=endFaceNo))||
(getFaceColorByVector(cube9,xLineAd)==zLine4Color&&
(getFaceColorByVector(cube9,YLine)==xLine14Color||rotateNum!=endFaceNo))||
(getFaceColorByVector(cube19,YLine)==zLine4Color&&
(getFaceColorByVector(cube19,zLineAd)==xLine14Color||rotateNum!=endFaceNo))||
(getFaceColorByVector(cube19,zLineAd)==zLine4Color&&
(getFaceColorByVector(cube19,YLine)==xLine14Color||rotateNum!=endFaceNo))){
u(0);
return;
}else if(getFaceColorByVector(cube5,xLine)==zLine4Color&&
(getFaceColorByVector(cube5,zLine)==xLine14Color||rotateNum!=endFaceNo)){
rotate402Opposite(rotateNum);
return;
}else if((getFaceColorByVector(cube21,xLineAd)==zLine4Color||getFaceColorByVector(cube21,zLineAd)==zLine4Color)&&rotateNum<=0){
//除非是刚开始否则不能影响已经还原好的面
rotate402Opposite(rotateNum-2);
return;
}else if(getFaceColorByVector(cube23,zLineAd)==zLine4Color&&rotateNum==startFaceNo){
rotate402Opposite(rotateNum-3);
return;
}else if(getFaceColorByVector(cube23,xLine)==zLine4Color&&
(getFaceColorByVector(cube23,zLineAd)==xLine14Color||rotateNum!=endFaceNo)){
rotate402Opposite(rotateNum-3);
return;
}
}
if(getFaceColorByVector(cube3,zLine)!=zLine4Color||getFaceColorByVector(cube5,zLine)!=zLine4Color){
//某个面出现极端情况,以该面为起始面重新还原
startFaceNo = currentFaceNo;
if(startFaceNo>0){
endFaceNo = startFaceNo-1;
}else{
endFaceNo = 3;
}
}else{
currentFaceNo++;
if(currentFaceNo>3){
currentFaceNo = 0;
}
}
step4();
}
}
//判断是否完成第四步 中棱归位
function checkStep4(){
if(!checkStep3()){
return false;
}
var cube3 = getCubeByIndex(3);
var cube4 = getCubeByIndex(4);
var cube5 = getCubeByIndex(5);
var zLine3Color = getFaceColorByVector(cube3,ZLine);
if(getFaceColorByVector(cube4,ZLine)!=zLine3Color||
getFaceColorByVector(cube5,ZLine)!=zLine3Color){
return false;
}
var cube14 = getCubeByIndex(14);
var cube23 = getCubeByIndex(23);
var xLine5Color = getFaceColorByVector(cube5,XLine);
if(getFaceColorByVector(cube14,XLine)!=xLine5Color||
getFaceColorByVector(cube23,XLine)!=xLine5Color){
return false;
}
var cube21 = getCubeByIndex(21);
var cube22 = getCubeByIndex(22);
var zLineAd23Color = getFaceColorByVector(cube23,ZLineAd);
if(getFaceColorByVector(cube21,ZLineAd)!=zLineAd23Color||
getFaceColorByVector(cube22,ZLineAd)!=zLineAd23Color){
return false;
}
var cube12 = getCubeByIndex(12);
var xLineAd3Color = getFaceColorByVector(cube3,XLineAd);
if(getFaceColorByVector(cube12,XLineAd)!=xLineAd3Color||
getFaceColorByVector(cube21,XLineAd)!=xLineAd3Color){
return false;
}
return true;
}
//第三步底角归位
function step3(){
if(checkStep3()){
console.log('start step4');
currentStep = 4;
startFaceNo = 0;
endFaceNo = 3;
step4();
return;
}
step3Case1(0);
step3Case1(1);
step3Case1(2);
step3Case1(3);
step3Case2(0);
step3Case2(1);
step3Case2(2);
step3Case2(3);
step3Case3(0);
step3Case3(1);
step3Case3(2);
step3Case3(3);
step3Case4(0);
step3Case4(1);
step3Case4(2);
step3Case4(3);
step3Case5(0);
step3Case5(1);
step3Case5(2);
step3Case5(3);
if(!isRotating){
isAutoReset = false;
console.log('something wrong in step3');
}
}
//底角归位第一种情况
function step3Case1(rotateNum,startNum){
if(!isRotating){
var cube2 = getCubeByIndex(2,rotateNum);
var cube4 = getCubeByIndex(4,rotateNum);
var cube7 = getCubeByIndex(7,rotateNum);
var cube14 = getCubeByIndex(14,rotateNum);
var cube17 = getCubeByIndex(17,rotateNum);
var cube8 = getCubeByIndex(8,rotateNum);
var xLine = rotateAxisByYLine(XLine,rotateNum);
var zLine = rotateAxisByYLine(ZLine,rotateNum);
var zLineAd = rotateAxisByYLine(ZLineAd,rotateNum);
var xLineAd = rotateAxisByYLine(XLineAd,rotateNum);
var zLine2Color = getFaceColorByVector(cube2,zLine);
var yLine2Color = getFaceColorByVector(cube2,YLine);
if(getFaceColorByVector(cube2,xLine)==bottomColor&&!cube2.skipNext){
if(getFaceColorByVector(cube8,YLineAd)!=bottomColor&&
getFaceColorByVector(cube4,zLine)==zLine2Color&&
getFaceColorByVector(cube7,zLine)==zLine2Color&&
getFaceColorByVector(cube14,xLine)==yLine2Color&&
getFaceColorByVector(cube17,xLine)==yLine2Color){
R(rotateNum,function(){
U(rotateNum,function(){
r(rotateNum)
})
})
}else{
u(rotateNum,function(){
rotateNum++;
if(rotateNum>=4){
rotateNum = 0;
}
if(startNum!=rotateNum){//防止重复检测造成无限循环
if(startNum==null||startNum==undefined){
startNum = rotateNum-1;
step3Case1(rotateNum,startNum);
}else{
step3Case1(rotateNum,startNum);
}
}else{
var cube2 = getCubeByIndex(2,rotateNum);
cube2.skipNext = true;//下一次不进入判断
step3();
}
})
}
}
}
}
//底角归位第二种情况
function step3Case2(rotateNum,startNum){
if(!isRotating){
var xLine = rotateAxisByYLine(XLine,rotateNum);
var zLine = rotateAxisByYLine(ZLine,rotateNum);
var zLineAd = rotateAxisByYLine(ZLineAd,rotateNum);
var xLineAd = rotateAxisByYLine(XLineAd,rotateNum);
var cube2 = getCubeByIndex(2,rotateNum);
var cube4 = getCubeByIndex(4,rotateNum);
var cube7 = getCubeByIndex(7,rotateNum);
var cube14 = getCubeByIndex(14,rotateNum);
var cube17 = getCubeByIndex(17,rotateNum);
var cube8 = getCubeByIndex(8,rotateNum);
var yLine2Color = getFaceColorByVector(cube2,YLine);
var xLine2Color = getFaceColorByVector(cube2,xLine);
if(getFaceColorByVector(cube2,zLine)==bottomColor&&!cube2.skipNext){
if(getFaceColorByVector(cube8,YLineAd)!=bottomColor&&
getFaceColorByVector(cube4,zLine)==yLine2Color&&
getFaceColorByVector(cube7,zLine)==yLine2Color&&
getFaceColorByVector(cube14,xLine)==xLine2Color&&
getFaceColorByVector(cube17,xLine)==xLine2Color){
f(rotateNum,function(){
u(rotateNum,function(){
F(rotateNum)
})
})
}else{
u(rotateNum,function(){
rotateNum++;
if(rotateNum>=4){
rotateNum = 0;
}
if(startNum!=rotateNum){//防止重复检测造成无限循环
if(startNum==null||startNum==undefined){
startNum = rotateNum-1;
step3Case2(rotateNum,startNum);
}else{
step3Case2(rotateNum,startNum);
}
}else{
var cube2 = getCubeByIndex(2,rotateNum);
cube2.skipNext = true;//下一次不进入判断
step3();
}
})
}
}
}
}
//底角归位第三种情况
function step3Case3(rotateNum,startNum){
if(!isRotating){
var xLine = rotateAxisByYLine(XLine,rotateNum);
var zLine = rotateAxisByYLine(ZLine,rotateNum);
var zLineAd = rotateAxisByYLine(ZLineAd,rotateNum);
var xLineAd = rotateAxisByYLine(XLineAd,rotateNum);
var cube2 = getCubeByIndex(2,rotateNum);
var cube14 = getCubeByIndex(14,rotateNum);
var cube4 = getCubeByIndex(4,rotateNum);
var cube7 = getCubeByIndex(7,rotateNum);
var cube8 = getCubeByIndex(8,rotateNum);
var cube17 = getCubeByIndex(17,rotateNum);
var zLine2Color = getFaceColorByVector(cube2,zLine);
var xLine2Color = getFaceColorByVector(cube2,xLine);
if(getFaceColorByVector(cube2,YLine)==bottomColor&&!cube2.skipNext){
if(getFaceColorByVector(cube8,YLineAd)!=bottomColor&&
getFaceColorByVector(cube14,xLine)==zLine2Color&&
getFaceColorByVector(cube17,xLine)==zLine2Color&&
getFaceColorByVector(cube4,zLine)==xLine2Color&&
getFaceColorByVector(cube7,zLine)==xLine2Color){
//转换为第二种情况
f(rotateNum,function(){
u(rotateNum,function(){
u(rotateNum,function(){
F(rotateNum,function(){