Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

I don't know if this would be considered an obfuscation or a golfing exercise, but nonetheless, in only 451 characters, I present you with John Conway's Game Of Life.

my($a,$c,$g,@b)=(40,10);$\=$/;for my$r(0..$a){$b [$r][$_]=!int(rand($c))?'*':$"for(0..$a)}sub g{ my($d,@e);for my$r(0..$a){for(0..$a){my$h=0;for my$i(-1..1){for my$j(-1..1){my($f,$g)=($r+$i,$_ +$j);next if$f<0||$f>$a||$g<0||$g>$a||!($i||$j); $h++if$b[$f][$g]ne$"}}$e[$r][$_]=($b[$r][$_]ne$" &&$h==2)||$h==3?'*':$";$d++if$e[$r][$_]ne$b[$r][ $_]}}@b=@e;$d}do{print`clear`,++$g;for my$r(0.. $a){print map{$b[$r][$_]} (0..$a)}select $&,$&, $&,.4}while(&g)

Update - Using a few tricks I picked up from teamster_jr's wonderful version below (and a few golf tricks I always seem to forget to implement, I knocked it down to 366 characters.

$a=40;$\=$/;@a=0..$a;for$r(@a){$b[$r][$_] =!int rand 10?0:$"for@a}sub g{my($d,@e); for$r(@a){for(@a){$h=0;for$i(-1..1){for$j (-1..1){$f=$r+$i;$k=$_+$j;$h++if$b[$f][$k ]ne$"&&!($f<0||$f>$a||$k<0||$k>$a||!$i&&! $j)}}$*=$e[$r][$_]=$h==3?0:$b[$r][$_]ne$" &&$h==2?0:$";$d++if$*ne$b[$r][$_]}}@b=@e; $d}{print"\ec",++$g,$\,map{@{$b[$_]},$\} @a;select$&,$&,$&,.1;redo if&g}

Final update - by eliminating a few niceties (printing the generation number, allowing the program to terminate by itself), and accidentally creating a fractal, I chopped it down to 282 characters (not counting new lines).

@a=0..40;for$r(@a){$b[$r][$_]=!int rand 9?0:$"for@a}{print"\ec",map{@{$b[$_]}, $/}@a;select$&,$&,$&,.1;my@e;for$r(@a){ for(@a){$h=0;for$i($r-1..$r+1){for$j($_ -1..$_+1){$h++if$b[$i][$j]ne$"&!($i<0|$i >40|$j<0|$j>40)&($i^$r||$j^$_)}}$e[$r][ $_]=$h==3|$h==2&$b[$r][$_]ne$"?0:$"}}@b= @e;redo}

Final, final update Fine, fine, since this node is still getting some play and I have continued to tinker with it, here's a 270 character version. How low can I go, indeed.

@a=0..40;$b[$_]=[map{rand 9<1?0:$"}@a] for@a;{print"\ec",map{@{$b[$_]},$/}@a; select$&,$&,$&,.1;@e=@b;for$r(@a){for(@a ){$h=0;for$i($r-1..$r+1){for$j($_-1..$_+1) {$h++if$e[$i][$j]ne$"&!($i<0|$i>40|$j<0| $j>40)&($i^$r||$j^$_)}}$b[$r][$_]=$h==3| $h==2&$e[$r][$_]ne$"?0:$"}}redo}

Now on to other things.


In reply to Game of Life by jimt

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



  • 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, 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
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others contemplating the Monastery: (3)
    As of 2020-09-19 20:56 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      If at first I donít succeed, I Ö










      Results (115 votes). Check out past polls.

      Notices?