YASudoku/Board.pm

filter: Statementstmt 87.1% 162 / 186 Branchbran 75.0% 36 / 48 Conditioncond 33.3% 7 / 21 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 94.4% 17 / 18 Podpod 92.8% 13 / 14 totaltotal 78.8% 235 / 298 CC 50 SCAR 22.0
CC
50
Cov
80%
show_board_detail
42.0
pass_two
10.0
pass_one
9.2
CRAP 9.0
↑ Summary YASudoku.pm »
1 package Games::YASudoku::Board;
2  
3 =head1 MODULE
4  
5 Games::YASudoku::Board
6  
7 =head1 DESCRIPTION
8  
9 This module defines the Sudoku board.
10  
11 =head1 METHODS
12  
13 =over
14  
15 =cut
16  
17  
18 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use Games::YASudoku::Square;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21  
22 1 my $GROUPS = [
23 [ 0, 1, 2, 9,10,11,18,19,20 ],
24 [ 3, 4, 5,12,13,14,21,22,23 ],
25 [ 6, 7, 8,15,16,17,24,25,26 ],
26 [ 27,28,29,36,37,38,45,46,47 ],
27 [ 30,31,32,39,40,41,48,49,50 ],
28 [ 33,34,35,42,43,44,51,52,53 ],
29 [ 54,55,56,63,64,65,72,73,74 ],
30 [ 57,58,59,66,67,68,75,76,77 ],
31 [ 60,61,62,69,70,71,78,79,80 ],
32 ];
33  
34  
35 4 ▶sub new {
Subroutine new: called CC=2 SCAR=7.1
Pod undocumented
36 4 my $proto = shift;
37 4 ▶ my $class = ref( $proto ) || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
040
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
38  
39 4 my $self = [];
40  
41 4 bless $self, $class;
42 4 $self->_init();
43  
44 4 return $self;
45 }
46  
47  
48 =item B<_init>
49  
50 initialize a new board
51 The game board is 9 rows by 9 columns, but we will store the board in
52 a one dimensional array - For example, element 9 will map to row 2,
53 column 1 (keep in mind that the first element of the array is 0).
54  
55 =cut
56  
57 4 ▶sub _init {
Subroutine _init: called CC=2 SCAR=6.9
58 4 my $self = shift;
59
60 4 for my $i ( 0 .. 80 ){
61 324 push @{$self}, Games::YASudoku::Square->new( $i );
62 }
63  
64 4 return $self;
65 }
66  
67  
68 =item B<get_rows/get_row>
69  
70 Get rows will return a ref to an array of all the rows. get_row will
71 just return one row - valid row numbers are 1 - 9.
72  
73 =cut
74  
75 1 ▶sub get_rows {
Subroutine get_rows: called CC=2 SCAR=6.9
Pod documented
76 1 my $self = shift;
77  
78 1 my @rows;
79 1 for my $i ( 1 .. 9 ){
80 9 push @rows, $self->get_row( $i );
81 }
82  
83 1 return \@rows;
84 }
85  
86  
87  
88 1642 ▶sub get_row {
Subroutine get_row: called CC=5 SCAR=17.5
Pod documented
89 1642 my $self = shift;
90 1642 my $row_num = shift;
91  
92 1642 ▶ return undef unless $row_num && ( $row_num >= 1 ) && ( $row_num <= 9);
Branch: 1 branch
BranchTrueFalseTotal
unless $row_num and $row_num >= 1 and $row_num <= 9 0 1642 1642
Condition33%2/6
$row_num and $row_num >= 1 and $row_num <= 9
!ll&&!rl&&r
001642
$row_num and $row_num >= 1
!ll&&!rl&&r
001642
MC/DC0%0/3
$row_num and $row_num >= 1 and $row_num <= 9
$row_num $row_num >= 1 $row_num <= 9
Truth table25%1/4
A and B and C
A= $row_numB= $row_num >= 1C= $row_num <= 9
ABCresult
0XX0
10X0
1100
1111
93  
94 1642 my $start = ( $row_num - 1 ) * 9;
95 1642 my @row;
96  
97 1642 for my $i ( $start .. ( $start + 8 ) ){
98 14778 push @row, $self->[ $i ];
99 }
100 1642 return \@row;
101 }
102  
103  
104  
105 =item B<get_cols/get_col>
106  
107 Get cols will return a ref to an array of all the cols. get_col will
108 just return one row - valid col numbers are 1 - 9.
109  
110 =cut
111  
112 1 ▶sub get_cols {
Subroutine get_cols: called CC=2 SCAR=6.9
Pod documented
113 1 my $self = shift;
114  
115 1 my @cols;
116 1 for my $i ( 1 .. 9 ){
117 9 push @cols, $self->get_col( $i );
118 }
119  
120 1 return \@cols;
121 }
122  
123 1642 ▶sub get_col {
Subroutine get_col: called CC=5 SCAR=17.3
Pod documented
124 1642 my $self = shift;
125 1642 my $col_num = shift;
126  
127 1642 ▶ return undef unless $col_num && ( $col_num >= 1 ) && ( $col_num <= 9);
Branch: 1 branch
BranchTrueFalseTotal
unless $col_num and $col_num >= 1 and $col_num <= 9 0 1642 1642
Condition33%2/6
$col_num and $col_num >= 1 and $col_num <= 9
!ll&&!rl&&r
001642
$col_num and $col_num >= 1
!ll&&!rl&&r
001642
MC/DC0%0/3
$col_num and $col_num >= 1 and $col_num <= 9
$col_num $col_num >= 1 $col_num <= 9
Truth table25%1/4
A and B and C
A= $col_numB= $col_num >= 1C= $col_num <= 9
ABCresult
0XX0
10X0
1100
1111
128  
129 1642 my $start = $col_num - 1;
130 1642 my @col;
131  
132 1642 foreach my $i ( 0 .. 8 ){
133 14778 my $column = $start + ( $i * 9 );
134 14778 push @col, $self->[ $column ];
135 }
136 1642 return \@col;
137 }
138  
139  
140 =item B<get_grps/get_grp>
141  
142 Groups are defined as a set of nine boxes group in squares, there are
143 three rows of groups and three groups in each row. They are numbered
144 as follows.
145  
146 1 | 2 | 3
147 ---|---|---
148 4 | 5 | 6
149 ---|---|---
150 7 | 8 | 9
151  
152 get_grps will return a ref to an array of all the groups. get_grp will
153 just return one group which can be specified by one of the numbers above.
154  
155 =cut
156  
157 1 ▶sub get_grps {
Subroutine get_grps: called CC=2 SCAR=6.9
Pod documented
158 1 my $self = shift;
159  
160 1 my @groups;
161 1 for my $i ( 1 .. 9 ){
162 9 push @groups, $self->get_grp( $i );
163 }
164  
165 1 return \@groups;
166 }
167  
168 1642 ▶sub get_grp {
Subroutine get_grp: called CC=5 SCAR=17.5
Pod documented
169 1642 my $self = shift;
170 1642 my $grp_num = shift;
171  
172 1642 ▶ return undef unless $grp_num && ( $grp_num >= 1 ) && ( $grp_num <= 9);
Branch: 1 branch
BranchTrueFalseTotal
unless $grp_num and $grp_num >= 1 and $grp_num <= 9 0 1642 1642
Condition33%2/6
$grp_num and $grp_num >= 1 and $grp_num <= 9
!ll&&!rl&&r
001642
$grp_num and $grp_num >= 1
!ll&&!rl&&r
001642
MC/DC0%0/3
$grp_num and $grp_num >= 1 and $grp_num <= 9
$grp_num $grp_num >= 1 $grp_num <= 9
Truth table25%1/4
A and B and C
A= $grp_numB= $grp_num >= 1C= $grp_num <= 9
ABCresult
0XX0
10X0
1100
1111
173  
174 1642 my @grp;
175  
176 1642 foreach my $i ( @{ $GROUPS->[ $grp_num - 1 ] } ){
177 14778 push @grp, $self->[ $i ];
178 }
179 1642 return \@grp;
180 }
181  
182 =item B<get_values>
183  
184 return a list of all the squares with values set
185  
186 =cut
187  
188 82 ▶sub get_values {
Subroutine get_values: called CC=3 SCAR=11.0
Pod documented
189 82 my $self = shift;
190  
191 82 my @values;
192 82 foreach my $square ( @{ $self } ){
193 6642 ▶ push @values, $square if ( $square->value );
Branch: 1 branch
BranchTrueFalseTotal
if $square->value 3664 2978 6642
194 }
195  
196 82 return \@values;
197 }
198  
199
200  
201 =item B<get_element_membership>
202  
203 This method will return the three groups that the element is a member of.
204 One row, one column and one group.
205  
206 =cut
207  
208 1632 ▶sub get_element_membership {
Subroutine get_element_membership: called CC=3 SCAR=11.0
Pod documented
209 1632 my $self = shift;
210 1632 my $element = shift;
211  
212 1632 my $element_id = $element->id;
213  
214 # what row is the element in
215 1632 my $row = int( $element_id / 9 ) + 1;
216 1632 my $col = ($element_id % 9) + 1;
217  
218 1632 my $grp;
219 1632 for my $i ( 0 .. 8 ){
220 14688 ▶ $grp = ($i + 1) if grep /^$element_id$/, @{ $GROUPS->[ $i ] };
Branch: 1 branch
BranchTrueFalseTotal
if grep /^$element_id$/, @{$$GROUPS[$i];} 1632 13056 14688
221 }
222  
223 1632 my @membership;
224 1632 push @membership, $self->get_row( $row ),
225 $self->get_col( $col ),
226 $self->get_grp( $grp );
227  
228 1632 return \@membership;
229 }
230  
231  
232 =item B<show_board>
233  
234 This method displays the current state of the board
235  
236 =cut
237  
238 2 ▶sub show_board {
Subroutine show_board: called CC=6 SCAR=17.9
Pod documented
239 2 my $self = shift;
240  
241 2 my $board_text;
242 2 my $reg_row_mark = '+...' x 9 . '+';
243 2 my $big_row_mark = '+---' x 9 . '+';
244  
245 2 for my $i ( 0 .. 80 ){
246 162 ▶ if ( ($i % 9) == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i % 9 == 0) 18 144 162
247 18 ▶ if ( ($i % 27 ) == 0 ){
Branch: 1 branch
BranchTrueFalseTotal
if ($i % 27 == 0) { } 6 12 18
248 6 $board_text .= "\n$big_row_mark\n|";
249 } else {
250 12 $board_text .= "\n$reg_row_mark\n|";
251 }
252 }
253 162 my $value = $self->[$i]->value;
254 162 ▶ if ( $value ){
Branch: 1 branch
BranchTrueFalseTotal
if ($value) { } 105 57 162
255 105 $board_text .= sprintf( " %1d ", $self->[$i]->value );
256 } else {
257 57 $board_text .= " ";
258 }
259 162 ▶ if ( (( $i +1 ) % 3 ) == 0 ){
Branch: 1 branch
BranchTrueFalseTotal
if (($i + 1) % 3 == 0) { } 54 108 162
260 54 $board_text .= '|';
261 } else {
262 108 $board_text .= ':';
263 }
264 }
265  
266 2 $board_text .= "\n$big_row_mark\n";
267  
268 2 return $board_text;
269 }
270  
271 =item B<show_board_detail>
272  
273 This method displays the current state of the board including the possible
274 values for each unsolved square.
275  
276 =cut
277  
278  
279 0 ▶sub show_board_detail {
Subroutine show_board_detail: not called CC=6 SCAR=37.4
Pod documented
280 0 my $self = shift;
281  
282 0 my $board_text;
283 0 my $reg_row_mark = '+.........' x 9 . '+';
284 0 my $big_row_mark = '+---------' x 9 . '+';
285  
286 0 for my $i ( 0 .. 80 ){
287 0 ▶ if ( ($i % 9) == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i % 9 == 0) 0 0 0
288 0 ▶ if ( ($i % 27 ) == 0 ){
Branch: 1 branch
BranchTrueFalseTotal
if ($i % 27 == 0) { } 0 0 0
289 0 $board_text .= "\n$big_row_mark\n|";
290 } else {
291 0 $board_text .= "\n$reg_row_mark\n|";
292 }
293 }
294 0 my $value = $self->[$i]->value;
295 0 ▶ if ( $value ){
Branch: 1 branch
BranchTrueFalseTotal
if ($value) { } 0 0 0
296 0 $board_text .= sprintf( " %7d ", $self->[$i]->value );
297 } else {
298 0 $board_text .= sprintf("(%7s)", join('', @{ $self->[$i]->valid }));
299 }
300 0 ▶ if ( (( $i +1 ) % 3 ) == 0 ){
Branch: 1 branch
BranchTrueFalseTotal
if (($i + 1) % 3 == 0) { } 0 0 0
301 0 $board_text .= '|';
302 } else {
303 0 $board_text .= ':';
304 }
305 }
306  
307 0 $board_text .= "\n$big_row_mark\n";
308  
309 0 return $board_text;
310 }
311  
312  
313 =item B<run_board>
314  
315 this method solve the board
316  
317 =cut
318  
319 1 ▶sub run_board {
Subroutine run_board: called CC=2 SCAR=6.9
Pod documented
320 1 my $self = shift;
321  
322 1 my $new_values = 0;
323 1 my $values = @{$self->get_values};
324 1 my $passes = 0;
325 1 ▶ while ( $values != $new_values ){
Branch: 1 branch
BranchTrueFalseTotal
if ($values != $new_values) 3 1 4
326 3 $self->pass_one;
327 3 $self->pass_two;
328  
329 3 $passes++;
330 3 $values = $new_values;
331 3 $new_values = @{$self->get_values};
332 }
333 1 return $passes;
334 }
335  
336  
337 =item B<pass_one>
338  
339 the first pass looks for values and reduces valid_num arrays
340  
341 =cut
342  
343 17 ▶sub pass_one {
Subroutine pass_one: called CC=9 SCAR=22.2
Pod documented
344 17 my $self = shift;
345
346 17 my $new_values = 0;
347 17 my $values = @{$self->get_values};
348 17 ▶ while ( $values != $new_values ){
Branch: 1 branch
BranchTrueFalseTotal
if ($values != $new_values) 41 17 58
349
350 41 foreach my $element ( @{ $self } ){
351 3321 ▶ next if $element->value;
Branch: 1 branch
BranchTrueFalseTotal
if $element->value 1776 1545 3321
352 1545 my $ms = $self->get_element_membership( $element );
353
354 1545 foreach my $member ( @{ $ms } ){
355 4635 foreach my $e ( @{ $member } ){
356 41715 ▶ next unless $e->value;
Branch: 1 branch
BranchTrueFalseTotal
unless $e->value 25198 16517 41715
357
358 16517 $element->valid_del( $e->value );
359 16517 ▶ if ( @{ $element->valid } == 0 ){
Branch: 1 branch
BranchTrueFalseTotal
if (@{$element->valid;} == 0) 0 16517 16517
360 0 warn $self->show_board_detail;
361 1 ▶ use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
362 0 warn Dumper $element;
363 0 warn Dumper $e;
364 0 warn Dumper $member;
365 0 die "Something very wrong here!!!!";
366 }
367 }
368 }
369  
370 1545 my @valid = $element->valid;
371 1545 ▶ if ( @{ $element->valid } == 1 ){
Branch: 1 branch
BranchTrueFalseTotal
if (@{$element->valid;} == 1) 43 1502 1545
372 43 $element->value( $element->valid->[0] );
373 }
374 }
375  
376 41 $values = $new_values;
377 41 $new_values = @{$self->get_values};
378 }
379 17 return @{$self->get_values};
380 }
381  
382 =item B<pass_two>
383  
384 this pass looks for valid_num arrays which have a unique value
385 and therefore need to have that value assigned to them
386  
387 Example: Square 1 can be (1,2,3)
388 Square 2 can be (2,3,4,6)
389 Square 3 can be (2,3,4,5)
390 Square 4 can be (5,6)
391 Since square 1 is the only one with a '1', it needs to be 1
392  
393 =cut
394  
395 3 ▶sub pass_two {
Subroutine pass_two: called CC=10 SCAR=23.0
Pod documented
396 3 my $self = shift;
397
398 3 foreach my $element ( @{ $self } ){
399 243 ▶ next if $element->value;
Branch: 1 branch
BranchTrueFalseTotal
if $element->value 157 86 243
400  
401  
402 # get all the groups this square is an member of
403 86 foreach my $member ( @{$self->get_element_membership($element)}){
404 238 my %valid_nums;
405 238 map { $valid_nums{ $_ } = $_ } @{ $element->valid };
406  
407 238 foreach my $e ( @{$member} ){
408 # don't work on the current element
409 2142 ▶ next if ( $e->id == $element->id );
Branch: 1 branch
BranchTrueFalseTotal
if $e->id == $element->id 238 1904 2142
410 1904 ▶ next if ( $e->value ); # skip element that is already processed
Branch: 1 branch
BranchTrueFalseTotal
if $e->value 806 1098 1904
411  
412 1098 foreach my $valid ( @{ $e->valid } ){
413 2403 delete $valid_nums{ $valid };
414 2403 ▶ last if (( keys %valid_nums ) == 0 );
Branch: 1 branch
BranchTrueFalseTotal
if keys %valid_nums == 0 665 1738 2403
415 }
416 }
417
418 238 ▶ if ( (keys %valid_nums) == 1 ){
Branch: 1 branch
BranchTrueFalseTotal
if (keys %valid_nums == 1) 14 224 238
419 14 my @keys = keys %valid_nums;
420 14 $element->value( $keys[0] );
421 14 $self->pass_one;
422 14 last;
423 }
424 }
425 }
426
427 3 return @{$self->get_values};
428 }
429  
430  
431 1 1;
432  
433  
434 =head1 AUTHOR
435  
436 Andrew Wyllie <wyllie@dilex.net>
437  
438 =head1 BUGS
439  
440 Please send any bugs to the author
441  
442 =head1 COPYRIGHT
443  
444 The Games::YASudoku moudule is free software and can be redistributed
445 and/or modified under the same terms as Perl itself.
446  
↑ Summary YASudoku.pm »