#!/usr/bin/perl6
given new life(dimension => 20)
{
for 1..Inf -> $count
{
.display();
print "TURN $count, press enter for next turn, ctl-c to quit";
<$*STDIN>;
.calculate();
}
}
class life
{
has int $.dimension;
has @.grid is dim($.dimension, $.dimension)
is base_index($.dimension/2, $.dimension/2)
of bit is default(0)
is str {<< - + >>[$_]};
method BUILD($.dimension)
{
for (-1, 0), (-1, 1), (0, 0), (0, -1), (1, 0) -> $x, $y
{
@.grid[$y][$x] = 1;
}
}
method will_live(int $y, int $x) returns bool is private
{
my $neighborhood = @.grid[$y+(-1|0|+1)][$x+(-1|0|+1)];
my $alive = sum($neighborhood.elems);
@.grid[$y][$x] ?? 1 < $alive-1 < 4 :: $alive == 3;
}
method calculate
{
my @new_grid is like @.grid;
for @new_grid.kv -> $y, @new_row is rw
{
for @row.kv -> $x, $new_cell is rw
{
$new_cell = .will_live($y,$x) ?? 1 :: 0;
}
}
@.grid = @new_grid;
}
method display
{
for @.grid -> @row
{
print @row, "\n";
}
}
}