Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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$/

In reply to functional cellular automata by jonadab

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • 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.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (3)
As of 2024-03-29 04:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found