summaryrefslogtreecommitdiff
path: root/raetselunit.inc
blob: 5d40b2d48116da17172efca605deb2a7335150f1 (plain)
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
{$IFDEF interface}

type
{$IFDEF hochhaus}
  tHochhausRaetsel = class(tFelderRaetsel)
{$ENDIF}
{$IFDEF buchstaben}
  tBuchstabenRaetsel = class(tFelderRaetsel)
{$ENDIF}
  private
//    procedure relativeInhaltsAenderung(diff: longint); override;
//    function absoluteInhaltsAenderung(key: word): boolean; override;
    function  passtZumZeichnen(spalte,zeile: integer): boolean; override;
    function  passt(spalte,zeile: integer): boolean; override;
    function  geloest: boolean; override;
  public
    {$IFDEF buchstaben}
    NBuchst,
    NLeer,
    {$ENDIF}
    NSqrt:               integer;
    AMoeglich,EMoeglich: array of boolean;
    constructor create(aOwner: tForm);
    destructor destroy; override;
    procedure gesamtRaenderErzeugen;
//    function  Loesen(lPos: integer): boolean;
//    procedure leeren;
//    procedure RandErzeugen;
//    function  anzLoesungen(lPos: integer): integer;
//    procedure Image2Bemalen;
  end;

{$IFDEF hochhaus}
function zahlenAlphabetFunktion(i: longint): string;
{$ENDIF}
{$IFDEF buchstaben}
function buchstabenAlphabetFunktion(i: longint): string;
{$ENDIF}
        
{$ENDIF}

{$IFDEF alphabetFunktion}
// function *AlphabetFunktion(i: longint): string;
begin
  if i<0 then
    result:=''
  else if i=0 then
    result:='-'
{$IFDEF hochhaus}
  else
    result:=inttostr(i);
{$ENDIF}
{$IFDEF buchstaben}
  else begin
    result:='';
    while i>0 do begin
      dec(i);
      result:=char(ord('A')+(i mod 26))+result;
      i:=i div 26;
    end;
  end;
{$ENDIF}
end;
{$ENDIF}

{$IFDEF create}
// constructor tHochhausRaetsel.create(aOwner: tForm);
begin
{$IFDEF hochhaus}
  inherited create(aOwner,1,@zahlenAlphabetFunktion);
  spinEdits[1].showHint:=true;
  spinEdits[1].hint:='Anzahl Spalten';
  spinEdits[1].value:=5;
{$ENDIF}
{$IFDEF buchstaben}
  inherited create(aOwner,2,@buchstabenAlphabetFunktion);
  spinEdits[1].showHint:=true;
  spinEdits[1].hint:='Anzahl Buchstaben';
  spinEdits[1].value:=5;
  spinEdits[2].showHint:=true;
  spinEdits[2].hint:='Anzahl Leerfelder';
  spinEdits[2].value:=1;
{$ENDIF}
  aktualisiereGroesze;
end;
{$ENDIF}

{$IFDEF destroy}
// destructor tHochhausRaetsel.destroy;
begin
  inherited destroy;
end;
{$ENDIF}

{$IFDEF passt}
// function  tHochhausRaetsel.passt(spalte,zeile: integer): boolean;
var
  I,KZ,KS: Integer;
  W,S,K:   Longint;
begin
  W:=0;
  S:=0;
  K:=0;
  if sudokuCB.checked then begin
    KZ:=(Zeile div dim)*dim;
    KS:=(Spalte div dim)*dim;
    for I:=0 to dim-1 do begin
      {$IFDEF buchstaben}
      W:=W*(NBuchst+2)+inhalt[Zeile*dim+I]+1;
      S:=S*(NBuchst+2)+inhalt[I*dim+Spalte]+1;
      K:=K*(NBuchst+2)+inhalt[(KZ+(I div NSqrt))*dim+KS+(I mod NSqrt)]+1;
      {$ENDIF}
      {$IFDEF hochhaus}
      W:=W*(dim+1)+inhalt[Zeile*dim+I];
      S:=S*(dim+1)+inhalt[I*dim+Spalte];
      K:=K*(dim+1)+inhalt[(KZ+(I div NSqrt))*dim+KS+(I mod NSqrt)];
      {$ENDIF}
    end;
    result:=
      {$IFDEF buchstaben}
      AMoeglich[K*(NBuchst+1)] and
      EMoeglich[S*(NBuchst+1)+Rand[Spalte]] and
      AMoeglich[W*(NBuchst+1)+Rand[dim+Zeile]] and
      AMoeglich[S*(NBuchst+1)+Rand[2*dim+Spalte]] and
      EMoeglich[W*(NBuchst+1)+Rand[3*dim+Zeile]];
      {$ENDIF}
      {$IFDEF hochhaus}
      AMoeglich[K*(dim+1)] and
      EMoeglich[S*(dim+1)+Rand[Spalte]] and
      AMoeglich[W*(dim+1)+Rand[dim+Zeile]] and
      AMoeglich[S*(dim+1)+Rand[2*dim+Spalte]] and
      EMoeglich[W*(dim+1)+Rand[3*dim+Zeile]];
      {$ENDIF}
  end
  else begin
    for I:=0 to dim-1 do begin
      {$IFDEF buchstaben}
      W:=W*(NBuchst+2)+inhalt[Zeile*dim+I]+1;
      S:=S*(NBuchst+2)+inhalt[I*dim+Spalte]+1;
      {$ENDIF}
      {$IFDEF hochhaus}
      W:=W*(dim+1)+inhalt[Zeile*dim+I];
      S:=S*(dim+1)+inhalt[I*dim+Spalte];
      {$ENDIF}
    end;
    result:=
      {$IFDEF buchstaben}
      EMoeglich[S*(NBuchst+1)+Rand[Spalte]] and
      AMoeglich[W*(NBuchst+1)+Rand[dim+Zeile]] and
      AMoeglich[S*(NBuchst+1)+Rand[2*dim+Spalte]] and
      EMoeglich[W*(NBuchst+1)+Rand[3*dim+Zeile]];
      {$ENDIF}
      {$IFDEF hochhaus}
      EMoeglich[S*(dim+1)+Rand[Spalte]] and
      AMoeglich[W*(dim+1)+Rand[dim+Zeile]] and
      AMoeglich[S*(dim+1)+Rand[2*dim+Spalte]] and
      EMoeglich[W*(dim+1)+Rand[3*dim+Zeile]];
      {$ENDIF}
  end;
  if diagonalenCB.checked then begin
    if Zeile=Spalte then begin
      W:=0;
      {$IFDEF buchstaben}
      for I:=0 to dim-1 do
        W:=W*(NBuchst+2)+inhalt[I*(dim+1)]+1;
      Result:=Result and AMoeglich[W*(NBuchst+1)];
      {$ENDIF}
      {$IFDEF hochhaus}
      for I:=0 to dim-1 do
        W:=W*(dim+1)+inhalt[I*(dim+1)];
      result:=result and AMoeglich[W*(dim+1)];
      {$ENDIF}
    end;
    if Zeile+Spalte=dim-1 then begin
      W:=0;
      {$IFDEF buchstaben}
      for I:=0 to dim-1 do
        W:=W*(NBuchst+2)+inhalt[(I+1)*(dim-1)]+1;
      Result:=Result and AMoeglich[W*(NBuchst+1)];
      {$ENDIF}
      {$IFDEF hochhaus}
      for I:=0 to dim-1 do
        W:=W*(dim+1)+inhalt[(I+1)*(dim-1)];
      Result:=Result and AMoeglich[W*(dim+1)];
      {$ENDIF}
    end;
  end;
end;
{$ENDIF}

{$IFDEF geloest}
// function  tHochhausRaetsel.geloest: boolean;
var
  i,j: integer;
begin
  result:=false;
  for i:=0 to dim-1 do
    for j:=0 to dim-1 do
      if (not passt(i,j)) or (inhalt[i+dim*j]<0)
    {$IFDEF hochhaus}
    or (inhalt[I+dim*j]=0)
    {$ENDIF}
    then exit;
  result:=true;
end;
{$ENDIF}

{$IFDEF gesamtRaenderErzeugen}
// procedure tHochhausRaetsel.gesamtRaenderErzeugen;
var
  I,J,K,Nullen:      Integer;
  {$IFNDEF buchstaben}
  AZ,
  {$ENDIF}
  AR:                Integer;
  AK,EK,NAK,NEK:     Longint;
  Nums:              tLongintArray;
  B:                 Boolean;
  Basis,Faktor:      Integer;
  Schritt:           Longint;
  dat:               File of Cardinal;
  lw1,lw2,L:         Cardinal;

const dat_name =
{$IFDEF buchstaben}
  'Buchstabenraetsel.dat';
{$ENDIF}
{$IFDEF hochhaus}
  'Hochhausraetsel.dat';
{$ENDIF}

function calcNums(I: Longint): tLongintArray;
var
  J: Integer;
begin
  Setlength(Result,dim);
  For J:=0 to dim-1 do begin
    Result[J]:=I mod Basis;
    I:=I div Basis;
  end;
end;

function calcIndex(Nums: TLongintArray): Longint;
var
  j: integer;
begin
  Result:=0;
  For j:=0 to dim-1 do
    Result:=Result*Basis + Nums[dim-J-1];
end;

function swapIndex(I: Longint): Longint;
var
  j: integer;
begin
  Result:=0;
  for J:=0 to dim-1 do begin
    Result:=Result*Basis+(I mod Basis);
    I:=I div Basis;
  end;
end;

begin
  {$IFDEF buchstaben}
  Basis:=NBuchst+2;
  Faktor:=NBuchst+1;
  {$ENDIF}
  {$IFDEF hochhaus}
  Basis:=dim+1;
  Faktor:=dim+1;
  {$ENDIF}
  AK:=round(power(Basis,dim)*Faktor);

  if (length(AMoeglich) = AK) and
     (length(EMoeglich) = AK) then
    exit;

  if fileexists(extractfilepath(application.exename)+dat_name) then begin
    assignfile(dat,extractfilepath(application.exename)+dat_name);
    reset(dat);
    while not eof(dat) do begin
      read(dat,lw1);
      if lw1=AK then begin
        Setlength(AMoeglich,AK);
        Setlength(EMoeglich,AK);
        for L:=0 to length(AMoeglich)-1 do begin
          if L mod 32 = 0 then
            if not eof(dat) then
              read(dat,lw2)
            else begin
              messageDlg('Frühzeitiges Dateiende!',mterror,[mbOk],0);
              exit;
            end;
          AMoeglich[L]:=odd(lw2 shr (L mod 32));
        end;
        For L:=0 to length(EMoeglich)-1 do begin
          if L mod 32 = 0 then
            if not eof(dat) then
              read(dat,lw2)
            else begin
              messageDlg('Frühzeitiges Dateiende!',mterror,[mbOk],0);
              exit;
            end;
          EMoeglich[L]:=odd(lw2 shr (L mod 32));
        end;
        exit;
      end
      else
        Seek(dat,Filepos(dat)+((lw1-1) div 32 +1)*2);
    end;
    closefile(dat);
  end;

  Progressbar1.Visible:=true;
  Progressbar1.Min:=0;
  Progressbar1.Max:=1000;
  Progressbar1.Position:=0;

  Setlength(AMoeglich,AK);
  Setlength(EMoeglich,AK);
  For AK:=0 to length(AMoeglich)-1 do begin
    AMoeglich[AK]:=False;
    EMoeglich[AK]:=False;
  end;
  Setlength(Nums,0);

  Schritt:=max(1,round((length(AMoeglich) div Faktor) / Progressbar1.Max));
  For AK:=0 to length(AMoeglich) div Faktor -1 do begin
    if AK mod Schritt = 0 then
     Progressbar1.StepIt;
    Nums:=calcNums(AK);
    B:=true;
    {$IFDEF buchstaben}
    Nullen:=NLeer;
    For I:=0 to length(Nums)-1 do
     begin
      B:=B and (Nums[I]<>0);
      if Nums[I]=1 then dec(Nullen)
       else
        For J:=0 to I-1 do
         B:=B and (Nums[I]<>Nums[J]);
     end;
    B:=B and (Nullen>=0);
    {$ENDIF}
    {$IFDEF hochhaus}
    For I:=0 to length(Nums)-1 do begin
      B:=B and (Nums[I]<>0);
      For J:=0 to I-1 do
       B:=B and (Nums[I]<>Nums[J]);
    end;
    {$ENDIF}
    if B then begin
      EK:=swapIndex(AK);
      AR:=0;
      {$IFNDEF buchstaben}
      AZ:=0;
      {$ENDIF}
      For I:=0 to dim-1 do begin
        {$IFDEF buchstaben}
        if Nums[I]>1 then begin
          AR:=Nums[I]-1;
          break;
        end;
        {$ENDIF}
        {$IFDEF hochhaus}
        if Nums[I]>AZ then begin
          inc(AR);
          AZ:=Nums[I];
        end;
        {$ENDIF}
      end;

      AMoeglich[AK*Faktor+AR]:=true;
      EMoeglich[EK*Faktor+AR]:=true;
      AMoeglich[AK*Faktor]:=true;
      EMoeglich[EK*Faktor]:=true;
    end;
  end;

  Progressbar1.Position:=0;
  Schritt:=Max(round(((length(AMoeglich) div Faktor)*dim) / Progressbar1.Max),1);
  For Nullen:=1 to dim do
    For AK:=0 to length(AMoeglich) div Faktor -1 do begin
      if AK mod Schritt = 0 then
        Progressbar1.StepIt;
      Nums:=calcNums(AK);
      J:=Nullen;
      For I:=0 to dim-1 do
        if Nums[I]=0 then
          dec(J);
      if J<>0 then
        continue;
      EK:=swapIndex(AK);
      For I:=0 to dim-1 do
        if Nums[I]=0 then begin
          For J:=1 to Basis-1 do begin
            Nums[I]:=J;
            NAK:=calcIndex(Nums);
            NEK:=swapIndex(NAK);
            For K:=0 to Faktor-1 do begin
              AMoeglich[AK*Faktor+K]:=AMoeglich[AK*Faktor+K] or AMoeglich[NAK*Faktor+K];
              EMoeglich[EK*Faktor+K]:=EMoeglich[EK*Faktor+K] or EMoeglich[NEK*Faktor+K];
            end;
          end;
          break;
        end;
    end;
  Progressbar1.Visible:=False;

  assignfile(dat,extractfilepath(application.exename)+dat_name);
  if Fileexists(extractfilepath(application.exename)+dat_name) then begin
    reset(dat);
    Seek(dat,Filesize(dat));
  end
  else
    Rewrite(dat);

  lw1:=length(AMoeglich);
  write(dat,lw1);
  lw1:=0;
  For I:=0 to length(AMoeglich)-1 do begin
    lw1:=lw1 or (Byte(AMoeglich[I]) shl (I mod 32));
    if ((I mod 32) = 31) or (I = length(AMoeglich)-1) then begin
      write(dat,lw1);
      lw1:=0;
    end;
  end;
  For I:=0 to length(EMoeglich)-1 do begin
    lw1:=lw1 or (Byte(EMoeglich[I]) shl (I mod 32));
    if ((I mod 32) = 31) or (I = length(EMoeglich)-1) then begin
      write(dat,lw1);
      lw1:=0;
    end;
  end;
  closefile(dat);
end;
{$ENDIF}

{$IFDEF passtZumZeichnen}
// function  tHochhausRaetsel.passtZumZeichnen(spalte,zeile: integer): boolean;
var
  I,KS,KZ: Integer;
  W,S:     Longint;
begin
  {$IFDEF buchstaben}
  if inhalt[Zeile*dim+Spalte]=-1 then begin
  {$ENDIF}
  {$IFDEF hochhaus}
  if inhalt[Zeile*dim+Spalte]=0 then begin
  {$ENDIF}
    result:=false;
    exit;
  end;
  W:=0;
  S:=0;
  for I:=0 to dim-1 do begin
    {$IFDEF buchstaben}
    W:=W*(NBuchst+2)+inhalt[Zeile*dim+I]+1;
    S:=S*(NBuchst+2)+inhalt[I*dim+Spalte]+1;
    {$ENDIF}
    {$IFDEF hochhaus}
    W:=W*(dim+1)+inhalt[Zeile*dim+I];
    S:=S*(dim+1)+inhalt[I*dim+Spalte];
    {$ENDIF}
  end;
  Result:=
    {$IFDEF buchstaben}
    (EMoeglich[S*(NBuchst+1)+Rand[Spalte]] or not EMoeglich[S*(NBuchst+1)]) and
    (AMoeglich[W*(NBuchst+1)+Rand[dim+Zeile]] or not AMoeglich[W*(NBuchst+1)]) and
    (AMoeglich[S*(NBuchst+1)+Rand[2*dim+Spalte]] or not AMoeglich[S*(NBuchst+1)]) and
    (EMoeglich[W*(NBuchst+1)+Rand[3*dim+Zeile]] or not EMoeglich[W*(NBuchst+1)]);
    {$ENDIF}
    {$IFDEF hochhaus}
    (EMoeglich[S*(dim+1)+Rand[Spalte]] or not EMoeglich[S*(dim+1)]) and
    (AMoeglich[W*(dim+1)+Rand[dim+Zeile]] or not AMoeglich[W*(dim+1)]) and
    (AMoeglich[S*(dim+1)+Rand[2*dim+Spalte]] or not AMoeglich[S*(dim+1)]) and
    (EMoeglich[W*(dim+1)+Rand[3*dim+Zeile]] or not EMoeglich[W*(dim+1)]);
    {$ENDIF}
  {$IFDEF buchstaben}
  if inhalt[Zeile*dim+Spalte]=0 then
    W:=NLeer
  else
  {$ENDIF}
    W:=1;
  if diagonalenCB.checked then begin
    if Zeile=Spalte then begin
      S:=W;
      for I:=0 to dim-1 do
        if (I<>Zeile) and (inhalt[I*(dim+1)]=inhalt[Zeile*dim+Spalte]) then
          dec(S);
      Result:=Result and (S>0);
    end;
    if Zeile+Spalte=dim-1 then begin
      S:=W;
      For I:=0 to dim-1 do
        if (I<>Zeile) and (inhalt[(I+1)*(dim-1)]=inhalt[Zeile*dim+Spalte]) then
          dec(S);
      Result:=Result and (S>0);
    end;
  end;
  if sudokuCB.checked then begin
    KZ:=(Zeile div NSqrt)*NSqrt;
    KS:=(Spalte div NSqrt)*NSqrt;
    S:=W+1;
    For I:=0 to dim-1 do
      if inhalt[(KZ+(I div NSqrt))*dim+KS+(I mod NSqrt)]=inhalt[Zeile*dim+Spalte] then
        dec(S);
    Result:=Result and (S>0);
  end;
  S:=W;
  For I:=0 to dim-1 do
    if (I<>Zeile) and (inhalt[I*dim+Spalte]=inhalt[Zeile*dim+Spalte]) then
      dec(S);
  Result:=Result and (S>0);
  S:=W;
  For I:=0 to dim-1 do
    if (I<>Spalte) and (inhalt[Zeile*dim+I]=inhalt[Zeile*dim+Spalte]) then
      dec(S);
  Result:=Result and (S>0);
end;
{$ENDIF}