Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
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 (); }

In reply to Re: Recursion: the Towers of Hanoi problem by terra incognita
in thread Recursion: The Towers of Hanoi problem by DigitalKitty

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others browsing the Monastery: (6)
    As of 2015-07-29 05:23 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









      Results (260 votes), past polls