blib/lib/Game/Life/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 SCAR 27.9
CC
15
Cov
82%
process
12.0
__ANON__
6.0
surround
4.1
CRAP 16.2
« blib/lib/Game/Life/NDim.pm ↑ Summary blib/lib/Game/Life/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.3');
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 sub seed {
49 1009 ▶ my ($self, $types) = @_;
Subroutine seed: called CC=4 SCAR=13.9
Pod documented
50 1009 my $new_type;
51  
52 TYPE:
53 1009 ▶ while (!defined $new_type) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $new_type) 1345 0 1345
54 1345 for my $type (keys %{$types}) {
55 2174 ▶ if ( rand() < $types->{$type} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (rand() < $types->{$type}) 1009 1165 2174
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 sub process {
69 0 ▶ my ($self, $rules) = @_;
Subroutine process: not called CC=3 SCAR=24.8
Pod documented
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 sub set {
91 0 ▶ my ($self) = @_;
Subroutine set: not called CC=1 SCAR=6.9
Pod documented
92  
93 0 $self->type($self->next_type);
94  
95 0 return $self;
96 }
97  
98 sub surround {
99 3 ▶ my ($self, $level) = @_;
Subroutine surround: called CC=4 SCAR=14.0
Pod documented
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 sub transformer {
119 4 ▶ my ($self) = @_;
Subroutine transformer: called CC=2 SCAR=6.9
Pod documented
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 $itter = sub {
131 70 ▶ if (!defined $point) {
Subroutine __ANON__: called CC=6 SCAR=17.9
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 sub clone {
159 0 ▶ my ($self) = @_;
Subroutine clone: not called CC=1 SCAR=6.9
Pod documented
160  
161 0 return __PACKAGE__->new(type => $self->type, board => $self->board, position => $self->position);
162 }
163  
164 sub to_string {
165 25 ▶ my ($self) = @_;
Subroutine to_string: called CC=1 SCAR=0.0
Pod documented
166  
167 25 return $self->type;
168 }
169  
170 3 1;
171  
172 __END__
173  
174 =head1 NAME
175  
176 Game::Life::NDim::Life - Object representing a life
177  
178 =head1 VERSION
179  
180 This documentation refers to Game::Life::NDim::Life version 0.0.3.
181  
182  
183 =head1 SYNOPSIS
184  
185 use Game::Life::NDim::Life;
186  
187 # Brief but working code example(s) here showing the most common usage(s)
188 # This section will be as far as many users bother reading, so make it as
189 # educational and exemplary as possible.
190  
191  
192 =head1 DESCRIPTION
193  
194 =head1 SUBROUTINES/METHODS
195  
196 =head2 C<seed ( )>
197  
198 =head2 C<process ( )>
199  
200 =head2 C<set ( )>
201  
202 =head2 C<surround ( )>
203  
204 =head2 C<transformer ( )>
205  
206 =head2 C<clone ( )>
207  
208 =head2 C<to_string ( )>
209  
210  
211 =head1 DIAGNOSTICS
212  
213 =head1 CONFIGURATION AND ENVIRONMENT
214  
215 =head1 DEPENDENCIES
216  
217 =head1 INCOMPATIBILITIES
218  
219 =head1 BUGS AND LIMITATIONS
220  
221 There are no known bugs in this module.
222  
223 Please report problems to Ivan Wills (ivan.wills@gmail.com).
224  
225 Patches are welcome.
226  
227 =head1 AUTHOR
228  
229 Ivan Wills - (ivan.wills@gmail.com)
230  
231 =head1 LICENSE AND COPYRIGHT
232  
233 Copyright (c) 2010 Ivan Wills (14 Mullion Close, Hornsby Heights, NSW Australia 2077).
234 All rights reserved.
235  
236 This module is free software; you can redistribute it and/or modify it under
237 the same terms as Perl itself. See L<perlartistic>. This program is
238 distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
239 without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
240 PARTICULAR PURPOSE.
241  
242 =cut
« blib/lib/Game/Life/NDim.pm ↑ Summary blib/lib/Game/Life/NDim/Dim.pm »