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
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.