Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
Okay, here's my take on this...
#!/usr/bin/perl package main; # We're Perl 6 we are use warnings; use strict; given Life.new(20) { while 1 { .display_on($*OUT); .calculate; } } class Life { has Int $.generations; has Int $.max; has @.grid; has @.neighbourhoods; method BUILD(Int $dim) { $.generations = 0; my @grid of Bit is dim(2, $dim, $dim) is default(0); @grid[0][$dim / 2 - 1][$dim / 2 ] = 1; @grid[0][$dim / 2 - 1][$dim / 2 + 1] = 1; @grid[0][$dim / 2 ][$dim / 2 ] = 1; @grid[0][$dim / 2 ][$dim / 2 - 1] = 1; @grid[0][$dim / 2 + 1][$dim / 2 ] = 1; @.grid := @grid; # Set up our neighbourhoods and cell count $.max = $dim - 1; for 0..$.max -> $i { for 0..$.max -> $j { my @hood is dim(2,3,3); @hood[0..1][0..2][0..2] >>:=<< @.grid[0..1][map $_%($.max+1), $i-1..$i+1] .[map $_ % ($.max+1),$j-1..$j+1]; push @.neighbourhoods, @hood; } } } method calculate { for @.neighbourhoods -> @hood { my @old := @hood[$.generations % 2]; my $new := @hood[($.generations + 1) % 2]; my $live = sum(*@old) - @old[1][1]; $new = $live == 2 && @old[1][1] || $live == 3; } $.generations++; } method display_on($fh) { for 0 .. $.max -> $i { for 0 .. $.max -> $j { $fh.print($.grid[$generation][$i][$j] ?? '+' :: '.'); LAST { $fh.print("\n") } } } print "Turn $.generation, press enter for next turn, ctl-c to quit +"; <STDIN> } }
Of course, all this presupposes that I actually understand what I'm doing with :=, which isn't necessarily a good assumption. The trick is that we're precalculating (and binding) all our neighbourhoods, and setting up the old/new grids at initialization time as well, which enables us to simplify the calculation somewhat.

In reply to Re: Prolegemona To A Future Scripting Language: Game Of Life In Perl 6 by pdcawley
in thread Prolegemona To A Future Scripting Language: Game Of Life In Perl 6 by jaldhar

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!
  • 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:
    & &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 taking refuge in the Monastery: (7)
    As of 2014-08-23 16:23 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The best computer themed movie is:











      Results (174 votes), past polls