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.