Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

comment on

( [id://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":



  • 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 chanting in the Monastery: (6)
As of 2024-04-25 08:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found