Component/Table.pm

filter: Statementstmt 90.7% 117 / 129 Branchbran 59.5% 25 / 42 Conditioncond 50.0% 4 / 8 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 90.4% 19 / 21 Podpod 100% 12 / 12 totaltotal 81.9% 177 / 216 CC 40 SCAR 18.6
CC
40
Cov
82%
as_HTML
15.8
num_of_finished
12.0
as_string
6.2
CRAP 6.4
« Component/Result.pm ↑ Summary Component/Table/Cell.pm »
1 package Games::Sudoku::Component::Table;
2 {
3 4 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶ use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6
7 4 our $VERSION = '0.02';
8
9 4 ▶ use base qw/Games::Sudoku::Component::Base/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶ use Games::Sudoku::Component::Table::Cell;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶ use Games::Sudoku::Component::Table::Item;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶ use Games::Sudoku::Component::Table::Permission;
Subroutine BEGIN: called CC=1 SCAR=0.0
13
14 9 ▶ sub _initialize {
Subroutine _initialize: called CC=3 SCAR=11.0
15 9 my ($this, %options) = @_;
16
17 9 my $size = $this->{size};
18 9 my $block_width = $this->{block_width};
19 9 my $block_height = $this->{block_height};
20
21 9 my $perm = Games::Sudoku::Component::Table::Permission->new(
22 block_width => $block_width,
23 block_height => $block_height,
24 );
25
26 9 foreach my $row (1..$size) {
27 93 foreach my $col (1..$size) {
28 1119 my $cell_id = $this->_cell_id($row, $col);
29 1119 $this->{cells}->[$cell_id] =
30 Games::Sudoku::Component::Table::Cell->new(
31 row => $row,
32 col => $col,
33 block_width => $block_width,
34 block_height => $block_height,
35 perm => $perm,
36 );
37 }
38 }
39 }
40
41 336 ▶ sub cells {
Subroutine cells: called CC=1 SCAR=0.0
Pod documented
42 336 my $this = shift;
43
44 336 my @cells = @{ $this->{cells} };
45 }
46
47 1034 ▶ sub cell {
Subroutine cell: called CC=1 SCAR=0.0
Pod documented
48 1034 my ($this, $row, $col) = @_;
49
50 1034 my $cell_id = $this->_cell_id($row, $col);
51
52 1034 $this->{cells}->[$cell_id];
53 }
54
55 2153 ▶ sub _cell_id {
Subroutine _cell_id: called CC=3 SCAR=11.4
56 2153 my ($this, $row, $col) = @_;
57
58 2153 ▶ croak "Invalid row: $row" unless $this->_check($row);
Branch: 1 branch
BranchTrueFalseTotal
unless $this->_check($row) 0 2153 2153
59 2153 ▶ croak "Invalid col: $col" unless $this->_check($col);
Branch: 1 branch
BranchTrueFalseTotal
unless $this->_check($col) 0 2153 2153
60
61 2153 ($row - 1) * $this->{size} + ($col - 1);
62 }
63
64 2 ▶ sub clear {
Subroutine clear: called CC=2 SCAR=6.9
Pod documented
65 2 my $this = shift;
66
67 2 foreach my $cell ($this->cells) {
68 117 $cell->unlock;
69 117 $cell->value(0);
70 }
71 }
72
73 318 ▶ sub find_next {
Subroutine find_next: called CC=6 SCAR=17.9
Pod documented
74 318 my $this = shift;
75
76 318 my $size = $this->{size};
77 318 my $min = $size + 1;
78 318 my $next = undef;
79 318 foreach my $cell ($this->cells) {
80 16073 ▶ my @allowed = $cell->allowed or next;
Branch: 1 branch
BranchTrueFalseTotal
my(@allowed) = $cell->allowed or next 6816 9257 16073
81
82 6816 ▶ if (scalar @allowed < $min) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @allowed < $min) 434 6382 6816
83 434 $min = scalar @allowed;
84 434 $next = {
85 row => $cell->row,
86 col => $cell->col,
87 allowed => \@allowed,
88 };
89 }
90 6816 ▶ last if $min == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $min == 1 155 6661 6816
91 }
92 318 ▶ $next ? Games::Sudoku::Component::Table::Item->new($next) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$next ? : 311 7 318
93 }
94
95 1 ▶ sub find_all {
Subroutine find_all: called CC=5 SCAR=16.2
Pod documented
96 1 my $this = shift;
97
98 1 my $size = $this->{size};
99 1 my $min = $size + 1;
100 1 my @all = ();
101 1 foreach my $cell ($this->cells) {
102 81 ▶ my @allowed = $cell->allowed or next;
Branch: 1 branch
BranchTrueFalseTotal
my(@allowed) = $cell->allowed or next 81 0 81
103 81 ▶ if (scalar @allowed < $min) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @allowed < $min) 1 80 81
104 1 @all = ();
105 1 $min = scalar @allowed;
106 }
107 81 ▶ if (scalar @allowed == $min) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @allowed == $min) 81 0 81
108 81 push @all, Games::Sudoku::Component::Table::Item->new(
109 row => $cell->row,
110 col => $cell->col,
111 allowed => \@allowed,
112 );
113 }
114 }
115 1 @all;
116 }
117
118 2 ▶ sub lock_all {
Subroutine lock_all: called CC=3 SCAR=11.2
Pod documented
119 2 my $this = shift;
120
121 2 foreach my $cell ($this->cells) {
122 72 ▶ $cell->lock if $cell->realvalue;
Branch: 1 branch
BranchTrueFalseTotal
if $cell->realvalue 72 0 72
123 }
124 }
125
126 0 ▶ sub unlock_all {
Subroutine unlock_all: not called CC=2 SCAR=17.9
Pod documented
127 0 my $this = shift;
128
129 0 foreach my $cell ($this->cells) {
130 0 $cell->unlock;
131 }
132 }
133
134 2 ▶ sub check_tmpvalue {
Subroutine check_tmpvalue: called CC=4 SCAR=13.9
Pod documented
135 2 my $this = shift;
136
137 2 my $size = $this->{size};
138
139 2 foreach my $cell ($this->cells) {
140 162 ▶ my $tmpvalue = $cell->tmpvalue or next;
Branch: 1 branch
BranchTrueFalseTotal
my $tmpvalue = $cell->tmpvalue or next 2 160 162
141 2 ▶ if ($cell->is_allowed($tmpvalue)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cell->is_allowed($tmpvalue)) 2 0 2
142 2 $cell->value($tmpvalue);
143 }
144 }
145 }
146
147 0 ▶ sub num_of_finished {
Subroutine num_of_finished: not called CC=3 SCAR=24.8
Pod documented
148 0 my $this = shift;
149
150 0 my $sum = 0;
151 0 foreach my $cell ($this->cells) {
152 0 ▶ $sum++ if $cell->realvalue;
Branch: 1 branch
BranchTrueFalseTotal
if $cell->realvalue 0 0 0
153 }
154 0 $sum;
155 }
156
157 8 ▶ sub is_finished {
Subroutine is_finished: called CC=3 SCAR=11.0
Pod documented
158 8 my $this = shift;
159
160 8 foreach my $cell ($this->cells) {
161 343 ▶ return 0 unless $cell->realvalue;
Branch: 1 branch
BranchTrueFalseTotal
unless $cell->realvalue 1 342 343
162 }
163 7 return 1;
164 }
165
166 6 ▶ sub as_string {
Subroutine as_string: called CC=6 SCAR=18.3
Pod documented
167 6 my $this = shift;
168
169 0 ▶ my %options = ref $_[0] ? %{ $_[0] } : @_;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 6 6
170
171 6 ▶ my $separator = $options{separator} || ' ';
Condition50%1/2
$options{'separator'} || ' '
l!l
06
MC/DC0%0/1
$options{'separator'} || ' '
$options{'separator'}
Truth table50%1/2
A
A= $options{'separator'}
Aresult
11
00
172 6 ▶ my $break = $options{linebreak} || "\n";
Condition50%1/2
$options{'linebreak'} || "\n"
l!l
06
MC/DC0%0/1
$options{'linebreak'} || "\n"
$options{'linebreak'}
Truth table50%1/2
A
A= $options{'linebreak'}
Aresult
11
00
173
174 6 my $size = $this->{size};
175 6 my $digit = int(log($size) / log(10)) + 1;
176 6 my $numfmt = '%'.$digit.'d';
177
178 6 my @lines;
179 6 foreach my $row (1..$size) {
180 39 my @cells;
181 39 foreach my $col (1..$size) {
182 261 my $value = $this->cell($row, $col)->value;
183 261 push @cells, sprintf($numfmt,$value);
184 }
185 39 push @lines, join $separator, @cells;
186 }
187 6 join $break, @lines;
188 }
189
190 1 ▶ sub as_HTML {
Subroutine as_HTML: called CC=11 SCAR=27.6
Pod documented
191 1 my $this = shift;
192
193 0 ▶ my %options = ref $_[0] ? %{ $_[0] } : @_;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 1 1
194
195 1 ▶ my $border = $options{border} || 1;
Condition50%1/2
$options{'border'} || 1
l!l
01
MC/DC0%0/1
$options{'border'} || 1
$options{'border'}
Truth table50%1/2
A
A= $options{'border'}
Aresult
11
00
196 1 ▶ my $break = $options{linebreak} || "\n";
Condition50%1/2
$options{'linebreak'} || "\n"
l!l
01
MC/DC0%0/1
$options{'linebreak'} || "\n"
$options{'linebreak'}
Truth table50%1/2
A
A= $options{'linebreak'}
Aresult
11
00
197
198 1 my $size = $this->{size};
199
200 1 my @lines;
201 1 push @lines, qq{<table class="sudoku" border="$border">};
202 1 foreach my $row (1..$size) {
203 9 my @cells;
204 9 foreach my $col (1..$size) {
205 81 my $value = $this->cell($row, $col)->value;
206 81 my $class;
207 81 ▶ if ($options{color_by_block}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"color_by_block"}) 0 81 81
208 0 ▶ $class =
Branch: 1 branch
BranchTrueFalseTotal
$this->_block_id($row, $col) % 2 ? : 0 0 0
209 $this->_block_id($row, $col) % 2 ? 'odd' : 'even';
210 }
211 81 ▶ if ($options{color_by_cell}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"color_by_cell"}) 0 81 81
212 0 ▶ $class =
Branch: 1 branch
BranchTrueFalseTotal
$this->_cell_id($row, $col) % 2 ? : 0 0 0
213 $this->_cell_id($row, $col) % 2 ? 'odd' : 'even';
214 }
215 81 ▶ push @cells,
Branch: 1 branch
BranchTrueFalseTotal
$class ? : 0 81 81
216 $class ? qq{<td class="$class">} : qq{<td>},
217 qq{$value</td>};
218 }
219 9 my $tds = join '', @cells;
220 9 push @lines, qq{<tr>$tds</tr>};
221 }
222 1 push @lines, qq{</table>};
223 1 join $break, @lines;
224 }
225 }
226
227 4 1;
228 __END__
« Component/Result.pm ↑ Summary Component/Table/Cell.pm »