#!C:\perl\bin use strict; #use warnings; # commented out because it throws a warning in the middle #use diagnostics; # of execution, about @polea being uninitialized. ?? # Towers of Hanoi # Perl version (5.10.0) my $numdisks = 0; my $count = 0; my $grandTotal = 0; print "Number of disks? "; chomp( $numdisks = ); clear (); my $i = 0; my @polea = ""; my @poleb = ""; my @polec = ""; my $loop = 0; my $string = ""; while ($numdisks > $loop++) { $string = $string ."x"; push (@polea, $string); } print "A\t\t\tB\t\t\tC\n"; for (my $len = 0 ;$len <= $numdisks; $len++) { print "$polea[$len]\t\t\t$poleb[$len]\t\t\t$polec[$len]\n"; } sleep 1; movedisks( $numdisks, 'A', 'B', 'C' ); print "Total: ", $grandTotal, " \n"; # 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( $numb, $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\t\t\tB\t\t\tC\n"; for (my $len = 0 ;$len <= ($numdisks -1); $len++) { print "$polea[$len]\t\t\t$poleb[$len]\t\t\t$polec[$len]\n"; } $grandTotal ++; sleep 1; # clear (); commented out so I can see the execution! }