| 1 | package Game::Life::NDim; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | # Created on: 2010-01-04 18:52:01 | ||||||||||||||||||||||||||||
| 4 | # Create by: Ivan Wills | ||||||||||||||||||||||||||||
| 5 | # $Id$ | ||||||||||||||||||||||||||||
| 6 | # $Revision$, $HeadURL$, $Date$ | ||||||||||||||||||||||||||||
| 7 | # $Revision$, $Source$, $Date$ | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 2 | ▶ | use version; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 2 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 2 | ▶ | use Scalar::Util; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 2 | ▶ | use List::Util qw/sum/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | #use List::MoreUtils; | ||||||||||||||||||||||||||||
| 16 | 2 | ▶ | use Data::Dumper qw/Dumper/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 2 | ▶ | use English qw/ -no_match_vars /; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 2 | ▶ | use Game::Life::NDim::Board; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 2 | ▶ | use overload '""' => \&to_string; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 2 | our $VERSION = version->new('0.0.4'); | |||||||||||||||||||||||||||
| 23 | 2 | our @EXPORT_OK = qw/game_of_life/; | |||||||||||||||||||||||||||
| 24 | 2 | our %EXPORT_TAGS = (); | |||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 2 | has board => ( | |||||||||||||||||||||||||||
| 27 | is => 'rw', | ||||||||||||||||||||||||||||
| 28 | isa => 'Game::Life::NDim::Board', | ||||||||||||||||||||||||||||
| 29 | required => 1, | ||||||||||||||||||||||||||||
| 30 | ); | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | has rules => ( | ||||||||||||||||||||||||||||
| 33 | is => 'rw', | ||||||||||||||||||||||||||||
| 34 | isa => 'ArrayRef', | ||||||||||||||||||||||||||||
| 35 | 2 | ▶ | default => sub {[]}, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 36 | 2 | ); | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 2 | ▶ | sub game_of_life { | ||||||||||||||||||||||||||
|
Subroutine
game_of_life: called CC=4 SCAR=15.0
Pod
documented
| |||||||||||||||||||||||||||||
| 39 | 2 | my %params = @_; | |||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 2 | my $board = Game::Life::NDim::Board->new(%params); | |||||||||||||||||||||||||||
| 42 | 2 | ▶ | die "Where's my wrap? " . Dumper \%params, $board if $params{wrap} && !$board->wrap; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$params{"wrap"} and not $board->wrap
MC/DC0%0/2
$params{"wrap"} and not $board->wrap
$params{"wrap"}
not $board->wrap
Truth table33%1/3
A and not(B)
A= $params{"wrap"}B= not $board->wrap
| |||||||||||||||||||||||||||||
| 43 | 2 | my %new = (board => $board); | |||||||||||||||||||||||||||
| 44 | 2 | ▶ | $new{types} = $params{types} if $params{types}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 2 | return __PACKAGE__->new(%new); | |||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 0 | ▶ | sub add_rule { | ||||||||||||||||||||||||||
|
Subroutine
add_rule: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||
| 50 | 0 | my ($self, @rules) = @_; | |||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 0 | ▶ | while (@rules) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | 0 | my $rule = shift @rules; | |||||||||||||||||||||||||||
| 54 | 0 | ▶ | if (ref $rule eq 'CODE') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | 0 | push @{ $self->rules }, $rule; | |||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | else { | ||||||||||||||||||||||||||||
| 58 | 0 | my $value = shift @rules; | |||||||||||||||||||||||||||
| 59 | 0 | ▶ | push @{ $self->rules }, | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 60 | 0 | ▶ | $rule eq 'live' ? sub { $_[0] ? undef : ( sum $_[0]->surround ) > $value ? 1 : undef } | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 61 | 0 | ▶ | : $rule eq 'die' ? sub { !$_[0] ? undef : ( sum $_[0]->surround ) < $value ? 0 : undef } | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 62 | 0 | : die "The rule \"$rule\" is unknown\n"; | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 0 | return $self; | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 0 | ▶ | sub process { | ||||||||||||||||||||||||||
|
Subroutine
process: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 70 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 0 | ▶ | while ( defined ( my $life = $self->board->next_life() ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | 0 | $life->process($self->rules); | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 0 | return $self; | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 0 | ▶ | sub set { | ||||||||||||||||||||||||||
|
Subroutine
set: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 80 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 0 | ▶ | while ( defined ( my $life = $self->board->next_life() ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | 0 | $life->set(); | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 0 | return $self; | |||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 0 | ▶ | sub to_string { | ||||||||||||||||||||||||||
|
Subroutine
to_string: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 90 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 0 | return $self->board->to_string(); | |||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 2 | 1; | |||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | __END__ | ||||||||||||||||||||||||||||