line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Game::Life::NDim::Board; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
# Created on: 2010-01-04 18:52:38 |
4
|
|
|
|
|
|
|
# Create by: Ivan Wills |
5
|
|
|
|
|
|
|
# $Id$ |
6
|
|
|
|
|
|
|
# $Revision$, $HeadURL$, $Date$ |
7
|
|
|
|
|
|
|
# $Revision$, $Source$, $Date$ |
8
|
|
|
|
|
|
|
|
9
|
3
|
|
|
3
|
|
46821
|
use Moose; |
|
3
|
|
|
|
|
480908
|
|
|
3
|
|
|
|
|
38
|
|
10
|
3
|
|
|
3
|
|
35582
|
use warnings; |
|
3
|
|
|
|
|
7
|
|
|
3
|
|
|
|
|
128
|
|
11
|
3
|
|
|
3
|
|
17
|
use feature qw/:5.10/; |
|
3
|
|
|
|
|
7
|
|
|
3
|
|
|
|
|
362
|
|
12
|
3
|
|
|
3
|
|
1031
|
use version; |
|
3
|
|
|
|
|
2157
|
|
|
3
|
|
|
|
|
25
|
|
13
|
3
|
|
|
3
|
|
227
|
use Carp qw/croak cluck confess/; |
|
3
|
|
|
|
|
5
|
|
|
3
|
|
|
|
|
241
|
|
14
|
3
|
|
|
3
|
|
18
|
use List::Util qw/max/; |
|
3
|
|
|
|
|
5
|
|
|
3
|
|
|
|
|
180
|
|
15
|
3
|
|
|
3
|
|
906
|
use Data::Dumper qw/Dumper/; |
|
3
|
|
|
|
|
6964
|
|
|
3
|
|
|
|
|
189
|
|
16
|
3
|
|
|
3
|
|
670
|
use English qw/ -no_match_vars /; |
|
3
|
|
|
|
|
3859
|
|
|
3
|
|
|
|
|
23
|
|
17
|
3
|
|
|
3
|
|
3499
|
use Game::Life::NDim::Life; |
|
3
|
|
|
|
|
11
|
|
|
3
|
|
|
|
|
172
|
|
18
|
3
|
|
|
3
|
|
2142
|
use Game::Life::NDim::Dim; |
|
3
|
|
|
|
|
13
|
|
|
3
|
|
|
|
|
115
|
|
19
|
3
|
|
|
3
|
|
2918
|
use Params::Coerce (); |
|
3
|
|
|
|
|
3856
|
|
|
3
|
|
|
|
|
95
|
|
20
|
|
|
|
|
|
|
|
21
|
3
|
|
|
3
|
|
18
|
use overload '""' => \&to_string; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
32
|
|
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
our $VERSION = version->new('0.0.4'); |
24
|
|
|
|
|
|
|
our @EXPORT_OK = qw//; |
25
|
|
|
|
|
|
|
our %EXPORT_TAGS = (); |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
has items => ( |
28
|
|
|
|
|
|
|
is => 'rw', |
29
|
|
|
|
|
|
|
isa => 'ArrayRef', |
30
|
|
|
|
|
|
|
lazy_build => 1, |
31
|
|
|
|
|
|
|
); |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
has dims => ( |
34
|
|
|
|
|
|
|
is => 'ro', |
35
|
|
|
|
|
|
|
isa => 'Game::Life::NDim::Dim', |
36
|
|
|
|
|
|
|
required => 1, |
37
|
|
|
|
|
|
|
); |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
has cursor => ( |
40
|
|
|
|
|
|
|
is => 'rw', |
41
|
|
|
|
|
|
|
isa => 'Game::Life::NDim::Dim', |
42
|
|
|
|
|
|
|
); |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
has types => ( |
45
|
|
|
|
|
|
|
is => 'rw', |
46
|
|
|
|
|
|
|
isa => 'HashRef', |
47
|
|
|
|
|
|
|
default => sub {{ 0 => 0.6, 1 => 0.4 }}, |
48
|
|
|
|
|
|
|
); |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
has wrap => ( |
51
|
|
|
|
|
|
|
is => 'rw', |
52
|
|
|
|
|
|
|
isa => 'Bool', |
53
|
|
|
|
|
|
|
default => 0, |
54
|
|
|
|
|
|
|
); |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
has verbose => ( |
57
|
|
|
|
|
|
|
is => 'rw', |
58
|
|
|
|
|
|
|
isa => 'Bool', |
59
|
|
|
|
|
|
|
default => 0, |
60
|
|
|
|
|
|
|
); |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
around new => sub { |
63
|
|
|
|
|
|
|
my ($new, $class, %params) = @_; |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
if (ref $params{dims} eq 'ARRAY') { |
66
|
|
|
|
|
|
|
$params{dims} = Game::Life::NDim::Dim->new($params{dims}); |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
my $self = $new->($class, %params); |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
$self->reset; |
72
|
|
|
|
|
|
|
$self->seed(%params) if $params{rand}; |
73
|
|
|
|
|
|
|
#$self->cursor(Game::Life::NDim::Dim->new([])); |
74
|
|
|
|
|
|
|
for (@{ $self->dims }) { |
75
|
|
|
|
|
|
|
push @{ $self->cursor }, 0; |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
return $self; |
79
|
|
|
|
|
|
|
}; |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
sub _build_items { |
82
|
7
|
|
|
7
|
|
20
|
my ($self, %params) = @_; |
83
|
|
|
|
|
|
|
|
84
|
7
|
50
|
|
|
|
21
|
$self->types = $params{types} if $params{types}; |
85
|
|
|
|
|
|
|
|
86
|
7
|
|
|
|
|
16
|
my $items = []; |
87
|
7
|
|
|
|
|
12
|
my $lives = 0; |
88
|
|
|
|
|
|
|
|
89
|
7
|
|
|
|
|
10
|
my $builder; |
90
|
|
|
|
|
|
|
$builder = sub { |
91
|
208
|
|
|
208
|
|
334
|
my ($items, $dims, $pos) = @_; |
92
|
208
|
|
|
|
|
315
|
my $count = $dims->[0]; |
93
|
|
|
|
|
|
|
|
94
|
208
|
|
|
|
|
523
|
for my $i ( 0 .. $count - 1 ) { |
95
|
1835
|
100
|
|
|
|
2159
|
if ( @{$dims} == 1 ) { |
|
1835
|
|
|
|
|
4285
|
|
96
|
|
|
|
|
|
|
$items->[$i] = Game::Life::NDim::Life->new( |
97
|
1634
|
|
|
|
|
2003
|
position => Game::Life::NDim::Dim->new([ @{ $pos }, $i ]), |
|
1634
|
|
|
|
|
6711
|
|
98
|
|
|
|
|
|
|
board => $self |
99
|
|
|
|
|
|
|
); |
100
|
1634
|
|
|
|
|
3052078
|
$lives++; |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
else { |
103
|
201
|
|
|
|
|
485
|
$items->[$i] = []; |
104
|
201
|
|
|
|
|
362
|
my $sub_dims = [ @{ $dims }[ 1 .. @{ $dims } - 1 ] ]; |
|
201
|
|
|
|
|
495
|
|
|
201
|
|
|
|
|
471
|
|
105
|
201
|
|
|
|
|
289
|
my $sub_pos = [ @{ $pos }, $i ]; |
|
201
|
|
|
|
|
451
|
|
106
|
201
|
|
|
|
|
306
|
my $sub_items = $items->[$i]; |
107
|
201
|
|
|
|
|
454
|
$builder->($sub_items, $sub_dims, $sub_pos); |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
} |
110
|
7
|
|
|
|
|
59
|
}; |
111
|
7
|
|
|
|
|
230
|
$builder->($items, $self->dims, []); |
112
|
|
|
|
|
|
|
|
113
|
7
|
|
|
|
|
243
|
return $items; |
114
|
|
|
|
|
|
|
} |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
sub seed { |
117
|
2
|
|
|
2
|
1
|
6
|
my ($self, %params) = @_; |
118
|
|
|
|
|
|
|
|
119
|
2
|
50
|
|
|
|
8
|
$self->types = $params{types} if $params{types}; |
120
|
|
|
|
|
|
|
|
121
|
2
|
|
|
|
|
4
|
my $i = 0; |
122
|
2
|
|
|
|
|
13
|
while ( ref (my $life = $self->next_life()) ) { |
123
|
1009
|
|
|
|
|
35809
|
$life->seed($self->types); |
124
|
|
|
|
|
|
|
} |
125
|
2
|
|
|
|
|
10
|
$self->reset; |
126
|
|
|
|
|
|
|
|
127
|
2
|
|
|
|
|
6
|
return $self; |
128
|
|
|
|
|
|
|
} |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
sub reset { |
131
|
12
|
|
|
12
|
1
|
1312
|
my ($self) = @_; |
132
|
12
|
|
|
|
|
23
|
my @cursor; |
133
|
|
|
|
|
|
|
|
134
|
12
|
|
|
|
|
23
|
for (@{ $self->dims }) { |
|
12
|
|
|
|
|
389
|
|
135
|
26
|
|
|
|
|
52
|
push @cursor, 0; |
136
|
|
|
|
|
|
|
} |
137
|
|
|
|
|
|
|
|
138
|
12
|
50
|
|
|
|
39
|
confess "Empty cursor!" if !@cursor; |
139
|
|
|
|
|
|
|
|
140
|
12
|
|
|
|
|
22
|
$cursor[-1] = -1; |
141
|
|
|
|
|
|
|
|
142
|
12
|
|
|
|
|
61
|
$self->cursor(Game::Life::NDim::Dim->new(\@cursor)); |
143
|
|
|
|
|
|
|
|
144
|
12
|
|
|
|
|
329
|
return $self; |
145
|
|
|
|
|
|
|
} |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
sub next_life { |
148
|
1021
|
|
|
1021
|
1
|
1582
|
my ($self) = @_; |
149
|
1021
|
|
|
|
|
1359
|
my $max_dim; |
150
|
|
|
|
|
|
|
|
151
|
1021
|
100
|
|
|
|
35767
|
return if !$self->cursor->increment($self->dims); |
152
|
|
|
|
|
|
|
|
153
|
1017
|
|
|
|
|
33807
|
my $life = $self->items; |
154
|
|
|
|
|
|
|
|
155
|
1017
|
|
|
|
|
1257
|
my @pos; |
156
|
1017
|
|
|
|
|
10661
|
for my $i ( 0 .. @{ $self->dims } - 1 ) { |
|
1017
|
|
|
|
|
32581
|
|
157
|
3034
|
50
|
|
|
|
101126
|
if ( ! exists $self->cursor->[$i] ) { |
158
|
0
|
|
|
|
|
0
|
die "here?\n"; |
159
|
0
|
|
|
|
|
0
|
$self->cursor->[$i] = 0; |
160
|
|
|
|
|
|
|
} |
161
|
3034
|
|
|
|
|
104757
|
my $pos = $self->cursor->[$i]; |
162
|
3034
|
|
|
|
|
5980
|
push @pos, $pos; |
163
|
3034
|
100
|
66
|
|
|
7786
|
if ( ref $life eq 'ARRAY' && @{ $life } < $pos + 1 ) { |
|
3034
|
|
|
|
|
10498
|
|
164
|
|
|
|
|
|
|
$life->[$pos] = |
165
|
301
|
100
|
|
|
|
376
|
$i < @{ $self->cursor } - 1 ? [] |
|
301
|
|
|
|
|
9791
|
|
166
|
|
|
|
|
|
|
: Game::Life::NDim::Life->new(board => $self, position => $self->cursor); |
167
|
|
|
|
|
|
|
} |
168
|
3034
|
|
|
|
|
396020
|
$life = $life->[$pos]; |
169
|
|
|
|
|
|
|
} |
170
|
|
|
|
|
|
|
|
171
|
1017
|
|
|
|
|
4118
|
return $life; |
172
|
|
|
|
|
|
|
} |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
sub set_life { |
175
|
0
|
|
|
0
|
1
|
0
|
my ($self, $life) = @_; |
176
|
|
|
|
|
|
|
|
177
|
0
|
|
|
|
|
0
|
my $curr = $self->items; |
178
|
|
|
|
|
|
|
|
179
|
0
|
|
|
|
|
0
|
for my $i ( @{ $self->cursor } ) { |
|
0
|
|
|
|
|
0
|
|
180
|
0
|
0
|
|
|
|
0
|
if ( ref $curr->[$i] eq 'ARRAY' ) { |
181
|
0
|
|
|
|
|
0
|
$curr = $curr->[$i]; |
182
|
|
|
|
|
|
|
} |
183
|
|
|
|
|
|
|
else { |
184
|
0
|
|
|
|
|
0
|
$curr->[$i] = $life; |
185
|
|
|
|
|
|
|
} |
186
|
|
|
|
|
|
|
} |
187
|
|
|
|
|
|
|
|
188
|
0
|
|
|
|
|
0
|
return $self; |
189
|
|
|
|
|
|
|
} |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
sub get_life { |
192
|
50
|
|
|
50
|
1
|
19678
|
my ($self, $position) = @_; |
193
|
|
|
|
|
|
|
|
194
|
50
|
|
|
|
|
1639
|
my $item = $self->items; |
195
|
50
|
100
|
|
|
|
1560
|
my $min = $self->wrap ? -1 : 0; |
196
|
50
|
50
|
|
|
|
110
|
die if !defined $min; |
197
|
|
|
|
|
|
|
|
198
|
50
|
|
|
|
|
56
|
for my $i (@{ $position } ) { |
|
50
|
|
|
|
|
131
|
|
199
|
124
|
100
|
66
|
|
|
505
|
croak "Cannot get game position from $position $i >= $min " if $i < $min || !exists $item->[$i]; |
200
|
123
|
|
|
|
|
201
|
$item = $item->[$i]; |
201
|
|
|
|
|
|
|
} |
202
|
|
|
|
|
|
|
|
203
|
49
|
|
|
|
|
444
|
return $item; |
204
|
|
|
|
|
|
|
} |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
sub to_string { |
207
|
1
|
|
|
1
|
1
|
308
|
my ($self) = @_; |
208
|
|
|
|
|
|
|
|
209
|
1
|
50
|
|
|
|
3
|
die "The dimension of this game is to large to sensibly convert to a string\n" if @{ $self->dims } > 3; |
|
1
|
|
|
|
|
37
|
|
210
|
|
|
|
|
|
|
|
211
|
1
|
50
|
|
|
|
3
|
my $spacer = ( 10 >= max (@{$self->dims}, scalar @{$self->dims}) ) ? ' ' : ''; |
|
1
|
|
|
|
|
36
|
|
|
1
|
|
|
|
|
32
|
|
212
|
|
|
|
|
|
|
|
213
|
1
|
|
|
|
|
3
|
my $out = ''; |
214
|
1
|
|
|
|
|
3
|
my @outs; |
215
|
1
|
|
|
|
|
3
|
$self->reset; |
216
|
1
|
|
|
|
|
3
|
my $i = 0; |
217
|
1
|
|
|
|
|
7
|
my $level = 0; |
218
|
1
|
|
|
|
|
7
|
while ( ref ( my $life = $self->next_life() ) ) { |
219
|
4
|
50
|
33
|
|
|
8
|
if ( @{$self->cursor} > 2 && $self->cursor->[0] != $level) { |
|
4
|
|
|
|
|
155
|
|
220
|
0
|
|
|
|
|
0
|
$out .= "\n"; |
221
|
0
|
|
|
|
|
0
|
$level = $self->cursor->[0]; |
222
|
0
|
|
|
|
|
0
|
push @outs, $out; |
223
|
0
|
|
|
|
|
0
|
$out = ''; |
224
|
|
|
|
|
|
|
} |
225
|
4
|
|
|
|
|
22
|
$out .= $life; |
226
|
4
|
100
|
|
|
|
152
|
$out .= $self->cursor->[-1] == $self->dims->[-1] ? "\n" : $spacer; |
227
|
4
|
|
|
|
|
16
|
$i++; |
228
|
|
|
|
|
|
|
} |
229
|
1
|
|
|
|
|
6
|
$self->reset; |
230
|
|
|
|
|
|
|
|
231
|
1
|
50
|
|
|
|
8
|
if (@outs) { |
232
|
0
|
|
|
|
|
0
|
$out .= "\n"; |
233
|
0
|
|
|
|
|
0
|
$level = $self->cursor->[0]; |
234
|
0
|
|
|
|
|
0
|
push @outs, $out; |
235
|
0
|
|
|
|
|
0
|
$out = ''; |
236
|
0
|
|
|
|
|
0
|
my @lines; |
237
|
0
|
|
|
|
|
0
|
for my $level (@outs) { |
238
|
0
|
|
|
|
|
0
|
my $i = 0; |
239
|
0
|
|
|
|
|
0
|
for my $line (split /\n/, $level) { |
240
|
0
|
|
0
|
|
|
0
|
$lines[$i] ||= ''; |
241
|
0
|
|
|
|
|
0
|
$lines[$i] .= " $line"; |
242
|
0
|
|
|
|
|
0
|
$i++; |
243
|
|
|
|
|
|
|
} |
244
|
|
|
|
|
|
|
} |
245
|
0
|
|
|
|
|
0
|
return join "\n", @lines, ''; |
246
|
|
|
|
|
|
|
} |
247
|
|
|
|
|
|
|
|
248
|
|
|
|
|
|
|
#return "Board:\n" . $out . "\nCount = $i\n"; |
249
|
1
|
|
|
|
|
12
|
return $out; |
250
|
|
|
|
|
|
|
} |
251
|
|
|
|
|
|
|
|
252
|
|
|
|
|
|
|
1; |
253
|
|
|
|
|
|
|
|
254
|
|
|
|
|
|
|
__END__ |
255
|
|
|
|
|
|
|
|
256
|
|
|
|
|
|
|
=head1 NAME |
257
|
|
|
|
|
|
|
|
258
|
|
|
|
|
|
|
Game::Life::NDim::Board - Object representing the board |
259
|
|
|
|
|
|
|
|
260
|
|
|
|
|
|
|
=head1 VERSION |
261
|
|
|
|
|
|
|
|
262
|
|
|
|
|
|
|
This documentation refers to Game::Life::NDim::Board version 0.0.4. |
263
|
|
|
|
|
|
|
|
264
|
|
|
|
|
|
|
|
265
|
|
|
|
|
|
|
=head1 SYNOPSIS |
266
|
|
|
|
|
|
|
|
267
|
|
|
|
|
|
|
use Game::Life::NDim::Board; |
268
|
|
|
|
|
|
|
|
269
|
|
|
|
|
|
|
# Brief but working code example(s) here showing the most common usage(s) |
270
|
|
|
|
|
|
|
# This section will be as far as many users bother reading, so make it as |
271
|
|
|
|
|
|
|
# educational and exemplary as possible. |
272
|
|
|
|
|
|
|
|
273
|
|
|
|
|
|
|
|
274
|
|
|
|
|
|
|
=head1 DESCRIPTION |
275
|
|
|
|
|
|
|
|
276
|
|
|
|
|
|
|
=head1 SUBROUTINES/METHODS |
277
|
|
|
|
|
|
|
|
278
|
|
|
|
|
|
|
=head2 C<seed ( )> |
279
|
|
|
|
|
|
|
|
280
|
|
|
|
|
|
|
=head2 C<reset ( )> |
281
|
|
|
|
|
|
|
|
282
|
|
|
|
|
|
|
=head2 C<next_life ( )> |
283
|
|
|
|
|
|
|
|
284
|
|
|
|
|
|
|
=head2 C<get_life ( )> |
285
|
|
|
|
|
|
|
|
286
|
|
|
|
|
|
|
=head2 C<set_life ( )> |
287
|
|
|
|
|
|
|
|
288
|
|
|
|
|
|
|
=head2 C<to_string ( )> |
289
|
|
|
|
|
|
|
|
290
|
|
|
|
|
|
|
=head2 C<_build_items ( )> |
291
|
|
|
|
|
|
|
|
292
|
|
|
|
|
|
|
=head1 DIAGNOSTICS |
293
|
|
|
|
|
|
|
|
294
|
|
|
|
|
|
|
=head1 CONFIGURATION AND ENVIRONMENT |
295
|
|
|
|
|
|
|
|
296
|
|
|
|
|
|
|
=head1 DEPENDENCIES |
297
|
|
|
|
|
|
|
|
298
|
|
|
|
|
|
|
=head1 INCOMPATIBILITIES |
299
|
|
|
|
|
|
|
|
300
|
|
|
|
|
|
|
=head1 BUGS AND LIMITATIONS |
301
|
|
|
|
|
|
|
|
302
|
|
|
|
|
|
|
There are no known bugs in this module. |
303
|
|
|
|
|
|
|
|
304
|
|
|
|
|
|
|
Please report problems to Ivan Wills (ivan.wills@gmail.com). |
305
|
|
|
|
|
|
|
|
306
|
|
|
|
|
|
|
Patches are welcome. |
307
|
|
|
|
|
|
|
|
308
|
|
|
|
|
|
|
=head1 AUTHOR |
309
|
|
|
|
|
|
|
|
310
|
|
|
|
|
|
|
Ivan Wills - (ivan.wills@gmail.com) |
311
|
|
|
|
|
|
|
|
312
|
|
|
|
|
|
|
=head1 LICENSE AND COPYRIGHT |
313
|
|
|
|
|
|
|
|
314
|
|
|
|
|
|
|
Copyright (c) 2010 Ivan Wills (14 Mullion Close, Hornsby Heights, NSW Australia 2077). |
315
|
|
|
|
|
|
|
All rights reserved. |
316
|
|
|
|
|
|
|
|
317
|
|
|
|
|
|
|
This module is free software; you can redistribute it and/or modify it under |
318
|
|
|
|
|
|
|
the same terms as Perl itself. See L<perlartistic>. This program is |
319
|
|
|
|
|
|
|
distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
320
|
|
|
|
|
|
|
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
321
|
|
|
|
|
|
|
PARTICULAR PURPOSE. |
322
|
|
|
|
|
|
|
|
323
|
|
|
|
|
|
|
=cut |