Nonogram/Grid.pm

filter: Statementstmt 11.1% 13 / 117 Branchbran 0.0% 0 / 48 Conditioncond 0.0% 0 / 19 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 20.0% 4 / 20 Podpod 100% 14 / 14 totaltotal 13.4% 31 / 230 CC 40 SCAR 41.6
CC
40
Cov
7%
update
210.0
new
30.0
load
30.0
CRAP 64.0
« Nonogram/CommandLine.pm ↑ Summary Nonogram/Shell.pm »
1 package Games::Nonogram::Grid;
2
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use base qw( Games::Nonogram::Base );
Subroutine BEGIN: called CC=1 SCAR=0.0
6
7 2 ▶use Games::Nonogram::Clue;
Subroutine BEGIN: called CC=1 SCAR=0.0
8
9 0 ▶sub new {
Subroutine new: not called CC=5 SCAR=34.0
Pod documented
10 0 my ($class, %options) = @_;
11
12 0 ▶ my $height = $options{height} || $options{size} or die "illegal height";
Branch: 1 branch
BranchTrueFalseTotal
my $height = $options{"height"} || $options{"size"} or die "illegal height" 0 0 0
Condition0%0/3
$options{'height'} || $options{'size'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$options{'height'} || $options{'size'}
$options{'height'} $options{'size'}
Truth table0%0/3
A || B
A= $options{'height'}B= $options{'size'}
ABresult
1X1
011
000
13 0 ▶ my $width = $options{width} || $options{size} or die "illegal width";
Branch: 1 branch
BranchTrueFalseTotal
my $width = $options{"width"} || $options{"size"} or die "illegal width" 0 0 0
Condition0%0/3
$options{'width'} || $options{'size'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$options{'width'} || $options{'size'}
$options{'width'} $options{'size'}
Truth table0%0/3
A || B
A= $options{'width'}B= $options{'size'}
ABresult
1X1
011
000
14
15 my @rows = map {
16 0 Games::Nonogram::Clue->new( id => "Row $_", size => $width )
17 } ( 1 .. $height );
18 my @cols = map {
19 0 Games::Nonogram::Clue->new( id => "Col $_", size => $height )
20 } ( 1 .. $width );
21
22 0 my $self = bless {
23 height => $height,
24 width => $width,
25 rows => \@rows,
26 cols => \@cols,
27 has_answers => 0,
28 is_dirty => 1,
29 }, $class;
30 }
31
32 0 ▶sub new_from {
Subroutine new_from: not called CC=2 SCAR=17.9
Pod documented
33 0 my ($class, $loader, @args) = @_;
34
35 0 $loader = ucfirst $loader;
36
37 0 my $pkg = "Games::Nonogram::Loader::$loader";
38 0 eval qq{ require $pkg };
39 0 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
40
41 0 my ($height, $width, @lines) = $pkg->load( @args );
42
43 0 my $self = $class->new( height => $height, width => $width );
44
45 0 $self->load( @lines );
46
47 0 $self;
48 }
49
50 0 ▶sub load {
Subroutine load: not called CC=5 SCAR=34.0
Pod documented
51 0 my ($self, @lines) = @_;
52
53 0 my @clues = $self->clues;
54
55 0 foreach my $line ( @lines ) {
56 0 chomp $line;
57 0 ▶ next unless $line =~ /^[\d,]+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /^[\d,]+$/ 0 0 0
58
59 0 my $clue = shift @clues;
60
61 0 ▶ die "clues mismatch" unless ref $clue;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $clue 0 0 0
62
63 0 $clue->set( split ',', $line );
64 }
65
66 0 ▶ die "clues mismatch" if @clues;
Branch: 1 branch
BranchTrueFalseTotal
if @clues 0 0 0
67
68 0 $self->clear_stash;
69 0 $self->is_dirty( 1 );
70 0 $self->{has_answers} = 0;
71 }
72
73 0 ▶sub rows { @{ shift->{rows} } }
Subroutine rows: not called CC=1 SCAR=6.9
Pod documented
74 0 ▶sub cols { @{ shift->{cols} } }
Subroutine cols: not called CC=1 SCAR=6.9
Pod documented
75 0 ▶sub clues { my $self = shift; return ( $self->rows, $self->cols ) }
Subroutine clues: not called CC=1 SCAR=6.9
Pod documented
76 0 ▶sub row { my ($self, $id) = @_; $self->{rows}->[$id - 1]; }
Subroutine row: not called CC=1 SCAR=6.9
Pod documented
77 0 ▶sub col { my ($self, $id) = @_; $self->{cols}->[$id - 1]; }
Subroutine col: not called CC=1 SCAR=6.9
Pod documented
78
79 0 ▶sub is_dirty {
Subroutine is_dirty: not called CC=2 SCAR=17.9
Pod documented
80 0 my $self = shift;
81 0 ▶ @_ ? $self->{is_dirty} = shift : $self->{is_dirty};
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
82 }
83
84 0 ▶sub as_string {
Subroutine as_string: not called CC=5 SCAR=34.0
Pod documented
85 0 my $self = shift;
86
87 0 my $str = '';
88 0 foreach my $row ( $self->rows ) {
89 0 $str .= sprintf "%s\n", $row->as_string;
90 }
91 0 ▶ if ( $self->debug ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->debug) 0 0 0
92 0 $str .= "\n";
93
94 0 foreach my $col ( $self->cols ) {
95 0 $str .= sprintf "%s\n", $col->as_string;
96 }
97 }
98
99 0 ▶ defined wantarray ? return $str : print $str;
Branch: 1 branch
BranchTrueFalseTotal
defined wantarray ? : 0 0 0
100 }
101
102 0 ▶sub update {
Subroutine update: not called CC=14 SCAR=53.5
Pod documented
103 0 my ($self, $mode) = @_;
104
105 0 $self->log( 'updating' );
106
107 0 $self->is_dirty( 0 );
108 0 foreach my $row ( 1 .. $self->{height} ) {
109 0 my $clue = $self->row( $row );
110
111 0 ▶ next if $clue->is_done && !$clue->line->is_dirty;
Branch: 1 branch
BranchTrueFalseTotal
if $clue->is_done and not $clue->line->is_dirty 0 0 0
Condition0%0/3
$clue->is_done and not $clue->line->is_dirty
!ll&&!rl&&r
000
MC/DC0%0/2
$clue->is_done and not $clue->line->is_dirty
$clue->is_done not $clue->line->is_dirty
Truth table0%0/3
A and not(B)
A= $clue->is_doneB= not $clue->line->is_dirty
ABresult
0X0
100
111
112
113 0 $self->_update( $clue, $mode );
114
115 0 foreach my $col ( $clue->line->dirty_items ) {
116 0 $self->is_dirty( 1 );
117 0 $self->_update_dirty_item(
118 $self->col( $col ),
119 $row,
120 $clue->line->value( $col )
121 );
122 }
123 }
124
125 0 foreach my $col ( 1 .. $self->{width} ) {
126 0 my $clue = $self->col( $col );
127
128 0 ▶ next if $clue->is_done && !$clue->line->is_dirty;
Branch: 1 branch
BranchTrueFalseTotal
if $clue->is_done and not $clue->line->is_dirty 0 0 0
Condition0%0/3
$clue->is_done and not $clue->line->is_dirty
!ll&&!rl&&r
000
MC/DC0%0/2
$clue->is_done and not $clue->line->is_dirty
$clue->is_done not $clue->line->is_dirty
Truth table0%0/3
A and not(B)
A= $clue->is_doneB= not $clue->line->is_dirty
ABresult
0X0
100
111
129
130 0 $self->_update( $clue, $mode );
131
132 0 foreach my $row ( $clue->line->dirty_items ) {
133 0 $self->is_dirty( 1 );
134 0 $self->_update_dirty_item(
135 $self->row( $row ),
136 $col,
137 $clue->line->value( $row )
138 );
139 }
140 }
141 0 ▶ return if $self->is_dirty;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_dirty 0 0 0
142 0 ▶ return if $self->is_done;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_done 0 0 0
143
144 0 ▶ unless ( $mode ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($mode) { } 0 0 0
if (do { $mode eq "more" }) 0 0 0
145 0 $self->update( 'more' );
146 }
147 0 elsif ( $mode eq 'more' ) {
148 0 require Games::Nonogram::BruteForce;
149 0 Games::Nonogram::BruteForce->run( $self );
150
151 0 ▶ if ( $self->answers ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->answers) 0 0 0
152 0 $self->{has_answers} = 1;
153 }
154 }
155 }
156
157 0 ▶sub has_answers { shift->{has_answers} }
Subroutine has_answers: not called CC=1 SCAR=6.9
Pod documented
158
159 0 ▶sub answers {
Subroutine answers: not called CC=3 SCAR=24.8
Pod documented
160 0 my $self = shift;
161
162 0 my %seen;
163 0 ▶ my @answers = grep { defined && !$seen{$_}++ }
Condition0%0/3
defined $_ && !$seen{$_}++
!ll&&!rl&&r
000
MC/DC0%0/2
defined $_ && !$seen{$_}++
defined $_ !$seen{$_}++
Truth table0%0/3
A && not(B)
A= defined $_B= !$seen{$_}++
ABresult
0X0
100
111
164 0 ▶ @{ $self->stash->{answers} || [] };
Condition0%0/2
$self->stash->{'answers'} || []
l!l
00
MC/DC0%0/1
$self->stash->{'answers'} || []
$self->stash->{'answers'}
Truth table0%0/2
A
A= $self->stash->{'answers'}
Aresult
11
00
165 }
166
167 0 ▶sub is_done {
Subroutine is_done: not called CC=5 SCAR=34.0
Pod documented
168 0 my $self = shift;
169
170 0 foreach my $clue ( $self->clues ) {
171 0 ▶ return unless $clue->is_done;
Branch: 1 branch
BranchTrueFalseTotal
unless $clue->is_done 0 0 0
172 }
173 0 ▶ unless ( $self->{has_answers} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"has_answers"}) 0 0 0
174 0 $self->{has_answers} = 1;
175 0 ▶ push @{ $self->stash->{answers} ||= [] }, $self->as_string;
Condition0%0/2
$self->stash->{"answers"} ||= []
l!l
00
MC/DC0%0/1
$self->stash->{"answers"} ||= []
$self->stash->{"answers"}
Truth table0%0/2
A
A= $self->stash->{"answers"}
Aresult
11
00
176 }
177 0 return 1;
178 }
179
180 0 ▶sub _update {
Subroutine _update: not called CC=4 SCAR=30.0
181 0 my ($self, $clue, $mode) = @_;
182
183 0 my ($before, $after);
184 0 ▶ if ( $self->debug ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->debug) 0 0 0
185 0 $before = $clue->dump_blocks;
186 0 $self->log( $before );
187 }
188
189 0 $clue->update( $mode );
190
191 0 ▶ if ( $self->debug ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->debug) 0 0 0
192 0 $after = $clue->dump_blocks;
193 0 ▶ $self->log( "TO: \n$after" ) if $before ne $after;
Branch: 1 branch
BranchTrueFalseTotal
if $before ne $after 0 0 0
194 }
195 }
196
197 0 ▶sub _update_dirty_item {
Subroutine _update_dirty_item: not called CC=4 SCAR=30.0
198 0 my ($self, $clue, $id, $value) = @_;
199
200 0 ▶ return unless $clue->value( $id ) != $value;
Branch: 1 branch
BranchTrueFalseTotal
unless $clue->value($id) != $value 0 0 0
201
202 0 ▶ $self->log( $clue->dump_blocks ) if $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 0 0
203
204 0 $clue->value( $id, $value );
205
206 0 ▶ $self->log( "TO:\n", $clue->dump_blocks ) if $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 0 0
207 }
208
209 2 1;
210
211 __END__
« Nonogram/CommandLine.pm ↑ Summary Nonogram/Shell.pm »