line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
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
|
|
|
2
|
|
68342
|
use Moose; |
|
2
|
|
|
|
|
1928704
|
|
|
2
|
|
|
|
|
15
|
|
10
|
2
|
|
|
2
|
|
28640
|
use warnings; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
94
|
|
11
|
2
|
|
|
2
|
|
2083
|
use version; |
|
2
|
|
|
|
|
4700
|
|
|
2
|
|
|
|
|
15
|
|
12
|
2
|
|
|
2
|
|
139
|
use Carp; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
159
|
|
13
|
2
|
|
|
2
|
|
11
|
use Scalar::Util; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
86
|
|
14
|
2
|
|
|
2
|
|
12
|
use List::Util qw/sum/; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
135
|
|
15
|
|
|
|
|
|
|
#use List::MoreUtils; |
16
|
2
|
|
|
2
|
|
2073
|
use Data::Dumper qw/Dumper/; |
|
2
|
|
|
|
|
15926
|
|
|
2
|
|
|
|
|
176
|
|
17
|
2
|
|
|
2
|
|
743564
|
use English qw/ -no_match_vars /; |
|
2
|
|
|
|
|
5121544
|
|
|
2
|
|
|
|
|
14
|
|
18
|
2
|
|
|
2
|
|
2255
|
use Game::Life::NDim::Board; |
|
2
|
|
|
|
|
9
|
|
|
2
|
|
|
|
|
108
|
|
19
|
|
|
|
|
|
|
|
20
|
2
|
|
|
2
|
|
17
|
use overload '""' => \&to_string; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
17
|
|
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
our $VERSION = version->new('0.0.4'); |
23
|
|
|
|
|
|
|
our @EXPORT_OK = qw/game_of_life/; |
24
|
|
|
|
|
|
|
our %EXPORT_TAGS = (); |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
default => sub {[]}, |
36
|
|
|
|
|
|
|
); |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
sub game_of_life { |
39
|
2
|
|
|
2
|
1
|
618
|
my %params = @_; |
40
|
|
|
|
|
|
|
|
41
|
2
|
|
|
|
|
25
|
my $board = Game::Life::NDim::Board->new(%params); |
42
|
2
|
50
|
33
|
|
|
13
|
die "Where's my wrap? " . Dumper \%params, $board if $params{wrap} && !$board->wrap; |
43
|
2
|
|
|
|
|
6
|
my %new = (board => $board); |
44
|
2
|
50
|
|
|
|
7
|
$new{types} = $params{types} if $params{types}; |
45
|
|
|
|
|
|
|
|
46
|
2
|
|
|
|
|
15
|
return __PACKAGE__->new(%new); |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
sub add_rule { |
50
|
0
|
|
|
0
|
1
|
|
my ($self, @rules) = @_; |
51
|
|
|
|
|
|
|
|
52
|
0
|
|
|
|
|
|
while (@rules) { |
53
|
0
|
|
|
|
|
|
my $rule = shift @rules; |
54
|
0
|
0
|
|
|
|
|
if (ref $rule eq 'CODE') { |
55
|
0
|
|
|
|
|
|
push @{ $self->rules }, $rule; |
|
0
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
else { |
58
|
0
|
|
|
|
|
|
my $value = shift @rules; |
59
|
0
|
|
|
|
|
|
push @{ $self->rules }, |
60
|
0
|
0
|
|
0
|
|
|
$rule eq 'live' ? sub { $_[0] ? undef : ( sum $_[0]->surround ) > $value ? 1 : undef } |
|
|
0
|
|
|
|
|
|
61
|
0
|
0
|
|
0
|
|
|
: $rule eq 'die' ? sub { !$_[0] ? undef : ( sum $_[0]->surround ) < $value ? 0 : undef } |
|
|
0
|
|
|
|
|
|
62
|
0
|
0
|
|
|
|
|
: die "The rule \"$rule\" is unknown\n"; |
|
|
0
|
|
|
|
|
|
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
|
66
|
0
|
|
|
|
|
|
return $self; |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
sub process { |
70
|
0
|
|
|
0
|
1
|
|
my ($self) = @_; |
71
|
|
|
|
|
|
|
|
72
|
0
|
|
|
|
|
|
while ( defined ( my $life = $self->board->next_life() ) ) { |
73
|
0
|
|
|
|
|
|
$life->process($self->rules); |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
0
|
|
|
|
|
|
return $self; |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
sub set { |
80
|
0
|
|
|
0
|
1
|
|
my ($self) = @_; |
81
|
|
|
|
|
|
|
|
82
|
0
|
|
|
|
|
|
while ( defined ( my $life = $self->board->next_life() ) ) { |
83
|
0
|
|
|
|
|
|
$life->set(); |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
|
86
|
0
|
|
|
|
|
|
return $self; |
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
sub to_string { |
90
|
0
|
|
|
0
|
1
|
|
my ($self) = @_; |
91
|
|
|
|
|
|
|
|
92
|
0
|
|
|
|
|
|
return $self->board->to_string(); |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
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.4. |
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 |