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!