| 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
Condition0%0/3
$options{'height'} || $options{'size'}
MC/DC0%0/2
$options{'height'} || $options{'size'}
$options{'height'}
$options{'size'}
Truth table0%0/3
A || B
A= $options{'height'}B= $options{'size'}
| |||||||||||||||||||||||||||||
| 13 | 0 | ▶ | my $width = $options{width} || $options{size} or die "illegal width"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$options{'width'} || $options{'size'}
MC/DC0%0/2
$options{'width'} || $options{'size'}
$options{'width'}
$options{'size'}
Truth table0%0/3
A || B
A= $options{'width'}B= $options{'size'}
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 0 | my $clue = shift @clues; | |||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 0 | ▶ | die "clues mismatch" unless ref $clue; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 0 | $clue->set( split ',', $line ); | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 0 | ▶ | die "clues mismatch" if @clues; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
Condition0%0/3
$clue->is_done and not $clue->line->is_dirty
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
| |||||||||||||||||||||||||||||
| 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
Condition0%0/3
$clue->is_done and not $clue->line->is_dirty
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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 142 | 0 | ▶ | return if $self->is_done; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 0 | ▶ | unless ( $mode ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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{$_}++
MC/DC0%0/2
defined $_ && !$seen{$_}++
defined $_
!$seen{$_}++
Truth table0%0/3
A && not(B)
A= defined $_B= !$seen{$_}++
| |||||||||||||||||||||||||||||
| 164 | 0 | ▶ | @{ $self->stash->{answers} || [] }; | ||||||||||||||||||||||||||
|
Condition0%0/2
$self->stash->{'answers'} || []
MC/DC0%0/1
$self->stash->{'answers'} || []
$self->stash->{'answers'}
Truth table0%0/2
A
A= $self->stash->{'answers'}
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||
| 173 | 0 | ▶ | unless ( $self->{has_answers} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 174 | 0 | $self->{has_answers} = 1; | |||||||||||||||||||||||||||
| 175 | 0 | ▶ | push @{ $self->stash->{answers} ||= [] }, $self->as_string; | ||||||||||||||||||||||||||
|
Condition0%0/2
$self->stash->{"answers"} ||= []
MC/DC0%0/1
$self->stash->{"answers"} ||= []
$self->stash->{"answers"}
Truth table0%0/2
A
A= $self->stash->{"answers"}
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 192 | 0 | $after = $clue->dump_blocks; | |||||||||||||||||||||||||||
| 193 | 0 | ▶ | $self->log( "TO: \n$after" ) if $before ne $after; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | 0 | ▶ | $self->log( $clue->dump_blocks ) if $self->debug; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | 0 | $clue->value( $id, $value ); | |||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | 0 | ▶ | $self->log( "TO:\n", $clue->dump_blocks ) if $self->debug; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 207 | } | ||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | 2 | 1; | |||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | __END__ | ||||||||||||||||||||||||||||