NDim/Board.pm

filter: Statementstmt 84.7% 144 / 170 Branchbran 73.8% 31 / 42 Conditioncond 45.4% 5 / 11 MC/DCmcdc 28.5% 2 / 7 Subroutinesub 95.4% 21 / 22 Podpod 100% 6 / 6 totaltotal 81.0% 209 / 258 CC 34 SCAR 20.5
CC
34
Cov
81%
to_string
22.2
set_life
12.0
next_life
7.1
CRAP 7.8
« NDim.pm ↑ Summary NDim/Life.pm »
1 package Game::Life::NDim::Board;
2  
3 # Created on: 2010-01-04 18:52:38
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 feature qw/:5.10/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use Carp qw/croak cluck confess/;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use List::Util qw/max/;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use Data::Dumper qw/Dumper/;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 3 ▶use English qw/ -no_match_vars /;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 3 ▶use Game::Life::NDim::Life;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 3 ▶use Game::Life::NDim::Dim;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 3 ▶use Params::Coerce ();
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 3 ▶use overload '""' => \&to_string;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 3 our $VERSION = version->new('0.0.4');
24 3 our @EXPORT_OK = qw//;
25 3 our %EXPORT_TAGS = ();
26  
27 3 has items => (
28 is => 'rw',
29 isa => 'ArrayRef',
30 lazy_build => 1,
31 );
32  
33 3 has dims => (
34 is => 'ro',
35 isa => 'Game::Life::NDim::Dim',
36 required => 1,
37 );
38  
39 3 has cursor => (
40 is => 'rw',
41 isa => 'Game::Life::NDim::Dim',
42 );
43  
44 has types => (
45 is => 'rw',
46 isa => 'HashRef',
47 7 ▶ default => sub {{ 0 => 0.6, 1 => 0.4 }},
Subroutine __ANON__: called CC=1 SCAR=0.0
48 3 );
49  
50 3 has wrap => (
51 is => 'rw',
52 isa => 'Bool',
53 default => 0,
54 );
55  
56 3 has verbose => (
57 is => 'rw',
58 isa => 'Bool',
59 default => 0,
60 );
61  
62 7 ▶around new => sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
63 7 my ($new, $class, %params) = @_;
64  
65 7 ▶ if (ref $params{dims} eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $params{"dims"} eq "ARRAY") 2 5 7
66 2 $params{dims} = Game::Life::NDim::Dim->new($params{dims});
67 }
68  
69 7 my $self = $new->($class, %params);
70  
71 7 $self->reset;
72 7 ▶ $self->seed(%params) if $params{rand};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"rand"} 2 5 7
73 #$self->cursor(Game::Life::NDim::Dim->new([]));
74 7 for (@{ $self->dims }) {
75 15 push @{ $self->cursor }, 0;
76 }
77  
78 7 return $self;
79 3 };
80  
81 7 ▶sub _build_items {
Subroutine _build_items: called CC=2 SCAR=6.9
82 7 my ($self, %params) = @_;
83  
84 7 ▶ $self->types = $params{types} if $params{types};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"types"} 0 7 7
85  
86 7 my $items = [];
87 7 my $lives = 0;
88  
89 7 my $builder;
90 208 ▶ $builder = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
91 208 my ($items, $dims, $pos) = @_;
92 208 my $count = $dims->[0];
93  
94 208 for my $i ( 0 .. $count - 1 ) {
95 1835 ▶ if ( @{$dims} == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$dims;} == 1) { } 1634 201 1835
96 $items->[$i] = Game::Life::NDim::Life->new(
97 1634 position => Game::Life::NDim::Dim->new([ @{ $pos }, $i ]),
98 board => $self
99 );
100 1634 $lives++;
101 }
102 else {
103 201 $items->[$i] = [];
104 201 my $sub_dims = [ @{ $dims }[ 1 .. @{ $dims } - 1 ] ];
105 201 my $sub_pos = [ @{ $pos }, $i ];
106 201 my $sub_items = $items->[$i];
107 201 $builder->($sub_items, $sub_dims, $sub_pos);
108 }
109 }
110 7 };
111 7 $builder->($items, $self->dims, []);
112  
113 7 return $items;
114 }
115  
116 2 ▶sub seed {
Subroutine seed: called CC=3 SCAR=11.0
Pod documented
117 2 my ($self, %params) = @_;
118  
119 2 ▶ $self->types = $params{types} if $params{types};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"types"} 0 2 2
120  
121 2 my $i = 0;
122 2 ▶ while ( ref (my $life = $self->next_life()) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref(my $life = $self->next_life)) 1009 2 1011
123 1009 $life->seed($self->types);
124 }
125 2 $self->reset;
126  
127 2 return $self;
128 }
129  
130 12 ▶sub reset {
Subroutine reset: called CC=3 SCAR=11.0
Pod documented
131 12 my ($self) = @_;
132 12 my @cursor;
133  
134 12 for (@{ $self->dims }) {
135 26 push @cursor, 0;
136 }
137  
138 12 ▶ confess "Empty cursor!" if !@cursor;
Branch: 1 branch
BranchTrueFalseTotal
unless @cursor 0 12 12
139  
140 12 $cursor[-1] = -1;
141  
142 12 $self->cursor(Game::Life::NDim::Dim->new(\@cursor));
143  
144 12 return $self;
145 }
146  
147 1021 ▶sub next_life {
Subroutine next_life: called CC=7 SCAR=19.6
Pod documented
148 1021 my ($self) = @_;
149 1021 my $max_dim;
150  
151 1021 ▶ return if !$self->cursor->increment($self->dims);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->cursor->increment($self->dims) 4 1017 1021
152  
153 1017 my $life = $self->items;
154  
155 1017 my @pos;
156 1017 for my $i ( 0 .. @{ $self->dims } - 1 ) {
157 3034 ▶ if ( ! exists $self->cursor->[$i] ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->cursor->[$i]) 0 3034 3034
158 0 die "here?\n";
159 0 $self->cursor->[$i] = 0;
160 }
161 3034 my $pos = $self->cursor->[$i];
162 3034 push @pos, $pos;
163 3034 ▶ if ( ref $life eq 'ARRAY' && @{ $life } < $pos + 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $life eq "ARRAY" and @{$life;} < $pos + 1) 301 2733 3034
Condition66%2/3
ref $life eq "ARRAY" and @{$life;} < $pos + 1
!ll&&!rl&&r
02733301
MC/DC50%1/2
ref $life eq "ARRAY" and @{$life;} < $pos + 1
ref $life eq "ARRAY" @{$life;} < $pos + 1
Truth table66%2/3
A and B
A= ref $life eq "ARRAY"B= @{$life;} < $pos + 1
ABresult
0X0
100
111
164 $life->[$pos] =
165 301 ▶ $i < @{ $self->cursor } - 1 ? []
Branch: 1 branch
BranchTrueFalseTotal
$i < @{$self->cursor;} - 1 ? : 22 279 301
166 : Game::Life::NDim::Life->new(board => $self, position => $self->cursor);
167 }
168 3034 $life = $life->[$pos];
169 }
170  
171 1017 return $life;
172 }
173  
174 0 ▶sub set_life {
Subroutine set_life: not called CC=3 SCAR=24.8
Pod documented
175 0 my ($self, $life) = @_;
176  
177 0 my $curr = $self->items;
178  
179 0 for my $i ( @{ $self->cursor } ) {
180 0 ▶ if ( ref $curr->[$i] eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $curr->[$i] eq 'ARRAY') { } 0 0 0
181 0 $curr = $curr->[$i];
182 }
183 else {
184 0 $curr->[$i] = $life;
185 }
186 }
187  
188 0 return $self;
189 }
190  
191 50 ▶sub get_life {
Subroutine get_life: called CC=6 SCAR=18.0
Pod documented
192 50 my ($self, $position) = @_;
193  
194 50 my $item = $self->items;
195 50 ▶ my $min = $self->wrap ? -1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->wrap ? : 1 49 50
196 50 ▶ die if !defined $min;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $min 0 50 50
197  
198 50 for my $i (@{ $position } ) {
199 124 ▶ croak "Cannot get game position from $position $i >= $min " if $i < $min || !exists $item->[$i];
Branch: 1 branch
BranchTrueFalseTotal
if $i < $min or not exists $item->[$i] 1 123 124
Condition66%2/3
$i < $min or not exists $item->[$i]
l!l&&r!l&&!r
10123
MC/DC50%1/2
$i < $min or not exists $item->[$i]
$i < $min not exists $item->[$i]
Truth table66%2/3
A or not(B)
A= $i < $minB= not exists $item->[$i]
ABresult
1X1
011
000
200 123 $item = $item->[$i];
201 }
202  
203 49 return $item;
204 }
205  
206 1 ▶sub to_string {
Subroutine to_string: called CC=11 SCAR=31.0
Pod documented
207 1 my ($self) = @_;
208  
209 1 ▶ die "The dimension of this game is to large to sensibly convert to a string\n" if @{ $self->dims } > 3;
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->dims;} > 3 0 1 1
210  
211 1 ▶ my $spacer = ( 10 >= max (@{$self->dims}, scalar @{$self->dims}) ) ? ' ' : '';
Branch: 1 branch
BranchTrueFalseTotal
10 >= &max(@{$self->dims;}, scalar @{$self->dims;}) ? : 1 0 1
212  
213 1 my $out = '';
214 1 my @outs;
215 1 $self->reset;
216 1 my $i = 0;
217 1 my $level = 0;
218 1 ▶ while ( ref ( my $life = $self->next_life() ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref(my $life = $self->next_life)) 4 1 5
219 4 ▶ if ( @{$self->cursor} > 2 && $self->cursor->[0] != $level) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->cursor;} > 2 and $self->cursor->[0] != $level) 0 4 4
Condition33%1/3
@{$self->cursor;} > 2 and $self->cursor->[0] != $level
!ll&&!rl&&r
400
MC/DC0%0/2
@{$self->cursor;} > 2 and $self->cursor->[0] != $level
@{$self->cursor;} > 2 $self->cursor->[0] != $level
Truth table33%1/3
A and B
A= @{$self->cursor;} > 2B= $self->cursor->[0] != $level
ABresult
0X0
100
111
220 0 $out .= "\n";
221 0 $level = $self->cursor->[0];
222 0 push @outs, $out;
223 0 $out = '';
224 }
225 4 $out .= $life;
226 4 ▶ $out .= $self->cursor->[-1] == $self->dims->[-1] ? "\n" : $spacer;
Branch: 1 branch
BranchTrueFalseTotal
$self->cursor->[-1] == $self->dims->[-1] ? : 2 2 4
227 4 $i++;
228 }
229 1 $self->reset;
230  
231 1 ▶ if (@outs) {
Branch: 1 branch
BranchTrueFalseTotal
if (@outs) 0 1 1
232 0 $out .= "\n";
233 0 $level = $self->cursor->[0];
234 0 push @outs, $out;
235 0 $out = '';
236 0 my @lines;
237 0 for my $level (@outs) {
238 0 my $i = 0;
239 0 for my $line (split /\n/, $level) {
240 0 ▶ $lines[$i] ||= '';
Condition0%0/2
$lines[$i] ||= ""
l!l
00
MC/DC0%0/1
$lines[$i] ||= ""
$lines[$i]
Truth table0%0/2
A
A= $lines[$i]
Aresult
11
00
241 0 $lines[$i] .= " $line";
242 0 $i++;
243 }
244 }
245 0 return join "\n", @lines, '';
246 }
247  
248 #return "Board:\n" . $out . "\nCount = $i\n";
249 1 return $out;
250 }
251  
252 3 1;
253  
254 __END__
« NDim.pm ↑ Summary NDim/Life.pm »