Procedure TForm1.MoveOne(A,C:integer);
{Move one disk from peg A to peg C}
begin
inc(movecount);
application.processmessages;
end;
procedure TForm1.MoveStack(n, A,C,B:integer);
{Use recursive calls to move n disks from Peg A to Peg C with Peg B as a spare}
begin
if tag=1 then exit; {User signaled stop}
If n=1 then
begin
MoveOne(A,C); {If there's only one to move, move it}
end
else
begin
MoveStack(n-1, A,B,C); {Move n-1 disks to peg B}
MoveOne(A,C); {Move 1 disk to Peg C}
MoveStack(n-1,B,C,A); {Then move n-1 disks from B back to Peg C}
end;
end;
procedure TForm1.SolveBtnClick(Sender: TObject);
var
i:integer;
begin
tag:=0; {Quit flag - set by stop button}
SolveBtn.enabled:=false; {Don't let the user try to start another until we're done}
StopBtn.enabled:=true; {But let him push the stop button}
movecount:=0;
starttime:=time;
MovesLbl.Caption:='Number of moves:' ;
timelbl.caption:='Number of Seconds: ';
ratelbl.caption:='Moves per second: ';
screen.cursor:=crHourGlass;
MoveStack(nbrdisks,1,3,2); {move nbrdisks from peg 1 to peg 3 using peg2 as the spare}
If tag=0 then makelabels('Complete');
SolveBtn.enabled:=true;
StopBtn.enabled:=false;
Screen.cursor:=CrDefault;
end;
Procedure TForm1.MakeLabels(S:String);
begin
MovesLbl.Caption:='Number of moves: '+floattostrf(movecount,ffnumber,9,0);
seconds:=(time-starttime)*SecsPerDay;
timelbl.caption:='Number of Seconds: '+floattostrf(seconds,ffnumber,6,1);
If seconds>=0.5 then
begin
rate:=movecount / seconds;
ratelbl.caption:='Moves per second: '+floattostrf(rate,ffnumber,9,0);
end
else
ratelbl.caption:='Moves per second: Need runtime of at least'
+#13+' 1/2 second to calculate rate';
end;
procedure TForm1.StopBtnClick(Sender: TObject);
{User clicked the stop button probably during a long run}
var
years:single;
s:string;
begin
makelabels('Not Complete');
Screen.cursor:=crArrow;
EstSecsLbl1.caption:= 'Estimated time to complete: '
+ floattostrf((power(2,nbrdisks)-1)/Rate,ffnumber,8,1)+' seconds';
years:=power(2,nbrdisks)/(365*24*3600);
if years>1 then s:=' years)' else s:=' year)';
EstSecsLbl2.caption:=
'Est. time at 1 move per second: '
+#13
+floattostrf(Power(2,nbrdisks),ffnumber,20,0)
+' seconds '+#13+'(That''s '
+floattostrf(years,ffnumber,15,1)+s;
tag:=1; {Set quit flag}
end;