summaryrefslogtreecommitdiff
path: root/raetselunit.pas
blob: b8f71717863fdd4366f948cd0b04bc7cae7b90bb (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
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
unit raetselunit;

{$mode objfpc}{$H+}

interface

// TODO: Cross-Compilieren

uses
  Classes, SysUtils, Forms, Spin, Controls, StdCtrls, ExtCtrls, LCLType,
  lowlevelunit, Messages, ComCtrls, Graphics;

type
  tAlphabetFunktion = function(i: longint): string;

  tButtonWithArrowKeys = class(tButton)
  private
    procedure wMGetDlgCode(var msg: tWMGetDlgCode); message wM_GETDLGCODE;
  end;

  tSmarterCheckBox = class(tCheckBox)
    function width: longint;
  end;

  tZug = record
    position:       integer;
    vorher:         integer;
    vorherFarbe,
    nachherFarbe,
    vorherMalFarbe: tColor;
  end;

  tOnSetCaption = procedure(c: string) of object;

  tRaetsel = class
  private
    besitzer:         tForm;
    farbWahlFlaeche,
      zeichenFlaeche: tImage;
    erzeugeBtn,
      speichernBtn,
      ladenBtn,
      druckenBtn:     tButtonWithArrowKeys;
    zufallSE:         tSpinEdit;
    progressBar1:     tProgressBar;
    aktuelleFarbe:    tColor;
    function besitzerHoehe: longint; dynamic;
    function besitzerBreite: longint; dynamic;
    procedure zeichenFlaecheNeuKreieren;
    procedure onKeyDown(sender: tObject; var key: word; shiftState: tShiftState); dynamic; abstract;
    procedure onMouseDown(sender: tObject; button: tMouseButton; shiftState: tShiftState; x,y: longint); dynamic; abstract;
    procedure onFarbWahlMouseDown(sender: tObject; button: tMouseButton; shiftState: tShiftState; x,y: longint);
    procedure onFarbWahlMouseMove(sender: tObject; shiftState: tShiftState; x,y: longint);
    procedure onFarbWahlMouseUp(sender: tObject; button: tMouseButton; shiftState: tShiftState; x,y: longint);
    procedure erzeugeOnClick(sender: tObject);
    procedure speichernOnClick(sender: tObject);
    procedure ladenOnClick(sender: tObject);
    procedure druckenOnClick(sender: tObject);
    procedure farbWahlFlaecheBemalen;
    procedure loeschen; dynamic; abstract;
    procedure leeren; dynamic; abstract;
    procedure vorbereiten; dynamic; abstract;
    function  loesen(lPos: longint): boolean; dynamic; abstract;
    function  anzLoesungen(lPos: longint): longint; dynamic; abstract;
    function  passtZumZeichnen(spalte,zeile: integer): boolean; dynamic; abstract;
    function  passt(spalte,zeile: integer): boolean; dynamic; abstract;
    function  geloest: boolean; dynamic; abstract;
    procedure randErzeugen; dynamic; abstract;
    procedure startFelderFestlegen; dynamic; abstract;
    procedure speichern(var datei: file); dynamic;
    procedure laden(var datei: file); dynamic;
  public
    onSetCaption: tOnSetCaption;
    constructor create(aOwner: tForm);
    destructor destroy; override;
    procedure zeichnen(cursor: boolean = true); dynamic; abstract;
  end;

  tFelderRaetsel = class(tRaetsel)
  private
    spinEdits:                         array of tSpinEdit;
    diagonalenCB,sudokuCB,puzzleCB:    tSmarterCheckBox;
    groeszen,inhalt,rand:              array of longint;
    invPuzzleTeile:                    array of tIntPoint;        // Ort -> [Teil, Kachel]
    puzzleTeile:                       array of array of longint; // [Teil,Kachel] -> Ort
    AMoeglich,EMoeglich:               array of boolean;
    dim,schriftGroesze,
      cursorPosition:                  longint;
    zellGroesze:                       extended;
    uebersetze:                        tAlphabetFunktion;
    feldFarben:                        array of tColor;
    startFeld:                         array of boolean;
    zuege:                             array of tZug;
    procedure anzSEsOnChange(sender: tObject);
    procedure cbOnChange(sender: tObject);
    procedure onKeyDown(sender: tObject; var key: word; shiftState: tShiftState); override;
    procedure onMouseDown(sender: tObject; button: tMouseButton; shiftState: tShiftState; x,y: longint); override;
    procedure aktualisiereGroesze;
    procedure loeschen; override;
    procedure leeren; override;
    procedure vorbereiten; override;
    procedure aktualisiereZeichenflaechenGroesze;
    function besitzerHoehe: longint; override;
    function besitzerBreite: longint; override;
    procedure schreibeZentriert(x,y,i: longint);
    procedure relativeInhaltsAenderung(diff: longint); dynamic; abstract;
    function naechsterWert(pos: longint): boolean; dynamic; abstract;
    function absoluteInhaltsAenderung(key: word): boolean; dynamic; abstract;
    procedure gesamtRaenderErzeugen; dynamic; abstract;
    procedure startFelderFestlegen; override;
    procedure alsZugSpeichern;
    procedure speichern(var datei: file); override;
    procedure laden(var datei: file); override;
    procedure findePuzzelierung;
  public
    constructor create(aOwner: tForm; anzInhTypen: longint; alphabetFunktion: tAlphabetFunktion);
    destructor destroy; override;
    procedure zeichnen(cursor: boolean = true); override;
  end;

{$DEFINE interface}

{$DEFINE hochhaus}
{$I raetselunit.inc}
{$UNDEF hochhaus}

{$DEFINE buchstaben}
{$I raetselunit.inc}
{$UNDEF buchstaben}

{$UNDEF interface}

function  farbVerlauf(wo: extended): tColor;
function  rgb2TColor(r,g,b: extended): tColor; inline;

const
  spacing = 2;

implementation

uses
  math, dialogs, lclintf, extDlgs, matheunit;

{$DEFINE alphabetFunktion}
function zahlenAlphabetFunktion(i: longint): string;
{$DEFINE hochhaus}
{$I raetselunit.inc}
{$UNDEF hochhaus}
function buchstabenAlphabetFunktion(i: longint): string;
{$DEFINE buchstaben}
{$I raetselunit.inc}
{$UNDEF buchstaben}
{$UNDEF alphabetFunktion}

function  farbVerlauf(wo: extended): tColor;
const
  r: array[0..6] of extended = (0.5,0.9,0.9,  0,  0,0.2,0);
  g: array[0..6] of extended = (  0,0.5,0.9,0.7,0.7,0.2,0);
  b: array[0..6] of extended = (0.7,  0,  0,  0,0.7,  1,0);
var
  i: integer;
begin
  wo:=wo*(length(r)-1);
  i:=floor(wo);
  wo:=wo-i;
  if i<0 then begin
    result:=rgb2TColor(r[0],g[0],b[0]);
    exit;
  end;
  if i>=(length(r)-1) then begin
    result:=rgb2TColor(r[length(r)-1],g[length(r)-1],b[length(r)-1]);
    exit;
  end;
  result:=rgb2TColor(r[i+1]*wo+r[i]*(1-wo),
                     g[i+1]*wo+g[i]*(1-wo),
                     b[i+1]*wo+b[i]*(1-wo));
end;

function  rgb2TColor(r,g,b: extended): tColor;
begin
  result:=max(0,min($FF,round(r*$100))) or
          (max(0,min($FF,round(g*$100))) shl 8) or
          (max(0,min($FF,round(b*$100))) shl 16);
end;

// tButtonWithArrowKeys ********************************************************

procedure tButtonWithArrowKeys.wMGetDlgCode(var msg: tWMGetDlgCode);// message wM_GETDLGCODE;
begin
  inherited;
  msg.result := msg.result or DLGC_WANTARROWS;
end;

// tSmarterCheckBox ************************************************************

function tSmarterCheckBox.width: longint;
var
  c: tCanvas;
begin
  c:=tCanvas.create;
  c.handle:=getDC(handle);
  c.font:=font;
  result:=height+c.textWidth(caption);
  releaseDC(handle,c.handle);
  c.free;
end;

// tRaetsel ********************************************************************

constructor tRaetsel.create(aOwner: tForm);
begin
  inherited create;
  randomize;
  besitzer:=aOwner;
  zeichenFlaeche:=tImage.create(besitzer);
  zeichenFlaeche.parent:=besitzer;
  zeichenFlaeche.onMouseDown:=@onMouseDown;
  farbWahlFlaeche:=tImage.create(besitzer);
  farbWahlFlaeche.parent:=besitzer;
  farbWahlFlaeche.onMouseDown:=@onFarbWahlMouseDown;
  farbWahlFlaeche.onMouseMove:=@onFarbWahlMouseMove;
  farbWahlFlaeche.onMouseUp:=@onFarbWahlMouseUp;
  erzeugeBtn:=tButtonWithArrowKeys.create(besitzer);
  erzeugeBtn.parent:=besitzer;
  erzeugeBtn.left:=spacing;
  erzeugeBtn.top:=spacing;
  erzeugeBtn.caption:='Erzeugen!';
  erzeugeBtn.onClick:=@erzeugeOnClick;
  erzeugeBtn.onKeyDown:=@onKeyDown;
  speichernBtn:=tButtonWithArrowKeys.create(besitzer);
  speichernBtn.parent:=besitzer;
  speichernBtn.left:=erzeugeBtn.left+erzeugeBtn.width+spacing;
  speichernBtn.top:=spacing;
  speichernBtn.caption:='Speichern!';
  speichernBtn.onClick:=@speichernOnClick;
  speichernBtn.onKeyDown:=@onKeyDown;
  ladenBtn:=tButtonWithArrowKeys.create(besitzer);
  ladenBtn.parent:=besitzer;
  ladenBtn.left:=speichernBtn.left+speichernBtn.width+spacing;
  ladenBtn.top:=spacing;
  ladenBtn.caption:='Laden!';
  ladenBtn.onClick:=@ladenOnClick;
  ladenBtn.onKeyDown:=@onKeyDown;
  druckenBtn:=tButtonWithArrowKeys.create(besitzer);
  druckenBtn.parent:=besitzer;
  druckenBtn.left:=ladenBtn.left+ladenBtn.width+spacing;
  druckenBtn.top:=spacing;
  druckenBtn.caption:='Drucken!';
  druckenBtn.onClick:=@druckenOnClick;
  druckenBtn.onKeyDown:=@onKeyDown;
  zufallSE:=tSpinEdit.create(besitzer);
  zufallSE.parent:=besitzer;
  zufallSE.top:=erzeugeBtn.top+erzeugeBtn.height+spacing;
  zufallSE.left:=spacing;
  zufallSE.width:=64;
  zufallSE.minValue:=0;
  zufallSE.maxValue:=99999;
  zufallSE.value:=random(zufallSE.maxValue+1);
  zufallSE.showHint:=true;
  zufallSE.hint:='Nummer';
  progressBar1:=tProgressBar.create(besitzer);
  progressBar1.visible:=false;
  progressBar1.smooth:=true;
  progressBar1.parent:=besitzer;
end;

destructor tRaetsel.destroy;
begin
  zeichenFlaeche.free;
  farbWahlFlaeche.free;
  inherited destroy;
end;

function tRaetsel.besitzerHoehe: longint;
begin
  result:=zeichenFlaeche.height+zeichenFlaeche.top+spacing;
end;

function tRaetsel.besitzerBreite: longint;
begin
  result:=
    max(
      zeichenFlaeche.width+zeichenFlaeche.left,
      druckenBtn.width+druckenBtn.left
    )+spacing;
end;

procedure tRaetsel.zeichenFlaecheNeuKreieren;
var
  i: tImage;
begin
  i:=tImage.create(besitzer);
  i.parent:=zeichenFlaeche.parent;
  i.left:=zeichenFlaeche.left;
  i.top:=zeichenFlaeche.top;
  i.width:=zeichenFlaeche.width;
  i.height:=zeichenFlaeche.height;
  i.canvas.font.size:=zeichenFlaeche.canvas.font.size;
  i.onMouseDown:=zeichenFlaeche.onMouseDown;
  zeichenFlaeche.free;
  zeichenFlaeche:=i;

  i:=tImage.create(besitzer);
  i.parent:=farbWahlFlaeche.parent;
  i.left:=farbWahlFlaeche.left;
  i.top:=farbWahlFlaeche.top;
  i.width:=farbWahlFlaeche.width;
  i.height:=farbWahlFlaeche.height;
  i.onMouseDown:=farbWahlFlaeche.onMouseDown;
  i.onMouseMove:=farbWahlFlaeche.onMouseMove;
  i.onMouseUp:=farbWahlFlaeche.onMouseUp;
  farbWahlFlaeche.free;
  farbWahlFlaeche:=i;
  farbWahlFlaecheBemalen;
end;

procedure tRaetsel.onFarbWahlMouseDown(sender: tObject; button: tMouseButton; shiftState: tShiftState; x,y: longint);
begin
  if button<>mbLeft then
    exit;
  aktuelleFarbe:=farbVerlauf(x/farbWahlFlaeche.width);
  farbWahlFlaecheBemalen;
end;

procedure tRaetsel.onFarbWahlMouseMove(sender: tObject; shiftState: tShiftState; x,y: longint);
begin
  if not(ssLeft in shiftState) then
    exit;
  aktuelleFarbe:=farbVerlauf(x/farbWahlFlaeche.width);
  farbWahlFlaecheBemalen;
end;

procedure tRaetsel.onFarbWahlMouseUp(sender: tObject; button: tMouseButton; shiftState: tShiftState; x,y: longint);
begin
  if button<>mbLeft then
    exit;
  aktuelleFarbe:=farbVerlauf(x/farbWahlFlaeche.width);
  farbWahlFlaecheBemalen;
end;

procedure tRaetsel.erzeugeOnClick(sender: tObject);
begin
  loeschen;
  randSeed:=zufallSE.value;
  if assigned(onSetCaption) then
    onSetCaption(intToStr(zufallSE.value));
  zufallSE.value:=random(zufallSE.maxValue+1);
  vorbereiten;
  loesen(-1);
  randErzeugen;
  leeren;
  startFelderFestlegen;
  zeichnen;
end;

procedure tRaetsel.speichernOnClick(sender: tObject);
var
  saveDialog1: tSaveDialog;
  dat:         file;
begin
  saveDialog1:=tSaveDialog.create(besitzer);
  if saveDialog1.execute then begin
    assignFile(dat,saveDialog1.fileName);
    rewrite(dat,1);
    speichern(dat);
    closeFile(dat);
  end;
  saveDialog1.free;
end;

procedure tRaetsel.ladenOnClick(sender: tObject);
var
  openDialog1: tOpenDialog;
  dat:         file;
begin
  openDialog1:=tOpenDialog.create(besitzer);
  if openDialog1.execute then begin
    assignFile(dat,openDialog1.fileName);
    reset(dat,1);
    laden(dat);
    assert(eof(dat),'Zu viele Daten in Datei!');
    closeFile(dat);
    zeichnen;
  end;
  openDialog1.free;
end;

procedure tRaetsel.druckenOnClick(sender: tObject);
var
  safePictureDialog1: tSavePictureDialog;
  img:                tImage;
begin
  safePictureDialog1:=TSavePictureDialog.create(besitzer);
  if safePictureDialog1.execute then begin
    img:=zeichenFlaeche;
    zeichenFlaeche:=tImage.create(img.parent);
    zeichenFlaeche.width:=img.width;
    zeichenFlaeche.height:=img.height;
    zeichenFlaeche.canvas.font.size:=img.canvas.font.size;
    zeichnen(false);
    zeichenFlaeche.picture.saveToFile(safePictureDialog1.fileName);
    zeichenFlaeche.free;
    zeichenFlaeche:=img;
    img:=nil;
  end;
  safePictureDialog1.free;
end;

procedure tRaetsel.farbWahlFlaecheBemalen;
var
  i: integer;
begin
  farbWahlFlaeche.canvas.pen.color:=aktuelleFarbe;
  farbWahlFlaeche.canvas.brush.color:=aktuelleFarbe;
  farbWahlFlaeche.canvas.rectangle(0,0,farbWahlFlaeche.width,farbWahlFlaeche.height div 2);
  for i:=0 to farbWahlFlaeche.width-1 do begin
    farbWahlFlaeche.canvas.pen.color:=farbVerlauf(i/farbWahlFlaeche.width);
    farbWahlFlaeche.canvas.moveTo(i,farbWahlFlaeche.height div 2);
    farbWahlFlaeche.canvas.lineTo(i,farbWahlFlaeche.height);
  end;
end;

procedure tRaetsel.speichern(var datei: file);
begin
  blockWrite(datei,aktuelleFarbe,sizeOf(aktuelleFarbe));
end;

procedure tRaetsel.laden(var datei: file);
begin
  blockRead(datei,aktuelleFarbe,sizeOf(aktuelleFarbe));
end;

// tFelderRaetsel **************************************************************

constructor tFelderRaetsel.create(aOwner: tForm; anzInhTypen: longint; alphabetFunktion: tAlphabetFunktion);
var
  i: longint;
begin
  inherited create(aOwner);
  uebersetze:=alphabetFunktion;
  cursorPosition:=-1;
  setLength(spinEdits,anzInhTypen+1);
  setLength(groeszen,length(spinEdits)-1);
  for i:=0 to length(spinEdits)-1 do begin
    spinEdits[i]:=tSpinEdit.create(besitzer);
    spinEdits[i].onKeyDown:=@onKeyDown;
    if i=1 then
      groeszen[i-1]:=5
    else if i>1 then
      groeszen[i-1]:=1;
    with spinEdits[i] do begin
      parent:=besitzer;
      top:=erzeugeBtn.top+erzeugeBtn.height+spacing;
      if i=0 then
        left:=zufallSE.left + zufallSE.width
      else begin
        left:=spinEdits[i-1].left + spinEdits[i-1].width;
        value:=groeszen[i-1];
      end;
      left:=left+spacing;
      onChange:=@anzSEsOnChange;
      onKeyDown:=@self.onKeyDown;
      tag:=i;
    end;
  end;
  spinEdits[0].showHint:=true;
  spinEdits[0].hint:='Schriftgröße';
  spinEdits[0].value:=14;
  diagonalenCB:=tSmarterCheckBox.create(besitzer);
  diagonalenCB.parent:=besitzer;
  diagonalenCB.caption:='Diagonalen';
  diagonalenCB.top:=spinEdits[length(spinEdits)-1].top+spinEdits[length(spinEdits)-1].height+spacing;
  diagonalenCB.left:=spacing;
  diagonalenCB.onKeyDown:=@onKeyDown;
  diagonalenCB.onChange:=@cbOnChange;
  sudokuCB:=tSmarterCheckBox.create(besitzer);
  sudokuCB.parent:=besitzer;
  sudokuCB.caption:='Sudoku';
  sudokuCB.top:=diagonalenCB.top;
  sudokuCB.left:=diagonalenCB.left+diagonalenCB.width+spacing;
  sudokuCB.onKeyDown:=@onKeyDown;
  sudokuCB.onChange:=@cbOnChange;
  puzzleCB:=tSmarterCheckBox.create(besitzer);
  puzzleCB.parent:=besitzer;
  puzzleCB.caption:='Puzzle';
  puzzleCB.top:=sudokuCB.top;
  puzzleCB.left:=sudokuCB.left+sudokuCB.width+spacing;
  puzzleCB.onKeyDown:=@onKeyDown;
  puzzleCB.onChange:=@cbOnChange;
  farbWahlFlaeche.left:=spacing;
  farbWahlFlaeche.top:=diagonalenCB.top+diagonalenCB.height+spacing;
  farbWahlFlaeche.height:=16;
  zeichenFlaeche.left:=spacing;
  zeichenFlaeche.top:=farbWahlFlaeche.top+farbWahlFlaeche.height+spacing;
  aktualisiereGroesze;
end;

destructor tFelderRaetsel.destroy;
begin
  inherited destroy;
end;

procedure tFelderRaetsel.anzSEsOnChange(sender: tObject);
begin
  if ((sender as tSpinEdit).tag = 0) and
     ((sender as tSpinEdit).value <> schriftGroesze) then begin
    schriftGroesze:=(sender as tSpinEdit).value;
    aktualisiereZeichenflaechenGroesze
  end
  else if (sender as tSpinEdit).value <> groeszen[(sender as tSpinEdit).tag-1] then
    aktualisiereGroesze;
end;

procedure tFelderRaetsel.cbOnChange(sender: tObject);
var
  i: longint;
begin
  if sender=sudokuCB then begin
    puzzleCB.enabled:=not sudokuCB.checked;
    if puzzleCB.checked and not puzzleCB.enabled then
      puzzleCB.checked:=false;
  end;
  if sender=puzzleCB then begin
    sudokuCB.enabled:=(intRoot(dim)>1) and not puzzleCB.checked;
    if sudokuCB.checked and not sudokuCB.checked then
      sudokuCB.checked:=false;
  end;
  findePuzzelierung;
  zeichnen;
end;

procedure tFelderRaetsel.onKeyDown(sender: tObject; var key: word; shiftState: tShiftState);
begin
  if ssCtrl in shiftState then begin
    if (key=ord('Z')) and (length(zuege)>0) then begin
      cursorPosition:=zuege[length(zuege)-1].position;
      inhalt[cursorPosition]:=zuege[length(zuege)-1].vorher;
      feldFarben[cursorPosition]:=zuege[length(zuege)-1].vorherFarbe;
      aktuelleFarbe:=zuege[length(zuege)-1].vorherMalFarbe;
      setLength(zuege,length(zuege)-1);
      farbWahlFlaecheBemalen;
    end;
    zeichnen;
    exit;
  end;

  if not absoluteInhaltsAenderung(key) then
    case key of
      VK_DOWN:
        cursorPosition:=
          (cursorPosition mod dim) +
          dim*min(dim-1,cursorPosition div dim + 1);
      VK_UP:
        cursorPosition:=
          (cursorPosition mod dim) +
          dim*max(0,cursorPosition div dim - 1);
      VK_LEFT:
        cursorPosition:=
          max(0,cursorPosition mod dim - 1) +
          dim*(cursorPosition div dim);
      VK_RIGHT:
        cursorPosition:=
          min(dim-1,cursorPosition mod dim + 1) +
          dim*(cursorPosition div dim);
      33,107,187:
        if (cursorPosition>=0) and (cursorPosition<dim*dim) then
          relativeInhaltsAenderung(1);
      34,109,189:
        if (cursorPosition>=0) and (cursorPosition<dim*dim) then
          relativeInhaltsAenderung(-1);
      else
        exit;
    end{of case};
  key:=0;
  zeichnen;
end;

procedure tFelderRaetsel.onMouseDown(sender: tObject; button: tMouseButton; shiftState: tShiftState; x,y: longint);
begin
  x:=floor(x/zellGroesze-1);
  y:=floor(y/zellGroesze-1);
  if (x<0) or (x>=dim) or (y<0) or (y>=dim) then exit;
  cursorPosition:=x+y*dim;
  zeichnen;
end;

procedure tFelderRaetsel.aktualisiereGroesze;
var
  i: longint;
begin
  dim:=0;
  schriftGroesze:=spinEdits[0].value;
  for i:=0 to length(groeszen)-1 do begin
    groeszen[i]:=spinEdits[i+1].value;
    dim:=dim+groeszen[i];
  end;
  setLength(inhalt,dim*dim);
  setLength(startFeld,dim*dim);
  setLength(feldFarben,dim*dim);
  setLength(rand,4*dim);
  setLength(invPuzzleTeile,dim*dim);
  for i:=0 to length(puzzleTeile)-1 do
    setLength(puzzleTeile[i],0);
  setLength(puzzleTeile,dim);
  for i:=0 to dim-1 do
    setLength(puzzleTeile[i],dim);

  sudokuCB.enabled:=(intRoot(dim)>1);
  sudokuCB.checked:=false;

  loeschen;
  cursorPosition:=0;
  gesamtRaenderErzeugen;
  aktualisiereZeichenflaechenGroesze;
end;

procedure tFelderRaetsel.loeschen;
var
  i: longint;
begin
  aktuelleFarbe:=$000000;
  setLength(zuege,0);
  for i:=0 to length(inhalt)-1 do begin
    inhalt[i]:=-1;
    startFeld[i]:=true;
    feldFarben[i]:=$000000;
  end;
  for i:=0 to length(rand)-1 do
    rand[i]:=-1;
  farbWahlFlaecheBemalen;
end;

procedure tFelderRaetsel.leeren;
var
  p:            tLongintArray;
  i,w:          longint;
  funktioniert: boolean;
begin
  p:=permutation(dim*dim);
  progressBar1.step:=1;
  progressBar1.min:=0;
  progressBar1.max:=dim*(dim+4);
  progressBar1.position:=0;
  progressBar1.visible:=true;
  for i:=0 to length(p)-1 do begin
    progressBar1.stepIt;
    application.processMessages;
    if inhalt[p[i]]<0 then
      continue;
    w:=inhalt[p[i]];
    inhalt[p[i]]:=-1;
    funktioniert:=true;
    while funktioniert and naechsterWert(p[i]) do begin
      if inhalt[p[i]]=w then
        continue;
      if passt(p[i] mod dim,p[i] div dim) and (anzLoesungen(-1)>0) then
        funktioniert:=false;
    end;
    writeln;
    if funktioniert then
      inhalt[p[i]]:=-1
    else
      inhalt[p[i]]:=w;
  end;

  p:=permutation(dim*4);
  for i:=0 to length(p)-1 do begin
    progressBar1.stepIt;
    application.processMessages;
    if rand[p[i]]<0 then continue;
    w:=rand[p[i]];
    rand[p[i]]:=0;
    funktioniert:=true;
    while funktioniert and (rand[p[i]]<dim) do begin
      inc(rand[p[i]]);
      if rand[p[i]]=w then
        continue;
      if anzLoesungen(-1)>0 then
        funktioniert:=false;
    end;
    writeln;
    if funktioniert then
      rand[p[i]]:=-1
    else
      rand[p[i]]:=w;
  end;
  progressBar1.visible:=false;
end;

procedure tFelderRaetsel.vorbereiten;
var
  i: longint;
begin
  for i:=0 to dim*dim-1 do begin
    puzzleTeile[i div dim][i mod dim]:=-1;
    invPuzzleTeile[i]:=intPoint(-1,-1);
  end;
  findePuzzelierung;
end;

procedure tFelderRaetsel.aktualisiereZeichenflaechenGroesze;
begin
  zeichenFlaeche.canvas.font.size:=schriftGroesze;
  zellGroesze:=
    2*spacing + zeichenFlaeche.canvas.pen.width +
    max(
      zeichenFlaeche.canvas.textWidth(uebersetze(dim)),
      zeichenFlaeche.canvas.textHeight(uebersetze(dim))
    );
  zeichenFlaeche.height:=round((dim+2)*zellGroesze);
  zeichenFlaeche.width:=zeichenFlaeche.height;
  farbWahlFlaeche.height:=16;
  farbWahlFlaeche.width:=zeichenFlaeche.width;
  zeichenFlaecheNeuKreieren;
  besitzer.height:=besitzerHoehe;
  besitzer.width:=besitzerBreite;
  if assigned(puzzleCB) then
    besitzer.width:=max(besitzer.width,puzzleCB.left+puzzleCB.width+spacing);
  if length(spinEdits)>0 then
    if assigned(spinEdits[length(spinEdits)-1]) then
      besitzer.width:=max(besitzer.width,spinEdits[length(spinEdits)-1].left+spinEdits[length(spinEdits)-1].width+spacing);
  progressBar1.width:=besitzer.width;
  zeichnen;
end;

function tFelderRaetsel.besitzerHoehe: longint;
var
  i: longint;
begin
  result:=inherited besitzerHoehe;
  if assigned(diagonalenCB) then
    result:=max(result,diagonalenCB.top+diagonalenCB.height+spacing);
  for i:=0 to length(spinEdits)-1 do
    if assigned(spinEdits[i]) then
      result:=max(result,spinEdits[i].top+spinEdits[i].height+spacing);
end;

function tFelderRaetsel.besitzerBreite: longint;
var
  i: longint;
begin
  result:=inherited besitzerBreite;
  if assigned(diagonalenCB) then
    result:=max(result,diagonalenCB.left+diagonalenCB.width+spacing);
  for i:=0 to length(spinEdits)-1 do
    if assigned(spinEdits[i]) then
      result:=max(result,spinEdits[i].left+spinEdits[i].width+spacing);
end;

procedure tFelderRaetsel.schreibeZentriert(x,y,i: longint);
var
  br,ho:     longint;
  s:         string;
begin
  s:=uebersetze(i);
  with zeichenFlaeche.canvas do begin
    brush.color:=$FFFFFF;
    if (x>=0) and (y>=0) and (x<dim) and (y<dim) and (diagonalenCB.checked and ((x=y) or (x+y=dim-1))) then
      brush.color:=brush.color - $181818;
    if not passtZumZeichnen(x,y) then
      font.color:=$0000FF
    else if geloest then
      font.color:=$007F00
    else if (x<0) or (y<0) or (x>=dim) or (y>=dim) then
      font.color:=$000000
    else if startFeld[x+y*dim] then
      font.color:=$7F7F7F
    else
      font.color:=feldFarben[x+y*dim];

    br:=textWidth(s);
    ho:=textHeight(s);
    textOut(
      round((x+1.5)*zellGroesze-br/2),
      round((y+1.5)*zellGroesze-ho/2),
      s
    );
  end;
end;

procedure tFelderRaetsel.zeichnen(cursor: boolean = true);
var
  i,j: longint;
begin
  if not (assigned(sudokuCB) and assigned(puzzleCB)) then
    exit;
  zeichenFlaeche.canvas.brush.color:=$ffffff;
  zeichenFlaeche.canvas.rectangle(-10,-10,zeichenFlaeche.width+10,zeichenFlaeche.height+10);
  zeichenFlaeche.canvas.pen.width:=1;
  zeichenFlaeche.canvas.pen.color:=$000000;
  if diagonalenCB.checked then
    for i:=1 to dim do begin
      zeichenFlaeche.canvas.brush.color:=$ffffff - $181818;
      zeichenFlaeche.canvas.fillRect(
        round(i*zellGroesze),
        round(i*zellGroesze),
        round((i+1)*zellGroesze),
        round((i+1)*zellGroesze)
      );
      zeichenFlaeche.canvas.fillRect(
        round(i*zellGroesze),
        round((dim-i+1)*zellGroesze),
        round((i+1)*zellGroesze),
        round((dim-i+2)*zellGroesze)
      );
    end;
  for i:=0 to dim do
    with zeichenFlaeche.canvas do begin
      pen.width:=3-2*byte((i>0) and (i<dim));
      moveTo(round((i+1)*zellGroesze),round(zellGroesze));
      lineTo(round((i+1)*zellGroesze),round((dim+1)*zellGroesze));
      moveTo(round(zellGroesze),round((i+1)*zellGroesze));
      lineTo(round((dim+1)*zellGroesze),round((i+1)*zellGroesze));
    end;
  zeichenFlaeche.canvas.pen.width:=3;
  if (length(invPuzzleTeile)>0) and (puzzleTeile[0][0]>=0) then begin
    for i:=0 to dim-1 do
      for j:=1 to dim-1 do
        if invPuzzleTeile[i+j*dim]['x']<>invPuzzleTeile[i+(j-1)*dim]['x'] then
          with zeichenFlaeche.canvas do begin
            moveTo(round((i+1)*zellGroesze),round((j+1)*zellGroesze));
            lineTo(round((i+2)*zellGroesze),round((j+1)*zellGroesze));
          end;
    for j:=0 to dim-1 do
      for i:=1 to dim-1 do
        if invPuzzleTeile[i+j*dim]['x']<>invPuzzleTeile[(i-1)+j*dim]['x'] then
          with zeichenFlaeche.canvas do begin
            moveTo(round((i+1)*zellGroesze),round((j+1)*zellGroesze));
            lineTo(round((i+1)*zellGroesze),round((j+2)*zellGroesze));
          end;
  end;
  zeichenFlaeche.canvas.pen.color:=$8080ff;
  zeichenFlaeche.canvas.pen.width:=1;
  if (cursorPosition>=0) and (dim>0) and cursor then begin
    zeichenFlaeche.canvas.brush.color:=
      $ffffff - $181818 * byte(
        diagonalenCB.checked and (
          (cursorPosition mod (dim+1)=0) or
          (cursorPosition mod (dim-1)=0)
        )
      );
    zeichenFlaeche.canvas.rectangle(
      round(((cursorPosition mod dim)+1)*zellGroesze),
      round(((cursorPosition div dim)+1)*zellGroesze),
      round(((cursorPosition mod dim)+2)*zellGroesze+1),
      round(((cursorPosition div dim)+2)*zellGroesze+1)
      );
  end;
  zeichenFlaeche.canvas.pen.width:=3;
  zeichenFlaeche.canvas.brush.color:=$ffffff;
  for i:=0 to dim-1 do begin
    schreibeZentriert(i,-1,rand[i]);
    schreibeZentriert(dim,i,rand[dim+i]);
    schreibeZentriert(i,dim,rand[2*dim+i]);
    schreibeZentriert(-1,i,rand[3*dim+i]);
  end;
  for i:=0 to length(inhalt)-1 do
    schreibeZentriert(i mod dim,i div dim,inhalt[i]);
end;

procedure tFelderRaetsel.startFelderFestlegen;
var
  i: longint;
begin
  for i:=0 to length(inhalt)-1 do
    startFeld[i]:=inhalt[i]>=0;
end;

procedure tFelderRaetsel.alsZugSpeichern;
begin
  setLength(zuege,length(zuege)+1);
  zuege[length(zuege)-1].position:=cursorPosition;
  zuege[length(zuege)-1].vorher:=inhalt[cursorPosition];
  zuege[length(zuege)-1].vorherFarbe:=feldFarben[cursorPosition];
  zuege[length(zuege)-1].nachherFarbe:=aktuelleFarbe;
  if length(zuege)=1 then
    zuege[length(zuege)-1].vorherMalFarbe:=$000000
  else
    zuege[length(zuege)-1].vorherMalFarbe:=zuege[length(zuege)-2].nachherFarbe;
end;

procedure tFelderRaetsel.speichern(var datei: file);
var
  b: byte;
  i: longint;
begin
  blockWrite(datei,'Fe',2);
  b:=byte(diagonalenCB.checked);
  blockWrite(datei,b,1);
  b:=byte(sudokuCB.enabled);
  blockWrite(datei,b,1);
  b:=byte(sudokuCB.checked);
  blockWrite(datei,b,1);
  b:=byte(puzzleCB.enabled);
  blockWrite(datei,b,1);
  b:=byte(puzzleCB.checked);
  blockWrite(datei,b,1);
  i:=length(groeszen);
  blockWrite(datei,i,sizeOf(i));
  if length(groeszen)>0 then
    blockWrite(datei,groeszen[0],length(groeszen)*sizeOf(groeszen[0]));
  i:=length(inhalt);
  blockWrite(datei,i,sizeOf(i));
  if length(inhalt)>0 then
    blockWrite(datei,inhalt[0],length(inhalt)*sizeOf(inhalt[0]));
  i:=length(rand);
  blockWrite(datei,i,sizeOf(i));
  if length(rand)>0 then
    blockWrite(datei,rand[0],length(rand)*sizeOf(rand[0]));
  blockWrite(datei,dim,sizeOf(dim));
  i:=length(puzzleTeile);
  blockWrite(datei,i,sizeOf(i));
  if length(puzzleTeile)>0 then
    blockWrite(datei,puzzleTeile[0],length(puzzleTeile)*sizeOf(puzzleTeile[0]));
  i:=length(invPuzzleTeile);
  blockWrite(datei,i,sizeOf(i));
  if length(invPuzzleTeile)>0 then
    blockWrite(datei,invPuzzleTeile[0],length(invPuzzleTeile)*sizeOf(invPuzzleTeile[0]));
  blockWrite(datei,cursorPosition,sizeOf(cursorPosition));
  i:=length(feldFarben);
  blockWrite(datei,i,sizeOf(i));
  if length(feldFarben)>0 then
    blockWrite(datei,feldFarben[0],length(feldFarben)*sizeOf(feldFarben[0]));
  i:=length(startFeld);
  blockWrite(datei,i,sizeOf(i));
  if length(startFeld)>0 then
    blockWrite(datei,startFeld[0],length(startFeld)*sizeOf(startFeld[0]));
  i:=length(zuege);
  blockWrite(datei,i,sizeOf(i));
  if length(zuege)>0 then
    blockWrite(datei,zuege[0],length(zuege)*sizeOf(zuege[0]));
  inherited speichern(datei);
end;

procedure tFelderRaetsel.laden(var datei: file);
var
  s: string[2];
  b: byte;
  i: longint;
begin
  s:=#0#0;
  b:=0;
  i:=0;
  blockRead(datei,s,2);
  assert(s='Fe','Die zu lesende Datei ist kein Felder-Rätsel!');
  blockRead(datei,b,1);
  assert(b<=1,'Syntaxfehler in Datei!');
  diagonalenCB.checked:=odd(b);
  blockRead(datei,b,1);
  assert(b<=1,'Syntaxfehler in Datei!');
  sudokuCB.enabled:=odd(b);
  blockRead(datei,b,1);
  assert(b<=1,'Syntaxfehler in Datei!');
  sudokuCB.checked:=odd(b);
  blockRead(datei,b,1);
  assert(b<=1,'Syntaxfehler in Datei!');
  puzzleCB.enabled:=odd(b);
  blockRead(datei,b,1);
  assert(b<=1,'Syntaxfehler in Datei!');
  puzzleCB.checked:=odd(b);
  blockRead(datei,i,sizeOf(i));
  assert(length(groeszen)=i,'Falsche Anzahl freier Paraemeter in gespeichertem Spiel!');
  if length(groeszen)>0 then
    blockRead(datei,groeszen[0],length(groeszen)*sizeOf(groeszen[0]));
  for i:=0 to length(groeszen)-1 do
    spinEdits[i+1].value:=groeszen[i];
  blockRead(datei,i,sizeOf(i));
  setLength(inhalt,i);
  if length(inhalt)>0 then
    blockRead(datei,inhalt[0],length(inhalt)*sizeOf(inhalt[0]));
  blockRead(datei,i,sizeOf(i));
  setLength(rand,i);
  if length(rand)>0 then
    blockRead(datei,rand[0],length(rand)*sizeOf(rand[0]));
  blockRead(datei,dim,sizeOf(dim));
  blockRead(datei,i,sizeOf(i));
  setLength(puzzleTeile,i);
  if length(puzzleTeile)>0 then
    blockRead(datei,puzzleTeile[0],length(puzzleTeile)*sizeOf(puzzleTeile[0]));
  blockRead(datei,i,sizeOf(i));
  setLength(invPuzzleTeile,i);
  if length(invPuzzleTeile)>0 then
    blockRead(datei,invPuzzleTeile[0],length(invPuzzleTeile)*sizeOf(invPuzzleTeile[0]));
  blockRead(datei,cursorPosition,sizeOf(cursorPosition));
  blockRead(datei,i,sizeOf(i));
  setLength(feldFarben,i);
  if length(feldFarben)>0 then
    blockRead(datei,feldFarben[0],length(feldFarben)*sizeOf(feldFarben[0]));
  blockRead(datei,i,sizeOf(i));
  setLength(startFeld,i);
  if length(startFeld)>0 then
    blockRead(datei,startFeld[0],length(startFeld)*sizeOf(startFeld[0]));
  blockRead(datei,i,sizeOf(i));
  setLength(zuege,i);
  if length(zuege)>0 then
    blockRead(datei,zuege[0],length(zuege)*sizeOf(zuege[0]));
  inherited laden(datei);
end;

procedure tFelderRaetsel.findePuzzelierung;
var
  i,j,k,l:  longint;
  perms:    array of array of longint;
  kAnzs:    array of longint;
  lw:       longword;
  gefunden: boolean;
begin
  if sudokuCB.checked then begin
    k:=intRoot(dim);
    l:=dim div k;
    for i:=0 to dim-1 do
      for j:=0 to dim-1 do begin
        invPuzzleTeile[i*dim + j]:=
          intPoint(
            j div l + (i div k) * k,
            j mod l + (i mod k) * l
          );
        puzzleTeile[
          invPuzzleTeile[i*dim + j]['x'],
          invPuzzleTeile[i*dim + j]['y']
        ]:=i*dim + j;
      end;
    exit;
  end;
  if not puzzleCB.checked then begin
    puzzleTeile[0][0]:=-1;
    exit;
  end;

  setLength(perms,dim*dim);
  for i:=0 to dim*dim-1 do begin
    invPuzzleTeile[i]:=intPoint(-1,-1);
    perms[i]:=permutation(dim);
  end;
  setLength(kAnzs,dim);
  for i:=0 to dim-1 do
    kAnzs[i]:=0;

  //invPuzzleTeile:                    array of tIntPoint;        // Ort -> [Teil, Kachel]
  //puzzleTeile:                       array of array of longint; // [Teil,Kachel] -> Ort

  i:=0;
  while i<dim*dim do begin
    j:=-1;
    // wenn hier schon was steht, setzen wir j auf perms[i]^-1[Inhalt]
    if invPuzzleTeile[i]['x']>=0 then begin
      repeat
        inc(j);
      until (j>=dim) or (perms[i][j]=invPuzzleTeile[i]['x']);
      assert(perms[i][j]=invPuzzleTeile[i]['x'], 'Could not invert '+intToStr(invPuzzleTeile[i]['x'])+' (dim='+intToStr(dim)+')!');
      dec(kAnzs[perms[i][j]]); // Kachel vom Puzzleteil entfernen
    end;
    inc(j); // nächsten Wert nehmen
    while (j<dim) and // bis zu Puzzleteil hochzählen, welches noch Kacheln braucht
      (kAnzs[perms[i][j]]>=dim) do
      inc(j);
    if (j>=dim) then begin
      // hier passt nichts
      invPuzzleTeile[i]:=intPoint(-1,-1);
      dec(i);
      continue;
    end;
    invPuzzleTeile[i]:=intPoint(perms[i][j],kAnzs[perms[i][j]]); // Kachel anfügen
    puzzleTeile[perms[i][j]][kAnzs[perms[i][j]]]:=i;
    inc(kAnzs[perms[i][j]]);
    if kAnzs[perms[i][j]]=dim then begin
      // prüfen, ob Puzzleteil zusammenhängt
      lw:=(1 shl dim) - 2;
      repeat
        gefunden:=false;
        for k:=0 to dim-1 do begin
          if odd(lw shr k) then
            continue;
          if (puzzleTeile[perms[i][j]][k] mod dim > 0) and
            (invPuzzleTeile[puzzleTeile[perms[i][j]][k]-1]['x']=perms[i][j]) and
            odd(lw shr invPuzzleTeile[puzzleTeile[perms[i][j]][k]-1]['y']) then begin
            lw:=lw and not (1 shl invPuzzleTeile[puzzleTeile[perms[i][j]][k]-1]['y']);
            gefunden:=true;
          end;
          if (puzzleTeile[perms[i][j]][k] mod dim < dim-1) and
            (invPuzzleTeile[puzzleTeile[perms[i][j]][k]+1]['x']=perms[i][j]) and
            odd(lw shr invPuzzleTeile[puzzleTeile[perms[i][j]][k]+1]['y']) then begin
            lw:=lw and not (1 shl invPuzzleTeile[puzzleTeile[perms[i][j]][k]+1]['y']);
            gefunden:=true;
          end;
          if (puzzleTeile[perms[i][j]][k] div dim > 0) and
            (invPuzzleTeile[puzzleTeile[perms[i][j]][k]-dim]['x']=perms[i][j]) and
            odd(lw shr invPuzzleTeile[puzzleTeile[perms[i][j]][k]-dim]['y']) then begin
            lw:=lw and not (1 shl invPuzzleTeile[puzzleTeile[perms[i][j]][k]-dim]['y']);
            gefunden:=true;
          end;
          if (puzzleTeile[perms[i][j]][k] div dim < dim-1) and
            (invPuzzleTeile[puzzleTeile[perms[i][j]][k]+dim]['x']=perms[i][j]) and
            odd(lw shr invPuzzleTeile[puzzleTeile[perms[i][j]][k]+dim]['y']) then begin
            lw:=lw and not (1 shl invPuzzleTeile[puzzleTeile[perms[i][j]][k]+dim]['y']);
            gefunden:=true;
          end;
        end;
      until not gefunden;
      if lw<>0 then begin
        // nicht zusammenhängend
        dec(kAnzs[perms[i][j]]);
        invPuzzleTeile[i]:=intPoint(-1,-1);
        dec(i);
        continue;
      end;
    end;
    inc(i);
  end;
end;

// tHochhausRaetsel und tBuchstabenRaetsel

{$DEFINE create}
constructor tHochhausRaetsel.create(aOwner: tForm);
{$DEFINE hochhaus}
{$I raetselunit.inc}
{$UNDEF hochhaus}

constructor tBuchstabenRaetsel.create(aOwner: tForm);
{$DEFINE buchstaben}
{$I raetselunit.inc}
{$UNDEF buchstaben}
{$UNDEF create}

{$DEFINE destroy}
destructor tHochhausRaetsel.destroy;
{$DEFINE hochhaus}
{$I raetselunit.inc}
{$UNDEF hochhaus}
destructor tBuchstabenRaetsel.destroy;
{$DEFINE buchstaben}
{$I raetselunit.inc}
{$UNDEF buchstaben}
{$UNDEF destroy}

{$DEFINE passt}
function  tHochhausRaetsel.passt(spalte,zeile: integer): boolean;
{$DEFINE hochhaus}
{$I raetselunit.inc}
{$UNDEF hochhaus}
function  tBuchstabenRaetsel.passt(spalte,zeile: integer): boolean;
{$DEFINE buchstaben}
{$I raetselunit.inc}
{$UNDEF buchstaben}
{$UNDEF passt}

{$DEFINE geloest}
function  tHochhausRaetsel.geloest: boolean;
{$DEFINE hochhaus}
{$I raetselunit.inc}
{$UNDEF hochhaus}
function  tBuchstabenRaetsel.geloest: boolean;
{$DEFINE buchstaben}
{$I raetselunit.inc}
{$UNDEF buchstaben}
{$UNDEF geloest}

{$DEFINE gesamtRaenderErzeugen}
procedure tHochhausRaetsel.gesamtRaenderErzeugen;
{$DEFINE hochhaus}
{$I raetselunit.inc}
{$UNDEF hochhaus}
procedure tBuchstabenRaetsel.gesamtRaenderErzeugen;
{$DEFINE buchstaben}
{$I raetselunit.inc}
{$UNDEF buchstaben}
{$UNDEF gesamtRaenderErzeugen}

{$DEFINE passtZumZeichnen}
function  tHochhausRaetsel.passtZumZeichnen(spalte,zeile: integer): boolean;
{$DEFINE hochhaus}
{$I raetselunit.inc}
{$UNDEF hochhaus}
function  tBuchstabenRaetsel.passtZumZeichnen(spalte,zeile: integer): boolean;
{$DEFINE buchstaben}
{$I raetselunit.inc}
{$UNDEF buchstaben}
{$UNDEF passtZumZeichnen}

{$DEFINE randErzeugen}
procedure tHochhausRaetsel.randErzeugen;
{$DEFINE hochhaus}
{$I raetselunit.inc}
{$UNDEF hochhaus}
procedure tBuchstabenRaetsel.randErzeugen;
{$DEFINE buchstaben}
{$I raetselunit.inc}
{$UNDEF buchstaben}
{$UNDEF randErzeugen}

{$DEFINE relativeInhaltsAenderung}
procedure tHochhausRaetsel.relativeInhaltsAenderung(diff: integer);
{$DEFINE hochhaus}
{$I raetselunit.inc}
{$UNDEF hochhaus}
procedure tBuchstabenRaetsel.relativeInhaltsAenderung(diff: integer);
{$DEFINE buchstaben}
{$I raetselunit.inc}
{$UNDEF buchstaben}
{$UNDEF relativeInhaltsAenderung}

{$DEFINE naechsterWert}
function tHochhausRaetsel.naechsterWert(pos: longint): boolean;
{$DEFINE hochhaus}
{$I raetselunit.inc}
{$UNDEF hochhaus}
function tBuchstabenRaetsel.naechsterWert(pos: longint): boolean;
{$DEFINE buchstaben}
{$I raetselunit.inc}
{$UNDEF buchstaben}
{$UNDEF naechsterWert}

{$DEFINE absoluteInhaltsAenderung}
function  tHochhausRaetsel.absoluteInhaltsAenderung(key: word): boolean;
{$DEFINE hochhaus}
{$I raetselunit.inc}
{$UNDEF hochhaus}
function  tBuchstabenRaetsel.absoluteInhaltsAenderung(key: word): boolean;
{$DEFINE buchstaben}
{$I raetselunit.inc}
{$UNDEF buchstaben}
{$UNDEF absoluteInhaltsAenderung}

{$DEFINE loesen}
function  tHochhausRaetsel.loesen(lPos: integer): boolean;
{$DEFINE hochhaus}
{$I raetselunit.inc}
{$UNDEF hochhaus}
function  tBuchstabenRaetsel.loesen(lPos: integer): boolean;
{$DEFINE buchstaben}
{$I raetselunit.inc}
{$UNDEF buchstaben}
{$UNDEF loesen}

{$DEFINE anzLoesungen}
function  tHochhausRaetsel.anzLoesungen(lPos: integer): integer;
{$DEFINE hochhaus}
{$I raetselunit.inc}
{$UNDEF hochhaus}
function  tBuchstabenRaetsel.anzLoesungen(lPos: integer): integer;
{$DEFINE buchstaben}
{$I raetselunit.inc}
{$UNDEF buchstaben}
{$UNDEF anzLoesungen}

{$DEFINE speichern}
procedure tHochhausRaetsel.speichern(var datei: file);
{$DEFINE hochhaus}
{$I raetselunit.inc}
{$UNDEF hochhaus}
procedure tBuchstabenRaetsel.speichern(var datei: file);
{$DEFINE buchstaben}
{$I raetselunit.inc}
{$UNDEF buchstaben}
{$UNDEF speichern}

{$DEFINE laden}
procedure tHochhausRaetsel.laden(var datei: file);
{$DEFINE hochhaus}
{$I raetselunit.inc}
{$UNDEF hochhaus}
procedure tBuchstabenRaetsel.laden(var datei: file);
{$DEFINE buchstaben}
{$I raetselunit.inc}
{$UNDEF buchstaben}
{$UNDEF laden}

end.