Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Re: Recursion: the Towers of Hanoi problem

by terra incognita (Pilgrim)
on Oct 20, 2004 at 20:11 UTC ( #400977=note: print w/replies, xml ) Need Help??

in reply to Recursion: The Towers of Hanoi problem

Here is a modified version that will display a simple ASCII representation for those people like myself that think better in pictures.
I am sure that this can be improved significantly.
Comments on where I can improve this code and what practices I should stay away from are appreciated.
This should work on both Windows and Solaris though the format may be a little off on Solaris.
#!/usr/local/bin/perl use strict; # Towers of Hanoi # Perl version (5.8.0) # Ported from Java my $numdisks = 0; my $count =0; print "Number of disks? "; chomp( $numdisks = <STDIN> ); clear (); my $i; my @polea; my @poleb; my @polec; my $loop = 0; my $string ; while ($numdisks > $loop++) { $string = $string ."x"; push (@polea, $string); push (@poleb, ""); push (@polec, ""); } print "A\tB\tC\n"; for (my $len = 0 ;$len <= ($numdisks -1); $len++) { print "$polea[$len]\t$poleb[$len]\t$polec[$len]\n"; } sleep 2; clear (); movedisks( $numdisks, 'A', 'B', 'C' ); # SUB LAND sub clear { if ($^O eq "MSWin32") { system 'cls'; }else{ system 'clear'; } } sub movedisks { my( $num, $from, $to, $aux ) = @_; if( $num == 1 ) { paintdisks ($num, $to, $from); }else{ movedisks( $num-1, $from, $aux, $to ); paintdisks ($num, $to, $from); movedisks( $num-1, $aux, $to, $from ); } } sub paintdisks { my( $num, $dest, $source) = @_; my $foo; if ($source eq "A") { $foo = $polea[0]; shift @polea; }elsif ($source eq "B") { $foo = $poleb[0]; shift @poleb; }else{ $foo = $polec[0]; shift @polec; } if ($dest eq "A") { unshift @polea, $foo; }elsif ($dest eq "B") { unshift @poleb, $foo; }else{ unshift @polec, $foo; } print "A\tB\tC\n"; for (my $len = 0 ;$len <= ($numdisks -1); $len++) { print "$polea[$len]\t$poleb[$len]\t$polec[$len]\n"; } sleep 2; clear (); }

Replies are listed 'Best First'.
Re^2: Recursion: the Towers of Hanoi problem
by pmonk4ever (Friar) on Dec 03, 2008 at 00:16 UTC

    This runs on ActiveState Perl 5.10.0 as it is written, which is really a nice implementation for visualization of the problem space... :)

    I tinkered with it a little, added a variable to count the number of moves. 10 disks were my limit, that solves fairly quickly. Never did get to the 21 disc version, I didn't want to wait that long. :)

    However, one of the links published by dragonchild led me to this site:

    where I found a short history of the tale, seems there are 64! discs, so the final solution works out to ~585 billion years or so, moving one disc a second...too much time for me to spend...but it was a very interesting exercise... ;>)


    "No trees were harmed in the creation of this node. However, a rather large number of electrons were somewhat inconvenienced."

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://400977]
[GotToBTru]: oh I know what it is .. but it is a number only slightly useful to me and of no possible use to our customer
[MidLifeXis]: Whew - you just saved the free world. <o)
[GotToBTru]: i guess it's a placeholder, the code will only fill it in if there is nothing else to use
[GotToBTru]: but then .. if you have nothing to say, why not say nothing?
[MidLifeXis]: I have a user who has a lot of say on how some of our processes work that abhors significant blanks. Perhaps that is a part of it. A not-so-obvious "this space intentionally left blank" indicator.
[MidLifeXis]: On the back end, however, that doesn't mean that i have to use that value to indicate "undefined", no matter how much he thinks I should.

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (10)
As of 2017-01-20 19:16 GMT
Find Nodes?
    Voting Booth?
    Do you watch meteor showers?

    Results (176 votes). Check out past polls.