<?xml version="1.0" encoding="windows-1252"?>
<node id="253394" title="Re: Prolegemona To A Future Scripting Language: Game Of Life In Perl 6" created="2003-04-26 14:26:27" updated="2005-07-03 08:52:29">
<type id="11">
note</type>
<author id="183551">
dpuu</author>
<data>
<field name="doctext">
I like the idea. Here's my attempt: I've tried to use a few more of the advanced features...
&lt;code&gt;
#!/usr/bin/perl6

given new life(dimension =&gt; 20)
{
    for 1..Inf -&gt; $count
    {
        .display();
        print "TURN $count, press enter for next turn, ctl-c to quit";
        &lt;$*STDIN&gt;;
        .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 {&lt;&lt; - + &gt;&gt;[$_]};

    method BUILD($.dimension)
    {
        for (-1, 0), (-1, 1), (0, 0), (0, -1), (1, 0) -&gt; $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 &lt; $alive-1 &lt; 4 :: $alive == 3;
    }

    method calculate
    {
        my @new_grid is like @.grid;
        for @new_grid.kv -&gt; $y, @new_row is rw
        {
            for @row.kv -&gt; $x, $new_cell is rw
            {
                $new_cell = .will_live($y,$x) ?? 1 :: 0;
            }
        }
        @.grid = @new_grid;
    }

    method display
    {
        for @.grid -&gt; @row
        {
            print @row, "\n";
        }
    }
}
&lt;/code&gt;
--Dave

&lt;p&gt;
&lt;b&gt;Update:&lt;/b&gt; I'm pretty sure the junction I gave for the neighborhood won't work: here's an improved will_live method:
&lt;code&gt;
    method will_live(int $y, int $x) returns bool
    {
        my $offsets = [-1|0|+1 , -1|0|+1] &amp; none([0,0]);
        my @neighbors = ($offsets &gt;&gt;+&lt;&lt; [$y,$x]).states;
        my @cells = @neighbors.map {@.grid[$_[0]][$_[1]]};
        my $alive = @cells.sum;

        @.grid[$y][$x] ?? $alive == 2|3 :: $alive == 3;
    }
&lt;/code&gt;
Still too much line noise though. There must be a simpler way.</field>
<field name="root_node">
253366</field>
<field name="parent_node">
253366</field>
</data>
</node>
