I was messing around with closures, looking for something
interesting to do (other than the obvious JAPH or quine)
and came up with this...
#!/usr/bin/perl -- -*- cperl -*-
my%t=('H'=>[99,6,0,3,4,0,1],'*'=>[5,3,1,2,1,2,11],'O'=>[7,9,0,4,8,3,4]
,'x'=>[12,3,0,1,9,2,4],'+'=>[17,3,0,1,9,2,4],);sub _{my($x,$y,$t)=@_;$
g[$x][$y]=l($t,$x,$y)}sub l{my($t,$x,$y,$a)=@_;+{v=>$t,i=>sub{++$a;my(
$n,$s,$o);my@t=@{$t{$t}};for$u(-1..1){for$z(-1..1){$n=$g[$x+$u][$y+$z]
;if($n->{v}eq$t){++$s}elsif($n->{v}){++$o}}}if($s<$t[2]or$s>$t[1]or$o>
$t[4]or$o+$s<$t[5]or$a>$t[0]){undef$g[$x][$y]}if(rand(10)<$t[6]or$s==$
t[3]){my($v,$w)=map{rand(42)%3-1}0..1;$v+=$x;$w+=$y;$g[$v][$w]=l($t,$v
,$w)if not$g[$v][$w];}}}}map{_($$_[0],$$_[1],"H")}([20,10],[20,9],[21,
10],[21,9]);_(40,10,'*');_(39,10,'*');map{my$q=$_;map{_($_,$q,'O')}(29
,30)}(18,19);_(30,3,"+")^_(29,3,"x");while($:){&c;for$y(1..22){for$x(1
..60){if($m=$g[$x][$y]){print$$m{v};$$m{i}->()}else{print" "}}print$/}
print$/;sleep 1}sub c{`clear`}#To support Win32 make that sub c{`cls`}
If you let it run, it becomes obvious relatively quickly
that the fast-growth type won't last, but it takes a
bit longer to see what happens between the others.
See if you can predict it before it becomes blindingly
obvious.
(Hint: it's not just how any two types interact with
eachother, but how they interact with the others, too.
For example, if you remove the symbiotic pair, the
interaction between the close-packing and slow-growth
types changes.)
Okay, so it's not the most original concept ever...
but hey, when I did it in QBasic in 1994, it didn't
use a functional paradigm, so that's something.
$;=sub{$/};@;=map{my($a,$b)=($_,$;);$;=sub{$a.$b->()}}
split//,".rekcah lreP rehtona tsuJ";$\=$ ;->();print$/
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
Outside of code tags, you may need to use entities for some characters:
| |
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.