NDim/Life.pm

filter: Statementstmt 84.6% 83 / 98 Branchbran 75.0% 15 / 20 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 81.2% 13 / 16 Podpod 100% 7 / 7 totaltotal 82.6% 119 / 144 CC 15 SCAR 14.0
CC
15
Cov
82%
process
12.0
__ANON__
6.0
surround
4.1
CRAP 4.0
« NDim/Board.pm ↑ Summary NDim/Dim.pm »
1 package Game::Life::NDim::Life;
2  
3 # Created on: 2010-01-04 18:54:13
4 # Create by: Ivan Wills
5 # $Id$
6 # $Revision$, $HeadURL$, $Date$
7 # $Revision$, $Source$, $Date$
8  
9 3 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use Data::Dumper qw/Dumper/;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use English qw/ -no_match_vars /;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use List::Util qw/sum max min/;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 3 ▶use overload '""' => \&to_string;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 3 our $VERSION = version->new('0.0.4');
20 3 our @EXPORT_OK = qw//;
21 3 our %EXPORT_TAGS = ();
22 #our @EXPORT = qw//;
23  
24 3 has type => (
25 is => 'rw',
26 isa => 'Str',
27 default => 0,
28 );
29  
30 3 has next_type => (
31 is => 'rw',
32 isa => 'Str',
33 );
34  
35 3 has board => (
36 is => 'rw',
37 isa => 'Game::Life::NDim::Board',
38 required => 1,
39 weak_ref => 1,
40 );
41  
42 3 has position => (
43 is => 'rw',
44 isa => 'Game::Life::NDim::Dim',
45 required => 1,
46 );
47  
48 1009 ▶sub seed {
Subroutine seed: called CC=4 SCAR=13.9
Pod documented
49 1009 my ($self, $types) = @_;
50 1009 my $new_type;
51  
52 TYPE:
53 1009 ▶ while (!defined $new_type) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $new_type) 1337 0 1337
54 1337 for my $type (keys %{$types}) {
55 1870 ▶ if ( rand() < $types->{$type} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (rand() < $types->{$type}) 1009 861 1870
56 1009 $new_type = $type;
57 1009 last TYPE;
58 }
59 }
60 }
61  
62 1009 $self->type($new_type);
63  
64 1009 return $self;
65 }
66  
67 # process
68 0 ▶sub process {
Subroutine process: not called CC=3 SCAR=24.8
Pod documented
69 0 my ($self, $rules) = @_;
70  
71 0 $self->next_type($self->type);
72  
73 # process the rules in order until a rule is found that returns a type to
74 # change too, rules that maintain status quoe return undef
75 RULE:
76 0 for my $rule (@{ $rules } ) {
77 0 my $change = $rule->($self);
78  
79 # next if status quoe
80 0 ▶ next RULE if !defined $change;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $change 0 0 0
81  
82 # stage the changed type
83 0 $self->next_type($change);
84 0 last RULE;
85 }
86  
87 0 return $self;
88 }
89  
90 0 ▶sub set {
Subroutine set: not called CC=1 SCAR=6.9
Pod documented
91 0 my ($self) = @_;
92  
93 0 $self->type($self->next_type);
94  
95 0 return $self;
96 }
97  
98 3 ▶sub surround {
Subroutine surround: called CC=4 SCAR=14.0
Pod documented
99 3 my ($self, $level) = @_;
100 3 my $max = $self->board->dims;
101 3 my @lives;
102 3 my $cursor = $self->position->clone;
103  
104 3 ▶ $level ||= 1;
Condition50%1/2
$level ||= 1
l!l
03
MC/DC0%0/1
$level ||= 1
$level
Truth table50%1/2
A
A= $level
Aresult
11
00
105 3 my $itter = $self->transformer;
106  
107 3 ▶ while (my $transform = $itter->()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $transform = &$itter()) 36 3 39
108 36 my $life = eval{ $self->board->get_life($self->position + $transform) };
109 36 ▶ if (!$EVAL_ERROR) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $Game::Life::NDim::Life::EVAL_ERROR) { } 36 0 36
110 36 push @lives, $life;
111 }
112 0 else { warn "Error: $EVAL_ERROR\n"; }
113 }
114  
115 3 return \@lives;
116 }
117  
118 4 ▶sub transformer {
Subroutine transformer: called CC=2 SCAR=6.9
Pod documented
119 4 my ($self) = @_;
120 4 my @max = @{ $self->board->dims };
121 4 my $max = @max - 1;
122 4 my @transform;
123 my @alter;
124 4 for (0 .. $max) {
125 9 push @transform, -1;
126 }
127 4 my $point;
128  
129 my $itter;
130 70 ▶ $itter = sub {
Subroutine __ANON__: called CC=6 SCAR=17.9
131 70 ▶ if (!defined $point) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $point) 4 66 70
132 4 $point = 0;
133 4 return [@transform];
134 }
135  
136 66 my $done = 0;
137 66 ▶ while (!$done) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($done) 93 0 93
138 93 ▶ if ($transform[$point] + 1 <= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($transform[$point] + 1 <= 1) 62 31 93
139 62 $transform[$point]++;
140 62 $done = 1;
141 62 $point = 0;
142 62 last;
143 }
144 31 $transform[$point] = -1;
145 31 $point++;
146 31 my $undef;
147 31 ▶ return $undef if !exists $transform[$point];
Branch: 1 branch
BranchTrueFalseTotal
unless exists $transform[$point] 4 27 31
148 }
149  
150 62 ▶ return $itter->() if ($max + 1 == (grep {$_ == 0} @transform));
Branch: 1 branch
BranchTrueFalseTotal
if $max + 1 == grep({$_ == 0;} @transform) 4 58 62
151  
152 58 return [@transform];
153 4 };
154  
155 4 return $itter;
156 }
157  
158 0 ▶sub clone {
Subroutine clone: not called CC=1 SCAR=6.9
Pod documented
159 0 my ($self) = @_;
160  
161 0 return __PACKAGE__->new(type => $self->type, board => $self->board, position => $self->position);
162 }
163  
164 25 ▶sub to_string {
Subroutine to_string: called CC=1 SCAR=0.0
Pod documented
165 25 my ($self) = @_;
166  
167 25 return $self->type;
168 }
169  
170 3 1;
171  
172 __END__
« NDim/Board.pm ↑ Summary NDim/Dim.pm »