#!/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'; ; .calculate(); } }