Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Hanoi

by teamster_jr (Curate)
on Jun 06, 2003 at 10:57 UTC ( [id://263640]=obfuscated: print w/replies, xml ) Need Help??

I know there have been discussions about this before (did a search for hanoi) and there is a shorter golf solution out there somewhere, but here's my effort anyway - it is parameterised so you can do hanoi.pl <number of rings>

enjoy!

sub d{print"\ec";select$q,$q,$q,0.3;for$p(0..2){printf"\e[%d;%dH%s".$/ +x$ d,$d-$_+2,2*$d*(1+$p)-($$p[$_]?(length$$p[$_])/2:1),$$p[$_]||"|"for 0. +.$ d-1}}push@0,"-"x(1-2*$_)for-($d=shift||3)+1..0;sub h{my($n,$s,$d,$i)=@ +_; if($n>0){h($n-1,$s,$i,$d);push@$d,pop@$s;d;h($n-1,$i,$d,$s)}}h($d,0,2, +1)

alex
Edit: Oh sorry guys i so should have said it requires VT100
Edit again: I've knocked up this non VT100 version for those that can't see the previous one.
HTH

Replies are listed 'Best First'.
Re: Hanoi
by dorko (Prior) on Jun 06, 2003 at 15:21 UTC
    ++ ! I had an instructor in college (circa 1990) who thought it would be instructive in the methodology of recursion to have us program a Towers of Hanoi solution in Motorola 68k assembly. Not one out of ~70 classmates got anything working correctly. (I always thought "Towers of Hanoi" sounded like a hotel...)

    Cheers,

    Brent
    -- Yeah, I'm a Delt.

Re: Hanoi
by one4k4 (Hermit) on Jun 06, 2003 at 18:40 UTC
    I'm ++'ing because it seems like it worked, I just don't get it..
    [3;3H| [4;7H| [3;7H| [4;10H--- [3;11H-
    What's happening here? I know what the game is, and I think I'm just missing the graphical representation that's going on.. Thoughts?

    One4k4 - perlmonks@poorheart.com (www.poorheart.com)
      You need a better VT100 terminal emulator (it looks fine in both xterm and OS X's Terminal.app).

      /s

      It works fine on my redhat 8.x box but I get the same type of output that one4k4 gets when I run it in a dos window while under WinME. Just though to try cygwin on the ME machine and still get the same as under dos. FWIW I'm running perl 5.8.0 on redhat and 5.6.1 built for MSWin32-x86 on ME.

      Daeve

        under win32 did you try it under rxvt?
        that usually solves my terminal needs.
Re: Hanoi
by teamster_jr (Curate) on Jun 20, 2003 at 09:00 UTC
    Here's the non vt100 version for those that can't see the other
    sub d{for(1..$d){for$p(0..2){{$s=$$p[$d-$_]||"|";$f=$"x(($d*2-1-length +$s )/2);printf"$f$s$f"}}print$/;}select$q,$q,$q,(print$/)/3}push@0,"-"x(1 +-2 *$_)for-($d=shift||3)+1..0;sub h{my($n,$s,$d,$i)=@_;if($n>0){h($n-1,$s +,$ i,$d);push@$d,pop@$s;d;h($n-1,$i,$d,$s)}}d;h($d,0,2,1)

    al

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: obfuscated [id://263640]
Approved by moxliukas
Front-paged by gmax
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2024-03-19 05:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found