-
Notifications
You must be signed in to change notification settings - Fork 0
/
CubiCube.pas
878 lines (803 loc) · 27.5 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
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;
PCorn,PCornTemp,cSwap: ^CornerCubie;
PEdge,PEdgeTemp,eSwap: ^EdgeCubie;
procedure Move(x:TurnAxis);
procedure Conjugate(s:Symmetry);overload;
procedure Conjugate(n:SymIdx);overload;
constructor Create; overload;
constructor Create(PFace: FaceletColor); overload;
constructor Create(cp,co,ep,eo:Integer); overload;
procedure Print(c:TCanvas;x,y:Longint);
//++++++++++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 UDSliceParityEven: Boolean;//not used in Cube Explorer
//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;
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;
//++++++++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;
CornMult(PCornTemp^,CornerCubieMove[x],PCorn^);
EdgeMult(PEdgeTemp^,EdgeCubieMove[x],PEdge^);
end;
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//++++++++++++++++++Do conjugation with symmetry++++++++++++++++++++++++++++++++
procedure CubieCube.Conjugate(s: Symmetry);
begin
cSwap:= PCorn; PCorn:= PCornTemp; PCornTemp:=cSwap;
eSwap:= PEdge; PEdge:= PEdgeTemp; PEdgeTemp:=eSwap;
CornMult(PCornTemp^,CornerCubieSym[s],PCorn^);
EdgeMult(PEdgeTemp^,EdgeCubieSym[s],PEdge^);
end;
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//+++++++++++++Do conjuation with symmetry++++++++++++++++++++++++++++++++++++++
procedure CubieCube.Conjugate(n: SymIdx);
begin
cSwap:= PCorn; PCorn:= PCornTemp; PCornTemp:=cSwap;
eSwap:= PEdge; PEdge:= PEdgeTemp; PEdgeTemp:=eSwap;
CornMult(PCornTemp^,CornSym[n],PCorn^);
EdgeMult(PEdgeTemp^,EdgeSym[n],PEdge^);
end;
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//++++++++++++++++Default constructor+++++++++++++++++++++++++++++++++++++++++++
constructor CubieCube.Create;
var i: Corner; j: Edge;
begin
PCorn:= @Corn1; PCornTemp:=@Corn2;
PEdge:= @Edge1; PEdgeTemp:=@Edge2;
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;
end;
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//+++++++++++++++Construct a CubieCube from facelet colors++++++++++++++++++++++
constructor CubieCube.Create(PFace:FaceletColor);//(fc: FaceletCube);
var ori,o1,o2: ShortInt; i,j: Corner; XCol1,XCol2: ColorIndex; k,m: Edge;
begin
PCorn:= @Corn1; PCornTemp:=@Corn2;
PEdge:= @Edge1; PEdgeTemp:=@Edge2;
for i:=URF to DRB do
begin
ori:=0;
while (PFace[CF[i,ori]]<>UCol) and (PFace[CF[i,ori]]<>DCol) do inc(ori);
o1:=ori+1; if o1=3 then o1:=0; XCol1:=PFace[CF[i,o1]];
o2:=o1+1; if o2=3 then o2:=0; XCol2:=PFace[CF[i,o2]];
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
break;//j
end;
end;
for k:=UR to BR do
for m:=UR to BR do
begin
if (PFace[EF[k,0]]=ECI[m,0]) and (PFace[EF[k,1]]=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
break;
end;
if (PFace[EF[k,0]]=ECI[m,1]) and (PFace[EF[k,1]]=ECI[m,0]) then
begin
PEdge^[k].e:=m;
PEdge^[k].o:=1;//flip
break;
end;
end;
end;
//+++++++++++End Construct a CubieCube from facelet colors++++++++++++++++++++++
//+++++++++++Create a CubieCube from coordinates++++++++++++++++++++++++++++++++
constructor CubieCube.Create(cp,co,ep,eo:Integer);
begin
PCorn:= @Corn1; PCornTemp:=@Corn2;
PEdge:= @Edge1; PEdgeTemp:=@Edge2;
InvCornPermCoord(cp);
InvCornOriCoord(co);
InvEdgePermCoord(ep);
InvEdgeOriCoord(eo);
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)++++++++++++++++++++++++++++
//+++++++++++++++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
s:=0;
for co:= URF to DRB do s:= s + PCorn^[co].o;
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
s:=0;
for ed:= UR to BR do s:= s + PEdge^[ed].o;
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;
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//++++++++++++++++++++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 (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;
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//++++++Create ClassIndexToRepresentantArray for FlipUDSlice coordinate+++++++++
procedure CreateFlipUDSliceToRawFlipUDSliceTable;
var c,d: CubieCube; i,j,k,n,classIdx,sym,byteOffset:Integer; prod: EdgeCubie;
occupied: array of byte;
fs: TFileStream;
const filename = 'RawFlipSlice';
//64430 classes, FlipSliceToRawFlipSlice[64429]=919506
const FREE = -1;
begin
if FileExists(filename) then
begin
Application.ProcessMessages;
fs := TFileStream.Create(filename, fmOpenRead);
fs.ReadBuffer(FlipUDSliceToRawFlipUDSlice,64430*4);
Form1.ProgressBar.Position:=10;
end
else
begin
SetLength(occupied,126720);//495*2048/8
c:= CubieCube.Create;
d:= CubieCube.Create;
classIdx:=0;
for i:=0 to 126720-1 do occupied[i]:= 0;
Form1.SetUpProgressBar(0,495-1,'Creating '+filename+' (252 KB)');
for i:=0 to 495-1 do
begin
Form1.ProgressBar.Position:=i;
Application.ProcessMessages;
c.InvUDSliceCoord(i);
for j:= 0 to 256-1 do//256=2048/8
begin
byteOffset:=256*i+j;
for k:= 0 to 7 do
begin
if (occupied[byteOffset] and (1 shl k))=0 then
begin
c.InvEdgeOriCoord(j shl 3 + k);
FlipUDSliceToRawFlipUDSlice[classIdx]:= byteOffset shl 3 + k;
Inc(classIdx);
for sym:= 0 to 15 do //conjugate with all symmtries which preserve UD-slice
begin
EdgeMult(EdgeSym[InvIdx[sym]],c.PEdge^,prod);
EdgeMult(prod,EdgeSym[sym],d.PEdge^);
n:= 2048*d.UDSliceCoord + d.EdgeOriCoord;
occupied[n shr 3]:= occupied[n shr 3] or (1 shl (n and 7));
end;
end;
end;
end;
end;
fs := TFileStream.Create(filename, fmCreate);
fs.WriteBuffer(FlipUDSliceToRawFlipUDSlice,64430*4);//Integer has 4 Bytes
c.Free;
d.Free;
Form1.SetUpProgressBar(0,100,'Loading...');
end;
fs.Free;
end;
//++++++End Create ClassIndexToRepresentantArray for FlipUDSlice coordinate+++++
//++++++Create ClassIndexToRepresentantArray for UDSliceSorted coordinate+++++++
procedure CreateSymUDSliceSortedToUDSliceSortedTable;
var c,d: CubieCube; i,j,sym,n,idx:Integer;
occupied: array of byte;
//788 classes
begin
SetLength(occupied,1485);//11880/8
c:= CubieCube.Create;
d:= CubieCube.Create;
Idx:=0;
for i:= 0 to 1485-1 do occupied[i]:=0;
for i:= 0 to 1485-1 do
for j:= 0 to 7 do
begin
if (occupied[i] and (1 shl j))=0 then
begin
UDSliceSortedSymToUDSliceSorted[idx]:=i shl 3 +j;
Inc(idx);
c.InvUDSliceSortedCoord(i shl 3+j);
for sym:= 0 to 15 do
begin
EdgeMult(EdgeSym[sym],c.PEdge^,d.PEdgeTemp^);
EdgeMult(d.PEdgeTemp^,EdgeSym[InvIdx[sym]],d.PEdge^);
n:=d.UDSliceSortedCoord;
occupied[n shr 3]:= occupied[n shr 3] or (1 shl (n and 7));
end;
end;
end;
c.Free;
d.Free;
end;
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//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++++++++++
procedure CreateSymCornPosToCornPosTable;
var prod: CornerCubie; c,d: CubieCube;
classIdx,i,k,n: Integer;
const FREE = -1;
//2768 Klassen
begin
c:= CubieCube.Create;
d:= CubieCube.Create;
classIdx:=0;
for i:=0 to 40320-1 do CornPosToSymCornPos[i]:= FREE;
for i:=0 to 40320-1 do
begin
if i and $ff=0 then Application.ProcessMessages;
c.InvCornPermCoord(i);
if CornPosToSymCornPos[i]=FREE then
begin
for k:= 0 to 15 do //conjugate with all symmtries which preserve UD-slice
begin
CornMult(CornSym[InvIdx[k]],c.PCorn^,prod);
CornMult(prod,CornSym[k],d.PCorn^);
n:= d.CornPermCoord;
if CornPosToSymCornPos[n]=FREE then
CornPosToSymCornPos[n]:= classIdx shl 4 + k;//pack classIdx and symmetry
if k=0 then SymCornPosToCornPos[classIdx]:=n;
end;
Inc(classIdx);
end;
end;
c.Free;
d.Free;
Form1.ProgressBar.Position:=20;
end;
//++++++++++++++++++++End CreateSymCornPosToCornPosTable++++++++++++++++++++++++
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
procedure CreateClassIndexToRepresentantTables;
begin
CreateFlipUDSliceToRawFlipUDSliceTable;
CreateSymUDSliceSortedToUDSliceSortedTable;
CreateSymCornPosToCornposTable;
end;
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
end.