blib/lib/Game/Life/NDim.pm

filter: Statementstmt 66.1% 43 / 65 Branchbran 8.3% 2 / 24 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 66.6% 12 / 18 Podpod 100% 5 / 5 totaltotal 53.8% 63 / 117 SCAR 36.5
CC
14
Cov
50%
add_rule
30.0
__ANON__
12.0
__ANON__
12.0
CRAP 38.5
« blib/lib/Game/Life/NDim/Board.pm ↑ Summary blib/lib/Game/Life/NDim/Life.pm »
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.3');
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 sub game_of_life {
39 2 ▶ my %params = @_;
Subroutine game_of_life: called CC=4 SCAR=15.0
Pod documented
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
BranchTrueFalseTotal
if $params{"wrap"} and not $board->wrap 0 2 2
Condition33%1/3
$params{"wrap"} and not $board->wrap
!ll&&!rl&&r
200
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
ABresult
0X0
100
111
43 2 my %new = (board => $board);
44 2 ▶ $new{types} = $params{types} if $params{types};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"types"} 0 2 2
45  
46 2 return __PACKAGE__->new(%new);
47 }
48  
49 sub add_rule {
50 0 ▶ my ($self, @rules) = @_;
Subroutine add_rule: not called CC=5 SCAR=34.0
Pod documented
51  
52 0 ▶ while (@rules) {
Branch: 1 branch
BranchTrueFalseTotal
if (@rules) 0 0 0
53 0 my $rule = shift @rules;
54 0 ▶ if (ref $rule eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $rule eq 'CODE') { } 0 0 0
55 0 push @{ $self->rules }, $rule;
56 }
57 else {
58 0 my $value = shift @rules;
59 0 ▶ push @{ $self->rules },
Branch: 2 branches
BranchTrueFalseTotal
$rule eq 'live' ? : 0 0 0
$rule eq 'die' ? : 0 0 0
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
BranchTrueFalseTotal
$_[0] ? : 0 0 0
&sum($_[0]->surround) > $value ? : 0 0 0
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
BranchTrueFalseTotal
!$_[0] ? : 0 0 0
&sum($_[0]->surround) < $value ? : 0 0 0
62 0 : die "The rule \"$rule\" is unknown\n";
63 }
64 }
65  
66 0 return $self;
67 }
68  
69 sub process {
70 0 ▶ my ($self) = @_;
Subroutine process: not called CC=2 SCAR=17.9
Pod documented
71  
72 0 ▶ while ( defined ( my $life = $self->board->next_life() ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $life = $self->board->next_life)) 0 0 0
73 0 $life->process($self->rules);
74 }
75  
76 0 return $self;
77 }
78  
79 sub set {
80 0 ▶ my ($self) = @_;
Subroutine set: not called CC=2 SCAR=17.9
Pod documented
81  
82 0 ▶ while ( defined ( my $life = $self->board->next_life() ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $life = $self->board->next_life)) 0 0 0
83 0 $life->set();
84 }
85  
86 0 return $self;
87 }
88  
89 sub to_string {
90 0 ▶ my ($self) = @_;
Subroutine to_string: not called CC=1 SCAR=6.9
Pod documented
91  
92 0 return $self->board->to_string();
93 }
94  
95 2 1;
96  
97 __END__
98  
99 =head1 NAME
100  
101 Game::Life::NDim - Infrastructure for playing Conway's game of life with support for multiple cell types and 2D or 3D boards.
102  
103 =head1 VERSION
104  
105 This documentation refers to Game::Life::NDim version 0.0.3.
106  
107 =head1 SYNOPSIS
108  
109 use Game::Life::NDim;
110  
111 # Brief but working code example(s) here showing the most common usage(s)
112 # This section will be as far as many users bother reading, so make it as
113 # educational and exemplary as possible.
114  
115  
116 =head1 DESCRIPTION
117  
118 TODO
119  
120 =head1 SUBROUTINES/METHODS
121  
122 =head2 Exportable Functions
123  
124 =head2 C<game_of_life ( %params )>
125  
126 =head2 Class Methods
127  
128 =head3 C<new ( %params )>
129  
130 Param: C<dims> - array of ints - The dimensions of the game (in zero based form ie [1,1] for a 2x2 board
131  
132 Param: C<rand> - bool - If true sets the board with random life types
133  
134 Param: C<types> - hash ref - List of types (keys) and their relative likely hood to be found default {0=> ,1=> }
135  
136 =head2 Object Methods
137  
138 =head3 C<add_rule ( )>
139  
140 =head3 C<process ()>
141  
142 =head3 C<set ()>
143  
144 =head3 C<to_string ()>
145  
146 =head1 DIAGNOSTICS
147  
148 =head1 CONFIGURATION AND ENVIRONMENT
149  
150 =head1 DEPENDENCIES
151  
152 =head1 INCOMPATIBILITIES
153  
154 =head1 BUGS AND LIMITATIONS
155  
156 There are no known bugs in this module.
157  
158 Please report problems to Ivan Wills (ivan.wills@gmail.com).
159  
160 Patches are welcome.
161  
162 =head1 AUTHOR
163  
164 Ivan Wills - (ivan.wills@gmail.com)
165  
166 =head1 LICENSE AND COPYRIGHT
167  
168 Copyright (c) 2010 Ivan Wills (14 Mullion Close, Hornsby Heights, NSW Australia 2077).
169 All rights reserved.
170  
171 This module is free software; you can redistribute it and/or modify it under
172 the same terms as Perl itself. See L<perlartistic>. This program is
173 distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
174 without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
175 PARTICULAR PURPOSE.
176  
177 =cut
« blib/lib/Game/Life/NDim/Board.pm ↑ Summary blib/lib/Game/Life/NDim/Life.pm »