Results 1 to 6 of 6

Thread: Lazarus : Towers of Hanoi

  1. #1
    Join Date
    Sep 2011
    Location
    Earth
    Posts
    63
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default Lazarus : Towers of Hanoi

    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.


    Code:
    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.
    Last edited by Lima Bean; 10-13-2011 at 10:07 AM. Reason: update

  2. #2
    Join Date
    Sep 2006
    Posts
    6,089
    Mentioned
    77 Post(s)
    Quoted
    43 Post(s)

    Default

    Fun, indeed
    Not much changing needed for a Simba/SCAR version

    Also, are you sure this is a correct implementation?
    Last edited by nielsie95; 10-12-2011 at 11:41 AM.
    Hup Holland Hup!

  3. #3
    Join Date
    Sep 2011
    Location
    Earth
    Posts
    63
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Quote Originally Posted by nielsie95 View Post
    Fun, indeed
    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.

  4. #4
    Join Date
    Sep 2006
    Posts
    6,089
    Mentioned
    77 Post(s)
    Quoted
    43 Post(s)

    Default

    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).
    Hup Holland Hup!

  5. #5
    Join Date
    Sep 2011
    Location
    Earth
    Posts
    63
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    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.
    Last edited by Lima Bean; 10-13-2011 at 10:09 AM. Reason: update

  6. #6
    Join Date
    Sep 2011
    Location
    Earth
    Posts
    63
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Reworked the code. Can now be replayed.

    Renamed it.

    Still chasing bugs.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •