Having read an article about perl 6 I was inspired to write a program in it. Here is an implementation of Conways' (not that Conways') Game of Life (not that game of life or that one.) There is a game of life implementation in the Parrot distribution but this isn't based on that one.

Of course as perl 6 doesn't exist yet I haven't actually tested this. (which also means you can't downvote me for mistakes ha! ha!) but I've tried to write this as if it were a real life program one would write. My original version had comments but I've stripped them out because I thought monks might find it interesting to ty and work out what's going on for themselves. Personally I was impressed by how easily the new features "clicked." Finally I'd like to say I used prolegemona correctly in the title but I kant. Here's the code:

#!/usr/bin/perl use warnings; use strict; my $life = new life(20); while(1) { $life.display(); } class life { has Int $.count; has Int $.dimension; has Array of Int @.grid is dim ($.dimension, $.dimension); method CREATE(Int $dimension) { $.count = 0; $.dimension = $dimension; loop (my $x = 0; $x < $dimenion; $x++) { loop (my $y = 0; $y < $dimension; $y++) { @.grid[$x][$y] = 0; } } @.grid[$dimension / 2 - 1][$dimension / 2] = 1; @.grid[$dimension / 2 - 1][$dimension / 2 + 1] = 1; @.grid[$dimension / 2][$dimension / 2] = 1; @.grid[$dimension / 2][$dimension / 2 - 1] = 1; @.grid[$dimension / 2 + 1][$dimension / 2] = 1; } method calculate() is private { my @newgrid; loop (my $x = 0; $x < .dimension; $x++) { loop (my $y = 0; $y < .dimension; $y++) { my $live = 0; for ($x - 1, $y - 1, $x, $y - 1, $x + 1, $y - 1, $x - 1, $y, $ +x + 1, $y, $x - 1, $y + 1, $x, $y + 1, $x + 1, $y + 1) -> ($nx, $ny +) { next if 0 > $nx > .dimension || 0 > $ny > .dimension; $live++ if @.grid[$nx][$ny] == 1; } $newgrid[$x][$y] = given @.grid[$x][$y] { when 0 { 1 if $live == 3}; when 1 { 1 if 1 < $live < 4 }: } || 0; } } @.grid = @newgrid; } method display { loop (my $x = 0; $x < $.dimension; $x++) { loop (my $y = 0; $y < $.dimension; $y++) { print $.grid[$x][$y] ? '+' : '.'; } print "\n"; } print "Turn $(++$.count), press enter for next turn, ctl-c to quit +'; <STDIN>; .calculate(); } }

--

જલધર

Comment onProlegemona To A Future Scripting Language: Game Of Life In Perl 6DownloadCode