Nonogram/BruteForce.pm

filter: Statementstmt 18.5% 10 / 54 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 50.0% 3 / 6 Podpod 100% 1 / 1 totaltotal 15.5% 14 / 90 CC 16 SCAR 49.5
CC
16
Cov
13%
run
210.0
_freeze
6.0
_thaw
6.0
CRAP 141.3
↑ Summary Nonogram/CommandLine.pm »
1 package Games::Nonogram::BruteForce;
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 Storable ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6
7 0 ▶sub run {
Subroutine run: not called CC=14 SCAR=53.5
Pod documented
8 0 my ($class, $grid) = @_;
9
10 0 my $data = _freeze( $grid );
11
12 0 ▶ return if $grid->stash->{bruteforce}->{$data};
Branch: 1 branch
BranchTrueFalseTotal
if $grid->stash->{"bruteforce"}{$data} 0 0 0
13
14 0 $grid->stash->{bruteforce}->{$data} = 1;
15
16 0 $grid->log( "Start brute forcing" );
17
18 0 my ($target, $free);
19 0 foreach my $clue ( $grid->clues ) {
20 0 ▶ next if $clue->is_done;
Branch: 1 branch
BranchTrueFalseTotal
if $clue->is_done 0 0 0
21 0 ▶ if ( !$free or $free > $clue->{free} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $free or $free > $clue->{"free"}) 0 0 0
Condition0%0/3
not $free or $free > $clue->{"free"}
l!l&&r!l&&!r
000
MC/DC0%0/2
not $free or $free > $clue->{"free"}
not $free $free > $clue->{"free"}
Truth table0%0/3
not(A) or B
A= not $freeB= $free > $clue->{"free"}
ABresult
1X1
011
000
22 0 $free = $clue->{free};
23 0 $target = $clue;
24 }
25 }
26 0 ▶ return unless $target; # should be solved already
Branch: 1 branch
BranchTrueFalseTotal
unless $target 0 0 0
27
28 0 $grid->log( "target: ", $target->id );
29
30 0 my @candidates = $target->candidates;
31
32 0 foreach my $candidate ( @candidates ) {
33 0 $target->line->as_vec( $candidate );
34 0 $grid->update;
35
36 0 eval {
37 0 my $str = '';
38 0 my $prev = $grid->as_string;
39 0 ▶ while( $str ne $prev and !$grid->is_done ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str ne $prev and not $grid->is_done) 0 0 0
Condition0%0/3
$str ne $prev and not $grid->is_done
!ll&&!rl&&r
000
MC/DC0%0/2
$str ne $prev and not $grid->is_done
$str ne $prev not $grid->is_done
Truth table0%0/3
A and not(B)
A= $str ne $prevB= not $grid->is_done
ABresult
0X0
100
111
40 0 $grid->update;
41 0 $prev = $str;
42 0 $str = $grid->as_string;
43 0 ▶ $grid->log( $str ) if $grid->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $grid->debug 0 0 0
44 }
45 0 ▶ if ( $grid->is_done ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($grid->is_done) { } 0 0 0
46 0 $grid->log( "Found an answer" );
47 0 ▶ push @{ $grid->stash->{answers} ||= [] }, $str;
Condition0%0/2
$grid->stash->{"answers"} ||= []
l!l
00
MC/DC0%0/1
$grid->stash->{"answers"} ||= []
$grid->stash->{"answers"}
Truth table0%0/2
A
A= $grid->stash->{"answers"}
Aresult
11
00
48 }
49 else {
50 0 $class->run( $grid );
51 }
52 };
53 0 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
54 0 $grid->log( "Skip" );
55 }
56
57 0 _thaw( $grid, $data );
58 }
59 }
60
61 0 ▶sub _freeze {
Subroutine _freeze: not called CC=2 SCAR=17.9
62 0 my $grid = shift;
63
64 0 my @data;
65 0 foreach my $clue ( $grid->clues ) {
66 0 push @data, $clue->line->as_vec;
67 }
68 0 return Storable::freeze( \@data );
69 }
70
71 0 ▶sub _thaw {
Subroutine _thaw: not called CC=2 SCAR=17.9
72 0 my ($grid, $data) = @_;
73
74 0 my @data = @{ Storable::thaw( $data ) };
75
76 0 foreach my $clue ( $grid->clues ) {
77 0 $clue->reset_blocks;
78 0 $clue->line->as_vec( shift @data );
79 }
80 }
81
82 2 1;
83
84 __END__
↑ Summary Nonogram/CommandLine.pm »