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 | Const
{These are the offsets from current locations to all 8 valid knight moves}
Offsets: array[1..8] of Tpoint= ((x:-2;y:-1),(x:-2;y:+1),(x:-1;y:-2),(x:-1;y:+2),
(x:+1;y:+2),(x:+1;y:-2),(x:+2;y:+1),(x:+2;y:-1)
);type
TBoard=class(TStringGrid)
Private
b: array of array of integer;
moves:array of TPoint; {array of moves made} size:integer; {board size}
manualplay:boolean; {true=user plays}
movecount:integer; {nbr of moves made}
totmoves:integer; {total moves tried, counting moves taken back }
delay:integer; {ms to wait between moves when autosolving} closedtour:boolean;
constructor create(Aowner:TComponent;
newsize:integer;
newlocrect:Trect );
procedure DrawAcell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState);
procedure Clicked(Sender:Tobject);
function IsValidMove(newcol,newrow:integer):boolean;
procedure MakeMove(newcol,newrow:integer);
function Canundo:boolean; procedure UndoMove;
function PossibleMoves(newcol,newrow:integer):integer;
function SolveFrom(newcol,newrow:integer):boolean;
end;
TForm1 = class(TForm)
Memo1: TMemo;
SolveBtn: TButton;
PlayBtn: TButton;
Panel1: TPanel; ColEdit: TSpinEdit;
RowEdit: TSpinEdit;
Label1: TLabel;
Label2: TLabel;
SolvingPanel: TPanel; Label4: TLabel;
Speedbar: TTrackBar;
StopBtn: TButton;
StatusBar1: TStatusBar;
ClosedBox: TCheckBox; Moveslbl: TLabel;
procedure FormCreate(Sender: TObject);
procedure PlayBtnClick(Sender: TObject);
procedure SolveBtnClick(Sender: TObject);
procedure SpeedbarChange(Sender: TObject); procedure StopBtnClick(Sender: TObject);
procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
procedure ClosedBoxClick(Sender: TObject);
public
{ Public declarations } board:TBoard;
Procedure makeboard;
end;
var Form1: TForm1;
implementation
{$R *.DFM}{****************** TBoard Methods ********************}
Constructor TBoard.create(Aowner:TComponent;
newsize:integer;
newlocrect:Trect );
var i,j:integer;
begin
inherited create(Aowner);
if Aowner is TWinControl then parent:=TWincontrol(Aowner);
scrollbars:=ssNone;
size:=newsize;
colcount:=size; fixedcols:=0;
rowcount:=size; fixedrows:=0; top:=newlocrect.top;
left:=newlocrect.left;
width:=newlocrect.right-left;
defaultcolwidth:= (width-size) div size-1;
defaultrowheight:=defaultcolwidth; width:=(defaultcolwidth+1)*size+3; {trim width to fit squares}
height:=width;
setlength(b,newsize,newsize);
{initialize board to zeros}
for i:= 0 to size-1 do for j:= 0 to size-1 do b[i,j]:=0; setlength(moves,size*size+1);
movecount:=0;
totmoves:=0;
OnDrawCell:=DrawACell;
OnClick:=Clicked; canvas.font.size:=12;
canvas.font.name:='Courier'; {fixed font size}
end;
procedure TBoard.DrawACell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState);
{OnDrawcell exit}
var
i,j:integer;
s:string;begin
i:=acol;
j:=arow;
with Sender as TBoard do
begin {make chessboard coloring }
if (i mod 2) = (j mod 2) {odd row&column or even row&column}
then Canvas.Brush.Color := clsilver
else canvas.brush.color:=clblack;
Canvas.FillRect(Rect); if b[i,j]>0
then
with canvas do
begin
font.size:=12; font.style:=[fsbold];
if (i mod 2) = (j mod 2) then font.color:=clblack
else font.color:=clwhite;
s:=format('%2d',[b[i,j]]);
canvas.textout(rect.left +3 , rect.top+3,s); end;
end;
end;
Procedure TBoard.clicked;{User clicked a cell}
begin
if not manualplay then exit;
If isValidmove(col,row) then makemove(col,row)
else if canundo then undomove else beep;
end;
Function TBoard.IsValidMove(newcol,newrow:integer):boolean;
{There are 8 possible move positions
relative to lastmove location. They are
col row
--- ---
-2 +1 -2 -1
-1 -2
-1 +2
+1 +2
+1 -2 +2 +1
+2 -1
}
{To be valid, col+newcol and row+newrow must match one of these
and board must be unoccupied at that loacation}
var
i:integer;
test:Tpoint;
OK:boolean; pcol,prow:integer;
begin
If (newcol<0) or (newcol>=size) or (newrow<0) or (newrow>=size)
then OK:=false
else If movecount=0 then OK:=true {1st move can be anywhere}
else
begin {get previous move}
test.x:=moves[movecount].x-newcol;
test.y:=moves[movecount].y-newrow; OK:=false;
for i:= 1 to 8 do {to be OK, it has to be to valid location}
if (test.x=offsets[i].x) and (test.y=offsets[i].y) then
begin
OK:=true; break;
end;
{and location has to be unoccupied}
If OK and (b[newcol,newrow]<>0) then OK :=false;
{12/20/02 - add code for closed tours - move must not make the starting
position unreachable unless it is the last move}
if closedtour and OK and ((newcol<>0) or (newrow<>0)) and (movecount<size*size-1) then
begin
b[newcol,newrow]:=1; ok:=false;
for i:= 1 to 8 do {count how many next moves exist}
begin
pcol:=moves[1].x+offsets[i].x;
prow:=moves[1].y+offsets[i].y; if (pcol>=0) and (pcol<size) and (prow>=0)and(prow<size) and (b[pcol,prow ]=0)
then
begin
Ok:=true;
break; end;
end;
b[newcol,newrow]:=0;
end;
end; result:=OK;
end;
Procedure TBoard.makemove(newcol,newrow:integer);
{make a move}begin
col:=newcol;
row:=newrow;
inc(movecount); {count the move}
{add move to moves array} with moves[movecount] do
begin
x:=col;
y:=row;
end; {fill in the move number to board and display}
b[col,row]:=movecount;
cells[col,row]:=inttostr(movecount);
end;
Function TBoard.Canundo:boolean;
begin
result:=(b[col,row]=movecount); {can only undo last move}
end;
Procedure TBoard.UndoMove;
{undo a move}
begin
If movecount>0 then
with moves[movecount] do begin
b[x,y]:=0; {0 the board cell}
cells[x,y]:=''; {blank the display cell}
dec(movecount); {decrease the count}
end;end;
Function TBoard.possiblemoves(newcol,newrow:integer):integer;
{ Return a count of valid moves from this location}
var i,count:integer;
begin
count:=0;
If isvalidmove(newcol,newrow) then
begin makemove(newcol,newrow); {make the trial move}
for i:= 1 to 8 do {count how many next moves exist}
if isvalidmove(newcol+offsets[i].x, newrow+offsets[i].y) then inc(count);
undomove; {and undo the move}
end; result:=count;
end;
Function TBoard.solvefrom(newcol,newrow:integer):boolean;{generate all possible next moves, and count the number of next moves from
each position. Pick one with lowest value and move there}
{If the lowest number of next moves is 0 and that would not be the
last move then this is a bad path. Need to backtrakc and try another path
- continue until solved or all paths have been tried}type
TMoverec = record
pcol,prow,nbrmoves:integer;
distfromstart:single;
end;var
possibles:array [1..8] of TMoveRec;
i:integer;
function dist(col,row:integer):single; begin
result:=sqr(moves[1].x-col)+sqr(moves[1].y-row);
end;
Procedure sortmoves;
{sort the possible moves by increasing next move count
then by distance from staring position}
var
i,j:integer;
procedure swap(i,j:integer);
var Hrec:TMoverec;
begin
Hrec:=possibles[i]; possibles[i]:=possibles[j];
possibles[j]:=hrec;
end;
begin begin
for i:= 1 to size-1 do
for j:= i+1 to size do
If possibles[i].nbrmoves>possibles[j].nbrmoves
then swap(i,j) else if (possibles[i].nbrmoves=possibles[j].nbrmoves) and
(possibles[i].distfromstart<possibles[j].distfromstart)
then swap(i,j);
end;
end; {Sortmoves}
begin {Solvefrom}
{Update display and wait awhile}
application.processmessages;
sleep(delay); {Done?}
If movecount=size*size then result:=true
else
{If not, then generate next moves for all possible moves from here} begin
result:=false;
If manualplay then exit;
for i:= 1 to size do
with possibles[i] do begin
pcol:=newcol+offsets[i].x;
prow:=newrow+offsets[i].y;
nbrmoves:=PossibleMoves(pcol,prow);
distfromstart:=dist(pcol,prow); {if no moves possible from this location,
then make sure it sorts to end of array.
Not really necessary, just saves the time of
checking and rejecting those cases
} if nbrmoves=0 then nbrmoves:=size+1;
end;
{Sort them by increasing possible moves - Warnsdorff heuristic}
sortmoves;
{Now, run through all the possibilities - making recursive call for valid ones}
{backtrack by calling undomove for paths that don't work}
i:=1;
while (i<=size) and (tag=0) do
with possibles[i] do begin
If isvalidmove(pcol,prow) then
begin
makemove(pcol,prow);
inc(totmoves); form1.moveslbl.caption:='Total moves tried: '+inttostr(totmoves);
if solvefrom(pcol,prow) then
begin
result:=true;
break; end
else if not manualplay then begin undomove; {beep;} end;
end;
inc(i);
end; end;
end;
{******************** Form Methods *******************}
procedure TForm1.FormCreate(Sender: TObject);
begin
randomize;
makeboard;
solvingpanel.bringtofront;end;
Procedure TForm1.makeboard;
begin
with Panel1 do {dummygrid is a stringgrid just to supply the size} board:=TBoard.create(self,8,rect(left,top,left+width,top+height));
board.delay:=speedbar.max-speedbar.position;
closedboxclick(self);
end;
procedure TForm1.PlayBtnClick(Sender: TObject);
begin
if board.manualplay then
begin
if assigned(board) then board.free; Makeboard;
end;
board.manualplay:=true;
end;
procedure TForm1.SolveBtnClick(Sender: TObject);
{Compute solution -
Start at a random location and try all paths - backtrack on those
that don't work - until solution is found.
Uses Warnsdorf heuristic - when a choice of moves is available,
choose the one that has the fewest next moves
}
begin
makeboard;
board.manualplay:=false;
with board do
begin with speedbar do position:=(max+min) div 2;
solvingpanel.visible:=true;
totmoves:=0;
moveslbl.caption:='Total moves tried: 0';
{makemove(random(size),random(size));} makemove(Coledit.value-1, Rowedit.value-1);
If solvefrom(col,row) then showmessage('Solved!')
else if not manualplay then showmessage('No solution found');
solvingpanel.visible:=false;
end; board.manualplay:=true;
end;
procedure TForm1.SpeedbarChange(Sender: TObject);
{set new ms delay between moves}begin
with speedbar do board.delay:=max-position;
end;
procedure TForm1.StopBtnClick(Sender: TObject);begin
board.tag:=1;
end;
procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);begin
board.tag:=1; {in case we're solving}
canclose:=true;
end;
procedure TForm1.ClosedBoxClick(Sender: TObject);
begin
board.closedtour:=closedbox.checked;
end;
(*
procedure TForm1.Button1Click(Sender: TObject);
{temp to save a copy of the board for website}
var
b:TBitmap; w,h:integer;
begin
b:=tBitmap.create;
h:=board.Height;
w:=board.Width; b.height:=h;
b.width:=w;
b.canvas.copyrect(rect(0,0,w,h),board.canvas,rect(0,0,w,h));
b.savetofile('kn.BMP');
b.free;end;
*)
end. |