-
Notifications
You must be signed in to change notification settings - Fork 33
/
PatternSearch.pas
297 lines (263 loc) · 9.46 KB
/
PatternSearch.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
unit PatternSearch;
interface
uses
Classes,CubeDefs,Forms,FaceCube;
// array for the patterns
type PatArray = array [1..5,1..8] of SingleFace;
type
// Thread Class to do the Pattern Search
PatSearch = class(TThread)
private
{ Private-Deklarationen }
protected
procedure FindPatterns;
procedure Execute; override;
procedure SetCorners(curPlace:Corner);
procedure SetEdges(curPlace:Edge);
procedure CubeAdd;
public
constructor Create;
end;
//
var fcc:faceletCube;
implementation
uses RubikMain,CubiCube;
var p: PatArray;
cornerUsed: array[URF..DRB] of boolean;
edgeUsed: array[UR..BR] of boolean;
// Add a found cube to the Main Window
constructor PatSearch.Create;
begin
inherited Create(true);
FreeOnTerminate:=true;
Priority := tpLower;
end;
procedure PatSearch.CubeAdd;
begin
Form1.AddCube(fcc,false,Form1.FindGenerators.Checked,false,0,false);
end;
//
//
procedure PatSearch.Execute;
begin
FindPatterns;
end;
//
// rotate a pattern by 90 degrees
procedure RotateSingleFace(var f0,f1:SingleFace);
begin
f1[9]:=f0[3];f1[3]:=f0[1];f1[1]:=f0[7];f1[7]:=f0[9];
f1[8]:=f0[6];f1[6]:=f0[2];f1[2]:=f0[4];f1[4]:=f0[8];
f1[5]:=f0[5];
end;
//
// reflection of pattern
procedure ReflectSingleFace(var f0,f1:SingleFace);
begin
f1[1]:=f0[3];f1[3]:=f0[1];
f1[4]:=f0[6];f1[6]:=f0[4];
f1[7]:=f0[9];f1[9]:=f0[7];
f1[2]:=f0[2];f1[5]:=f0[5];f1[8]:=f0[8];
end;
//
// Find the patterns
procedure PatSearch.FindPatterns;
var i,j:Integer; c:Corner; e:Edge; emptyPat:boolean;
begin
for i:= 1 to 5 do
begin
for j:= 1 to 9 do p[i,1,j]:= ColorIndex(Square[i,j].Tag);
RotateSingleFace(p[i,1],p[i,2]); //generate all possible isomorphic patterns
RotateSingleFace(p[i,2],p[i,3]); //by rotating and reflecting the original
RotateSingleFace(p[i,3],p[i,4]);
ReflectSingleFace(p[i,1],p[i,5]);
ReflectSingleFace(p[i,2],p[i,6]);
ReflectSingleFace(p[i,3],p[i,7]);
ReflectSingleFace(p[i,4],p[i,8]);
end;
fcc:=FaceletCube.create(nil);
fcc.Empty;
for c:= URF to DRB do cornerUsed[c]:=false;
for e:= UR to BR do edgeUsed[e]:=false;
for i:= 1 to 5 do
begin
emptyPat:=true;
for j:=1 to 9 do if p[i,1,j]<>NoCol then emptyPat:=false;
if emptyPat=true then ACheckBox[i].Checked:=false;
end;
setCorners(URF);//we start with URF, rest is done recursive
end;
// Find the patterns
// Check if the corners of faces a and b match
// a must be complete, b may be partially incomplete (col=noCol)
function CornerMatch(a,b:singleFace):Boolean;
var i: Integer;tab,tba: array[UCol..NoCol] of ColorIndex;
begin
Result:=true;
i:=1;
while i<=9 do
begin
if b[i]<>noCol then begin tba[b[i]]:=a[i];tab[a[i]]:=b[i] end;
i:=i 2;
end;
i:=1;
while i<=9 do
begin
if (b[i]<>noCol) and ((tba[b[i]]<>a[i]) or (tab[a[i]]<>b[i])) then Result:=false;
i:=i 2;
end;
end;
//
// Return true, if corner does not fit to pattern
function CornerNotOk(c:Corner):Boolean;
var i,j,k:Integer; dir: TurnAxis; a:SingleFace;
x:Face; match: Boolean;
label xxx;
begin
x:=U1;//any value
Result:=false;
for i:= 0 to 2 do
begin
dir:=TurnAxis(CCI[c,i]);//directions to check
case dir of
U: x:=U1;
R: x:=R1;
F: x:=F1;
D: x:=D1;
L: x:=L1;
B: x:=B1;
end;
j:=1;
while true do
begin a[j]:=fcc.PFace^[x];if j=9 then break; Inc(x);Inc(x);Inc(j);Inc(j);end;
match:=false;
for j:= 1 to 5 do
if CheckBox[dir,j].Checked then
for k:= 1 to 8 do if CornerMatch(p[j,k],a) then begin match:=true;goto xxx;end;
xxx:
if match=false then begin Result:= true;break; end;
end;
end;
//
// recursive procedure to set the corners
procedure PatSearch.SetCorners(curPlace: Corner);
var c: Corner; i,j: Integer;
begin
if terminated then exit;
for c:= URF to DRB do
begin
if cornerUsed[c] then continue else cornerUsed[c]:=true;
for i:= 0 to 2 do
begin
for j:= 0 to 2 do fcc.PFace^[CF[curPlace,(j i) mod 3]]:=CCI[c,j];
if cornerNotOk(curPlace) then continue;
if curPlace=DRB then
begin
if not fcc.TwistOk then continue; /////////////////////////////////////////////////////////////////////////
// if fcc.TwistOk then continue;//das ist falsch//////////////////////////////////////////////////////////
setEdges(UR);
end //kanten setzen
else setCorners(Succ(curPlace));//recursion
end;
cornerUsed[c]:=false;
for j:= 0 to 2 do fcc.PFace^[CF[curPlace,j]]:=NoCol;//restore
end;
end;
//
// Check if the edges of faces a and b match
// a must be complete, b may be partially incomplete (col=noCol)
function EdgeMatch(a,b:singleFace):Boolean;
var i: Integer;tab,tba: array[UCol..NoCol] of ColorIndex;
begin
Result:=true;
for i:=1 to 9 do if b[i]<>noCol then begin tba[b[i]]:=a[i];tab[a[i]]:=b[i] end;
for i:= 1 to 9 do
if (b[i]<>noCol) and ((tba[b[i]]<>a[i]) or (tab[a[i]]<>b[i])) then Result:=false;
end;
//
// return true if edge does not fit
function EdgeNotOk(e:Edge):Boolean;
var i,j,k:Integer; dir: TurnAxis; a:SingleFace;
x:Face; match: Boolean;
label xxx;
begin
x:=U1;
Result:=false;
for i:= 0 to 1 do
begin
dir:=TurnAxis(ECI[e,i]);//directions to check
case dir of
U: x:=U1;
R: x:=R1;
F: x:=F1;
D: x:=D1;
L: x:=L1;
B: x:=B1;
end;
j:=1;
while true do
begin a[j]:=fcc.PFace^[x];If j=9 then break;;Inc(j);Inc(x);end;
match:=false;
for j:= 1 to 5 do
if CheckBox[dir,j].Checked then
for k:= 1 to 8 do if EdgeMatch(p[j,k],a) then begin match:=true;goto xxx;end;
xxx:
if match=false then begin Result:= true;break; end;
end;
end;
//
// Check if pattern is continuous
function IsContinuous(e:Edge):Boolean;//does the pattern mach along the edge?
var c:Corner;i,j:Integer;ci:colorIndex;ef0,ef1,cf0,cf1:Face;
begin
Result:=true;
for i:=0 to 1 do
begin
c:=EN[e,i];//edgeNeighbour
ci:=ECI[e,0];//colorIndex of first face of the edge
j:=0;
while ci<>CCI[c,j] do Inc(j);//find face of corner with the same colorIndex
ef0:=EF[e,0];cf0:=CF[c,j];
ci:=ECI[e,1];//colorIndex of second face of the edge
j:=0;
while ci<>CCI[c,j] do Inc(j);
ef1:=EF[e,1];cf1:=CF[c,j];
if (fcc.PFace^[ef0]=fcc.Pface^[cf0]) <> (fcc.PFace^[ef1]=fcc.Pface^[cf1])
then begin Result:=false;break;end;
end;
end;
//
// set the edges recursively
procedure PatSearch.SetEdges(curPlace: Edge);
var cCube:CubieCube;
var e: Edge;i,j: Integer;parityWrong:boolean;
begin
if terminated then exit;
for e:=UR to BR do
begin
if edgeUsed[e] then continue else edgeUsed[e]:=true;
for i:=0 to 1 do
begin
for j:= 0 to 1 do fcc.PFace^[EF[curPlace,(j i) mod 2]]:=ECI[e,j];
if edgeNotOk(curPlace) then continue;
if Form1.CheckBoxContinuous.Checked and not IsContinuous(curPlace) then continue;
if curPlace=BR then
begin
if not fcc.FlipOk then continue;////////////////////////////////////////////////////////////////
//if fcc.FlipOk then continue;//das ist falsch//////////////////////////////////////////////////
cCube:= CubieCube.Create(fcc);
parityWrong:=(cCube.EdgeParityEven and not cCube.CornParityEven) or
(cCube.CornParityEven and not cCube.EdgeParityEven);
cCube.Free;
if parityWrong then continue;
// if not parityWrong then continue; //das ist falsch//////////////////////////////////////////////////
Synchronize(CubeAdd);
end
else setEdges(Succ(curPlace));
end;
edgeUsed[e]:=false;
for j:= 0 to 1 do fcc.PFace^[EF[curPlace,j]]:=NoCol;//restore
end;
end;
//
end.