Component/Controller.pm

filter: Statementstmt 79.8% 91 / 114 Branchbran 52.9% 18 / 34 Conditioncond 40.0% 6 / 15 MC/DCmcdc 10.0% 1 / 10 Subroutinesub 90.4% 19 / 21 Podpod 100% 14 / 14 totaltotal 71.6% 149 / 208 CC 26 SCAR 24.1
CC
26
Cov
71%
next
37.1
new
6.6
make_blank
5.1
CRAP 11.1
« Component/Controller/Loader.pm ↑ Summary Component/Base.pm »
1 package Games::Sudoku::Component::Controller;
2 {
3 3 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶ use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6
7 3 our $VERSION = '0.02';
8
9 3 ▶ use Games::Sudoku::Component::Table;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶ use Games::Sudoku::Component::Table::Item;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶ use Games::Sudoku::Component::Controller::History;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶ use Games::Sudoku::Component::Controller::Status;
Subroutine BEGIN: called CC=1 SCAR=0.0
13
14 6 ▶ sub new {
Subroutine new: called CC=5 SCAR=18.9
Pod documented
15 6 my $class = shift;
16 6 ▶ my $this = bless {}, (ref $class || $class);
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
060
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
17
18 0 ▶ my %options = ref $_[0] ? %{ $_[0] } : @_;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 6 6
19
20 $this->{table} = Games::Sudoku::Component::Table->new(
21 size => $options{size},
22 block_width => $options{block_width},
23 block_height => $options{block_height},
24 6 );
25
26 6 my $size = $this->{table}->size;
27
28 $this->{status} = Games::Sudoku::Component::Controller::Status->new(
29 rewind_max => $options{rewind_max} || $size,
30 6 ▶ retry_max => $options{retry_max} || int(sqrt($size)),
Condition33%2/6
$options{'rewind_max'} || $size
l!l&&r!l&&!r
060
$options{'retry_max'} || int sqrt $size
l!l&&r!l&&!r
060
MC/DC0%0/2
$options{'rewind_max'} || $size
$options{'rewind_max'} $size
MC/DC0%0/2
$options{'retry_max'} || int sqrt $size
$options{'retry_max'} int sqrt $size
Truth table33%1/3
A || B
A= $options{'rewind_max'}B= $size
ABresult
1X1
011
000
Truth table33%1/3
A || B
A= $options{'retry_max'}B= int sqrt $size
ABresult
1X1
011
000
31 );
32 6 $this->{history} = Games::Sudoku::Component::Controller::History->new;
33
34 6 $this;
35 }
36
37 16 ▶ sub table { $_[0]->{table}; }
Subroutine table: called CC=1 SCAR=0.0
Pod documented
38 1 ▶ sub history { $_[0]->{history}; }
Subroutine history: called CC=1 SCAR=0.0
Pod documented
39 1 ▶ sub status { $_[0]->{status}; }
Subroutine status: called CC=1 SCAR=0.0
Pod documented
40
41 1 ▶ sub clear {
Subroutine clear: called CC=1 SCAR=0.0
Pod documented
42 1 my $this = shift;
43
44 1 $this->{table}->clear;
45 1 $this->{history}->clear;
46 1 $this->{status}->clear;
47 }
48
49 1 ▶ sub load {
Subroutine load: called CC=3 SCAR=11.0
Pod documented
50 1 my $this = shift;
51
52 1 require Games::Sudoku::Component::Controller::Loader;
53 1 my @cells = Games::Sudoku::Component::Controller::Loader->load(@_);
54
55 1 my $table = $this->{table};
56 1 foreach my $item (@cells) {
57 36 $table->cell($item->row,$item->col)->value($item->value);
58 36 ▶ $table->cell($item->row,$item->col)->lock if $item->value;
Branch: 1 branch
BranchTrueFalseTotal
if $item->value 6 30 36
59 }
60 1 $this->{history}->clear;
61 }
62
63 0 ▶ sub set {
Subroutine set: not called CC=1 SCAR=6.9
Pod documented
64 0 my ($this, $row, $col, $value) = @_;
65
66 0 my $table = $this->{table};
67 0 my @allowed = $table->cell($row,$col)->allowed;
68
69 0 $table->cell($row,$col)->value($value);
70
71 0 $table->check_tmpvalue($row,$col);
72
73 0 my $item = Games::Sudoku::Component::Table::Item->new(
74 row => $row,
75 col => $col,
76 allowed => \@allowed,
77 value => $value,
78 );
79
80 0 $this->{history}->push($item);
81 }
82
83 317 ▶ sub find_and_set {
Subroutine find_and_set: called CC=3 SCAR=11.0
Pod documented
84 317 my ($this, $item) = @_;
85
86 317 my $table = $this->{table};
87
88 317 ▶ $item ||= $table->find_next;
Condition66%2/3
$item ||= $table->find_next
l!l&&r!l&&!r
03107
MC/DC50%1/2
$item ||= $table->find_next
$item $table->find_next
Truth table66%2/3
A ||= B
A= $itemB= $table->find_next
ABresult
1X1
011
000
89
90 317 ▶ return 0 unless defined $item;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $item 7 310 317
91
92 310 $table->cell($item->row,$item->col)->value($item->random_value);
93
94 310 $this->{history}->push($item);
95
96 310 return $item;
97 }
98
99 0 ▶ sub find_hints { $_[0]->{table}->find_all; }
Subroutine find_hints: not called CC=1 SCAR=6.9
Pod documented
100
101 317 ▶ sub next {
Subroutine next: called CC=11 SCAR=36.1
Pod documented
102 317 my $this = shift;
103
104 317 my $status = $this->{status};
105
106 317 ▶ $status->turn_to_ok if $status->is_null;
Branch: 1 branch
BranchTrueFalseTotal
if $status->is_null 7 310 317
107
108 317 my $result;
109 317 ▶ if ($status->is_ok) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status->is_ok) 317 0 317
110 317 $result = $this->find_and_set;
111
112 317 ▶ unless ($result) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($result) 7 310 317
113 7 ▶ if ($this->{table}->is_finished) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{'table'}->is_finished) { } 7 0 7
114 7 $status->turn_to_solved;
115 }
116 else {
117 0 ▶ if ($status->can_rewind) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status->can_rewind) { } 0 0 0
118 0 $status->turn_to_rewind;
119 }
120 else {
121 0 ▶ if ($status->can_retry) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status->can_retry) { } 0 0 0
122 0 $this->rewind_all;
123 0 $status->turn_to_ok;
124 }
125 else {
126 0 $status->turn_to_giveup;
127 }
128 }
129 }
130 }
131 }
132 317 ▶ if ($status->is_rewind) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status->is_rewind) 0 317 317
133 0 $result = $this->rewind;
134
135 0 ▶ if ($result) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result) { } 0 0 0
136 0 ▶ if ($result->allowed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result->allowed) 0 0 0
137 0 $this->find_and_set($result);
138 0 $status->turn_to_ok;
139 }
140 }
141 else {
142 0 ▶ if ($status->can_retry) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status->can_retry) { } 0 0 0
143 0 $status->turn_to_ok;
144 }
145 else {
146 0 $status->turn_to_giveup;
147 }
148 }
149 }
150 317 return $result;
151 }
152
153 7 ▶ sub solve {
Subroutine solve: called CC=2 SCAR=6.9
Pod documented
154 7 my $this = shift;
155
156 7 $this->{status}->clear;
157
158 7 ▶ until($this->{status}->is_finished) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($this->{"status"}->is_finished) 317 7 324
159 317 $this->next;
160 }
161 }
162
163 2 ▶ sub make_blank {
Subroutine make_blank: called CC=5 SCAR=16.2
Pod documented
164 2 my ($this, $count) = @_;
165
166 2 my $table = $this->{table};
167 2 my $size = $table->size;
168
169 2 ▶ croak "Invalid count: $count"
Branch: 1 branch
BranchTrueFalseTotal
if $count > $size ** 2 or $count < 1 0 2 2
Condition33%1/3
$count > $size ** 2 or $count < 1
l!l&&r!l&&!r
002
MC/DC0%0/2
$count > $size ** 2 or $count < 1
$count > $size ** 2 $count < 1
Truth table33%1/3
A or B
A= $count > $size ** 2B= $count < 1
ABresult
1X1
011
000
170 if $count > ($size ** 2) || $count < 1;
171
172 2 $table->lock_all;
173
174 2 foreach my $id (1..$count) {
175 57 my $row = int(rand($size)) + 1;
176 57 my $col = int(rand($size)) + 1;
177 57 ▶ redo unless $table->cell($row,$col)->value;
Branch: 1 branch
BranchTrueFalseTotal
unless $table->cell($row, $col)->value 17 40 57
178 40 $table->cell($row,$col)->unlock;
179 40 my $prev = $table->cell($row,$col)->value;
180 40 $table->cell($row,$col)->value(0);
181 }
182
183 2 $this->{status}->clear;
184 2 $this->{history}->clear;
185 }
186
187 56 ▶ sub rewind {
Subroutine rewind: called CC=2 SCAR=7.0
Pod documented
188 56 my $this = shift;
189
190 56 my $table = $this->{table};
191 56 my $item = $this->{history}->pop;
192
193 56 ▶ $table->cell($item->row,$item->col)->value(0) if defined $item;
Branch: 1 branch
BranchTrueFalseTotal
if defined $item 56 0 56
194
195 56 $item;
196 }
197
198 2 ▶ sub rewind_all {
Subroutine rewind_all: called CC=2 SCAR=6.9
Pod documented
199 2 my $this = shift;
200
201 2 foreach (1..$this->{history}->count) {
202 55 $this->rewind;
203 }
204 }
205 }
206
207 3 1;
208 __END__
« Component/Controller/Loader.pm ↑ Summary Component/Base.pm »