http://qs321.pair.com?node_id=97132

These two JAPHs draw worms on the screen, like the worms program under Unix. But with a Perlish twist, of course! Both JAPHs use console escapes to do the animation, so they may not work on all platforms.

First I wrote a JAPH to draw a single worm crawling around the screen:

@D=(0,0,-1,$|=print"\e[H\e[J");$x=30;$y=10;{select$,,$,,$,,.07;$p=($j %=@j=',rekcah lreP rehtona tsuJ'=~/./g)==$M{"@{*t=shift@w}"}&&"\e[H". "\e[B"x$t[0]."\e[C"x$t[1].$"if@j<=push@w,[$y,$x];{$D+=$D[rand@D];$_= +( A,D,B,C)[$D%=4];/A/&&$y?$y--:/D/&&$x?--$x:/B/&$y<20?$y++:/C/&$x<60? +++ $x:redo}print$p,"\e[H","\e[B"x$y,"\e[C"x$x,$j[$M{"$y $x"}=$j++];re +do}
I had fun with that, but I still felt something was lacking... so I wrote a new JAPH to draw any number of crawling worms! With this JAPH, you can specify the number of worms on the command line, like `perl worms 4`:
@T=(0,0,-1,$|=print"\e[H\e[J");@w=[([10,30])x($n=0+pop||3)];{select$,, +$,,$,, .07;my@W;$j%=@j=',rekcah lreP rehtona tsuJ'=~/./g;for$i(0..$n-1){($y, +$x)=@{$ w[-1][$i]};{$D[$i]+=$T[rand@T];$_=(A,D,B,C)[$D[$i]%=4];/A/&&$y?--$y: +/D/&&$x? $x--:/B/&$y<20?++$y:/C/&$x<60?$x++:redo}($Y,$X)=@{$w[0][$i]},$p="$j. +$i"eq$M{ $Y,$X}&&"\e[H"."\e[B"x$Y."\e[C"x$X.$"if@j<=@w;push@W,[$y,$x];print$p, +"\e[H", "\e[B"x$y,"\e[C"x$x,$j[$M{$y,$x}="$j.$i"]}shift@w if@j<push@w,\@W;++$j +;redo}
Enjoy!

Replies are listed 'Best First'.
Re: Worms!
by stefan k (Curate) on Jul 17, 2001 at 14:14 UTC
    There are some uninitialized values when you run with -w. This interferes with the worms in my terminal. I think you should fix it. Otherwise a nice thing :)

    Regards... Stefan

      It's a JAPH! Why run it with -w in the first place?

      When I wrote this JAPH, I had only two things in mind: do something neat, and use as few characters as possible. So, run it with warnings turned off, and all will be well. :)

        Why run it with -w in the first place?

        Because my XEmacs inserts all the usual stuff (including -w) automatically when I open a new perl file ;-)

        I just thought you might be interested...

        Regards... Stefan

Re: Worms!
by wiz (Scribe) on Jul 17, 2001 at 01:40 UTC
    Don't run this on Windoze........ *shutters at the infinite loop program that resulted
    ----------------------------
    Wiz, The VooDoo Doll
    Head Master of 12:30 Productions
    ----------------------------
      It's actually meant to be an infinite loop. Those crazy worms just keep on goin'. :) When you want it to stop, control-C should kill the program under Unix or Windows.

      Sorry about any inconvenience; I'll remember to note this behavior if I do another JAPH like this in the future.

      It works just fine on Windows, under cygwin :)
         MeowChow                                   
                     s aamecha.s a..a\u$&owag.print