From 56dd7c61a4cf5c9b698f63e6755069d53743f915 Mon Sep 17 00:00:00 2001 From: Jay Hannah Date: Sat, 15 Nov 2014 12:11:58 -0600 Subject: [PATCH] more --- code_retreat/2014/perl/Board.pm | 15 +++++++++++++++ code_retreat/2014/perl/board.t | 27 ++++++++++++++++----------- 2 files changed, 31 insertions(+), 11 deletions(-) diff --git a/code_retreat/2014/perl/Board.pm b/code_retreat/2014/perl/Board.pm index 4dc1810..b8ba0fd 100644 --- a/code_retreat/2014/perl/Board.pm +++ b/code_retreat/2014/perl/Board.pm @@ -62,6 +62,21 @@ sub will_live { return 0; } +sub all_living_cells { + my $self = shift; + my @cells; + + foreach (my $x (keys($self->board))) { + foreach (my $y (keys($self->board->{$x}))) { + if($self->board->{$x}->{$y} == 1) { + push (@cells, "$x,$y"); + } + } + + return join (@cells, "|"); + } + +} 1; diff --git a/code_retreat/2014/perl/board.t b/code_retreat/2014/perl/board.t index 8093e49..ac3fa1e 100644 --- a/code_retreat/2014/perl/board.t +++ b/code_retreat/2014/perl/board.t @@ -2,17 +2,22 @@ use Test::More; use Board; ok(my $b = Board->new()); -#ok($b->set_cell(5,5), "set_cell"); -#ok($b->set_cell(4,5), "set_cell"); -#ok($b->set_cell(5,6), "set_cell"); -#ok($b->set_cell(5,3), "set_cell"); -ok($b->set_cell(3,3,1), "set_cell"); -ok($b->check_cell(3,3), "check_cell"); -ok($b = $b->go, "go"); -ok(!$b->check_cell(3,3), "check_cell"); - -ok($b = $b->go, "go"); -ok($b->display, "display"); + +# Lone living cell dies: +ok($b->set_cell(3,3,1), "set_cell"); +ok($b->check_cell(3,3), "check_cell"); +ok($b = $b->go, "go"); +ok(!$b->check_cell(3,3), "check_cell"); + +# Dead cell can burst into life: +ok($b->set_cell(3,3,1), "set_cell"); +ok($b->set_cell(3,4,1), "set_cell"); +ok($b->set_cell(3,5,1), "set_cell"); +ok($b = $b->go, "go"); +ok(!$b->check_cell(3,3), "check_cell"); +is($b->all_living_cells, '2,4', "all_living_cells"); + +ok($b->display, "display"); done_testing();