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
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
33
|
|
10
|
3
|
|
|
3
|
|
19068
|
use warnings; |
|
3
|
|
|
|
|
7
|
|
|
3
|
|
|
|
|
81
|
|
11
|
3
|
|
|
3
|
|
14
|
use version; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
24
|
|
12
|
3
|
|
|
3
|
|
203
|
use Carp; |
|
3
|
|
|
|
|
4
|
|
|
3
|
|
|
|
|
200
|
|
13
|
3
|
|
|
3
|
|
16
|
use Data::Dumper qw/Dumper/; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
142
|
|
14
|
3
|
|
|
3
|
|
14
|
use English qw/ -no_match_vars /; |
|
3
|
|
|
|
|
5
|
|
|
3
|
|
|
|
|
19
|
|
15
|
3
|
|
|
3
|
|
1405
|
use List::Util qw/sum max min/; |
|
3
|
|
|
|
|
5
|
|
|
3
|
|
|
|
|
268
|
|
16
|
|
|
|
|
|
|
|
17
|
3
|
|
|
3
|
|
16
|
use overload '""' => \&to_string; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
33
|
|
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
our $VERSION = version->new('0.0.4'); |
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
|
1426
|
my ($self, $types) = @_; |
50
|
1009
|
|
|
|
|
951
|
my $new_type; |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
TYPE: |
53
|
1009
|
|
|
|
|
2048
|
while (!defined $new_type) { |
54
|
1290
|
|
|
|
|
1670
|
for my $type (keys %{$types}) { |
|
1290
|
|
|
|
|
3362
|
|
55
|
1766
|
100
|
|
|
|
5608
|
if ( rand() < $types->{$type} ) { |
56
|
1009
|
|
|
|
|
1185
|
$new_type = $type; |
57
|
1009
|
|
|
|
|
1697
|
last TYPE; |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
1009
|
|
|
|
|
34497
|
$self->type($new_type); |
63
|
|
|
|
|
|
|
|
64
|
1009
|
|
|
|
|
3629
|
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
|
|
|
|
|
|
|
RULE: |
76
|
0
|
|
|
|
|
0
|
for my $rule (@{ $rules } ) { |
|
0
|
|
|
|
|
0
|
|
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
|
23
|
my ($self, $level) = @_; |
100
|
3
|
|
|
|
|
101
|
my $max = $self->board->dims; |
101
|
3
|
|
|
|
|
8
|
my @lives; |
102
|
3
|
|
|
|
|
101
|
my $cursor = $self->position->clone; |
103
|
|
|
|
|
|
|
|
104
|
3
|
|
50
|
|
|
1618
|
$level ||= 1; |
105
|
3
|
|
|
|
|
14
|
my $itter = $self->transformer; |
106
|
|
|
|
|
|
|
|
107
|
3
|
|
|
|
|
8
|
while (my $transform = $itter->()) { |
108
|
36
|
|
|
|
|
48
|
my $life = eval{ $self->board->get_life($self->position + $transform) }; |
|
36
|
|
|
|
|
1155
|
|
109
|
36
|
50
|
|
|
|
103
|
if (!$EVAL_ERROR) { |
110
|
36
|
|
|
|
|
3424
|
push @lives, $life; |
111
|
|
|
|
|
|
|
} |
112
|
0
|
|
|
|
|
0
|
else { warn "Error: $EVAL_ERROR\n"; } |
113
|
|
|
|
|
|
|
} |
114
|
|
|
|
|
|
|
|
115
|
3
|
|
|
|
|
11
|
return \@lives; |
116
|
|
|
|
|
|
|
} |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
sub transformer { |
119
|
4
|
|
|
4
|
1
|
1672
|
my ($self) = @_; |
120
|
4
|
|
|
|
|
7
|
my @max = @{ $self->board->dims }; |
|
4
|
|
|
|
|
128
|
|
121
|
4
|
|
|
|
|
11
|
my $max = @max - 1; |
122
|
4
|
|
|
|
|
8
|
my @transform; |
123
|
|
|
|
|
|
|
my @alter; |
124
|
4
|
|
|
|
|
15
|
for (0 .. $max) { |
125
|
9
|
|
|
|
|
20
|
push @transform, -1; |
126
|
|
|
|
|
|
|
} |
127
|
4
|
|
|
|
|
7
|
my $point; |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
my $itter; |
130
|
|
|
|
|
|
|
$itter = sub { |
131
|
70
|
100
|
|
70
|
|
206
|
if (!defined $point) { |
132
|
4
|
|
|
|
|
8
|
$point = 0; |
133
|
4
|
|
|
|
|
52
|
return [@transform]; |
134
|
|
|
|
|
|
|
} |
135
|
|
|
|
|
|
|
|
136
|
66
|
|
|
|
|
75
|
my $done = 0; |
137
|
66
|
|
|
|
|
143
|
while (!$done) { |
138
|
93
|
100
|
|
|
|
209
|
if ($transform[$point] + 1 <= 1) { |
139
|
62
|
|
|
|
|
73
|
$transform[$point]++; |
140
|
62
|
|
|
|
|
62
|
$done = 1; |
141
|
62
|
|
|
|
|
60
|
$point = 0; |
142
|
62
|
|
|
|
|
84
|
last; |
143
|
|
|
|
|
|
|
} |
144
|
31
|
|
|
|
|
237
|
$transform[$point] = -1; |
145
|
31
|
|
|
|
|
41
|
$point++; |
146
|
31
|
|
|
|
|
29
|
my $undef; |
147
|
31
|
100
|
|
|
|
105
|
return $undef if !exists $transform[$point]; |
148
|
|
|
|
|
|
|
} |
149
|
|
|
|
|
|
|
|
150
|
62
|
100
|
|
|
|
108
|
return $itter->() if ($max + 1 == (grep {$_ == 0} @transform)); |
|
174
|
|
|
|
|
606
|
|
151
|
|
|
|
|
|
|
|
152
|
58
|
|
|
|
|
236
|
return [@transform]; |
153
|
4
|
|
|
|
|
32
|
}; |
154
|
|
|
|
|
|
|
|
155
|
4
|
|
|
|
|
12
|
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
|
1514
|
my ($self) = @_; |
166
|
|
|
|
|
|
|
|
167
|
25
|
|
|
|
|
854
|
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.4. |
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 |