[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

[MacPerl] The Powers of Annoy



Title: The Powers of Annoy
Sure itıs a classic. But as sure as my first program is helloing the world, my second program as a learner (in this case, MacPerl) rearranges disks on sticks. Now I would like to "improve" :) my programming style, i. e. learn how make the program even *more compact* and shorter, or even *trickier*. Do you have any suggestions?

B.T.W. In this program, the disks are represented as numbers; the higher the number, the bigger the disk. You can enter the initial disk height on stick one in line one, right at the beginning of the code. For instance, if you want only four disks to be carried to another stick, omit the "5". Or, if you want a higher initial tower, add "876". If you want to get higher than "9", add letters, for instance "BA987654321".

B.T.W.2: How can I stop a running script under MacPerl? Command-. does not seem to work, neither does the Escape key.

But now the script for the "Powers of Annoy":

$f = "54321"; $fl= length($f); $e = 2**$fl-1; $" = ", ";
@o = ($f, "", ""); print "\n\nThe Towers of Annoy\n\nInitial state: @o\n";
$y=0; $n=0; until ($n >= $e) {chop(@o[$y]); $u = $y; $y=(($y+1)%3); $o[$y] .= "1";
$n++; print "Move #$n: @o\n"; unless ($n >= $e) {
$p = @o[$u]; $s = length($p); $g = substr($p,$s-1,1);
$v = (3-($y+$u));  $r = @o[$v]; $t = length($r); $h = substr($r,$t-1,1);
if ($h eq "") {chop(@o[$u]); @o[$v] .= $g}
elsif (($g eq "") || ($h lt $g)) {chop(@o[$v]); @o[$u] .= $h}
else {chop(@o[$u]); @o[$v] .= $g} $n++; print "Move #$n: @o\n";}}

Have fun!
Michael