PDA

View Full Version : Lazarus : Towers of Hanoi



Lima Bean
10-12-2011, 09:56 AM
Code found somewhere in the pubic domain. Now has printing of moves (Thanx nielsie95)

Changing a few variables in the code can and will make it crash, that's my idea. Break it, fix it, and repeat until it does what it is supposed to do or get tired of playing......lol

I know that Pascal is considered an older language and not very useful to some, but when I have gotten under the hood of other languages they seem to point back to this one, so here I am using Lazarus.

Tests run:
1. I used the number of disks 0 1 2 3 4 with 3 pegs to verify the code works. (mentally verify)
2. I pushed the upper limit to 32 (disks) and got "-1 moves"
3. I added another a 3rd Hanoi (lft, ctr, rgt, n - 1) and got a false answer
4. entered a text instead of number and got "error 106" (easy to fix)

what was verified:
1. code works
2. min disks is 0 max is 31
3. 32 bit machine
4. the code though recursive, stays with in the 3 peg limit
5. needs more work :)

I know it's not much, but it is fun.



program Pegtoy;

{$mode objfpc}{$H+}

uses
crt, sysutils,
{$IFDEF UNIX}{$IFDEF UseCThreads}
cthreads,
{$ENDIF}{$ENDIF}
Classes
{ you can add units after this };

{$R *.res}

{Changelog}
{renamed to Pegtoy}
{hanoitowers4. printing was out of order}
{hanoitowers3. no printing}
{---------------------------------------------------}
{original code in public domain somewhere}
{further adapted from the link provived by nielsie95}


Var
disks: integer;
m: integer;
kb: boolean;
st: string;

Procedure Pegtoy(Lfm, Lto, Lus, n: integer);
begin
if n > 0 then
begin
Pegtoy(Lfm, Lus, Lto, n - 1);
m := m + 1;
writeln('move ', m:2,' ', Lfm:1, ' --> ', Lto:1);
Pegtoy(Lus, Lto, Lfm, n - 1);
end;
end;

procedure getdisks();
begin
writeln('working ...' );
Pegtoy(1, 3, 2,disks);
writeln('Solution takes: ', m:2 ,' moves.');
disks := 0;
m := 0;
writeln;
end;

begin //main
kb := true;
while (kb = true) do
begin
clrscr();
write('Enter the number of disks: ');
{$I-} // corrects runtime error 106
readln(disks);
{$I+}
begin
if (IOResult = 0) then
getdisks()
else
//-----//
writeln;
writeln;
write('Press <Enter> To Quit or <P> to Play Again ');
readln(st);
st := UpperCase(st);
begin
if (st = 'P') or (st = 'p') then
kb := true
else
kb := false
end;
end;
end;
end.

nielsie95
10-12-2011, 11:37 AM
Fun, indeed :p
Not much changing needed for a Simba/SCAR version :)

Also, are you sure this is a correct implementation? ;)

Lima Bean
10-12-2011, 12:06 PM
Fun, indeed :p
Not much changing needed for a Simba/SCAR version :)

Also, are you sure this is a correct implementation? ;)


No. I am not sure if it is the right implementation :)

I see the "cthreads" at the top and start thinking pointer math (Pchar ?)

I don't like the double Hanoi() but that is what the code came with, I shortened the variables though.

I want to get it right, even if it means I have to learn thread/mem management.

What I like about the Lazarus Ide is write compile run. :):) I still have more basics to relearn, but I am getting there.

nielsie95
10-12-2011, 12:11 PM
You shouldn't worry about cthreads, that's something for behind the screens thread management. The thing I noticed was that you don't do any actual moves (and that's the whole point with Hanoi (http://www.kernelthread.com/projects/hanoi//)).

Lima Bean
10-12-2011, 12:34 PM
The "moves" line that came with the code didn't show the correct move sequence so i took it out. and that is part of the "needs work part."

Thanks for the Link :):)

Printing now corrected.

Lima Bean
10-13-2011, 10:10 AM
Reworked the code. Can now be replayed.

Renamed it.

Still chasing bugs.