Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Re: Prolegemona To A Future Scripting Language: Game Of Life In Perl 6

by dpuu (Chaplain)
on Apr 26, 2003 at 18:26 UTC ( #253394=note: print w/replies, xml ) Need Help??

in reply to Prolegemona To A Future Scripting Language: Game Of Life In Perl 6

I like the idea. Here's my attempt: I've tried to use a few more of the advanced features...
#!/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"; } } }

Update: I'm pretty sure the junction I gave for the neighborhood won't work: here's an improved will_live method:

method will_live(int $y, int $x) returns bool { my $offsets = [-1|0|+1 , -1|0|+1] & none([0,0]); my @neighbors = ($offsets >>+<< [$y,$x]).states; my @cells = {@.grid[$_[0]][$_[1]]}; my $alive = @cells.sum; @.grid[$y][$x] ?? $alive == 2|3 :: $alive == 3; }
Still too much line noise though. There must be a simpler way.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://253394]
[LanX]: remote rules, I've been offered jobs in the US with the promise to stay outside
[MidLifeXis]: Nope. Just looking around, as there are multiple culture and business changes in the area I am in.
[MidLifeXis]: not yet, anyway. Who knows. Just do my best and whatever happens happens.
[LanX]: should be even easier for you
[Discipulus]: oh well we cant afford two monks fired in a day!
[LanX]: any news from tye ' s search?
[MidLifeXis]: Ugh - who else?
[Discipulus]: so the right term is 'gired'
[Discipulus]: look at ot by karl
Discipulus gired: adj the work position that stands between hired and fired..

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (16)
As of 2017-03-23 12:43 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (286 votes). Check out past polls.