forked from hkociemba/CubeExplorer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CubiCube.pas
1369 lines (1217 loc) · 40.9 KB
/
CubiCube.pas
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
unit CubiCube;
interface
uses Graphics, CubeDefs, FaceCube, Symmetries;
type
//++++++++++Class representing the Cube on the Cubie Level++++++++++++++++++++++
CubieCube = Class
public
Corn1,Corn2: CornerCubie;//a move applied to Corn1 is saved in Corn2 and vica versa
Edge1,Edge2: EdgeCubie;
Cent1,Cent2: CenterCubie;
PCorn,PCornTemp,cSwap: ^CornerCubie;
PEdge,PEdgeTemp,eSwap: ^EdgeCubie;
PCent,PCentTemp,cnSwap: ^CenterCubie;
isOriented: Boolean;
procedure Move(x:TurnAxis);
procedure SymMult(s:Symmetry);overload;
procedure SymMult(n:SymIdx);overload;
constructor Create; overload;
constructor Create(fc: FaceletCube); overload;
constructor Create(cp,co,ep,eo,cno:Integer); overload;
procedure Print(c:TCanvas;x,y:Longint);
function CentOriRFLBMod2Coord: Word;
procedure InvCentOriRFLBMod2Coord(w:Word);
//++++++++++Permutation of the centers+++++++++++++++++++++++++++++++++++++
function CentPermCoord: Word;
//+++Set permutation of centers such that the centercoordinate is w+++++++++
procedure InvCentPermCoord(w:Word);
//++++++++++Orientation of the centers+++++++++++++++++++++++++++++++++++++
function CentOriCoord: Word;
//+++Set orientation of centers such that the centercoordinate is w+++++++++
procedure InvCentOriCoord(w:Word);
//++++++++++corner orientation raw-coordinate++++++++++++++++++++++++++++++
function CornOriCoord: Word;
//++++++++++Set corners that the corner orientation coordinate is w++++++++
procedure InvCornOriCoord(w:Word);
//++++++++++Get the sum of all corner twists mod 3+++++++++++++++++++++++++
function CornOriMod3: Integer;
//++++++++++++++edge orientation raw-coordinate++++++++++++++++++++++++++++
function EdgeOriCoord: Word;
//++++++++++Set edges that the corner orientation coordinate is w++++++++++
procedure InvEdgeOriCoord(w:Word);
//++++++++++Get the sum of all edge flips mod 2++++++++++++++++++++++++++++
function EdgeOriMod2: Integer;
//+++++++++Get phase 1 UDSlice raw-coordinate+++++++++++++++++++++++++++++
function UDSliceCoord: Word;
//+++++++++Set the UDslice edges, so that UDSlice coordinate is w++++++++++
procedure InvUDSliceCoord(w: Word);
//+++++The same as above, but also regard the order of the four edges++++++
function UDSliceSortedCoord: Word;
procedure InvUDSliceSortedCoord(w: Word);
//++++++++the sym-coordinate version of the UDSliceSorted coordinate+++++++
function UDSliceSortedSymCoord: Integer;
procedure InvUDSliceSortedSymCoord(n: Integer);
//+++++++++++sym-coordinate FlipUDSlice++++++++++++++++++++++++++++++++++++
function FlipUDSliceCoord: Integer;
procedure InvFlipUDSliceCoord(n: Integer);
//+++++++++++++++raw-coordinate corner permutation+++++++++++++++++++++++++
function CornPermCoord: Word;
procedure InvCornPermCoord(w:Word);
//+++++++++++++++Phase 2 edge permutation coordinate+++++++++++++++++++++++
function Phase2EdgePermCoord: Word;
procedure InvPhase2EdgePermCoord(w:Word);
//+++++++++++++++++++Edge permutation coordinate for 12 edges++++++++++++++
function EdgePermCoord: Integer;//only used for random cube
procedure InvEdgePermCoord(w:Integer);
//++++++++++Parity Checks for corner and edge permutations+++++++++++++++++
function CornParityEven: Boolean;
function EdgeParityEven: Boolean;
function CentParityEven: Boolean;
function UDSliceParityEven: Boolean;
function RFLBCentOriParityEven: Boolean;
//Look if the raw-FlipUDSlice coordinate n is in the
//FlipUDSliceToRawFlipUDSlice array of representants. Return the classindex
//if so, or -1 if not.
function FlipUDSliceRawCoordClassIndex(n: Integer): Integer;
//Return true, if the raw-FlipUDSlice coordinate is in the representant array
function IsFlipUDSliceCoordRep: Boolean;
//++++++Sorted version of above for Big Optimal Solver+++++++++++++++++++++
function UDSliceSortedRawCoordClassIndex(n: Integer): Integer;
function IsUDSliceSortedCoordRep: Boolean;
//++++++++++++++++++Tetraeder-Coordinate+++++++++++++++++++++++++++++++++++
function TetraCoord: Word;
procedure InvTetraCoord(w:Word);
//++++++++++für Coset-Explorer notwendig++++++++++++++++++++++++++++++++++
procedure InvCorn8PermCoord(w:Word); //immer als letztes aufrufen, da Parität bestimmt wird.
function cube20TetraCoord : Word;//für cube20 Projekt
procedure InvCube20TetraCoord(w: Word);
function cube20SliceCoord : Word;
procedure InvCube20SliceCoord(w: Word);
end;
//++++++++++End Class representing the Cube on the Cubie Level++++++++++++++++++
//+++++++++++++++++++++++ClassIndexToRepresentant arrays++++++++++++++++++++++++
var FlipUDSliceToRawFlipUDSlice: array[0..64430-1] of Integer;
UDSliceSortedSymToUDSliceSorted: array[0..788-1] of Integer;
SymCornPosToCornPos: array[0..2768-1] of Word;
//because we need the sym-corner permutation coordinate from the raw coordinate
//each time we compute the pruning depth in phase 2, we use this array++++++++++
CornPosToSymCornPos: array[0..40320-1] of Integer;
procedure CreateClassIndexToRepresentantTables;
implementation
uses MathFuncs, classes, SysUtils, RubikMain, Forms,CordCube;
//++++++++Do a move on the cubie-level++++++++++++++++++++++++++++++++++++++++++
procedure CubieCube.Move(x: TurnAxis);
begin
cSwap:= PCorn; PCorn:= PCornTemp; PCornTemp:=cSwap;
eSwap:= PEdge; PEdge:= PEdgeTemp; PEdgeTemp:=eSwap;
cnSwap:= PCent; PCent:= PCentTemp; PCentTemp:=cnSwap;
CornMult(PCornTemp^,CornerCubieMove[x],PCorn^);
EdgeMult(PEdgeTemp^,EdgeCubieMove[x],PEdge^);
CentMult(PCentTemp^,CenterCubieMove[x],PCent^);
end;
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//++++++++++++++++++Do multiplication with symmetry++++++++++++++++++++++++++++++++
procedure CubieCube.SymMult(s: Symmetry);
begin
cSwap:= PCorn; PCorn:= PCornTemp; PCornTemp:=cSwap;
eSwap:= PEdge; PEdge:= PEdgeTemp; PEdgeTemp:=eSwap;
cnSwap:= PCent; PCent:= PCentTemp; PCentTemp:=cnSwap;
CornMult(PCornTemp^,CornerCubieSym[s],PCorn^);
EdgeMult(PEdgeTemp^,EdgeCubieSym[s],PEdge^);
CentMult(PCentTemp^,CenterCubieSym[s],PCent^);
end;
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//+++++++++++++Do multiplication with symmetry++++++++++++++++++++++++++++++++++++++
procedure CubieCube.SymMult(n: SymIdx);
begin
cSwap:= PCorn; PCorn:= PCornTemp; PCornTemp:=cSwap;
eSwap:= PEdge; PEdge:= PEdgeTemp; PEdgeTemp:=eSwap;
cnSwap:= PCent; PCent:= PCentTemp; PCentTemp:=cnSwap;
CornMult(PCornTemp^,CornSym[n],PCorn^);
EdgeMult(PEdgeTemp^,EdgeSym[n],PEdge^);
CentMult(PCentTemp^,CentSym[n],PCent^);
end;
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//++++++++++++++++Default constructor+++++++++++++++++++++++++++++++++++++++++++
constructor CubieCube.Create;
var i: Corner; j: Edge; k: TurnAxis;
begin
isOriented:=false;
PCorn:= @Corn1; PCornTemp:=@Corn2;
PEdge:= @Edge1; PEdgeTemp:=@Edge2;
PCent:= @Cent1; PCentTemp:=@Cent2;
for i:= URF to DRB do begin PCorn^[i].c:= i; PCorn^[i].o:= 0; end;
for j:= UR to BR do begin PEdge^[j].e:= j; PEdge^[j].o:= 0; end;
for k:= U to B do begin PCent^[k].c:= k; PCent^[k].o:= 0; end;
end;
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//+++++++++++++++Construct a CubieCube from facelet colors++++++++++++++++++++++
//constructor CubieCube.Create(PFace:FaceletColor);//(fc: FaceletCube);
constructor CubieCube.Create(fc:FaceletCube);//(fc: FaceletCube);
var ori,o1,o2: ShortInt; i,j: Corner; XCol0,XCol1,XCol2: ColorIndex; k,m: Edge;
cn: TurnAxis;
n: Integer;
co: array[URF..DRB] of Integer;
ed: array[UR..BR] of Integer;
begin
PCorn:= @Corn1; PCornTemp:=@Corn2;
PEdge:= @Edge1; PEdgeTemp:=@Edge2;
PCent:= @Cent1; PCentTemp:=@Cent2;
isOriented:=fc.isOriented;
for cn:= U to B do begin PCent^[cn].c:= cn; PCent^[cn].o:=fc.CenTwist[cn]; end;
for i:=URF to DRB do
begin
if (fc.PFace[CF[i,0]]=NoCol) and (fc.PFace[CF[i,1]]=NoCol) and (fc.PFace[CF[i,2]]=NoCol)
//empty corner
then
begin
PCorn^[i].c:=NNN;
PCorn^[i].o:=6;//6 means do not care
end
else if (fc.PFace[CF[i,0]]= OriCol) then begin PCorn^[i].c:=NNN; PCorn^[i].o:=0 end
else if (fc.PFace[CF[i,1]]= OriCol) then begin PCorn^[i].c:=NNN; PCorn^[i].o:=1 end
else if (fc.PFace[CF[i,2]]= OriCol) then begin PCorn^[i].c:=NNN; PCorn^[i].o:=2 end
else
begin
ori:=0;
while ((fc.PFace[CF[i,ori]]<>UCol) and (fc.PFace[CF[i,ori]]<>UColA))
and ((fc.PFace[CF[i,ori]]<>DCol) and (fc.PFace[CF[i,ori]]<>DColA)) do inc(ori);
o1:=ori+1; if o1=3 then o1:=0;
XCol1:=fc.PFace[CF[i,o1]];
if Ord(XCol1)>=7 then XCol1:=ColorIndex(Ord(XCol1) -7);//>=7 sind die speziellen Farben
o2:=o1+1; if o2=3 then o2:=0;
XCol2:=fc.PFace[CF[i,o2]];
if Ord(XCol2)>=7 then XCol2:=ColorIndex(Ord(XCol2) -7);
for j:=URF to DRB do if (XCol1=CCI[j,1]) and (XCol2=CCI[j,2]) then
begin
PCorn^[i].c:=j;//corner j sits in corner i's clean cube position
PCorn^[i].o:=ori;//twist of corner j
if Ord(fc.PFace[CF[i,0]])>=7 then PCorn^[i].o:=6;//Orientierung dann egal
break;//j
end;
end;
end;
for k:=UR to BR do
begin
if (fc.PFace[EF[k,0]]=NoCol) and (fc.PFace[EF[k,1]]=NoCol)
//empty corner
then
begin
PEdge^[k].e:=NN;
PEdge^[k].o:=6;//6 means do not care
end
else if (fc.PFace[EF[k,0]]= OriCol) then begin PEdge^[k].e:=NN; PEdge^[k].o:=0 end
else if (fc.PFace[EF[k,1]]= OriCol) then begin PEdge^[k].e:=NN; PEdge^[k].o:=1 end
else
begin
for m:=UR to BR do
begin
XCol0:=fc.PFace[EF[k,0]];XCol1:=fc.PFace[EF[k,1]];
if Ord(XCol0)>=7 then XCol0:=ColorIndex(Ord(XCol0) -7);
if Ord(XCol1)>=7 then XCol1:=ColorIndex(Ord(XCol1) -7);
if (XCol0=ECI[m,0]) and (XCol1=ECI[m,1]) then
begin
PEdge^[k].e:=m;//edge m sits in edge k's clean cube position;
PEdge^[k].o:=0;//no flip
if Ord(fc.PFace[EF[k,0]])>=7 then PEdge^[k].o:=6;//Orientierung dann egal
break;
end;
if (XCol0=ECI[m,1]) and (XCol1=ECI[m,0]) then
begin
PEdge^[k].e:=m;
PEdge^[k].o:=1;//flip
if Ord(fc.PFace[EF[k,0]])>=7 then PEdge^[k].o:=6;//Orientierung dann egal
break;
end;
end;
end;
end;
//jetzt evtl. die Positionen ergänzen, falls nur ein Stein fehlt
for i:=URF to DRB do co[i]:=0; for k:= UR to BR do ed[k]:=0;
n:=0;
for i:=URF to DRB do if PCorn^[i].c=NNN then
begin Inc(n); j:=i; end else Inc(co[PCorn^[i].c]);
if n=1 then
begin
for i:=URF to DRB do if co[i]=0 then
begin
PCorn^[j].c:=i;
break;
end;
end;
n:=0;
for k:=UR to BR do if PEdge^[k].e=NN then
begin Inc(n); m:=k; end else Inc(ed[PEdge^[k].e]);
if n=1 then
begin
for k:=UR to BR do if ed[k]=0 then
begin
PEdge^[m].e:=k;
break;
end;
end;
end;
//+++++++++++End Construct a CubieCube from facelet colors++++++++++++++++++++++
//+++++++++++Create a CubieCube from coordinates++++++++++++++++++++++++++++++++
constructor CubieCube.Create(cp,co,ep,eo,cno:Integer);
begin
isOriented:=false;
PCorn:= @Corn1; PCornTemp:=@Corn2;
PEdge:= @Edge1; PEdgeTemp:=@Edge2;
PCent:= @Cent1; PCentTemp:=@Cent2;
InvCornPermCoord(cp);
InvCornOriCoord(co);
InvEdgePermCoord(ep);
InvEdgeOriCoord(eo);
InvCentOriCoord(cno);
end;
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//++++++++++++++++++Print CubieCube (debugging only)++++++++++++++++++++++++++++
procedure CubieCube.Print(c:TCanvas;x,y:Longint);
var i: Corner; var j: Edge; s: string;
begin
c.Brush.Color:=clRed;
s:=' ';
c.TextOut(x,y,s);
c.TextOut(x,y+20,s);
s:='';
for i:= URF to DRB do
begin
s:=s+' '+CornerToString[PCorn^[i].c];
case PCorn^[i].o of
0: s:=s+' ';
1: s:=s+'+ ';
2: s:=s +'- ';
3: s:=s+'* ';
4: s:=s+'+* ';
5: s:=s +'-* ';
end;
end;
c.TextOut(x,y,s);
s:='';
for j:= UR to BR do
begin
s:=s+' '+EdgeToString[PEdge^[j].e];
case PEdge^[j].o of
0: s:=s+' ';
1: s:=s+'+';
end;
end;
c.TextOut(x,y+20,s);
end;
//++++++++++++++End Print CubieCube (debugging only)++++++++++++++++++++++++++++
function CubieCube.CentOriRFLBMod2Coord: Word;//Wichtig für den two phase alg, weil Phase 2 diese Koordinate nicht mehr verändert
begin
Result:=8*(PCent^[B].o mod 2)+ 4*(PCent^[L].o mod 2)
+ 2*(PCent^[F].o mod 2) + PCent^[R].o mod 2;
end;
procedure CubieCube.InvCentOriRFLBMod2Coord(w:Word);
begin
PCent^[R].o:= w mod 2; w:=w div 2;
PCent^[F].o:= w mod 2; w:=w div 2;
PCent^[L].o:= w mod 2; w:=w div 2;
PCent^[B].o:= w mod 2;
end;
//+++++++++++++++Center orientation coordinate++++++++++++++++++++++++++++++
function CubieCube.CentOriCoord:Word;
var c: TurnAxis; s: Word;//orientations must be 0..3
begin
s:=0;
for c := U to B do s:= 4*s + PCent^[c].o;
Result:= s;
end;
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//++++++++++++++++++++Inverse function of above+++++++++++++++++++++++++++++++++
procedure CubieCube.InvCentOriCoord(w:Word);
var c: Turnaxis;
begin
for c:=B downto U do
begin
PCent^[c].o:= w mod 4;
w:= w div 4;
end;
end;
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//+++++++++++++++corner orientation raw-coordinate++++++++++++++++++++++++++++++
function CubieCube.CornOriCoord:Word;
var co: Corner; s: Word;//orientations must be 0..2
begin
s:=0;
for co:= URF to Pred(DRB) do s:= 3*s + PCorn^[co].o;
Result:= s;
end;
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//++++++++++++++++++++Inverse function of above+++++++++++++++++++++++++++++++++
procedure CubieCube.InvCornOriCoord(w:Word);
var co: Corner; parity: ShortInt;
begin
parity:=0;
for co:=Pred(DRB) downto URF do
begin
parity:= parity + w mod 3;
PCorn^[co].o:= w mod 3;
w:= w div 3;
end;
parity:= parity mod 3;
case parity of
0: PCorn^[DRB].o:= 0;
1: PCorn^[DRB].o:= 2;
2: PCorn^[DRB].o:= 1;
end;
end;
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//+++++++++++++++++++++Get the sum of all corner twists mod3++++++++++++++++++++
function CubieCube.CornOriMod3: Integer;
var co: Corner; s: Word;
begin
Result:=111;
s:=0;
for co:= URF to DRB do
begin
s:= s + PCorn^[co].o;
if PCorn^[co].o=6 then begin Result:=0; break; end;//Orientierung egal
end;
if Result=111 then Result:= s mod 3;
end;
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//++++++++++++++++++++edge orientation raw-coordinate+++++++++++++++++++++++++++
function CubieCube.EdgeOriCoord:Word;
var ed: Edge; s: Word;
begin
s:=0;
for ed:= UR to Pred(BR) do s:= 2*s + PEdge^[ed].o;
Result:= s;
end;
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//+++++++++++++++++++++++++++++inverse of above+++++++++++++++++++++++++++++++++
procedure CubieCube.InvEdgeOriCoord(w:Word);
var ed: Edge; parity: ShortInt;
begin
parity:=0;
for ed:=Pred(BR) downto UR do
begin
parity:= parity + w mod 2;
PEdge^[ed].o:= w mod 2;
w:= w div 2;
end;
PEdge^[BR].o:= parity mod 2;
end;
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//++++++++++Get the sum of all edge flips mod 2++++++++++++++++++++++++++++
function CubieCube.EdgeOriMod2: Integer;
var ed: Edge; s: Word;
begin
Result:=111;
s:=0;
for ed:= UR to BR do
begin
s:= s + PEdge^[ed].o;
if PEdge^[ed].o=6 then begin Result:=0; break; end;//Orientierung egal
end;
if Result=111 then Result:= s mod 2;
end;
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//+++++++++++++++Get phase 1 UDSlice raw-coordinate+++++++++++++++++++++++++++++
function CubieCube.UDSliceCoord;
var s: Word; k,n: Integer; occupied: array[0..11] of boolean; ed: Edge;
begin
for n:= 0 to 11 do occupied[n]:=false;
for ed:=UR to BR do if PEdge^[ed].e >= FR then occupied[Word(ed)]:=true;
s:=0; k:=3; n:=11;
while k>= 0 do
begin
if occupied[n] then Dec(k)
else s:= s + Cnk(n,k);
Dec(n);
end;
Result:= s;
end;
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//++++++++++++++++++++++++++inverse of above++++++++++++++++++++++++++++++++++++
procedure CubieCube.InvUDSliceCoord(w: Word);
var n,k,v: Integer; udSliceEdge,ed,i: Edge; occupied: array[0..11] of boolean;
begin
for n:= 0 to 11 do occupied[n]:=false;
n:= 11; k:= 3;
while k>=0 do
begin
v:= Cnk(n,k);
if w<v then begin Dec(k); occupied[n]:= true; end
else w:= w-v;
Dec(n);
end;
uDSliceEdge:=FR;
for ed:=UR to BR do if occupied[Ord(ed)] then
begin
for i:= UR to BR do
if PEdge^[i].e= udSLiceEdge then
begin PEdge^[i].e := PEdge^[ed].e; break; end;
PEdge^[ed].e:= udSliceEdge;
if udSliceEdge<BR then Inc(udSliceEdge);
end;
end;
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//++++++++++++++++++++UDSliceSorted raw-coordinate++++++++++++++++++++++++++++++
function CubieCube.UDSliceSortedCoord: Word;
var j,k,s,x: Integer; i,e: Edge; arr: array[0..3] of Edge;
begin
j:=0;
for i:= UR to BR do
begin
e:=PEdge^[i].e;
if (e=FR) or (e=FL) or (e=BL) or (e=BR) then begin arr[j]:= e; Inc(j); end;
end;
x:= 0;
for j:= 3 downto 1 do
begin
s:=0;
for k:= j-1 downto 0 do
begin
if arr[k]>arr[j] then Inc(s);
end;
x:= (x+s)*j;
end;
Result:= UDSliceCoord*24 + x;
end;
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//+++++++++++++++++++++++inverse of above+++++++++++++++++++++++++++++++++++++++
procedure CubieCube.InvUDSliceSortedCoord(w: Word);
var x: Word; order: array[0..3] of Integer; used: array[FR..BR] of Boolean;
j,k,e: Edge; i,m: Integer;
begin
x:= w mod 24;
InvUDSliceCoord(w div 24);
for j:= FR to BR do used[j]:=false;
for i:= 0 to 3 do
begin
order[i]:= x mod (i+1);
x:= x div (i+1);
end;
for i:= 3 downto 0 do
begin
k:=BR; while used[k] do Dec(k);
while order[i]>0 do
begin
Dec(order[i]);
repeat
Dec(k);
until not used[k];
end;
m:=-1;
for j:= UR to BR do //search the i. UD-slice edge
begin
e:= PEdge^[j].e;
if (e=FR) or (e=FL) or (e=BL) or (e=BR) then Inc(m);
if m=i then
begin
PEdge^[j].e:=k;
used[k]:=true;
break;
end;
end;
end;
end;
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//+++++++++++++the sym-coordinate version of the UDSliceSorted coordinate+++++++
function CubieCube.UDSliceSortedSymCoord: Integer;
var k,n,coord: Integer; c:CubieCube;
begin
Result:=-1;
c:=CubieCube.Create;
for k:= 0 to 15 do
begin
EdgeMult(EdgeSym[k],PEdge^,c.PEdgeTemp^);
EdgeMult(c.PEdgeTemp^,EdgeSym[InvIdx[k]],c.PEdge^);
n:= c.UDSliceSortedCoord;
coord:= UDSliceSortedRawCoordClassIndex(n);
if coord<>-1 then
begin
Result:= coord shl 4 + k;
break;
end;
end;
assert(Result<>-1);
c.Free;
end;
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//+++++++++++++++++++++inverse of above+++++++++++++++++++++++++++++++++++++++++
procedure CubieCube.InvUDSliceSortedSymCoord(n: Integer);
var idx,k,m: Integer;
begin
idx:= n shr 4;
k:= n and 15;
m:= UDSliceSortedSymToUDSliceSorted[idx];
InvUDSliceSortedCoord(m);
EdgeMult(EdgeSym[InvIdx[k]],PEdge^,PEdgeTemp^);
EdgeMult(PEdgeTemp^,EdgeSym[k],PEdge^);
end;
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//++++++++++++++++++sym-coordinate FlipUDSlice++++++++++++++++++++++++++++++++++
function CubieCube.FlipUDSliceCoord: Integer;
var k,n,coord: Integer; prod: EdgeCubie; c,d: CubieCube;
begin
c:= CubieCube.Create;
d:= CubieCube.Create;
c.InvUDSliceCoord(UDSliceCoord);
c.InvEdgeOriCoord(EdgeOriCoord);
Result:=-1;
for k:= 0 to 15 do
begin
EdgeMult(EdgeSym[k],c.PEdge^,prod);
EdgeMult(prod,EdgeSym[InvIdx[k]],d.PEdge^);
n:= 2048*d.UDSliceCoord + d.EdgeOriCoord;//raw coordinate
coord:= FlipUDSliceRawCoordClassIndex(n);
if coord<>-1 then
begin
Result:= coord shl 4 + k;
break;
end;
end;
assert(Result<>-1);
c.Free;
d.Free;
end;
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//+++++++++++++++++++++++++++++++++Inverse of above+++++++++++++++++++++++++++++
procedure CubieCube.InvFlipUDSliceCoord(n: Integer);
var idx,k,m: Integer; prod: EdgeCubie;
begin
idx:= n shr 4;
k:= n and 15;
m:= FlipUDSliceToRawFlipUDSlice[idx];
InvUDSLiceCoord(m div 2048);
InvEdgeOriCoord(m mod 2048);
EdgeMult(EdgeSym[InvIdx[k]],PEdge^,prod);
EdgeMult(prod,EdgeSym[k],PEdge^);
end;
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//++++++++++++++++++++center permutation+++++++++++++++++++++++++
function CubieCube.CentPermCoord: Word;
var i,j: TurnAxis; x,s: Integer;
begin
x:= 0;
for i:= B downto Succ(U) do
begin
s:=0;
for j:= Pred(i) downto U do
begin
if PCent^[j].c>PCent^[i].c then Inc(s);
end;
x:= (x+s)*Ord(i);
end;
Result:=x;
end;
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//+++++++++++++++++++++++++++inverse of above+++++++++++++++++++++++++++++++++++
procedure CubieCube.InvCentPermCoord(w: Word);
var used: array[U..B] of boolean; i,k: TurnAxis; order: array[U..B] of Integer;
begin
for i:= U to B do
begin
used[i]:=false;
order[i]:= w mod (Ord(i)+1);
w:= w div (Ord(i)+1);
end;
for i:= B downto U do
begin
k:=B; while used[k] do Dec(k);
while order[i]>0 do
begin
Dec(order[i]);
repeat
Dec(k);
until not used[k];
end;
PCent^[i].c:=k;
used[k]:=true;
end;
end;
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//++++++++++++++++++++raw-coordinate corner permutation+++++++++++++++++++++++++
function CubieCube.CornPermCoord: Word;
var i,j: Corner; x,s: Integer;
begin
x:= 0;
for i:= DRB downto Succ(URF) do
begin
s:=0;
for j:= Pred(i) downto URF do
begin
if PCorn^[j].c>PCorn^[i].c then Inc(s);
end;
x:= (x+s)*Ord(i);
end;
Result:=x;
end;
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//+++++++++++++++++++++++++++inverse of above+++++++++++++++++++++++++++++++++++
procedure CubieCube.InvCornPermCoord(w: Word);
var used: array[URF..DRB] of boolean; i,k: Corner; order: array[URF..DRB] of Integer;
begin
for i:= URF to DRB do
begin
used[i]:=false;
order[i]:= w mod (Ord(i)+1);
w:= w div (Ord(i)+1);
end;
for i:= DRB downto URF do
begin
k:=DRB; while used[k] do Dec(k);
while order[i]>0 do
begin
Dec(order[i]);
repeat
Dec(k);
until not used[k];
end;
PCorn^[i].c:=k;
used[k]:=true;
end;
end;
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//++++++++++++++++++++Phase 2 edge permutation coordinate+++++++++++++++++++++++
function CubieCube.Phase2EdgePermCoord: Word;
var i,j: Edge; x,s: Integer;
begin
x:= 0;
for i:= DB downto Succ(UR) do
begin
s:=0;
for j:= Pred(i) downto UR do
begin
if PEdge^[j].e>PEdge^[i].e then Inc(s);
end;
x:= (x+s)*Ord(i);
end;
Result:=x;
end;
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//++++++++++++++++++++++++++++inverse of above++++++++++++++++++++++++++++++++++
procedure CubieCube.InvPhase2EdgePermCoord(w: Word);
var used: array[UR..DB] of boolean; i,k: Edge; order: array[UR..DB] of Integer;
begin
for i:= UR to DB do
begin
used[i]:=false;
order[i]:= w mod (Ord(i)+1);
w:= w div (Ord(i)+1);
end;
for i:= DB downto UR do
begin
k:=DB; while used[k] do Dec(k);
while order[i]>0 do
begin
Dec(order[i]);
repeat
Dec(k);
until not used[k];
end;
PEdge^[i].e:=k;
used[k]:=true;
end;
for i:= FR to BR do PEdge^[i].e:=i;//fill up UD-slice
end;
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//+++++++++++++++++++Phase 2 edge permutation coordinate++++++++++++++++++++++++
function CubieCube.EdgePermCoord: Integer;
var i,j: Edge; x,s: Integer;
begin
x:= 0;
for i:= BR downto Succ(UR) do
begin
s:=0;
for j:= Pred(i) downto UR do
begin
if PEdge^[j].e>PEdge^[i].e then Inc(s);
end;
x:= (x+s)*Ord(i);
end;
Result:=x;
end;
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//++++++++++++++++++++++++++inverse of above++++++++++++++++++++++++++++++++++++
procedure CubieCube.InvEdgePermCoord(w: Integer);
var used: array[UR..BR] of boolean; i,k: Edge; order: array[UR..BR] of Integer;
begin
for i:= UR to BR do
begin
used[i]:=false;
order[i]:= w mod (Ord(i)+1);
w:= w div (Ord(i)+1);
end;
for i:= BR downto UR do
begin
k:=BR; while used[k] do Dec(k);
while order[i]>0 do
begin
Dec(order[i]);
repeat
Dec(k);
until not used[k];
end;
PEdge^[i].e:=k;
used[k]:=true;
end;
end;
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//+++++++++++++++++++check corner permutation parity++++++++++++++++++++++++++++
function CubieCube.CornParityEven: Boolean;
var i,j: Corner; s: Integer;
begin
s:= 0;
for i:= DRB downto Succ(URF) do
for j:= Pred(i) downto URF do
if PCorn^[j].c>PCorn^[i].c then Inc(s);
if odd(s) then Result:= false else Result:= true;
end;
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//++++++++++++++++++++++check edge permutation parity+++++++++++++++++++++++++++
function CubieCube.EdgeParityEven: Boolean;
var i,j: Edge; s: Integer;
begin
s:= 0;
for i:= BR downto Succ(UR) do
for j:= Pred(i) downto UR do
if PEdge^[j].e>PEdge^[i].e then Inc(s);
if odd(s) then Result:= false else Result:= true;
end;
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//+++++++++++check parity of UDSLice edges+++++++++++++++++++++++++++
function CubieCube.UDSliceParityEven: Boolean;
var i,j: Edge; s: Integer;
begin
s:= 0;
for i:= BR downto Succ(FR) do
for j:= Pred(i) downto FR do
if PEdge^[j].e>PEdge^[i].e then Inc(s);
if odd(s) then Result:= false else Result:= true;
end;
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//+++++++++++check parity of RFLB centertwists+++++++++++++++++++++++++++++++++
function CubieCube.RFLBCentOriParityEven: Boolean;
begin
if (PCent^[R].o+PCent^[F].o+PCent^[L].o+PCent^[B].o) mod 4 =0
then Result:= True
else Result:= false;
end;
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
function CubieCube.CentParityEven: Boolean;
var i: TurnAxis; s: Integer;
begin
s:= 0;
for i:= U to B do s:=s+ PCent^[i].o;
if odd(s) then Result:= false else Result:= true;
end;
{
//+++++++++++++++++++check parity of udslice edges (not used)+++++++++++++++++++
function CubieCube.UDSliceParityEven: Boolean;
var j,k,s: Integer; i,e: Edge; arr: array[0..3] of Edge;
begin
j:=0;
for i:= UR to BR do
begin
e:=PEdge^[i].e;
if (e=FR) or (e=FL) or (e=BL) or (e=BR) then begin arr[j]:= e; Inc(j); end;
end;
s:=0;
for j:= 0 to 2 do
for k:=j+1 to 3 do
if arr[j]>arr[k] then Inc(s);
if Odd(s) then Result:= false
else Result:= true;
end;
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
}
//Look if the raw-FlipUDSlice coordinate n is in the
//FlipUDSliceToRawFlipUDSlice array of representants. Return the classindex
//if so, or -1 if not.
function CubieCube.FlipUDSliceRawCoordClassIndex(n: Integer): Integer;
var rBound,lBound,a,aOld : Integer;
begin
Result:= -1;
if n<=FlipUDSliceToRawFlipUDSlice[64429] then
begin
rBound:=64430;
lBound:=0;
a:=-1;
repeat
aOld:=a;
a:= (rBound + lBound) div 2;
if n< FlipUDSliceToRawFlipUDSlice[a] then rBound:= a
else
if n > FlipUDSliceToRawFlipUDSlice[a] then lBound:= a
else begin Result:= a; break; end;
until (a=aOld);
end;
end;
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//Return true, if the raw-FlipUDSlice coordinate is in the representant array
function CubieCube.IsFlipUDSliceCoordRep: Boolean;
var n,coord: Integer;
begin
n:= 2048*UDSliceCoord + EdgeOriCoord;//raw coordinate
coord:=FlipUDSliceRawCoordClassIndex(n);
if coord=-1 then Result:= false else Result:=true;
end;
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//++++++++++analogous to FlipUDSliceRawCoordClassIndex++++++++++++++++++++++++++
function CubieCube.UDSliceSortedRawCoordClassIndex(n: Integer): Integer;
var rBound,lBound,a,aOld : Integer;
begin
Result:= -1;
if n<= UDSliceSortedSymToUDSliceSorted[788-1] then
begin
rBound:=788;
lBound:=0;
a:=-1;
repeat
aOld:=a;
a:= (rBound + lBound) div 2;
if n< UDSliceSortedSymToUDSliceSorted[a] then rBound:= a
else
if n > UDSliceSortedSymToUDSliceSorted[a] then lBound:= a
else begin Result:= a; break; end;
until (a=aOld);
end;
end;
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//Return true, if the raw-UDSliceSorted coordinate is in the representant array
function CubieCube.IsUDSliceSortedCoordRep: Boolean;
var n,coord: Integer;
begin
n:= UDSliceSortedCoord;
coord:=UDSliceSortedRawCoordClassIndex(n);
if coord=-1 then Result:= false else Result:=true;
end;
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++