blib/lib/Game/Life/Infinite/Board.pm

filter: Statementstmt 79.7% 418 / 524 Branchbran 67.6% 203 / 300 Conditioncond 65.3% 51 / 78 MC/DCmcdc 43.1% 19 / 44 Subroutinesub 85.7% 24 / 28 Podpod 77.2% 17 / 22 totaltotal 73.4% 732 / 996 CC 200 SCAR 37.5
CC
200
Cov
75%
loadRLE
132.3
saveGridTxt
72.0
crudePrintBoard
56.0
CRAP 42.3
↑ Summary
1 #!/usr/bin/perl
2  
3 package Game::Life::Infinite::Board;
4
5 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 9 ▶use Time::HiRes;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 9 require 5.10.1;
9  
10 BEGIN {
11 9 ▶ use Exporter ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 9 ▶ use vars qw($VERSION @ISA @EXPORT_OK %EXPORT_TAGS);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 9 ▶ $VERSION = sprintf( "%d.%02d", q($Revision: 0.07 $) =~ /\s(\d+)\.(\d+)/ );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 9 @ISA = qw(Exporter);
15 9 @EXPORT_OK = qw(format_is);
16 9 %EXPORT_TAGS = ( );
17 }
18  
19 sub new {
20 11 ▶ my ( $class, $rulesRef, @args ) = @_;
Subroutine new: called CC=1 SCAR=0.0
Pod documented
21 11 my $self = {};
22 11 $self->{'maxx'} = $self->{'maxy'} = $self->{'minx'} = $self->{'miny'} = 0; # Board boundaries.
23 11 $self->{'gen'} = $self->{'liveCells'} = $self->{'usedCells'} = 0;
24 11 $self->{'delta'} = -1; # Number of cells that changed state since previous epoch (newborns+dead).
25 11 $self->{'factor2'} = 1;
26 11 $self->{'oscilator'} = 0; # If oscilator is detected, contains the period.
27 11 $self->{'cells'} = {};
28 11 $self->{'currentFn'} = 'Untitled.cells';
29 11 $self->{'name'} = 'Untitled';
30 11 $self->{'totalTime'} = 0;
31 11 $self->{'osccheck'} = 0;
32 11 $self->{'color'} = 0;
33 # Check for rules:
34 11 &setRules($self, $rulesRef);
35 11 &updateCell($self, 0, 0, 0); # Create first cell in 0,0 coordinates.
36 11 bless $self, $class;
37 11 return $self;
38 };
39  
40 sub setRules {
41 42 ▶ my ( $self, $rulesRef) = @_;
Subroutine setRules: called CC=5 SCAR=16.2
Pod documented
42 42 ▶ my ($breedRef, $liveRef) = (ref($rulesRef) eq "ARRAY") ? ($rulesRef->[0], $rulesRef->[1]) : ([3], [2,3]);
Branch: 1 branch
BranchTrueFalseTotal
ref $rulesRef eq 'ARRAY' ? : 1 41 42
43 42 ▶ if ($self->{'color'} > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"color"} > 0) 30 12 42
44 # Force standard rules in colourised board.
45 30 ($breedRef, $liveRef) = ([3], [2,3]);
46 };
47 42 ▶ $self->{'breedRules'} = (ref($breedRef) eq "ARRAY") ? $breedRef : [];
Branch: 1 branch
BranchTrueFalseTotal
ref $breedRef eq 'ARRAY' ? : 42 0 42
48 42 ▶ $self->{'liveRules'} = (ref($liveRef) eq "ARRAY") ? $liveRef : [];
Branch: 1 branch
BranchTrueFalseTotal
ref $liveRef eq 'ARRAY' ? : 42 0 42
49 42 return;
50 };
51  
52 sub setColor {
53 30 ▶ my ($self, $color) = @_;
Subroutine setColor: called CC=6 SCAR=20.7
Pod documented
54 30 ▶ $color = (defined $color) ? $color : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $color ? : 30 0 30
55 30 ▶ if ($self->{'gen'} > 0) {return;};
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"gen"} > 0) 0 30 30
56 30 ▶ if (lc($color) eq 'immigration') {
Branch: 3 branches
BranchTrueFalseTotal
if (lc $color eq 'immigration') { } 13 17 30
elsif (lc $color eq 'quadlife') { } 17 0 17
elsif (lc $color eq 'normal') { } 0 0 0
57 13 $self->{'color'} = 1;
58 # Reset rules to standard:
59 13 &setRules($self, undef);
60 13 return 1;
61 } elsif (lc($color) eq 'quadlife') {
62 17 $self->{'color'} = 2;
63 # Reset rules to standard:
64 17 &setRules($self, undef);
65 17 return 2;
66 } elsif (lc($color) eq 'normal') {
67 0 $self->{'color'} = 0;
68 0 return 0;
69 } else {
70 0 return;
71 };
72 };
73  
74 sub getColor {
75 0 ▶ my $self = shift;
Subroutine getColor: not called CC=1 SCAR=6.9
Pod documented
76 0 return $self->{'color'};
77 };
78  
79 sub updateCell {
80 # Update the state of a cell. If non-existing, create it.
81 19509 ▶ my ( $self, $xpos, $ypos, $state ) = @_;
Subroutine updateCell: called CC=22 SCAR=31.9
Pod documented
82 19509 ▶ defined ($self->{'cells'}->{$xpos, $ypos}) or &createCell($self, $xpos, $ypos);
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{"cells"}{join $;, $xpos, $ypos} or &createCell($self, $xpos, $ypos) 256 19253 19509
83 19509 ▶ if (($self->{'cells'}->{$xpos, $ypos}->{'state'}) and (not $state)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"cells"}{join $;, $xpos, $ypos}{"state"} and not $state) 9042 10467 19509
Condition66%2/3
$self->{"cells"}{join $;, $xpos, $ypos}{"state"} and not $state
!ll&&!rl&&r
1046709042
MC/DC50%1/2
$self->{"cells"}{join $;, $xpos, $ypos}{"state"} and not $state
$self->{"cells"}{join $;, $xpos, $ypos}{"state"} not $state
Truth table66%2/3
A and not(B)
A= $self->{"cells"}{join $;, $xpos, $ypos}{"state"}B= not $state
ABresult
0X0
100
111
84 9042 my $oldstate = $self->{'cells'}->{$xpos, $ypos}->{'state'};
85 9042 --$self->{'liveCells'};
86 # Update neighbours counts:
87 9042 foreach my $xx ($xpos-1 .. $xpos+1) {
88 27126 foreach my $yy ($ypos-1 .. $ypos+1) {
89 81378 ▶ if (($xx == $xpos) and ($yy == $ypos)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($xx == $xpos and $yy == $ypos) 9042 72336 81378
Condition100%3/3
$xx == $xpos and $yy == $ypos
!ll&&!rl&&r
54252180849042
MC/DC100%2/2
$xx == $xpos and $yy == $ypos
$xx == $xpos $yy == $ypos
Truth table100%3/3
A and B
A= $xx == $xposB= $yy == $ypos
ABresult
0X0
100
111
90 9042 next;
91 };
92 72336 ▶ if (defined ($self->{'cells'}->{$xx, $yy})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'cells'}{join $;, $xx, $yy}) { } 72336 0 72336
93 72336 --$self->{'cells'}->{$xx, $yy}->{'neighbours'}->{$oldstate};
94 72336 --$self->{'cells'}->{$xx, $yy}->{'neighbours'}->{'total'};
95 } else {
96 #&createCell($self, $xx, $yy);
97 };
98 };
99 };
100 };
101 19509 ▶ if ((not $self->{'cells'}->{$xpos, $ypos}->{'state'}) and ($state)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{"cells"}{join $;, $xpos, $ypos}{"state"} and $state) 10456 9053 19509
Condition100%3/3
not $self->{"cells"}{join $;, $xpos, $ypos}{"state"} and $state
!ll&&!rl&&r
90421110456
MC/DC100%2/2
not $self->{"cells"}{join $;, $xpos, $ypos}{"state"} and $state
not $self->{"cells"}{join $;, $xpos, $ypos}{"state"} $state
Truth table100%3/3
not(A) and B
A= not $self->{"cells"}{join $;, $xpos, $ypos}{"state"}B= $state
ABresult
0X0
100
111
102 # Check validity:
103 10456 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($state > 1 and $self->{"color"} < 1 or $state > 2 and $self->{"color"} < 2 or $state > 4 or $state < 0) 0 10456 10456
Condition46%7/15
$state > 1 and $self->{"color"} < 1 or $state > 2 and $self->{"color"} < 2 or $state > 4 or $state < 0
l!l&&r!l&&!r
0010456
$state > 1 and $self->{"color"} < 1 or $state > 2 and $self->{"color"} < 2 or $state > 4
l!l&&r!l&&!r
0010456
$state > 1 and $self->{"color"} < 1 or $state > 2 and $self->{"color"} < 2
l!l&&r!l&&!r
0010456
$state > 1 and $self->{"color"} < 1
!ll&&!rl&&r
102382180
$state > 2 and $self->{"color"} < 2
!ll&&!rl&&r
103171390
MC/DC0%0/6
$state > 1 and $self->{"color"} < 1 or $state > 2 and $self->{"color"} < 2 or $state > 4 or $state < 0
$state > 1 $self->{"color"} < 1 $state > 2 $self->{"color"} < 2 $state > 4 $state < 0
Truth table20%3/15
A and B or C and D or E or F
A= $state > 1B= $self->{"color"} < 1C= $state > 2D= $self->{"color"} < 2E= $state > 4F= $state < 0
ABCDEFresult
11XXXX1
0X11XX1
1011XX1
0X0X1X1
0X101X1
100X1X1
10101X1
0X0X011
0X10011
100X011
1010011
0X0X000
0X10000
100X000
1010000
104 (($state > 1) and ($self->{'color'} < 1)) or
105 (($state > 2) and ($self->{'color'} < 2)) or
106 ($state > 4) or
107 ($state < 0)
108 ) {
109 # Invalid state for current color.
110 0 return -1;
111 };
112 10456 ++$self->{'liveCells'};
113 # Update neighbours counts:
114 10456 foreach my $xx ($xpos-1 .. $xpos+1) {
115 31368 foreach my $yy ($ypos-1 .. $ypos+1) {
116 94104 ▶ if (($xx == $xpos) and ($yy == $ypos)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($xx == $xpos and $yy == $ypos) 10456 83648 94104
Condition100%3/3
$xx == $xpos and $yy == $ypos
!ll&&!rl&&r
627362091210456
MC/DC100%2/2
$xx == $xpos and $yy == $ypos
$xx == $xpos $yy == $ypos
Truth table100%3/3
A and B
A= $xx == $xposB= $yy == $ypos
ABresult
0X0
100
111
117 10456 next;
118 };
119 83648 ▶ if (defined ($self->{'cells'}->{$xx, $yy})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'cells'}{join $;, $xx, $yy}) { } 73211 10437 83648
120 73211 ++$self->{'cells'}->{$xx, $yy}->{'neighbours'}->{$state};
121 73211 ++$self->{'cells'}->{$xx, $yy}->{'neighbours'}->{'total'};
122 } else {
123 10437 &createCell($self, $xx, $yy);
124 10437 ++$self->{'cells'}->{$xx, $yy}->{'neighbours'}->{$state};
125 10437 ++$self->{'cells'}->{$xx, $yy}->{'neighbours'}->{'total'};
126 };
127 };
128 };
129 };
130 19509 $self->{'cells'}->{$xpos, $ypos}->{'state'} = $state;
131 19509 return;
132 };
133  
134 sub queryCell {
135 # Return the state of a cell:
136 0 ▶ my ( $self, $xpos, $ypos ) = @_;
Subroutine queryCell: not called CC=2 SCAR=17.9
Pod documented
137 0 ▶ if (defined $self->{'cells'}->{$xpos, $ypos}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'cells'}{join $;, $xpos, $ypos}) { } 0 0 0
138 0 return $self->{'cells'}->{$xpos, $ypos}->{'state'};
139 } else {
140 0 return;
141 };
142 };
143  
144 sub createCell {
145 # Create an empty cell.
146 10693 ▶ my ($self, $xpos, $ypos, @rest) = @_;
Subroutine createCell: called CC=5 SCAR=16.1
Pod undocumented
147 10693 $self->{'cells'}->{$xpos, $ypos}->{'state'} = 0;
148 10693 $self->{'cells'}->{$xpos, $ypos}->{'neighbours'}->{1} = 0;
149 10693 $self->{'cells'}->{$xpos, $ypos}->{'neighbours'}->{2} = 0;
150 10693 $self->{'cells'}->{$xpos, $ypos}->{'neighbours'}->{3} = 0;
151 10693 $self->{'cells'}->{$xpos, $ypos}->{'neighbours'}->{4} = 0;
152 10693 $self->{'cells'}->{$xpos, $ypos}->{'neighbours'}->{'total'} = 0;
153 # Update boundaries:
154 10693 ▶ $self->{'maxx'} < $xpos and $self->{'maxx'} = $xpos;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"maxx"} < $xpos and $self->{"maxx"} = $xpos 117 10576 10693
155 10693 ▶ $self->{'minx'} > $xpos and $self->{'minx'} = $xpos;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"minx"} > $xpos and $self->{"minx"} = $xpos 232 10461 10693
156 10693 ▶ $self->{'maxy'} < $ypos and $self->{'maxy'} = $ypos;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"maxy"} < $ypos and $self->{"maxy"} = $ypos 599 10094 10693
157 10693 ▶ $self->{'miny'} > $ypos and $self->{'miny'} = $ypos;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"miny"} > $ypos and $self->{"miny"} = $ypos 27 10666 10693
158 10693 ++$self->{'usedCells'};
159 10693 return;
160 };
161  
162 sub loadInit {
163 # Load an initial grid from a file.
164 11 ▶ my ($self, $fn, @rest) = @_;
Subroutine loadInit: called CC=12 SCAR=27.1
Pod documented
165 11 my $untitled = 'Untitled.cells';
166 11 ▶ if (not defined $fn) { return $untitled; };
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $fn) 0 11 11
167 11 my @array;
168 11 ▶ open(my $fh,"<:crlf", $fn) or die "Failed to open $fn: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:crlf", $fn or die "Failed to open ${fn}: $!" 0 11 11
169 11 ▶ while (<$fh>) { push @array, $_; };
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 706 11 717
170 11 close $fh;
171 11 my $ftype = &format_is(\@array);
172 11 my $loadok;
173 11 ▶ if (not defined $ftype) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ftype) 0 11 11
174 0 print "DEBUG: File format could not be detected.\n";
175 0 return $untitled;
176 };
177 11 ▶ if ($ftype->{'format'} eq 'CELLS') {
Branch: 4 branches
BranchTrueFalseTotal
if ($ftype->{'format'} eq 'CELLS') { } 8 3 11
elsif ($ftype->{'format'} eq 'RLE') { } 1 2 3
elsif ($ftype->{'format'} eq 'Life' and $ftype->{'version'} eq '1.05') { } 1 1 2
elsif ($ftype->{'format'} eq 'Life' and $ftype->{'version'} eq '1.06') { } 1 0 1
178 8 $loadok = $self->loadCells(\@array);
179 8 print "DEBUG: Loaded CELLS\n";
180 } elsif ($ftype->{'format'} eq 'RLE') {
181 1 $loadok = $self->loadRLE(\@array);
182 1 print "DEBUG: Loaded RLE\n";
183 ▶ } elsif (($ftype->{'format'} eq 'Life') and ($ftype->{'version'} eq '1.05')) {
Condition66%2/3
$ftype->{"format"} eq "Life" and $ftype->{"version"} eq "1.05"
!ll&&!rl&&r
011
MC/DC50%1/2
$ftype->{"format"} eq "Life" and $ftype->{"version"} eq "1.05"
$ftype->{"format"} eq "Life" $ftype->{"version"} eq "1.05"
Truth table66%2/3
A and B
A= $ftype->{"format"} eq "Life"B= $ftype->{"version"} eq "1.05"
ABresult
0X0
100
111
184 1 $loadok = $self->loadL105(\@array);
185 1 print "DEBUG: Loaded Life 1.05\n";
186 ▶ } elsif (($ftype->{'format'} eq 'Life') and ($ftype->{'version'} eq '1.06')) {
Condition33%1/3
$ftype->{"format"} eq "Life" and $ftype->{"version"} eq "1.06"
!ll&&!rl&&r
001
MC/DC0%0/2
$ftype->{"format"} eq "Life" and $ftype->{"version"} eq "1.06"
$ftype->{"format"} eq "Life" $ftype->{"version"} eq "1.06"
Truth table33%1/3
A and B
A= $ftype->{"format"} eq "Life"B= $ftype->{"version"} eq "1.06"
ABresult
0X0
100
111
187 1 $loadok = $self->loadL106(\@array);
188 1 print "DEBUG: Loaded Life 1.06\n";
189 } else {
190 0 print "DEBUG: " . $ftype->{'format'} . " V. " . $ftype->{'version'} . " not supported yet.\n";
191 0 return $untitled;
192 };
193 11 ▶ if (not $loadok) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $loadok) { } 0 11 11
194 0 print "DEBUG: Load failed!\n";
195 0 return $untitled;
196 } else {
197 11 $self->{'currentFn'} = $fn;
198 11 return $fn;
199 };
200 };
201  
202 sub saveGridTxt {
203 # Save a grid to a txt file.
204 0 ▶ my ($self, $fn, @rest) = @_;
Subroutine saveGridTxt: not called CC=8 SCAR=42.8
Pod documented
205 0 ▶ if (not defined $fn) { return; };
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $fn) 0 0 0
206 0 my $output;
207 0 ▶ open($output, ">", $fn) or die "cannot open $fn: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open $output, ">", $fn or die "cannot open ${fn}: $!\n" 0 0 0
208 0 ▶ for (my $yy = $self->{'miny'}; $yy <= $self->{'maxy'}; $yy++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($yy <= $self->{"maxy"}) 0 0 0
209 0 foreach my $xx ($self->{'minx'} .. $self->{'maxx'}) {
210 0 ▶ if (defined ($self->{'cells'}->{$xx, $yy})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'cells'}{join $;, $xx, $yy}) { } 0 0 0
211 0 ▶ if ($self->{'cells'}->{$xx, $yy}->{'state'} == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'cells'}{join $;, $xx, $yy}{'state'} == 1) { } 0 0 0
elsif ($self->{'cells'}{join $;, $xx, $yy}{'state'} > 1) { } 0 0 0
212 0 print $output 'O';
213 } elsif ($self->{'cells'}->{$xx, $yy}->{'state'} > 1) {
214 0 print $output $self->{'cells'}->{$xx, $yy}->{'state'};
215 } else {
216 0 print $output '.';
217 };
218 } else {
219 0 print $output '.';
220 };
221 };
222 0 print $output "\n";
223 };
224 0 close $output;
225 0 return $fn;
226 };
227  
228  
229 sub crudePrintBoard {
230 # Basic board print.
231 0 ▶ my $self = shift;
Subroutine crudePrintBoard: not called CC=7 SCAR=40.3
Pod documented
232 0 for (1 .. 80) {
233 0 print "-";
234 };
235 0 print "\n";
236 0 ▶ for (my $yy = $self->{'miny'}; $yy <= $self->{'maxy'}; $yy++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($yy <= $self->{"maxy"}) 0 0 0
237 0 foreach my $xx ($self->{'minx'} .. $self->{'maxx'}) {
238 0 ▶ if (defined ($self->{'cells'}->{$xx, $yy})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'cells'}{join $;, $xx, $yy}) { } 0 0 0
239 0 ▶ if ($self->{'cells'}->{$xx, $yy}->{'state'} == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'cells'}{join $;, $xx, $yy}{'state'} == 1) { } 0 0 0
elsif ($self->{'cells'}{join $;, $xx, $yy}{'state'} > 1) { } 0 0 0
240 0 print 'O';
241 } elsif ($self->{'cells'}->{$xx, $yy}->{'state'} > 1) {
242 0 print $self->{'cells'}->{$xx, $yy}->{'state'};
243 } else {
244 0 print '_';
245 };
246 } else {
247 0 print '.';
248 };
249 };
250 0 print "\n";
251 };
252 0 my $stats = &statistics($self);
253 0 print "---\tGeneration: " . $stats->{'generation'} . " x: " . $stats->{'minx'} . ".." . $stats->{'maxx'} . " y: " . $stats->{'miny'} . ".." . $stats->{'maxy'} . " Size: $stats->{'size'} LiveCells: " . $stats->{'liveCells'} . "\n";
254 0 print "\tDelta: " . $stats->{'delta'} . "\n";
255 0 return;
256 };
257  
258 sub tick {
259 # Calculate next epoch.
260 1048 ▶ my ($self, $oscCheck) = @_;
Subroutine tick: called CC=9 SCAR=22.1
Pod documented
261 1048 my $t0 = [Time::HiRes::gettimeofday()];
262 1048 $oscCheck = &setOscCheck($self, $oscCheck);
263  
264 1048 my $resref = &tickMainLoop($self);
265 1048 my @newCells = @{$resref->{'newCells'}};
266 1048 my @dieCells = @{$resref->{'dieCells'}};
267 1048 my @delCells = @{$resref->{'delCells'}};
268 #use Data::Dumper;
269 1048 $self->{'delta'} = scalar(@newCells) + scalar(@dieCells);
270 # Apply changes on board:
271 1048 foreach my $rec (@newCells) {
272 # TODO: Do something in case of error?
273 9040 my $error = &updateCell($self, $rec->[0], $rec->[1], $rec->[2]);
274 9040 ▶ $error = (defined $error) ? $error : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $error ? : 0 9040 9040
275 };
276 1048 foreach my $rec (@dieCells) {
277 9042 my $error = &updateCell($self, $rec->[0], $rec->[1], 0);
278 };
279 1048 foreach my $rec (@delCells) {
280 # Verify that these cells are still without neighbours:
281 9310 ▶ if ($self->{'cells'}->{$rec->[0], $rec->[1]}->{'neighbours'}->{'total'} == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"cells"}{join $;, $rec->[0], $rec->[1]}{"neighbours"}{"total"} == 0) 5361 3949 9310
282 5361 delete $self->{'cells'}->{$rec->[0], $rec->[1]};
283 5361 --$self->{'usedCells'};
284 };
285 };
286 1048 $self->{'gen'} = $self->{'gen'} + 1;
287 1048 ▶ $self->{'factor2'} = ((defined $self->{'usedCells'}) and ($self->{'usedCells'} > 0)) ? $self->{'liveCells'} / $self->{'usedCells'} : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'usedCells'} && $self->{'usedCells'} > 0 ? : 1048 0 1048
Condition33%1/3
defined $self->{"usedCells"} and $self->{"usedCells"} > 0
!ll&&!rl&&r
001048
MC/DC0%0/2
defined $self->{"usedCells"} and $self->{"usedCells"} > 0
defined $self->{"usedCells"} $self->{"usedCells"} > 0
Truth table33%1/3
A and B
A= defined $self->{"usedCells"}B= $self->{"usedCells"} > 0
ABresult
0X0
100
111
288 1048 ▶ if ($oscCheck > 1) { &oscCheck($self, $oscCheck); };
Branch: 1 branch
BranchTrueFalseTotal
if ($oscCheck > 1) 207 841 1048
289 1048 my $t1 = [Time::HiRes::gettimeofday];
290 1048 my $t0_t1 = Time::HiRes::tv_interval( $t0, $t1 );
291 1048 $self->{'lastTI'} = $t0_t1; # Time spend to calculate last epoch.
292 1048 $self->{'totalTime'} += $t0_t1; # Total Time spend calculating this board.
293 1048 return;
294 };
295  
296 sub tickMainLoop {
297 # TODO: Return new cell's color if board is immigration or quadlife.
298 1048 ▶ my ($self) = @_;
Subroutine tickMainLoop: called CC=22 SCAR=30.9
Pod undocumented
299 1048 my @newCells;
300 my @dieCells;
301 1048 my @delCells;
302 1048 foreach my $key (keys %{ $self->{'cells'} }) {
303 88902 my ($xx, $yy) = split(/$;/, $key, 2);
304 88902 my $rec = [$xx, $yy];
305 88902 ▶ if (
Branch: 3 branches
BranchTrueFalseTotal
if ($self->{'cells'}{join $;, $xx, $yy}{'state'} > 0 and not $self->{'cells'}{join $;, $xx, $yy}{'neighbours'}{'total'} ~~ $self->{'liveRules'}) { } 9042 79860 88902
elsif ($self->{'cells'}{join $;, $xx, $yy}{'state'} == 0 and $self->{'cells'}{join $;, $xx, $yy}{'neighbours'}{'total'} ~~ $self->{'breedRules'}) { } 9040 70820 79860
elsif ($self->{'cells'}{join $;, $xx, $yy}{'state'} == 0 and $self->{'cells'}{join $;, $xx, $yy}{'neighbours'}{'total'} == 0) { } 9310 74269 83579
Condition100%3/3
$self->{"cells"}{join $;, $xx, $yy}{"state"} > 0 and not $self->{"cells"}{join $;, $xx, $yy}{"neighbours"}{"total"} ~~ $self->{"liveRules"}
!ll&&!rl&&r
67101127599042
MC/DC100%2/2
$self->{"cells"}{join $;, $xx, $yy}{"state"} > 0 and not $self->{"cells"}{join $;, $xx, $yy}{"neighbours"}{"total"} ~~ $self->{"liveRules"}
$self->{"cells"}{join $;, $xx, $yy}{"state"} > 0 not $self->{"cells"}{join $;, $xx, $yy}{"neighbours"}{"total"} ~~ $self->{"liveRules"}
Truth table100%3/3
A and not(B)
A= $self->{"cells"}{join $;, $xx, $yy}{"state"} > 0B= not $self->{"cells"}{join $;, $xx, $yy}{"neighbours"}{"total"} ~~ $self->{"liveRules"}
ABresult
0X0
100
111
306 ($self->{'cells'}->{$xx, $yy}->{'state'} > 0) and
307 (not $self->{'cells'}->{$xx, $yy}->{'neighbours'}->{'total'} ~~ $self->{'liveRules'})
308 ) {
309 # Die.
310 9042 push @dieCells, $rec;
311 ▶ } elsif (
Condition100%3/3
$self->{"cells"}{join $;, $xx, $yy}{"state"} == 0 and $self->{"cells"}{join $;, $xx, $yy}{"neighbours"}{"total"} ~~ $self->{"breedRules"}
!ll&&!rl&&r
12759580619040
MC/DC100%2/2
$self->{"cells"}{join $;, $xx, $yy}{"state"} == 0 and $self->{"cells"}{join $;, $xx, $yy}{"neighbours"}{"total"} ~~ $self->{"breedRules"}
$self->{"cells"}{join $;, $xx, $yy}{"state"} == 0 $self->{"cells"}{join $;, $xx, $yy}{"neighbours"}{"total"} ~~ $self->{"breedRules"}
Truth table100%3/3
A and B
A= $self->{"cells"}{join $;, $xx, $yy}{"state"} == 0B= $self->{"cells"}{join $;, $xx, $yy}{"neighbours"}{"total"} ~~ $self->{"breedRules"}
ABresult
0X0
100
111
312 ($self->{'cells'}->{$xx, $yy}->{'state'} == 0) and
313 ($self->{'cells'}->{$xx, $yy}->{'neighbours'}->{'total'} ~~ $self->{'breedRules'})
314 ) {
315 # New.
316 9040 ▶ if ($self->{'color'} == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'color'} == 0) { } 8792 248 9040
317 # Standard
318 8792 $rec->[2] = 1;
319 8792 push @newCells, $rec;
320 } else {
321 # Colorized
322 #use Data::Dumper;
323 248 my @colorcounts = ();
324 248 my $ccnt = 0;
325 248 foreach my $state (keys %{ $self->{'cells'}->{$xx, $yy}->{'neighbours'} }) {
326 1240 ▶ if ($state ne 'total') {
Branch: 1 branch
BranchTrueFalseTotal
if ($state ne "total") 992 248 1240
327 992 ▶ if ($self->{'cells'}->{$xx, $yy}->{'neighbours'}->{$state} > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"cells"}{join $;, $xx, $yy}{"neighbours"}{$state} > 0) 309 683 992
328 309 $colorcounts[$state] = $self->{'cells'}->{$xx, $yy}->{'neighbours'}->{$state};
329 309 $ccnt++;
330 };
331 };
332 };
333 248 ▶ if ($ccnt == 2) {
Branch: 3 branches
BranchTrueFalseTotal
if ($ccnt == 2) { } 57 191 248
elsif ($ccnt == 3) { } 2 189 191
elsif ($ccnt == 1) { } 189 0 189
334 # Immigration, or first rule of Quadlife (Identical).
335 # Select maximum.
336 57 my $max = -1;
337 57 for my $i (1..4) {
338 228 ▶ if (defined $colorcounts[$i]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $colorcounts[$i]) 114 114 228
339 114 ▶ if ($colorcounts[$i] > $max) {
Branch: 1 branch
BranchTrueFalseTotal
if ($colorcounts[$i] > $max) 103 11 114
340 103 $max = $colorcounts[$i];
341 103 $rec->[2] = $i
342 };
343 };
344 };
345 } elsif ($ccnt == 3) {
346 # Second rule of Quadlife
347 2 for my $i (1..4) {
348 6 ▶ if (not defined $colorcounts[$i]) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $colorcounts[$i]) 2 4 6
349 2 $rec->[2] = $i;
350 2 last;
351 };
352 };
353 } elsif ($ccnt == 1) {
354 # Same color as parents:
355 189 for my $i (1..4) {
356 471 ▶ if (defined $colorcounts[$i]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $colorcounts[$i]) 189 282 471
357 189 $rec->[2] = $i;
358 189 last;
359 };
360 };
361 } else {
362 # Unsupported or standard. Return first color.
363 0 $rec->[2] = 1;
364 };
365 248 push @newCells, $rec;
366 };
367 ▶ } elsif (
Condition100%3/3
$self->{"cells"}{join $;, $xx, $yy}{"state"} == 0 and $self->{"cells"}{join $;, $xx, $yy}{"neighbours"}{"total"} == 0
!ll&&!rl&&r
12759487519310
MC/DC100%2/2
$self->{"cells"}{join $;, $xx, $yy}{"state"} == 0 and $self->{"cells"}{join $;, $xx, $yy}{"neighbours"}{"total"} == 0
$self->{"cells"}{join $;, $xx, $yy}{"state"} == 0 $self->{"cells"}{join $;, $xx, $yy}{"neighbours"}{"total"} == 0
Truth table100%3/3
A and B
A= $self->{"cells"}{join $;, $xx, $yy}{"state"} == 0B= $self->{"cells"}{join $;, $xx, $yy}{"neighbours"}{"total"} == 0
ABresult
0X0
100
111
368 ($self->{'cells'}->{$xx, $yy}->{'state'} == 0) and
369 ($self->{'cells'}->{$xx, $yy}->{'neighbours'}->{'total'} == 0)
370 ) {
371 # Isolated empty cell. GC.
372 9310 push @delCells, $rec;
373 };
374 };
375 1048 return {'newCells' => \@newCells, 'dieCells' => \@dieCells, 'delCells' => \@delCells};
376 };
377  
378 sub setOscCheck {
379 1048 ▶ my ($self, $oscCheck) = @_;
Subroutine setOscCheck: called CC=3 SCAR=11.0
Pod undocumented
380  
381 1048 ▶ $oscCheck = (defined $oscCheck) ? $oscCheck : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $oscCheck ? : 1048 0 1048
382 1048 ▶ if ($oscCheck != $self->{'osccheck'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($oscCheck != $self->{"osccheck"}) 3 1045 1048
383 # Change, delete all previous snapshots:
384 3 delete $self->{'snapshots'};
385 };
386 1048 $self->{'osccheck'} = $oscCheck;
387 1048 return $oscCheck;
388 };
389  
390 sub oscCheck {
391 207 ▶ my ($self, $oscCheck) = @_;
Subroutine oscCheck: called CC=8 SCAR=20.9
Pod undocumented
392 207 my $lgen = $self->{'gen'};
393 207 my $lgenString = sprintf("s%d", $lgen);
394 207 my $ogen;
395 my $ogenString;
396 207 $self->{'snapshots'}->{$lgenString} = &snapshot($self); # Smile!
397 207 ▶ for (my $i = 2; $i <= $oscCheck; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $oscCheck) 262 197 459
398 262 $ogen = $lgen - $i;
399 262 $ogenString = sprintf("s%d", $ogen);
400 262 ▶ if (defined ($self->{'snapshots'}->{$ogenString})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"snapshots"}{$ogenString}) 219 43 262
401 # Snapshot of grandma!
402 219 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'snapshots'}{$ogenString}{'snapshot'} eq $self->{'snapshots'}{$lgenString}{'snapshot'} and $self->{'snapshots'}{$ogenString}{'minx'} == $self->{'snapshots'}{$lgenString}{'minx'} and $self->{'snapshots'}{$ogenString}{'maxx'} == $self->{'snapshots'}{$lgenString}{'maxx'} and $self->{'snapshots'}{$ogenString}{'miny'} == $self->{'snapshots'}{$lgenString}{'miny'} and $self->{'snapshots'}{$ogenString}{'maxy'} == $self->{'snapshots'}{$lgenString}{'maxy'}) { } 10 209 219
Condition66%8/12
$self->{"snapshots"}{$ogenString}{"snapshot"} eq $self->{"snapshots"}{$lgenString}{"snapshot"} and $self->{"snapshots"}{$ogenString}{"minx"} == $self->{"snapshots"}{$lgenString}{"minx"} and $self->{"snapshots"}{$ogenString}{"maxx"} == $self->{"snapshots"}{$lgenString}{"maxx"} and $self->{"snapshots"}{$ogenString}{"miny"} == $self->{"snapshots"}{$lgenString}{"miny"} and $self->{"snapshots"}{$ogenString}{"maxy"} == $self->{"snapshots"}{$lgenString}{"maxy"}
!ll&&!rl&&r
209010
$self->{"snapshots"}{$ogenString}{"snapshot"} eq $self->{"snapshots"}{$lgenString}{"snapshot"} and $self->{"snapshots"}{$ogenString}{"minx"} == $self->{"snapshots"}{$lgenString}{"minx"} and $self->{"snapshots"}{$ogenString}{"maxx"} == $self->{"snapshots"}{$lgenString}{"maxx"} and $self->{"snapshots"}{$ogenString}{"miny"} == $self->{"snapshots"}{$lgenString}{"miny"}
!ll&&!rl&&r
209010
$self->{"snapshots"}{$ogenString}{"snapshot"} eq $self->{"snapshots"}{$lgenString}{"snapshot"} and $self->{"snapshots"}{$ogenString}{"minx"} == $self->{"snapshots"}{$lgenString}{"minx"} and $self->{"snapshots"}{$ogenString}{"maxx"} == $self->{"snapshots"}{$lgenString}{"maxx"}
!ll&&!rl&&r
209010
$self->{"snapshots"}{$ogenString}{"snapshot"} eq $self->{"snapshots"}{$lgenString}{"snapshot"} and $self->{"snapshots"}{$ogenString}{"minx"} == $self->{"snapshots"}{$lgenString}{"minx"}
!ll&&!rl&&r
209010
MC/DC20%1/5
$self->{"snapshots"}{$ogenString}{"snapshot"} eq $self->{"snapshots"}{$lgenString}{"snapshot"} and $self->{"snapshots"}{$ogenString}{"minx"} == $self->{"snapshots"}{$lgenString}{"minx"} and $self->{"snapshots"}{$ogenString}{"maxx"} == $self->{"snapshots"}{$lgenString}{"maxx"} and $self->{"snapshots"}{$ogenString}{"miny"} == $self->{"snapshots"}{$lgenString}{"miny"} and $self->{"snapshots"}{$ogenString}{"maxy"} == $self->{"snapshots"}{$lgenString}{"maxy"}
$self->{"snapshots"}{$ogenString}{"snapshot"} eq $self->{"snapshots"}{$lgenString}{"snapshot"} $self->{"snapshots"}{$ogenString}{"minx"} == $self->{"snapshots"}{$lgenString}{"minx"} $self->{"snapshots"}{$ogenString}{"maxx"} == $self->{"snapshots"}{$lgenString}{"maxx"} $self->{"snapshots"}{$ogenString}{"miny"} == $self->{"snapshots"}{$lgenString}{"miny"} $self->{"snapshots"}{$ogenString}{"maxy"} == $self->{"snapshots"}{$lgenString}{"maxy"}
Truth table33%2/6
A and B and C and D and E
A= $self->{"snapshots"}{$ogenString}{"snapshot"} eq $self->{"snapshots"}{$lgenString}{"snapshot"}B= $self->{"snapshots"}{$ogenString}{"minx"} == $self->{"snapshots"}{$lgenString}{"minx"}C= $self->{"snapshots"}{$ogenString}{"maxx"} == $self->{"snapshots"}{$lgenString}{"maxx"}D= $self->{"snapshots"}{$ogenString}{"miny"} == $self->{"snapshots"}{$lgenString}{"miny"}E= $self->{"snapshots"}{$ogenString}{"maxy"} == $self->{"snapshots"}{$lgenString}{"maxy"}
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
403 ($self->{'snapshots'}->{$ogenString}->{'snapshot'} eq $self->{'snapshots'}->{$lgenString}->{'snapshot'}) and
404 ($self->{'snapshots'}->{$ogenString}->{'minx'} == $self->{'snapshots'}->{$lgenString}->{'minx'}) and
405 ($self->{'snapshots'}->{$ogenString}->{'maxx'} == $self->{'snapshots'}->{$lgenString}->{'maxx'}) and
406 ($self->{'snapshots'}->{$ogenString}->{'miny'} == $self->{'snapshots'}->{$lgenString}->{'miny'}) and
407 ($self->{'snapshots'}->{$ogenString}->{'maxy'} == $self->{'snapshots'}->{$lgenString}->{'maxy'})
408 ) {
409 # Grandma and grandson are identical!
410 10 $self->{'oscilator'} = $i;
411 10 last;
412 } else {
413 209 $self->{'oscilator'} = 0;
414 };
415 };
416 };
417 # Delete oldest snapshot.
418 207 delete $self->{'snapshots'}->{$ogenString};
419 207 return;
420 };
421  
422 sub snapshot {
423 # Take a snapshot of the board, in a way that it can be easily stored and compared
424 # to another snapshot.
425 221 ▶ my $self = shift;
Subroutine snapshot: called CC=6 SCAR=17.9
Pod undocumented
426 221 my $snapshot = '';
427 221 ▶ for (my $yy = $self->{'miny'}; $yy <= $self->{'maxy'}; $yy++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($yy <= $self->{"maxy"}) 6790 221 7011
428 6790 foreach my $xx ($self->{'minx'} .. $self->{'maxx'}) {
429 263028 ▶ if (defined ($self->{'cells'}->{$xx, $yy})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'cells'}{join $;, $xx, $yy}) { } 66796 196232 263028
430 66796 ▶ if ($self->{'cells'}->{$xx, $yy}->{'state'} == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'cells'}{join $;, $xx, $yy}{'state'} == 1) { } 17392 49404 66796
elsif ($self->{'cells'}{join $;, $xx, $yy}{'state'} > 1) { } 51 49353 49404
431 17392 $snapshot .= 'O';
432 } elsif ($self->{'cells'}->{$xx, $yy}->{'state'} > 1) {
433 51 $snapshot .= $self->{'cells'}->{$xx, $yy}->{'state'};
434 } else {
435 49353 $snapshot .= '.';
436 };
437 } else {
438 196232 $snapshot .= '.';
439 };
440 };
441 6790 $snapshot .= "\n";
442 };
443 return {
444 'snapshot' => $snapshot,
445 'minx' => $self->{'minx'},
446 'maxx' => $self->{'maxx'},
447 'miny' => $self->{'miny'},
448 221 'maxy' => $self->{'maxy'},
449 };
450 };
451  
452 sub shrinkBoard {
453 # Shrink board: Now mostly used to keep boundaries track, for printing and snapshot.
454 7 ▶ my $self = shift;
Subroutine shrinkBoard: called CC=9 SCAR=22.0
Pod documented
455 7 $self->{'minx'} = $self->{'maxx'} = $self->{'miny'} = $self->{'maxy'} = 0;
456 7 my $ok = 0;
457 7 foreach my $key (keys %{ $self->{'cells'} }) {
458 524 my ($xx, $yy) = split(/$;/, $key, 2);
459 524 ▶ if (($self->{'cells'}->{$xx, $yy}->{'state'} == 0) and ($self->{'cells'}->{$xx, $yy}->{'neighbours'}->{'total'} == 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'cells'}{join $;, $xx, $yy}{'state'} == 0 and $self->{'cells'}{join $;, $xx, $yy}{'neighbours'}{'total'} == 0) { } 68 456 524
Condition100%3/3
$self->{"cells"}{join $;, $xx, $yy}{"state"} == 0 and $self->{"cells"}{join $;, $xx, $yy}{"neighbours"}{"total"} == 0
!ll&&!rl&&r
11833868
MC/DC100%2/2
$self->{"cells"}{join $;, $xx, $yy}{"state"} == 0 and $self->{"cells"}{join $;, $xx, $yy}{"neighbours"}{"total"} == 0
$self->{"cells"}{join $;, $xx, $yy}{"state"} == 0 $self->{"cells"}{join $;, $xx, $yy}{"neighbours"}{"total"} == 0
Truth table100%3/3
A and B
A= $self->{"cells"}{join $;, $xx, $yy}{"state"} == 0B= $self->{"cells"}{join $;, $xx, $yy}{"neighbours"}{"total"} == 0
ABresult
0X0
100
111
460 68 delete $self->{'cells'}->{$key};
461 68 --$self->{'usedCells'};
462 } else {
463 456 ▶ if ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ok) { } 449 7 456
464 449 ▶ if ($xx > $self->{'maxx'}) { $self->{'maxx'} = $xx; };
Branch: 1 branch
BranchTrueFalseTotal
if ($xx > $self->{"maxx"}) 13 436 449
465 449 ▶ if ($xx < $self->{'minx'}) { $self->{'minx'} = $xx; };
Branch: 1 branch
BranchTrueFalseTotal
if ($xx < $self->{"minx"}) 11 438 449
466 449 ▶ if ($yy > $self->{'maxy'}) { $self->{'maxy'} = $yy; };
Branch: 1 branch
BranchTrueFalseTotal
if ($yy > $self->{"maxy"}) 7 442 449
467 449 ▶ if ($yy < $self->{'miny'}) { $self->{'miny'} = $yy; };
Branch: 1 branch
BranchTrueFalseTotal
if ($yy < $self->{"miny"}) 13 436 449
468 } else {
469 7 $self->{'minx'} = $xx;
470 7 $self->{'maxx'} = $xx;
471 7 $self->{'miny'} = $yy;
472 7 $self->{'maxy'} = $yy;
473 7 $ok = 1;
474 };
475 };
476 };
477 7 return;
478 };
479  
480 sub statistics {
481 14 ▶ my $self = shift;
Subroutine statistics: called CC=1 SCAR=0.0
Pod documented
482 return {
483 'size' => (($self->{'maxx'} - $self->{'minx'}) * ($self->{'maxy'} - $self->{'miny'})),
484 'generation' => $self->{'gen'},
485 'minx' => $self->{'minx'},
486 'maxx' => $self->{'maxx'},
487 'miny' => $self->{'miny'},
488 'maxy' => $self->{'maxy'},
489 'liveCells' => $self->{'liveCells'},
490 'delta' => $self->{'delta'},
491 'oscilator' => $self->{'oscilator'},
492 'totalTime' => $self->{'totalTime'},
493 'usedCells' => $self->{'usedCells'},
494 'factor2' => $self->{'factor2'},
495 14 'lastTI' => $self->{'lastTI'},
496 };
497 };
498  
499 sub format_is {
500 11 ▶ my ($faref) = @_; # Array ref containing file.
Subroutine format_is: called CC=8 SCAR=21.2
Pod documented
501 11 my %result = (
502 format => undef,
503 version => undef
504 );
505 11 foreach my $line (@{ $faref }) {
506 15 chomp($line);
507 15 ▶ if (($line =~ /^#Life 1.05/) or ($line =~ /^#Life 1.06/) or ($line =~ /^#MCell/)) {
Branch: 4 branches
BranchTrueFalseTotal
if ($line =~ /^#Life 1.05/ or $line =~ /^#Life 1.06/ or $line =~ /^#MCell/) { } 2 13 15
elsif ($line =~ /^(b|o|[[:digit:]])/) { } 1 12 13
elsif ($line =~ /^(\.|O|!)/) { } 8 4 12
elsif ($line =~ /^(\.|O|2|3|4|!)/) { } 0 4 4
Condition83%5/6
$line =~ /^#Life 1.05/ or $line =~ /^#Life 1.06/ or $line =~ /^#MCell/
l!l&&r!l&&!r
2013
$line =~ /^#Life 1.05/ or $line =~ /^#Life 1.06/
l!l&&r!l&&!r
1113
MC/DC66%2/3
$line =~ /^#Life 1.05/ or $line =~ /^#Life 1.06/ or $line =~ /^#MCell/
$line =~ /^#Life 1.05/ $line =~ /^#Life 1.06/ $line =~ /^#MCell/
Truth table75%3/4
A or B or C
A= $line =~ /^#Life 1.05/B= $line =~ /^#Life 1.06/C= $line =~ /^#MCell/
ABCresult
1XX1
01X1
0011
0000
508 2 ($result{format}, $result{version}) = split / /, $line, 2;
509 2 $result{format} =~ s/#//;
510 2 return \%result;
511 } elsif ($line =~ /^(b|o|[[:digit:]])/) {
512 1 ($result{format}, $result{version}) = ('RLE', 'N/A');
513 1 return \%result;
514 } elsif ($line =~ /^(\.|O|!)/) {
515 8 ($result{format}, $result{version}) = ('CELLS', 'N/A');
516 8 return \%result;
517 } elsif ($line =~ /^(\.|O|2|3|4|!)/) {
518 0 ($result{format}, $result{version}) = ('CELLS', 'N/A');
519 0 return \%result;
520 };
521 };
522 0 return undef;
523 };
524  
525 sub loadL106 {
526 # Load an initial grid from an array containing
527 # a file in Life 1.06 format.
528 1 ▶ my ($self, $array, @rest) = @_;
Subroutine loadL106: called CC=5 SCAR=16.4
Pod documented
529 1 ▶ if (not defined $array) { return undef; };
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $array) 0 1 1
530 1 foreach my $input (@{ $array }) {
531 325 chomp($input);
532 325 $input =~ s/^\s*(.*?)\s*$/$1/;
533 325 ▶ if ($input eq '') { next; };
Branch: 1 branch
BranchTrueFalseTotal
if ($input eq "") 0 325 325
534 325 ▶ if ($input =~ /^#Life 1.06/) { next; };
Branch: 1 branch
BranchTrueFalseTotal
if ($input =~ /^#Life 1.06/) 1 324 325
535 # Not much checks here. Assume that data are numbers.
536 324 my ($xx, $yy) = split / /, $input, 2;
537 324 $xx += 0;
538 324 $yy += 0;
539 324 $self->updateCell($xx, $yy, 1);
540 };
541 1 $self->{'name'} = '#Life 1.06 file'; # :-)
542 1 return 1;
543 };
544  
545  
546 sub loadL105 {
547 # Load an initial grid from an array containing
548 # a file in Life 1.05 format.
549 1 ▶ my ($self, $array, @rest) = @_;
Subroutine loadL105: called CC=21 SCAR=38.4
Pod documented
550 1 ▶ if (not defined $array) { return undef; };
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $array) 0 1 1
551 1 my $posState = 0;
552 1 my $name = 'Untitled';
553 1 my $rules = '';
554 1 my $dataline = '';
555 1 my @descrArr = ();
556 1 my $ulcx = my $ulcy = 0; # Upper left corner
557 1 my $dlinecnt = 0;
558 1 foreach my $input (@{ $array }) {
559 203 chomp($input);
560 203 ▶ if ($input eq '') { next; };
Branch: 1 branch
BranchTrueFalseTotal
if ($input eq "") 1 202 203
561 202 ▶ if ($input =~ /^#Life 1.05/) {
Branch: 7 branches
BranchTrueFalseTotal
if ($input =~ /^#Life 1.05/) { } 1 201 202
elsif ($input =~ /^#D Name: /) { } 1 200 201
elsif ($input =~ /^#D /) { } 2 198 200
elsif ($input =~ /^#N/) { } 1 197 198
elsif ($input =~ /^#R /) { } 0 197 197
elsif ($input =~ /^#P /) { } 2 195 197
elsif ($input =~ /^(\.|\*)/ and $posState == 1) { } 195 0 195
562 1 next;
563 } elsif ($input =~ /^#D Name: /) {
564 # Out of spec, but it seems that it's used:
565 1 (undef, undef, $name) = split /(^#D Name: )/, $input, 3;
566 1 next;
567 } elsif ($input =~ /^#D /) {
568 2 ▶ if ($dlinecnt >= 22) { return undef }; # Specs.
Branch: 1 branch
BranchTrueFalseTotal
if ($dlinecnt >= 22) 0 2 2
569 2 my (undef, undef, $dline) = split /(^#D )/, $input, 3;
570 2 $dline =~ s/^\s*(.*?)\s*$/$1/;
571 2 ▶ if (length($dline) > 78) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $dline > 78) { } 0 2 2
572 0 print "DEBUG ERROR: D-line too long\n";
573 0 return undef; # Specs.
574 } else {
575 2 push @descrArr, $dline;
576 };
577 2 $dlinecnt++;
578 } elsif ($input =~ /^#N/) {
579 1 next;
580 } elsif ($input =~ /^#R /) {
581 # Rules.
582 0 (undef, undef, $rules) = split /(^#R )/, $input, 3;
583 0 my ($sstr, $bstr) = split /\//, $rules, 2;
584  
585 0 my $barr = [];
586 0 my $sarr = [];
587 0 ▶ for (my $i = 0; $i < length($bstr); $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < length $bstr) 0 0 0
588 0 ▶ if (substr($bstr, $i, 1) =~ /[0-8]/) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($bstr, $i, 1) =~ /[0-8]/) { } 0 0 0
589 0 push @{ $barr }, substr($bstr, $i, 1);
590 } else {
591 # Fail. Specs.
592 0 print "DEBUG ERROR: wrong rules\n";
593 0 return undef;
594 };
595 };
596 0 ▶ for (my $i = 0; $i < length($sstr); $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < length $sstr) 0 0 0
597 0 ▶ if (substr($sstr, $i, 1) =~ /[0-8]/) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($sstr, $i, 1) =~ /[0-8]/) { } 0 0 0
598 0 push @{ $sarr }, substr($sstr, $i, 1);
599 } else {
600 # Fail. Specs.
601 0 print "DEBUG ERROR: wrong rules\n";
602 0 return undef;
603 };
604 };
605 0 $self->setRules([$barr, $sarr]);
606 } elsif ($input =~ /^#P /) {
607 2 (undef, $ulcx, $ulcy) = split / /, $input, 3;
608 2 $ulcx += 0;
609 2 $ulcy += 0;
610 2 $posState = 1;
611 ▶ } elsif (
Condition33%1/3
$input =~ /^(\.|\*)/ and $posState == 1
!ll&&!rl&&r
00195
MC/DC0%0/2
$input =~ /^(\.|\*)/ and $posState == 1
$input =~ /^(\.|\*)/ $posState == 1
Truth table33%1/3
A and B
A= $input =~ /^(\.|\*)/B= $posState == 1
ABresult
0X0
100
111
612 ($input =~/^(\.|\*)/) and
613 ($posState == 1)
614 ) {
615 # Data line:
616 #print "DEBUG: DL: $input\n";
617 195 my $xx = $ulcx;
618 195 my $yy = $ulcy;
619 195 ▶ while (length($input) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $input > 0) 4086 195 4281
620 4086 my $char = substr($input, 0, 1);
621 4086 $input = substr($input, 1);
622 4086 ▶ if ($char eq '*') {
Branch: 2 branches
BranchTrueFalseTotal
if ($char eq '*') { } 324 3762 4086
elsif ($char eq '.') { } 3762 0 3762
623 #print "\tDEBUG: LIVE ($xx,$yy)\n";
624 324 $self->updateCell($xx, $yy, 1);
625 324 $xx++;
626 } elsif ($char eq '.') {
627 3762 $xx++;
628 } else {
629 # Fail.
630 0 print "DEBUG ERROR: unknown char\n";
631 0 return undef;
632 };
633 };
634 195 $ulcy++;
635 } else {
636 # Unrecognized input:
637 0 print "DEBUG ERROR: unknown input:\n\t|$input|\n";
638 0 return undef;
639 };
640 };
641 1 $self->{'description'} = \@descrArr;
642 1 $self->{'name'} = $name;
643 1 return 1;
644 };
645  
646  
647 sub loadRLE {
648 # Load an initial grid from an array containing
649 # a file in RLE format.
650 1 ▶ my ($self, $array, @rest) = @_;
Subroutine loadRLE: called CC=43 SCAR=48.9
Pod documented
651 1 ▶ if (not defined $array) { return undef; };
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $array) 0 1 1
652 1 my $posState = 0;
653 1 my $name = 'Untitled';
654 1 my $headRules = my $rules = '';
655 1 my $dataline = '';
656 1 my $ulcx = my $ulcy = 0; # Upper left corner
657 1 foreach my $input (@{ $array }) {
658 20 chomp($input);
659 20 ▶ if ($input =~ /^#N /) {
Branch: 5 branches
BranchTrueFalseTotal
if ($input =~ /^#N /) { } 1 19 20
elsif ($input =~ /^#r /) { } 0 19 19
elsif ($input =~ /^#P / or $input =~ /^#R /) { } 0 19 19
elsif ($input =~ /^#/) { } 2 17 19
elsif ($input =~ /^x/) { } 1 16 17
660 1 (undef, undef, $name) = split /(^#N )/, $input, 3;
661 1 next;
662 } elsif ($input =~ /^#r /) {
663 # Rules:
664 0 (undef, undef, $headRules) = split /(^#r )/, $input, 3;
665 0 next;
666 ▶ } elsif (($input =~ /^#P /) or ($input =~ /^#R /)) {
Condition33%1/3
$input =~ /^#P / or $input =~ /^#R /
l!l&&r!l&&!r
0019
MC/DC0%0/2
$input =~ /^#P / or $input =~ /^#R /
$input =~ /^#P / $input =~ /^#R /
Truth table33%1/3
A or B
A= $input =~ /^#P /B= $input =~ /^#R /
ABresult
1X1
011
000
667 0 (undef, $ulcx, $ulcy) = split / /, $input, 3;
668 0 next;
669 } elsif ($input =~ /^#/) {
670 # Ignore all other # lines:
671 2 next;
672 } elsif ($input =~ /^x/) {
673 # Header line.
674 1 $input =~ s/ //g;
675 1 my ($xc, $yc);
676 1 ($xc, $yc, $rules) = split /,/ , $input;
677 1 ▶ if ((not ($xc =~ /x=/)) or (not ($yc =~ /y=/))) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($xc =~ /x=/ and $yc =~ /y=/) 0 1 1
Condition33%1/3
$xc =~ /x=/ and $yc =~ /y=/
!ll&&!rl&&r
001
MC/DC0%0/2
$xc =~ /x=/ and $yc =~ /y=/
$xc =~ /x=/ $yc =~ /y=/
Truth table33%1/3
A and B
A= $xc =~ /x=/B= $yc =~ /y=/
ABresult
0X0
100
111
678 # Fail.
679 0 return undef;
680 };
681 1 $posState = 1;
682 1 next;
683 } else {
684 # Normal line:
685 16 ▶ if (not $posState) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($posState) 0 16 16
686 # No header?
687 0 return undef;
688 };
689 # Join in one big string:
690 16 $dataline .= $input;
691 16 next;
692 };
693 };
694 # Now parse actual data:
695 1 my @dataArr = split /\$/, $dataline;
696 1 my %extraStates = ();
697 1 my $extraStatesCnt = 0;
698 1 my $xx = $ulcx;
699 1 my $yy = $ulcy;
700 #print "DEBUG: " . scalar(@dataArr) . " lines\n";
701 1 REC: foreach my $rec (@dataArr) {
702 #print "DEBUG: RLE REC: \n\t$rec\n";
703 #print "\tLINE: $yy\n";
704 81 my $cntstr = '0';
705 81 ▶ CHAR: while (length($rec) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $rec > 0) 994 80 1074
706 994 my $char = substr($rec, 0, 1);
707 994 $rec = substr($rec, 1);
708 994 ▶ if ($char =~ /\d/) {
Branch: 4 branches
BranchTrueFalseTotal
if ($char =~ /\d/) { } 518 476 994
elsif ($char eq 'o') { } 200 276 476
elsif ($char eq 'b') { } 275 1 276
elsif ($char eq '!') { } 1 0 1
709 518 $cntstr .= $char;
710 518 next CHAR;
711 } elsif ($char eq 'o') {
712 200 ▶ my $cnt = ($cntstr eq '0') ? 1 : $cntstr + 0;
Branch: 1 branch
BranchTrueFalseTotal
$cntstr eq '0' ? : 124 76 200
713 200 $cntstr = '0'; # Reset.
714 200 ▶ for (my $i = 0; $i < $cnt; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $cnt) 324 200 524
715 324 $self->updateCell($xx++, $yy, 1);
716 };
717 } elsif ($char eq 'b') {
718 275 ▶ my $cnt = ($cntstr eq '0') ? 1 : $cntstr + 0;
Branch: 1 branch
BranchTrueFalseTotal
$cntstr eq '0' ? : 19 256 275
719 275 $cntstr = '0'; # Reset.
720 275 $xx += $cnt;
721 } elsif ($char eq '!') {
722 # No more!
723 1 last REC;
724 } else {
725 # Handle unknown characters as extra states. Good luck with that.
726 # We will handle a max of 3 extra states (Immigration/Quadlife).
727 0 ▶ my $cnt = ($cntstr eq '0') ? 1 : $cntstr + 0;
Branch: 1 branch
BranchTrueFalseTotal
$cntstr eq '0' ? : 0 0 0
728 0 ▶ if (defined $extraStates{$char}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $extraStates{$char}) { } 0 0 0
729 0 ▶ for (my $i = 0; $i < $cnt; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $cnt) 0 0 0
730 0 $self->updateCell($xx++, $yy, $extraStates{$char});
731 };
732 } else {
733 # New state
734 0 $extraStatesCnt++;
735 0 ▶ if ($extraStatesCnt > 3) {
Branch: 1 branch
BranchTrueFalseTotal
if ($extraStatesCnt > 3) { } 0 0 0
736 # Fail.
737 0 return undef;
738 } else {
739 0 $extraStates{$char} = $extraStatesCnt+1;
740 };
741 0 ▶ if ($extraStatesCnt == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($extraStatesCnt == 1) { } 0 0 0
elsif ($extraStatesCnt > 1) { } 0 0 0
742 0 ▶ $self->setColor('immigration') or die "Failed to set color on load!\n";
Branch: 1 branch
BranchTrueFalseTotal
$self->setColor("immigration") or die "Failed to set color on load!\n" 0 0 0
743 } elsif ($extraStatesCnt > 1) {
744 0 ▶ $self->setColor('quadlife') or die "Failed to set color on load!\n";
Branch: 1 branch
BranchTrueFalseTotal
$self->setColor("quadlife") or die "Failed to set color on load!\n" 0 0 0
745 };
746 0 ▶ for (my $i = 0; $i < $cnt; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $cnt) 0 0 0
747 0 $self->updateCell($xx++, $yy, $extraStates{$char});
748 };
749 };
750 };
751 };
752 # If there are digits left, they are empty lines.
753 80 ▶ my $cnt = ($cntstr eq '0') ? 1 : $cntstr + 0;
Branch: 1 branch
BranchTrueFalseTotal
$cntstr eq '0' ? : 76 4 80
754 80 $cntstr = '0'; # Reset.
755 80 $yy += $cnt; # Next line or skip lines.
756 80 $xx = $ulcx; # Reset
757 };
758 1 $self->{'name'} = $name;
759 1 ▶ if ($extraStatesCnt > 1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($extraStatesCnt > 1) { } 0 1 1
elsif ($extraStatesCnt == 1) { } 0 1 1
760 0 ▶ $self->setColor('quadlife') or die "Failed to set color on load!\n";
Branch: 1 branch
BranchTrueFalseTotal
$self->setColor("quadlife") or die "Failed to set color on load!\n" 0 0 0
761 #print "DEBUG: RLE: Quadlife\n";
762 } elsif ($extraStatesCnt == 1) {
763 0 ▶ $self->setColor('immigration') or die "Failed to set color on load!\n";
Branch: 1 branch
BranchTrueFalseTotal
$self->setColor("immigration") or die "Failed to set color on load!\n" 0 0 0
764 #print "DEBUG: RLE: Immigration\n";
765 };
766 # Handle rules:
767 1 my $bstr = my $sstr = '';
768 1 ▶ if ($rules ne '') {
Branch: 2 branches
BranchTrueFalseTotal
if ($rules ne '') { } 1 0 1
elsif ($headRules ne '') { } 0 0 0
769 # Rules in header line have priority over comment line rules:
770 1 (undef, $rules) = split /=/, $rules, 2;
771 1 ($bstr, $sstr) = split /\//, $rules, 2;
772 } elsif ($headRules ne '') {
773 0 my ($sstr, $bstr) = split /\//, $headRules, 2;
774 };
775 1 ▶ if (($rules ne '') or ($headRules ne '')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rules ne "" or $headRules ne "") 1 0 1
Condition33%1/3
$rules ne "" or $headRules ne ""
l!l&&r!l&&!r
100
MC/DC0%0/2
$rules ne "" or $headRules ne ""
$rules ne "" $headRules ne ""
Truth table33%1/3
A or B
A= $rules ne ""B= $headRules ne ""
ABresult
1X1
011
000
776 1 my $barr = [];
777 1 my $sarr = [];
778 1 ▶ for (my $i = 0; $i < length($bstr); $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < length $bstr) 2 1 3
779 2 ▶ if (substr($bstr, $i, 1) =~ /\d/) { push @{ $barr }, substr($bstr, $i, 1); };
Branch: 1 branch
BranchTrueFalseTotal
if (substr($bstr, $i, 1) =~ /\d/) 1 1 2
780 };
781 1 ▶ for (my $i = 0; $i < length($sstr); $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < length $sstr) 3 1 4
782 3 ▶ if (substr($sstr, $i, 1) =~ /\d/) { push @{ $sarr }, substr($sstr, $i, 1); };
Branch: 1 branch
BranchTrueFalseTotal
if (substr($sstr, $i, 1) =~ /\d/) 2 1 3
783 };
784 1 $self->setRules([$barr, $sarr]);
785 };
786 1 return 1;
787 };
788  
789 sub loadCells {
790 # Load an initial grid from an array containing
791 # a file in cells (ASCII) format.
792 8 ▶ my ($self, $array, @rest) = @_;
Subroutine loadCells: called CC=17 SCAR=28.5
Pod documented
793 8 ▶ if (not defined $array) { return undef; };
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $array) 0 8 8
794 8 my $name = 'Untitled';
795 8 my @descrArr = ();
796 8 my $xx = my $yy = my $cnt = 0;
797 8 my $immigration = my $quadlife = 0;
798 #my $input;
799 #open($input, "<", $fn) or die "cannot open $input: $!\n";
800 #while (<$input>) {
801 8 foreach my $input (@{ $array }) {
802 158 chomp($input);
803 158 ▶ if ($input =~ /^!Name: /) {
Branch: 2 branches
BranchTrueFalseTotal
if ($input =~ /^!Name: /) { } 1 157 158
elsif ($input =~ /^!/) { } 2 155 157
804 1 (undef, undef, $name) = split /(^!Name: )/, $input, 3;
805 1 next;
806 } elsif ($input =~ /^!/) {
807 2 push @descrArr, $input;
808 2 next;
809 };
810 155 ▶ for ($yy = 0; $yy <= length($input); $yy++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($yy <= length $input) 5835 155 5990
811 5835 ▶ if (substr($input, $yy, 1) eq 'O') {
Branch: 4 branches
BranchTrueFalseTotal
if (substr($input, $yy, 1) eq 'O') { } 417 5418 5835
elsif (substr($input, $yy, 1) eq '2') { } 12 5406 5418
elsif (substr($input, $yy, 1) eq '3') { } 9 5397 5406
elsif (substr($input, $yy, 1) eq '4') { } 6 5391 5397
812 417 $self->updateCell($yy, $xx, 1);
813 417 $cnt++;
814 } elsif (substr($input, $yy, 1) eq '2') {
815 12 ▶ $self->setColor('immigration') or die "Failed to set color on load!\n";
Branch: 1 branch
BranchTrueFalseTotal
$self->setColor("immigration") or die "Failed to set color on load!\n" 0 12 12
816 12 $self->updateCell($yy, $xx, 2);
817 12 $immigration = 1;
818 12 $cnt++;
819 } elsif (substr($input, $yy, 1) eq '3') {
820 9 ▶ $self->setColor('quadlife') or die "Failed to set color on load!\n";
Branch: 1 branch
BranchTrueFalseTotal
$self->setColor("quadlife") or die "Failed to set color on load!\n" 0 9 9
821 9 $self->updateCell($yy, $xx, 3);
822 9 $quadlife = 1;
823 9 $cnt++;
824 } elsif (substr($input, $yy, 1) eq '4') {
825 6 ▶ $self->setColor('quadlife') or die "Failed to set color on load!\n";
Branch: 1 branch
BranchTrueFalseTotal
$self->setColor("quadlife") or die "Failed to set color on load!\n" 0 6 6
826 6 $self->updateCell($yy, $xx, 4);
827 6 $quadlife = 1;
828 6 $cnt++;
829 };
830 };
831 155 $xx++;
832 };
833 8 $self->{'name'} = $name;
834 8 $self->{'description'} = \@descrArr;
835 # Finalize board colour:
836 8 ▶ if ($quadlife) {
Branch: 2 branches
BranchTrueFalseTotal
if ($quadlife) { } 2 6 8
elsif ($immigration) { } 1 5 6
837 2 ▶ $self->setColor('quadlife') or die "Failed to set color on load!\n";
Branch: 1 branch
BranchTrueFalseTotal
$self->setColor("quadlife") or die "Failed to set color on load!\n" 0 2 2
838 } elsif ($immigration) {
839 1 ▶ $self->setColor('immigration') or die "Failed to set color on load!\n";
Branch: 1 branch
BranchTrueFalseTotal
$self->setColor("immigration") or die "Failed to set color on load!\n" 0 1 1
840 };
841 8 return 1;
842 };
843  
844 9 42;
845  
846 __END__
↑ Summary