Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
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":



  • 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 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
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2021-06-15 23:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What does the "s" stand for in "perls"? (Whence perls)












    Results (74 votes). Check out past polls.

    Notices?