Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

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

by pdcawley (Hermit)
on Apr 28, 2003 at 21:07 UTC ( #253838=note: print w/ replies, xml ) Need Help??


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

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.


Comment on Re: Prolegemona To A Future Scripting Language: Game Of Life In Perl 6
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://253838]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2014-09-23 02:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (210 votes), past polls