line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
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
|
|
|
3
|
|
14
|
use Moose; |
|
3
|
|
|
|
|
5
|
|
|
3
|
|
|
|
|
29
|
|
10
|
3
|
|
|
3
|
|
16125
|
use warnings; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
77
|
|
11
|
3
|
|
|
3
|
|
13
|
use version; |
|
3
|
|
|
|
|
4
|
|
|
3
|
|
|
|
|
19
|
|
12
|
3
|
|
|
3
|
|
194
|
use Carp; |
|
3
|
|
|
|
|
3
|
|
|
3
|
|
|
|
|
187
|
|
13
|
3
|
|
|
3
|
|
12
|
use Data::Dumper qw/Dumper/; |
|
3
|
|
|
|
|
5
|
|
|
3
|
|
|
|
|
121
|
|
14
|
3
|
|
|
3
|
|
13
|
use English qw/ -no_match_vars /; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
19
|
|
15
|
3
|
|
|
3
|
|
1039
|
use List::Util qw/sum max min/; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
215
|
|
16
|
|
|
|
|
|
|
|
17
|
3
|
|
|
3
|
|
13
|
use overload '""' => \&to_string; |
|
3
|
|
|
|
|
3
|
|
|
3
|
|
|
|
|
33
|
|
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
our $VERSION = version->new('0.0.3'); |
20
|
|
|
|
|
|
|
our @EXPORT_OK = qw//; |
21
|
|
|
|
|
|
|
our %EXPORT_TAGS = (); |
22
|
|
|
|
|
|
|
#our @EXPORT = qw//; |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
has type => ( |
25
|
|
|
|
|
|
|
is => 'rw', |
26
|
|
|
|
|
|
|
isa => 'Str', |
27
|
|
|
|
|
|
|
default => 0, |
28
|
|
|
|
|
|
|
); |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
has next_type => ( |
31
|
|
|
|
|
|
|
is => 'rw', |
32
|
|
|
|
|
|
|
isa => 'Str', |
33
|
|
|
|
|
|
|
); |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
has board => ( |
36
|
|
|
|
|
|
|
is => 'rw', |
37
|
|
|
|
|
|
|
isa => 'Game::Life::NDim::Board', |
38
|
|
|
|
|
|
|
required => 1, |
39
|
|
|
|
|
|
|
weak_ref => 1, |
40
|
|
|
|
|
|
|
); |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
has position => ( |
43
|
|
|
|
|
|
|
is => 'rw', |
44
|
|
|
|
|
|
|
isa => 'Game::Life::NDim::Dim', |
45
|
|
|
|
|
|
|
required => 1, |
46
|
|
|
|
|
|
|
); |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
sub seed { |
49
|
1009
|
|
|
1009
|
1
|
1185
|
my ($self, $types) = @_; |
50
|
1009
|
|
|
|
|
796
|
my $new_type; |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
TYPE: |
53
|
1009
|
|
|
|
|
1619
|
while (!defined $new_type) { |
54
|
1362
|
|
|
|
|
967
|
for my $type (keys %{$types}) { |
|
1362
|
|
|
|
|
2666
|
|
55
|
2222
|
100
|
|
|
|
4771
|
if ( rand() < $types->{$type} ) { |
56
|
1009
|
|
|
|
|
1053
|
$new_type = $type; |
57
|
1009
|
|
|
|
|
1429
|
last TYPE; |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
1009
|
|
|
|
|
26471
|
$self->type($new_type); |
63
|
|
|
|
|
|
|
|
64
|
1009
|
|
|
|
|
2896
|
return $self; |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
# process |
68
|
|
|
|
|
|
|
sub process { |
69
|
0
|
|
|
0
|
1
|
0
|
my ($self, $rules) = @_; |
70
|
|
|
|
|
|
|
|
71
|
0
|
|
|
|
|
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
|
0
|
|
|
|
|
0
|
RULE: |
76
|
0
|
|
|
|
|
0
|
for my $rule (@{ $rules } ) { |
77
|
0
|
|
|
|
|
0
|
my $change = $rule->($self); |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
# next if status quoe |
80
|
0
|
0
|
|
|
|
0
|
next RULE if !defined $change; |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
# stage the changed type |
83
|
0
|
|
|
|
|
0
|
$self->next_type($change); |
84
|
0
|
|
|
|
|
0
|
last RULE; |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
|
87
|
0
|
|
|
|
|
0
|
return $self; |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
sub set { |
91
|
0
|
|
|
0
|
1
|
0
|
my ($self) = @_; |
92
|
|
|
|
|
|
|
|
93
|
0
|
|
|
|
|
0
|
$self->type($self->next_type); |
94
|
|
|
|
|
|
|
|
95
|
0
|
|
|
|
|
0
|
return $self; |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
sub surround { |
99
|
3
|
|
|
3
|
1
|
24
|
my ($self, $level) = @_; |
100
|
3
|
|
|
|
|
101
|
my $max = $self->board->dims; |
101
|
3
|
|
|
|
|
4
|
my @lives; |
102
|
3
|
|
|
|
|
95
|
my $cursor = $self->position->clone; |
103
|
|
|
|
|
|
|
|
104
|
3
|
|
50
|
|
|
1690
|
$level ||= 1; |
105
|
3
|
|
|
|
|
13
|
my $itter = $self->transformer; |
106
|
|
|
|
|
|
|
|
107
|
3
|
|
|
|
|
8
|
while (my $transform = $itter->()) { |
108
|
36
|
|
|
|
|
69
|
my $life = eval{ $self->board->get_life($self->position + $transform) }; |
|
36
|
|
|
|
|
1079
|
|
109
|
36
|
50
|
|
|
|
104
|
if (!$EVAL_ERROR) { |
110
|
36
|
|
|
|
|
3276
|
push @lives, $life; |
111
|
|
|
|
|
|
|
} |
112
|
0
|
|
|
|
|
0
|
else { warn "Error: $EVAL_ERROR\n"; } |
113
|
|
|
|
|
|
|
} |
114
|
|
|
|
|
|
|
|
115
|
3
|
|
|
|
|
10
|
return \@lives; |
116
|
|
|
|
|
|
|
} |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
sub transformer { |
119
|
4
|
|
|
4
|
1
|
1358
|
my ($self) = @_; |
120
|
4
|
|
|
|
|
8
|
my @max = @{ $self->board->dims }; |
|
4
|
|
|
|
|
128
|
|
121
|
4
|
|
|
|
|
13
|
my $max = @max - 1; |
122
|
4
|
|
|
|
|
7
|
my @transform; |
123
|
|
|
|
|
|
|
my @alter; |
124
|
4
|
|
|
|
|
16
|
for (0 .. $max) { |
125
|
9
|
|
|
|
|
18
|
push @transform, -1; |
126
|
|
|
|
|
|
|
} |
127
|
4
|
|
|
|
|
6
|
my $point; |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
my $itter; |
130
|
|
|
|
|
|
|
$itter = sub { |
131
|
70
|
100
|
|
70
|
|
166
|
if (!defined $point) { |
132
|
4
|
|
|
|
|
7
|
$point = 0; |
133
|
4
|
|
|
|
|
21
|
return [@transform]; |
134
|
|
|
|
|
|
|
} |
135
|
|
|
|
|
|
|
|
136
|
66
|
|
|
|
|
60
|
my $done = 0; |
137
|
66
|
|
|
|
|
114
|
while (!$done) { |
138
|
93
|
100
|
|
|
|
189
|
if ($transform[$point] + 1 <= 1) { |
139
|
62
|
|
|
|
|
58
|
$transform[$point]++; |
140
|
62
|
|
|
|
|
43
|
$done = 1; |
141
|
62
|
|
|
|
|
47
|
$point = 0; |
142
|
62
|
|
|
|
|
70
|
last; |
143
|
|
|
|
|
|
|
} |
144
|
31
|
|
|
|
|
40
|
$transform[$point] = -1; |
145
|
31
|
|
|
|
|
31
|
$point++; |
146
|
31
|
|
|
|
|
36
|
my $undef; |
147
|
31
|
100
|
|
|
|
79
|
return $undef if !exists $transform[$point]; |
148
|
|
|
|
|
|
|
} |
149
|
|
|
|
|
|
|
|
150
|
62
|
100
|
|
|
|
81
|
return $itter->() if ($max + 1 == (grep {$_ == 0} @transform)); |
|
174
|
|
|
|
|
285
|
|
151
|
|
|
|
|
|
|
|
152
|
58
|
|
|
|
|
206
|
return [@transform]; |
153
|
4
|
|
|
|
|
29
|
}; |
154
|
|
|
|
|
|
|
|
155
|
4
|
|
|
|
|
10
|
return $itter; |
156
|
|
|
|
|
|
|
} |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
sub clone { |
159
|
0
|
|
|
0
|
1
|
0
|
my ($self) = @_; |
160
|
|
|
|
|
|
|
|
161
|
0
|
|
|
|
|
0
|
return __PACKAGE__->new(type => $self->type, board => $self->board, position => $self->position); |
162
|
|
|
|
|
|
|
} |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
sub to_string { |
165
|
25
|
|
|
25
|
1
|
1547
|
my ($self) = @_; |
166
|
|
|
|
|
|
|
|
167
|
25
|
|
|
|
|
808
|
return $self->type; |
168
|
|
|
|
|
|
|
} |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
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 |