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!
In reply to Worms!
by chipmunk
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.