Sudoku.pm

filter: Statementstmt 99.4% 369 / 371 Branchbran 93.6% 103 / 110 Conditioncond 89.4% 76 / 85 MC/DCmcdc 67.3% 31 / 46 Subroutinesub 100% 62 / 62 Podpod 8.1% 4 / 49 totaltotal 89.2% 645 / 723 CC 120 SCAR 18.6
CC
120
Cov
97%
cells
16.0
init_diagonals
14.0
must_differ
11.0
CRAP 6.4
↑ Summary Sudoku/Constants.pm »
1 package Regexp::Sudoku;
2  
3 38 ▶use 5.028;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 38 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 38 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 38 ▶no warnings 'syntax';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 38 ▶use experimental 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 38 ▶use experimental 'lexical_subs';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 38 our $VERSION = '2022022401';
12  
13 38 ▶use Hash::Util::FieldHash qw [fieldhash];
Subroutine BEGIN: called CC=1 SCAR=0.0
14 38 ▶use List::Util qw [min];
Subroutine BEGIN: called CC=1 SCAR=0.0
15 38 ▶use Math::Sequence::DeBruijn;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 38 ▶use Regexp::Sudoku::Constants qw [:Diagonals :Houses :Constraints];
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 38 ▶use Exporter ();
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 38 my $DEFAULT_SIZE = 9;
21 38 my $SENTINEL = "\n";
22 38 my $CLAUSE_LIST = ",";
23  
24 38 my $NR_OF_DIGITS = 9;
25 38 my $NR_OF_LETTERS = 26;
26 38 my $NR_OF_SYMBOLS = $NR_OF_DIGITS + $NR_OF_LETTERS;
27  
28  
29 38 fieldhash my %size;
30 38 fieldhash my %values;
31 38 fieldhash my %evens;
32 38 fieldhash my %odds;
33 38 fieldhash my %box_width;
34 38 fieldhash my %box_height;
35 38 fieldhash my %values_range;
36 38 fieldhash my %cell2houses;
37 38 fieldhash my %house2cells;
38 38 fieldhash my %clues;
39 38 fieldhash my %is_even;
40 38 fieldhash my %is_odd;
41 38 fieldhash my %subject;
42 38 fieldhash my %pattern;
43 38 fieldhash my %houses;
44 38 fieldhash my %constraints;
45  
46  
47 224434 ▶my sub has_bit ($vec) {$vec =~ /[^\x{00}]/}
Subroutine has_bit: called CC=1 SCAR=0.0
48  
49  
50 ################################################################################
51 #
52 # new ($class)
53 #
54 # Create an uninitialized object.
55 #
56 ################################################################################
57  
58 115 ▶sub new ($class) {bless \do {my $v} => $class}
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
59  
60  
61 ################################################################################
62 #
63 # sub cell_name ($row, $column)
64 #
65 # Given a row number and a cell number, return the name of the cell.
66 #
67 # TESTS: 090-cell_name_row_column.t
68 #
69 ################################################################################
70  
71 ▶sub cell_name ($row, $column) {
Subroutine cell_name: called CC=1 SCAR=0.0
Pod undocumented
72 40500 "R" . $row . "C" . $column
73 }
74  
75  
76 ################################################################################
77 #
78 # sub cell_row_column ($cell_name)
79 #
80 # Given the name of a cell, return its row and column.
81 #
82 # TESTS: 090-cell_name_row_column.t
83 #
84 ################################################################################
85  
86 ▶sub cell_row_column ($name) {
Subroutine cell_row_column: called CC=2 SCAR=7.6
Pod undocumented
87 291781 ▶ $name =~ /R([0-9]+)C([0-9]+)/ ? ($1, $2) : (0, 0)
Branch: 1 branch
BranchTrueFalseTotal
$name =~ /R([0-9]+)C([0-9]+)/u ? : 291781 0 291781
88 }
89  
90  
91 ################################################################################
92 #
93 # init_sizes ($self, $size)
94 #
95 # Initialize the sizes of the soduko.
96 #
97 # Calls init_size () and init_box () doing the work.
98 #
99 # TESTS: 010_size.t
100 #
101 ################################################################################
102  
103 ▶sub init_sizes ($self, $args) {
Subroutine init_sizes: called CC=1 SCAR=0.0
Pod undocumented
104 112 $self -> init_size ($args)
105 -> init_box ($args);
106 }
107  
108  
109 ################################################################################
110 #
111 # init_size ($self, $size)
112 #
113 # Initialize the size of a sudoku. If the size is not given, use the default.
114 #
115 # TESTS: 010_size.t
116 #
117 ################################################################################
118  
119 112 ▶sub init_size ($self, $args = {}) {
Subroutine init_size: called CC=4 SCAR=13.9
Pod undocumented
120 112 ▶ $size {$self} = delete $$args {size} || $DEFAULT_SIZE;
Condition66%2/3
delete $args->{'size'} || $DEFAULT_SIZE
l!l&&r!l&&!r
102100
MC/DC0%0/2
delete $args->{'size'} || $DEFAULT_SIZE
delete $args->{'size'} $DEFAULT_SIZE
Truth table66%2/3
A || B
A= delete $args->{'size'}B= $DEFAULT_SIZE
ABresult
1X1
011
000
121 die "Size should not exceed $NR_OF_SYMBOLS\n"
122 112 ▶ if $size {$self} > $NR_OF_SYMBOLS;
Branch: 1 branch
BranchTrueFalseTotal
if $size{$self} > $NR_OF_SYMBOLS 1 111 112
123 111 $self;
124 }
125  
126  
127 ################################################################################
128 #
129 # size ($self)
130 #
131 # Returns the size of the sudoku. If there is no size supplied, use
132 # the default (9).
133 #
134 # TESTS: 010_size.t
135 #
136 ################################################################################
137  
138 ▶sub size ($self) {
Subroutine size: called CC=1 SCAR=0.0
Pod documented
139 1009 $size {$self}
140 }
141  
142  
143 ################################################################################
144 #
145 # init_values ($self, $args)
146 #
147 # Initializes the values. We calculate them from the range (1-9, A-Z),
148 # as many as needed.
149 #
150 # TESTS: 020_values.t
151 #
152 ################################################################################
153  
154 96 ▶sub init_values ($self, $args = {}) {
Subroutine init_values: called CC=3 SCAR=11.0
Pod undocumented
155 96 my $size = $self -> size;
156 96 my $values = join "" => 1 .. min $size, $NR_OF_DIGITS;
157 96 ▶ if ($size > $NR_OF_DIGITS) {
Branch: 1 branch
BranchTrueFalseTotal
if ($size > $NR_OF_DIGITS) 21 75 96
158 $values .= join "" =>
159 21 map {chr (ord ('A') + $_ - $NR_OF_DIGITS - 1)}
160 ($NR_OF_DIGITS + 1) .. min $size, $NR_OF_SYMBOLS;
161 }
162  
163 96 my $evens = do {my $i = 1; join "" => grep {$i = !$i} split // => $values};
164 96 my $odds = do {my $i = 0; join "" => grep {$i = !$i} split // => $values};
165  
166 96 $values {$self} = $values;
167 96 $evens {$self} = $evens;
168 96 $odds {$self} = $odds;
169  
170 96 $self -> init_values_range ($args);
171 }
172  
173  
174 ################################################################################
175 #
176 # init_value_ranges ($self, $args)
177 #
178 # Processes the values to turn them into ranges for use in a character class
179 #
180 # TESTS: 020_values.t
181 #
182 ################################################################################
183  
184 96 ▶sub init_values_range ($self, $args = {}) {
Subroutine init_values_range: called CC=4 SCAR=14.0
Pod undocumented
185 96 my @values = sort {$a cmp $b} $self -> values;
186 96 my $size = $self -> size;
187 96 my $range = "1-";
188  
189 96 ▶ if ($size < 10) {$range .= $values [-1];}
Branch: 2 branches
BranchTrueFalseTotal
if ($size < 10) { } 75 21 96
elsif ($size == 10) { } 0 21 21
190 0 elsif ($size == 10) {$range .= "9A"; }
191 21 else {$range .= "9A-" . $values [-1];}
192  
193 96 $values_range {$self} = $range;
194  
195 96 $self;
196 }
197  
198  
199 ################################################################################
200 #
201 # values ($self)
202 #
203 # Return the set of values used in the sudoku. In list context, this will
204 # be an array of characters; in scalar context, a string.
205 #
206 # TESTS: 020_values.t
207 #
208 ################################################################################
209  
210 ▶sub values ($self) {
Subroutine values: called CC=2 SCAR=6.9
Pod undocumented
211 18275 ▶ wantarray ? split // => $values {$self} : $values {$self};
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 17091 1184 18275
212 }
213  
214  
215 ################################################################################
216 #
217 # evens ($self)
218 #
219 # Return the set of even values used in the sudoku. In list context, this will
220 # be an array of characters; in scalar context, a string.
221 #
222 # TESTS: 020_values.t
223 #
224 ################################################################################
225  
226 ▶sub evens ($self) {
Subroutine evens: called CC=2 SCAR=6.9
Pod undocumented
227 80 ▶ wantarray ? split // => $evens {$self} : $evens {$self};
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 5 75 80
228 }
229  
230  
231 ################################################################################
232 #
233 # odds ($self)
234 #
235 # Return the set of odd values used in the sudoku. In list context, this will
236 # be an array of characters; in scalar context, a string.
237 #
238 # TESTS: 020_values.t
239 #
240 ################################################################################
241  
242 ▶sub odds ($self) {
Subroutine odds: called CC=2 SCAR=6.9
Pod undocumented
243 71 ▶ wantarray ? split // => $odds {$self} : $odds {$self};
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 5 66 71
244 }
245  
246  
247 ################################################################################
248 #
249 # values_range ($self)
250 #
251 # Return the set of values used in the sudoku, as ranges to be used in
252 # a character class. Calls $self -> values () to get the values.
253 #
254 # TESTS: 020_values.t
255 #
256 ################################################################################
257  
258 ▶sub values_range ($self) {
Subroutine values_range: called CC=1 SCAR=0.0
Pod undocumented
259 18302 $values_range {$self}
260 }
261  
262  
263 ################################################################################
264 #
265 # init_box ($self, $args)
266 #
267 # Find the width and height of a box. If the size of the sudoku is a square,
268 # the width and height of a box are equal, and the square root of the size
269 # of the sudoku. Else, we'll find the most squarish width and height (with
270 # the width larger than the height). The width and height are stored as
271 # attributes. If they are already set, the function immediately returns.
272 #
273 # TESTS: 030-box.t
274 #
275 ################################################################################
276  
277 111 ▶sub init_box ($self, $args = {}) {
Subroutine init_box: called CC=5 SCAR=16.4
Pod undocumented
278 111 ▶ return if $box_height {$self} && $box_width {$self};
Branch: 1 branch
BranchTrueFalseTotal
if $box_height{$self} and $box_width{$self} 0 111 111
Condition33%1/3
$box_height{$self} and $box_width{$self}
!ll&&!rl&&r
11100
MC/DC0%0/2
$box_height{$self} and $box_width{$self}
$box_height{$self} $box_width{$self}
Truth table33%1/3
A and B
A= $box_height{$self}B= $box_width{$self}
ABresult
0X0
100
111
279 111 my $size = $self -> size;
280 111 my $box_height = int sqrt $size;
281 111 ▶ $box_height -- while $size % $box_height;
Branch: 1 branch
BranchTrueFalseTotal
$size % $box_height and do { --$box_height } 3 111 114
282 111 my $box_width = $size / $box_height;
283  
284 111 $box_height {$self} = $box_height;
285 111 $box_width {$self} = $box_width;
286  
287 111 $self;
288 }
289  
290  
291 ################################################################################
292 #
293 # box_height ($self)
294 # box_width ($self)
295 #
296 # Return the height and width of a box in the sudoku. These methods will
297 # call $self -> box_init first, to calculate the values if necessary.
298 #
299 # TESTS: 030_values.t
300 #
301 ################################################################################
302  
303 ▶sub box_height ($self) {
Subroutine box_height: called CC=1 SCAR=0.0
Pod undocumented
304 206 $box_height {$self};
305 }
306  
307 ▶sub box_width ($self) {
Subroutine box_width: called CC=1 SCAR=0.0
Pod undocumented
308 206 $box_width {$self};
309 }
310  
311  
312 ################################################################################
313 #
314 # create_house ($self, $house_name, @cells)
315 #
316 # Create a house with the given name, containing the passed in cells.
317 #
318 # TESTS: 040-create_house.t
319 #
320 ################################################################################
321  
322 ▶sub create_house ($self, $name, @cells) {
Subroutine create_house: called CC=2 SCAR=6.9
Pod undocumented
323 3084 for my $cell (@cells) {
324 39607 $cell2houses {$self} {$cell} {$name} = 1;
325 39607 $house2cells {$self} {$name} {$cell} = 1;
326 }
327 3084 $self;
328 }
329  
330  
331 ################################################################################
332 #
333 # init_rows ($self, $args)
334 #
335 # Initialize the rows in the sudoku. Calculates which cells belong to which
336 # rows, and calls create_house for each row. Called from init_houses.
337 # Rows are named "R1" .. "Rn", where n is the size of the sudoku.
338 #
339 # TESTS: 041-init_rows
340 #
341 ################################################################################
342  
343 100 ▶sub init_rows ($self, $args = {}) {
Subroutine init_rows: called CC=3 SCAR=11.0
Pod undocumented
344 100 my $size = $self -> size;
345 100 for my $r (1 .. $size) {
346 1001 my $row_name = "R$r";
347 1001 my @cells = map {cell_name $r, $_} 1 .. $size;
348 1001 $self -> create_house ($row_name, @cells);
349 }
350 100 $self;
351 }
352  
353  
354 ################################################################################
355 #
356 # init_columns ($self, $args)
357 #
358 # Initialize the columns in the sudoku. Calculates which cells belong to which
359 # columns, and calls create_house for each column. Called from init_houses.
360 # Columns are named "C1" .. "Cn", where n is the size of the sudoku.
361 #
362 # TESTS: 042-init_columns
363 #
364 ################################################################################
365  
366 100 ▶sub init_columns ($self, $args = {}) {
Subroutine init_columns: called CC=3 SCAR=11.0
Pod undocumented
367 100 my $size = $self -> size;
368 100 for my $c (1 .. $size) {
369 1001 my $col_name = "C$c";
370 1001 my @cells = map {cell_name $_, $c} 1 .. $size;
371 1001 $self -> create_house ($col_name, @cells);
372 }
373 100 $self;
374 }
375  
376  
377 ################################################################################
378 #
379 # init_boxes ($self, $args)
380 #
381 # Initialize the boxes in the sudoku. Calculates which cells belong to which
382 # boxes, and calls create_house for each box. Called from init_houses.
383 # Boxes are named "B1-1" .. "Bh-w" where we have h rows of w boxes.
384 #
385 # TESTS: 043-init_boxes
386 #
387 ################################################################################
388  
389 100 ▶sub init_boxes ($self, $args = {}) {
Subroutine init_boxes: called CC=6 SCAR=17.9
Pod undocumented
390 100 my $size = $self -> size;
391 100 my $box_width = $self -> box_width;
392 100 my $box_height = $self -> box_height;
393  
394 100 my $bc = $size / $box_width;
395 100 my $br = $size / $box_height;
396 100 for my $r (1 .. $br) {
397 335 for my $c (1 .. $bc) {
398 1001 my $box_name = "B${r}-${c}";
399 1001 my $tlr = 1 + ($r - 1) * $box_height;
400 1001 my $tlc = 1 + ($c - 1) * $box_width;
401 1001 my @cells;
402 1001 for my $dr (1 .. $box_height) {
403 3227 for my $dc (1 .. $box_width) {
404 12967 my $cell = cell_name $tlr + $dr - 1, $tlc + $dc - 1;
405 12967 push @cells => $cell;
406 }
407 }
408 1001 $self -> create_house ($box_name, @cells);
409 }
410 }
411 100 $self;
412 }
413  
414  
415 ################################################################################
416 #
417 # init_houses ($self, $args)
418 # init_rows ($self)
419 # init_columns ($self)
420 # init_boxes ($self)
421 #
422 # Calculate which cells go into which houses.
423 #
424 # Calls init_rows (), init_columns (), and init_boxes () to initialize
425 # the rows, columns and boxes.
426 #
427 # Depending on the parameters, it may call:
428 # - init_nrc_houses ()
429 # - init_asterisk_house ()
430 #
431 # TESTS: 045-init_houses.t
432 #
433 ################################################################################
434  
435 96 ▶sub init_houses ($self, $args = {}) {
Subroutine init_houses: called CC=7 SCAR=19.5
Pod undocumented
436 96 ▶ my $houses = $houses {$self} = delete $$args {houses} || "";
Condition100%2/2
delete $args->{'houses'} || ''
l!l
1482
MC/DC100%1/1
delete $args->{'houses'} || ''
delete $args->{'houses'}
Truth table100%2/2
A
A= delete $args->{'houses'}
Aresult
11
00
437 96 ▶ if (has_bit ($houses &. ~. $ALL_HOUSES) ||
Branch: 1 branch
BranchTrueFalseTotal
if (has_bit $houses &. ~.$Regexp::Sudoku::ALL_HOUSES or length $houses =~ s/\x{00}*$//ru > length $Regexp::Sudoku::ALL_HOUSES) 1 95 96
Condition66%2/3
has_bit $houses &. ~.$Regexp::Sudoku::ALL_HOUSES or length $houses =~ s/\x{00}*$//ru > length $Regexp::Sudoku::ALL_HOUSES
l!l&&r!l&&!r
1095
MC/DC50%1/2
has_bit $houses &. ~.$Regexp::Sudoku::ALL_HOUSES or length $houses =~ s/\x{00}*$//ru > length $Regexp::Sudoku::ALL_HOUSES
has_bit $houses &. ~.$Regexp::Sudoku::ALL_HOUSES length $houses =~ s/\x{00}*$//ru > length $Regexp::Sudoku::ALL_HOUSES
Truth table66%2/3
A or B
A= has_bit $houses &. ~.$Regexp::Sudoku::ALL_HOUSESB= length $houses =~ s/\x{00}*$//ru > length $Regexp::Sudoku::ALL_HOUSES
ABresult
1X1
011
000
438 length ($houses =~ s/\x{00}*$//r) > length ($ALL_HOUSES)) {
439 1 my $out = "";
440 1 my $r = $houses &. ~. $ALL_HOUSES;
441 1 ▶ for (my $i = 0; $i < 8 * length ($r); $i ++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < 8 * length($r)) 8 1 9
442 8 ▶ $out .= vec ($r, $i, 1) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
vec($r, $i, 1) ? : 2 6 8
443 }
444 1 die sprintf "Unknown house(s): %s\n", $out;
445 }
446  
447 95 $self -> init_rows ($args)
448 -> init_columns ($args)
449 -> init_boxes ($args)
450 -> init_nrc_houses ($args)
451 -> init_asterisk_house ($args)
452 -> init_girandola_house ($args)
453 -> init_center_dot_house ($args);
454 }
455  
456  
457 ################################################################################
458 #
459 # init_nrc_houses ($self, $args)
460 #
461 # For NRC style puzzles, handle creating the houses.
462 #
463 # There are four NRC houses (9 x 9 Sudokus only):
464 #
465 # . . . . . . . . .
466 # . * * * . * * * .
467 # . * * * . * * * .
468 #
469 # . * * * . * * * .
470 # . . . . . . . . .
471 # . * * * . * * * .
472 #
473 # . * * * . * * * .
474 # . * * * . * * * .
475 # . . . . . . . . .
476 #
477 #
478 # TESTS: 046-init_nrc_houses.t
479 #
480 ################################################################################
481  
482 95 ▶sub init_nrc_houses ($self, $args = {}) {
Subroutine init_nrc_houses: called CC=7 SCAR=19.5
Pod undocumented
483 return $self unless $self -> size == $DEFAULT_SIZE &&
484 95 ▶ has_bit ($houses {$self} &. $NRC);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->size == $DEFAULT_SIZE and has_bit $houses{$self} &. $Regexp::Sudoku::NRC 92 3 95
Condition100%3/3
$self->size == $DEFAULT_SIZE and has_bit $houses{$self} &. $Regexp::Sudoku::NRC
!ll&&!rl&&r
39533
MC/DC100%2/2
$self->size == $DEFAULT_SIZE and has_bit $houses{$self} &. $Regexp::Sudoku::NRC
$self->size == $DEFAULT_SIZE has_bit $houses{$self} &. $Regexp::Sudoku::NRC
Truth table100%3/3
A and B
A= $self->size == $DEFAULT_SIZEB= has_bit $houses{$self} &. $Regexp::Sudoku::NRC
ABresult
0X0
100
111
485  
486 3 my @top_left = ([2, 2], [2, 6], [6, 2], [6, 6]);
487 3 foreach my $i (keys @top_left) {
488 12 my $top_left = $top_left [$i];
489 12 my $house = "NRC" . ($i + 1);
490 12 my @cells;
491 12 foreach my $dr (0 .. 2) {
492 36 foreach my $dc (0 .. 2) {
493 108 my $cell = cell_name $$top_left [0] + $dr,
494 $$top_left [1] + $dc;
495 108 push @cells => $cell;
496 }
497 }
498 12 $self -> create_house ($house, @cells);
499 }
500  
501 3 $self;
502 }
503  
504  
505 ################################################################################
506 #
507 # sub init_asterisk_house ($self, $args)
508 #
509 # An asterisk sudoku has an additional house: one cell from each box.
510 # This method initializes that house.
511 #
512 # An asterisk is defined for a 9 x 9 sudoku as follows:
513 #
514 # . . . . . . . . .
515 # . . . . * . . . .
516 # . . * . . . * . .
517 #
518 # . . . . . . . . .
519 # . * . . * . . * .
520 # . . . . . . . . .
521 #
522 # . . * . . . * . .
523 # . . . . * . . . .
524 # . . . . . . . . .
525 #
526 # TESTS: 047-init_asterisk_house.t
527 #
528 ################################################################################
529  
530 95 ▶sub init_asterisk_house ($self, $args = {}) {
Subroutine init_asterisk_house: called CC=4 SCAR=13.9
Pod undocumented
531 return $self unless $self -> size == $DEFAULT_SIZE &&
532 95 ▶ has_bit ($houses {$self} &. $ASTERISK);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->size == $DEFAULT_SIZE and has_bit $houses{$self} &. $Regexp::Sudoku::ASTERISK 91 4 95
Condition100%3/3
$self->size == $DEFAULT_SIZE and has_bit $houses{$self} &. $Regexp::Sudoku::ASTERISK
!ll&&!rl&&r
39524
MC/DC100%2/2
$self->size == $DEFAULT_SIZE and has_bit $houses{$self} &. $Regexp::Sudoku::ASTERISK
$self->size == $DEFAULT_SIZE has_bit $houses{$self} &. $Regexp::Sudoku::ASTERISK
Truth table100%3/3
A and B
A= $self->size == $DEFAULT_SIZEB= has_bit $houses{$self} &. $Regexp::Sudoku::ASTERISK
ABresult
0X0
100
111
533  
534 4 $self -> create_house ("AS" => map {cell_name @$_}
535 [3, 3], [2, 5], [3, 7],
536 [5, 2], [5, 5], [5, 8],
537 [7, 3], [8, 5], [7, 7]);
538 }
539  
540  
541 ################################################################################
542 #
543 # sub init_girandola_house ($self, $args)
544 #
545 # An asterisk sudoku has an additional house: one cell from each box.
546 # This method initializes that house.
547 #
548 # An asterisk is defined for a 9 x 9 sudoku as follows:
549 #
550 # * . . . . . . . *
551 # . . . . * . . . .
552 # . . . . . . . . .
553 #
554 # . . . . . . . . .
555 # . * . . * . . * .
556 # . . . . . . . . .
557 #
558 # . . . . . . . . .
559 # . . . . * . . . .
560 # * . . . . . . . *
561 #
562 # TESTS: 048-init_girandola_house.t
563 #
564 ################################################################################
565  
566 95 ▶sub init_girandola_house ($self, $args = {}) {
Subroutine init_girandola_house: called CC=4 SCAR=13.9
Pod undocumented
567 return $self unless $self -> size == $DEFAULT_SIZE &&
568 95 ▶ has_bit ($houses {$self} &. $GIRANDOLA);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->size == $DEFAULT_SIZE and has_bit $houses{$self} &. $Regexp::Sudoku::GIRANDOLA 92 3 95
Condition100%3/3
$self->size == $DEFAULT_SIZE and has_bit $houses{$self} &. $Regexp::Sudoku::GIRANDOLA
!ll&&!rl&&r
39533
MC/DC100%2/2
$self->size == $DEFAULT_SIZE and has_bit $houses{$self} &. $Regexp::Sudoku::GIRANDOLA
$self->size == $DEFAULT_SIZE has_bit $houses{$self} &. $Regexp::Sudoku::GIRANDOLA
Truth table100%3/3
A and B
A= $self->size == $DEFAULT_SIZEB= has_bit $houses{$self} &. $Regexp::Sudoku::GIRANDOLA
ABresult
0X0
100
111
569  
570 3 $self -> create_house ("GR" => map {cell_name @$_}
571 [1, 1], [2, 5], [1, 9],
572 [5, 2], [5, 5], [5, 8],
573 [9, 1], [8, 5], [9, 9]);
574 }
575  
576 ################################################################################
577 #
578 # sub init_center_dot_house ($self, $args)
579 #
580 # An asterisk sudoku has an additional house: one cell from each box.
581 # This method initializes that house.
582 #
583 # An asterisk is defined for a 9 x 9 sudoku as follows:
584 #
585 # . . . . . . . . .
586 # . * . . * . . * .
587 # . . . . . . . . .
588 #
589 # . . . . . . . . .
590 # . * . . * . . * .
591 # . . . . . . . . .
592 #
593 # . . . . . . . . .
594 # . * . . * . . * .
595 # . . . . . . . . .
596 #
597 # TESTS: 049-init_center_dot_house.t
598 #
599 ################################################################################
600  
601 95 ▶sub init_center_dot_house ($self, $args = {}) {
Subroutine init_center_dot_house: called CC=7 SCAR=19.5
Pod undocumented
602 95 my $width = $self -> box_width;
603 95 my $height = $self -> box_height;
604 95 my $size = $self -> size;
605  
606 #
607 # We can only do center dots if boxes are odd sized width and heigth.
608 #
609 return $self unless $width % 2 && $height % 2 &&
610 95 ▶ has_bit ($houses {$self} &. $CENTER_DOT);
Branch: 1 branch
BranchTrueFalseTotal
unless $width % 2 and $height % 2 and has_bit $houses{$self} &. $Regexp::Sudoku::CENTER_DOT 66 3 69
Condition100%6/6
$width % 2 and $height % 2 and has_bit $houses{$self} &. $Regexp::Sudoku::CENTER_DOT
!ll&&!rl&&r
33593
$width % 2 and $height % 2
!ll&&!rl&&r
26762
MC/DC100%3/3
$width % 2 and $height % 2 and has_bit $houses{$self} &. $Regexp::Sudoku::CENTER_DOT
$width % 2 $height % 2 has_bit $houses{$self} &. $Regexp::Sudoku::CENTER_DOT
Truth table100%4/4
A and B and C
A= $width % 2B= $height % 2C= has_bit $houses{$self} &. $Regexp::Sudoku::CENTER_DOT
ABCresult
0XX0
10X0
1100
1111
611  
612 3 my $width_start = ($width + 1) / 2;
613 3 my $height_start = ($height + 1) / 2;
614  
615 3 my @center_cells;
616 3 ▶ for (my $x = $width_start; $x <= $size; $x += $width) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x <= $size) 9 3 12
617 9 ▶ for (my $y = $height_start; $y <= $size; $y += $height) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y <= $size) 33 9 42
618 33 push @center_cells => [$x, $y];
619 }
620 }
621  
622 3 $self -> create_house ("CD" => map {cell_name @$_} @center_cells);
623 }
624  
625  
626 ################################################################################
627 #
628 # sub init_diagonals ($self, $args)
629 #
630 # If we have diagonals, it means cells on one or more diagonals
631 # should differ. This method initializes the houses for that.
632 #
633 # The main diagonal for a 9 x 9 sudoku is defined as follows:
634 #
635 # * . . . . . . . .
636 # . * . . . . . . .
637 # . . * . . . . . .
638 #
639 # . . . * . . . . .
640 # . . . . * . . . .
641 # . . . . . * . . .
642 #
643 # . . . . . . * . .
644 # . . . . . . . * .
645 # . . . . . . . . *
646 #
647 # The minor diagonal for a 9 x 9 sudoku is defined as follows:
648 #
649 # . . . . . . . . *
650 # . . . . . . . * .
651 # . . . . . . * . .
652 #
653 # . . . . . * . . .
654 # . . . . * . . . .
655 # . . . * . . . . .
656 #
657 # . . * . . . . . .
658 # . * . . . . . . .
659 # * . . . . . . . .
660 #
661 # TESTS: 050-init_diagonals.t
662 #
663 ################################################################################
664  
665  
666 95 ▶sub init_diagonals ($self, $args = {}) {
Subroutine init_diagonals: called CC=14 SCAR=26.4
Pod undocumented
667 95 ▶ my $diagonals = delete $$args {diagonals} or return $self;
Branch: 1 branch
BranchTrueFalseTotal
my $diagonals = delete $args->{"diagonals"} or return $self 73 22 95
668  
669 22 ▶ if (has_bit ($diagonals &. ~. $ALL_DIAGONALS) ||
Branch: 1 branch
BranchTrueFalseTotal
if (has_bit $diagonals &. ~.$Regexp::Sudoku::ALL_DIAGONALS or length $diagonals =~ s/\x{00}*$//ru > length $Regexp::Sudoku::ALL_DIAGONALS) 1 21 22
Condition66%2/3
has_bit $diagonals &. ~.$Regexp::Sudoku::ALL_DIAGONALS or length $diagonals =~ s/\x{00}*$//ru > length $Regexp::Sudoku::ALL_DIAGONALS
l!l&&r!l&&!r
0121
MC/DC50%1/2
has_bit $diagonals &. ~.$Regexp::Sudoku::ALL_DIAGONALS or length $diagonals =~ s/\x{00}*$//ru > length $Regexp::Sudoku::ALL_DIAGONALS
has_bit $diagonals &. ~.$Regexp::Sudoku::ALL_DIAGONALS length $diagonals =~ s/\x{00}*$//ru > length $Regexp::Sudoku::ALL_DIAGONALS
Truth table66%2/3
A or B
A= has_bit $diagonals &. ~.$Regexp::Sudoku::ALL_DIAGONALSB= length $diagonals =~ s/\x{00}*$//ru > length $Regexp::Sudoku::ALL_DIAGONALS
ABresult
1X1
011
000
670 length ($diagonals =~ s/\x{00}*$//r) > length ($ALL_DIAGONALS)) {
671 1 my $out = "";
672 1 my $r = $diagonals &. ~. $ALL_DIAGONALS;
673 1 ▶ for (my $i = 0; $i < 8 * length ($r); $i ++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < 8 * length($r)) 144 1 145
674 144 ▶ $out .= vec ($r, $i, 1) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
vec($r, $i, 1) ? : 0 144 144
675 }
676 1 die sprintf "Unknown diagonal(s): %s\n", $out;
677 }
678  
679 21 my $size = $self -> size;
680  
681 #
682 # Top left to bottom right
683 #
684 21 ▶ if (has_bit ($diagonals &. $MAIN)) {
Branch: 1 branch
BranchTrueFalseTotal
if (has_bit $diagonals &. $Regexp::Sudoku::MAIN) 7 14 21
685 7 $self -> create_house ("DM" => map {cell_name $_, $_} 1 .. $size)
686 }
687  
688 #
689 # Bottom left to top right
690 #
691 21 ▶ if (has_bit ($diagonals &. $MINOR)) {
Branch: 1 branch
BranchTrueFalseTotal
if (has_bit $diagonals &. $Regexp::Sudoku::MINOR) 6 15 21
692 6 $self -> create_house ("Dm" => map {cell_name $size - $_ + 1, $_}
693 1 .. $size)
694 }
695  
696 #
697 # Offsets
698 #
699 21 foreach my $s (1 .. $size - 1) {
700 196 my ($sub, $super, $minor_sub, $minor_super) = do {
701 38 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
702 196 (${"SUB$s"}, ${"SUPER$s"}, ${"MINOR_SUB$s"}, ${"MINOR_SUPER$s"});
703 };
704 196 ▶ if (has_bit ($diagonals &. $super)) {
Branch: 1 branch
BranchTrueFalseTotal
if (has_bit $diagonals &. $super) 11 185 196
705 11 my $name = "DMS$s";
706 $self -> create_house ($name =>
707 11 map {cell_name $_, $_ + $s} 1 .. $size - $s);
708 }
709 196 ▶ if (has_bit ($diagonals &. $sub)) {
Branch: 1 branch
BranchTrueFalseTotal
if (has_bit $diagonals &. $sub) 11 185 196
710 11 my $name = "DMs$s";
711 $self -> create_house ($name =>
712 11 map {cell_name $_, $_ - $s} 1 + $s .. $size);
713 }
714 196 ▶ if (has_bit ($diagonals &. $minor_super)) {
Branch: 1 branch
BranchTrueFalseTotal
if (has_bit $diagonals &. $minor_super) 11 185 196
715 11 my $name = "DmS$s";
716 $self -> create_house ($name =>
717 11 map {cell_name $size - $_ + 1, $_ - $s} 1 + $s .. $size);
718 }
719 196 ▶ if (has_bit ($diagonals &. $minor_sub)) {
Branch: 1 branch
BranchTrueFalseTotal
if (has_bit $diagonals &. $minor_sub) 11 185 196
720 11 my $name = "Dms$s";
721 $self -> create_house ($name =>
722 11 map {cell_name $size - $_ + 1, $_ + $s} 1 .. $size - $s);
723 }
724 }
725  
726 $self
727 21 }
728  
729  
730 ################################################################################
731 #
732 # cell2houses ($self, $cell)
733 #
734 # Give the name of a cell, return the names of all the houses this cell
735 # is part off.
736 #
737 # TESTS: 040-houses.t
738 #
739 ################################################################################
740  
741 ▶sub cell2houses ($self, $cell) {
Subroutine cell2houses: called CC=2 SCAR=7.2
Pod undocumented
742 302222 ▶ keys %{$cell2houses {$self} {$cell} || {}}
Branch: 1 branch
BranchTrueFalseTotal
$cell2houses{$self}{$cell} or {} 0 302222 302222
743 }
744  
745  
746 ################################################################################
747 #
748 # house2cells ($self, $house)
749 #
750 # Give the name of a house, return the names of all the cells in this house.
751 #
752 # TESTS: 040-houses.t
753 #
754 ################################################################################
755  
756 ▶sub house2cells ($self, $house) {
Subroutine house2cells: called CC=2 SCAR=7.2
Pod undocumented
757 370 ▶ keys %{$house2cells {$self} {$house} || {}}
Branch: 1 branch
BranchTrueFalseTotal
$house2cells{$self}{$house} or {} 0 370 370
758 }
759  
760  
761 ################################################################################
762 #
763 # cells ($self)
764 #
765 # Return the names of all the cells in the sudoku.
766 #
767 # TESTS: 040-houses.t
768 #
769 ################################################################################
770  
771 31 ▶sub cells ($self, $sorted = 0) {
Subroutine cells: called CC=16 SCAR=27.7
Pod undocumented
772 31 my @cells = sort keys %{$cell2houses {$self}};
773 31 ▶ if ($sorted) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sorted) 25 6 31
774 25 state $CELL_NAME = 0;
775 25 state $IS_CLUE = $CELL_NAME + 1;
776 25 state $EVEN_ODD = $IS_CLUE + 1;
777 25 state $CLUES_SEEN = $EVEN_ODD + 1;
778 25 state $NR_OF_HOUSES = $CLUES_SEEN + 1;
779  
780 #
781 # For each cell, determine how many different clues it sees.
782 #
783 25 my %sees;
784 25 foreach my $cell1 (@cells) {
785 1805 ▶ next if $self -> clue ($cell1); # Don't care about clues
Branch: 1 branch
BranchTrueFalseTotal
if $self->clue($cell1) 555 1250 1805
786 1250 foreach my $cell2 (@cells) {
787 97635 ▶ if ($self -> clue ($cell2) &&
Branch: 1 branch
BranchTrueFalseTotal
if ($self->clue($cell2) and $self->must_differ($cell1, $cell2)) 7877 89758 97635
Condition100%3/3
$self->clue($cell2) and $self->must_differ($cell1, $cell2)
!ll&&!rl&&r
68134216247877
MC/DC100%2/2
$self->clue($cell2) and $self->must_differ($cell1, $cell2)
$self->clue($cell2) $self->must_differ($cell1, $cell2)
Truth table100%3/3
A and B
A= $self->clue($cell2)B= $self->must_differ($cell1, $cell2)
ABresult
0X0
100
111
788 $self -> must_differ ($cell1, $cell2)) {
789 7877 $sees {$cell1} {$self -> clue ($cell2)} = 1;
790 }
791 }
792 }
793  
794 1805 @cells = map {$$_ [$CELL_NAME]}
795 8959 ▶ sort {$$b [$IS_CLUE] <=> $$a [$IS_CLUE] ||
Branch: 1 branch
BranchTrueFalseTotal
$Regexp::Sudoku::b->[$IS_CLUE] <=> $Regexp::Sudoku::a->[$IS_CLUE] or $Regexp::Sudoku::b->[$EVEN_ODD] <=> $Regexp::Sudoku::a->[$EVEN_ODD] or $Regexp::Sudoku::b->[$CLUES_SEEN] <=> $Regexp::Sudoku::a->[$CLUES_SEEN] or $Regexp::Sudoku::b->[$NR_OF_HOUSES] <=> $Regexp::Sudoku::a->[$NR_OF_HOUSES] or $Regexp::Sudoku::a->[$CELL_NAME] cmp $Regexp::Sudoku::b->[$CELL_NAME] 3480 5479 8959
Condition100%9/9
$Regexp::Sudoku::b->[$IS_CLUE] <=> $Regexp::Sudoku::a->[$IS_CLUE] or $Regexp::Sudoku::b->[$EVEN_ODD] <=> $Regexp::Sudoku::a->[$EVEN_ODD] or $Regexp::Sudoku::b->[$CLUES_SEEN] <=> $Regexp::Sudoku::a->[$CLUES_SEEN] or $Regexp::Sudoku::b->[$NR_OF_HOUSES] <=> $Regexp::Sudoku::a->[$NR_OF_HOUSES]
l!l&&r!l&&!r
48716083480
$Regexp::Sudoku::b->[$IS_CLUE] <=> $Regexp::Sudoku::a->[$IS_CLUE] or $Regexp::Sudoku::b->[$EVEN_ODD] <=> $Regexp::Sudoku::a->[$EVEN_ODD] or $Regexp::Sudoku::b->[$CLUES_SEEN] <=> $Regexp::Sudoku::a->[$CLUES_SEEN]
l!l&&r!l&&!r
210927624088
$Regexp::Sudoku::b->[$IS_CLUE] <=> $Regexp::Sudoku::a->[$IS_CLUE] or $Regexp::Sudoku::b->[$EVEN_ODD] <=> $Regexp::Sudoku::a->[$EVEN_ODD]
l!l&&r!l&&!r
2010996850
MC/DC0%0/4
$Regexp::Sudoku::b->[$IS_CLUE] <=> $Regexp::Sudoku::a->[$IS_CLUE] or $Regexp::Sudoku::b->[$EVEN_ODD] <=> $Regexp::Sudoku::a->[$EVEN_ODD] or $Regexp::Sudoku::b->[$CLUES_SEEN] <=> $Regexp::Sudoku::a->[$CLUES_SEEN] or $Regexp::Sudoku::b->[$NR_OF_HOUSES] <=> $Regexp::Sudoku::a->[$NR_OF_HOUSES]
$Regexp::Sudoku::b->[$IS_CLUE] <=> $Regexp::Sudoku::a->[$IS_CLUE] $Regexp::Sudoku::b->[$EVEN_ODD] <=> $Regexp::Sudoku::a->[$EVEN_ODD] $Regexp::Sudoku::b->[$CLUES_SEEN] <=> $Regexp::Sudoku::a->[$CLUES_SEEN] $Regexp::Sudoku::b->[$NR_OF_HOUSES] <=> $Regexp::Sudoku::a->[$NR_OF_HOUSES]
Truth table0%0/5
A or B or C or D
A= $Regexp::Sudoku::b->[$IS_CLUE] <=> $Regexp::Sudoku::a->[$IS_CLUE]B= $Regexp::Sudoku::b->[$EVEN_ODD] <=> $Regexp::Sudoku::a->[$EVEN_ODD]C= $Regexp::Sudoku::b->[$CLUES_SEEN] <=> $Regexp::Sudoku::a->[$CLUES_SEEN]D= $Regexp::Sudoku::b->[$NR_OF_HOUSES] <=> $Regexp::Sudoku::a->[$NR_OF_HOUSES]
ABCDresult
1XXX1
01XX1
001X1
00011
00000
796 $$b [$EVEN_ODD] <=> $$a [$EVEN_ODD] ||
797 $$b [$CLUES_SEEN] <=> $$a [$CLUES_SEEN] ||
798 $$b [$NR_OF_HOUSES] <=> $$a [$NR_OF_HOUSES] ||
799 $$a [$CELL_NAME] cmp $$b [$CELL_NAME]}
800 25 map {my $r = [];
801 1805 $$r [$CELL_NAME] = $_;
802 1805 ▶ $$r [$IS_CLUE] = $self -> clue ($_) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->clue($_) ? : 555 1250 1805
803 1805 ▶ $$r [$EVEN_ODD] = $self -> is_even ($_) ||
Branch: 1 branch
BranchTrueFalseTotal
$self->is_even($_) || $self->is_odd($_) ? : 91 1714 1805
Condition100%3/3
$self->is_even($_) or $self->is_odd($_)
l!l&&r!l&&!r
49421714
MC/DC100%2/2
$self->is_even($_) or $self->is_odd($_)
$self->is_even($_) $self->is_odd($_)
Truth table100%3/3
A or B
A= $self->is_even($_)B= $self->is_odd($_)
ABresult
1X1
011
000
804 $self -> is_odd ($_) ? 1 : 0;
805 1805 ▶ $$r [$CLUES_SEEN] = keys (%{$sees {$_} || {}});
Branch: 1 branch
BranchTrueFalseTotal
$sees{$_} or {} 555 1250 1805
806 1805 $$r [$NR_OF_HOUSES] = $self -> cell2houses ($_);
807 1805 $r}
808 @cells;
809 }
810 31 @cells;
811 }
812  
813  
814 ################################################################################
815 #
816 # houses ($self)
817 #
818 # Return the names of all the houses in the sudoku.
819 #
820 # TESTS: 040-houses.t
821 #
822 ################################################################################
823  
824 ▶sub houses ($self) {
Subroutine houses: called CC=1 SCAR=0.0
Pod documented
825 6 keys %{$house2cells {$self}}
826 }
827  
828  
829 ################################################################################
830 #
831 # init_clues ($self, $args)
832 #
833 # Take the supplied clues (if any!), and return a structure which maps cell
834 # names to clue values.
835 #
836 # The clues could be one of:
837 # - A 2-d array, with false values indicating the cell doesn't have a clue.
838 # A "." will also be consider to be not a clue.
839 # - A string, newlines separating rows, and whitespace clues. A value
840 # of 0 or "." indicates no clue.
841 #
842 # We wil populate the clues attribute, mapping cell names to clue values.
843 # Cells without clues won't be set.
844 #
845 # TESTS: 080-clues.t
846 #
847 ################################################################################
848  
849 ▶sub init_clues ($self, $args) {
Subroutine init_clues: called CC=9 SCAR=22.0
Pod undocumented
850 94 ▶ my $in_clues = delete $$args {clues} or return $self;
Branch: 1 branch
BranchTrueFalseTotal
my $in_clues = delete $args->{"clues"} or return $self 65 29 94
851  
852 29 my $clues = {};
853 29 my $is_even = {};
854 29 my $is_odd = {};
855 #
856 # Turn a string into an array
857 #
858 29 ▶ if (!ref $in_clues) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $in_clues) 28 1 29
859 28 my @rows = grep {/\S/} split /\n/ => $in_clues;
860 28 $in_clues = [map {[split]} @rows];
861 }
862 29 foreach my $r (keys @$in_clues) {
863 245 foreach my $c (keys @{$$in_clues [$r]}) {
864 2093 my $val = $$in_clues [$r] [$c];
865 2093 ▶ next if !$val || $val eq ".";
Branch: 1 branch
BranchTrueFalseTotal
if not $val or $val eq "." 1263 830 2093
Condition100%3/3
not $val or $val eq "."
l!l&&r!l&&!r
781185830
MC/DC100%2/2
not $val or $val eq "."
not $val $val eq "."
Truth table100%3/3
not(A) or B
A= not $valB= $val eq "."
ABresult
1X1
011
000
866 830 my $cell = cell_name $r + 1, $c + 1;
867 830 ▶ if ($val eq 'e') {$$is_even {$cell} = 1}
Branch: 2 branches
BranchTrueFalseTotal
if ($val eq 'e') { } 94 736 830
elsif ($val eq 'o') { } 82 654 736
868 82 elsif ($val eq 'o') {$$is_odd {$cell} = 1}
869 654 else {$$clues {$cell} = $val}
870 }
871 }
872 29 $clues {$self} = $clues;
873 29 $is_even {$self} = $is_even;
874 29 $is_odd {$self} = $is_odd;
875  
876 29 $self;
877 }
878  
879  
880 ################################################################################
881 #
882 # clues ($self)
883 #
884 # Return an hashref mapping cell names to clues.
885 #
886 # TESTS: 080-clues.t
887 #
888 ################################################################################
889  
890 ▶sub clues ($self) {
Subroutine clues: called CC=1 SCAR=0.0
Pod documented
891 1 $clues {$self};
892 }
893  
894  
895 ################################################################################
896 #
897 # clue ($self, $cell)
898 #
899 # Returns the clue in the given cell. If the cell does not have a clue,
900 # return false.
901 #
902 # TESTS: 080-clues.t
903 #
904 ################################################################################
905  
906 ▶sub clue ($self , $cell) {
Subroutine clue: called CC=1 SCAR=0.0
Pod undocumented
907 189706 $clues {$self} {$cell}
908 }
909  
910  
911 ################################################################################
912 #
913 # is_even ($self, $cell)
914 # is_odd ($self, $cell)
915 #
916 # Returns wether the cell is given to be even/odd.
917 #
918 # TESTS: 081-is_even_odd.t
919 #
920 ################################################################################
921  
922 ▶sub is_even ($self, $cell) {
Subroutine is_even: called CC=1 SCAR=0.0
Pod undocumented
923 3187 $is_even {$self} {$cell}
924 }
925 ▶sub is_odd ($self, $cell) {
Subroutine is_odd: called CC=1 SCAR=0.0
Pod undocumented
926 3071 $is_odd {$self} {$cell}
927 }
928  
929  
930 ################################################################################
931 #
932 # init_constraints ($self, $args)
933 #
934 # Set the constraints for the sudoku. Die if the constrainst do not validate.
935 #
936 # TESTS: 060-constraints.t
937 #
938 ################################################################################
939  
940 98 ▶sub init_constraints ($self, $args = {}) {
Subroutine init_constraints: called CC=7 SCAR=19.5
Pod undocumented
941 98 ▶ my $constraints = $constraints {$self} = delete $$args {constraints} || "";
Condition100%2/2
delete $args->{'constraints'} || ''
l!l
1088
MC/DC100%1/1
delete $args->{'constraints'} || ''
delete $args->{'constraints'}
Truth table100%2/2
A
A= delete $args->{'constraints'}
Aresult
11
00
942 98 ▶ if (has_bit ($constraints &. ~. $ALL_CONSTRAINTS) ||
Branch: 1 branch
BranchTrueFalseTotal
if (has_bit $constraints &. ~.$Regexp::Sudoku::ALL_CONSTRAINTS or length $constraints =~ s/\x{00}*$//ru > length $Regexp::Sudoku::ALL_CONSTRAINTS) 1 97 98
Condition66%2/3
has_bit $constraints &. ~.$Regexp::Sudoku::ALL_CONSTRAINTS or length $constraints =~ s/\x{00}*$//ru > length $Regexp::Sudoku::ALL_CONSTRAINTS
l!l&&r!l&&!r
1097
MC/DC50%1/2
has_bit $constraints &. ~.$Regexp::Sudoku::ALL_CONSTRAINTS or length $constraints =~ s/\x{00}*$//ru > length $Regexp::Sudoku::ALL_CONSTRAINTS
has_bit $constraints &. ~.$Regexp::Sudoku::ALL_CONSTRAINTS length $constraints =~ s/\x{00}*$//ru > length $Regexp::Sudoku::ALL_CONSTRAINTS
Truth table66%2/3
A or B
A= has_bit $constraints &. ~.$Regexp::Sudoku::ALL_CONSTRAINTSB= length $constraints =~ s/\x{00}*$//ru > length $Regexp::Sudoku::ALL_CONSTRAINTS
ABresult
1X1
011
000
943 length ($constraints =~ s/\x{00}*$//r) > length ($ALL_CONSTRAINTS)) {
944 1 my $out = "";
945 1 my $r = $constraints &. ~. $ALL_CONSTRAINTS;
946 1 ▶ for (my $i = 0; $i < 8 * length ($r); $i ++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < 8 * length($r)) 8 1 9
947 8 ▶ $out .= vec ($r, $i, 1) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
vec($r, $i, 1) ? : 2 6 8
948 }
949 1 die sprintf "Unknown constraint(s) %s\n", $out;
950 }
951  
952 97 $self;
953 }
954  
955  
956 ################################################################################
957 #
958 # constraints ($self)
959 #
960 # Return the constraints set for this sudoku.
961 #
962 # TESTS: 060-constraints.t
963 #
964 ################################################################################
965  
966 ▶sub constraints ($self) {
Subroutine constraints: called CC=2 SCAR=6.9
Pod documented
967 145853 ▶ $constraints {$self} || 0;
Branch: 1 branch
BranchTrueFalseTotal
$constraints{$self} or 0 121996 23857 145853
968 }
969  
970  
971  
972 ################################################################################
973 #
974 # init ($self, %args)
975 #
976 # Configure the Regexp::Sudoku object.
977 #
978 # TESTS: *.t
979 #
980 ################################################################################
981  
982  
983 ▶sub init ($self, %args) {
Subroutine init: called CC=2 SCAR=7.4
Pod undocumented
984 97 my $args = {%args};
985  
986 97 $self -> init_sizes ($args)
987 -> init_values ($args)
988 -> init_houses ($args)
989 -> init_diagonals ($args)
990 -> init_constraints ($args)
991 -> init_clues ($args);
992  
993 94 ▶ if (keys %$args) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %$args) 0 94 94
994 0 die "Unknown parameter(s) to init: " . join (", " => keys %$args)
995 . "\n";
996 }
997  
998 94 $self;
999 }
1000  
1001  
1002 ################################################################################
1003 #
1004 # make_clue ($self, $cell, $value)
1005 #
1006 # Given a cell name, and a value, return a sub subject, and sub pattern
1007 # which sets the capture '$cell' to '$value'
1008 #
1009 # TESTS: 110-make_clue.t
1010 # 120-make_cell.t
1011 #
1012 ################################################################################
1013  
1014 ▶sub make_clue ($self, $cell) {
Subroutine make_clue: called CC=1 SCAR=0.0
Pod undocumented
1015 594 my $value = $self -> clue ($cell);
1016 594 my $subsub = $value;
1017 594 my $subpat = "(?<$cell>$value)";
1018  
1019 594 map {$_ . $SENTINEL} $subsub, $subpat;
1020 }
1021  
1022  
1023 ################################################################################
1024 #
1025 # make_empty ($cell)
1026 #
1027 # Given a cell name, return a sub subject and a sub pattern allowing the
1028 # cell to pick up one of the values in the sudoku.
1029 #
1030 # TESTS: 100-make_empty.t
1031 # 120-make_cell.t
1032 #
1033 ################################################################################
1034  
1035 1310 ▶sub make_empty ($self, $cell, $method = "values") {
Subroutine make_empty: called CC=2 SCAR=6.9
Pod undocumented
1036 1310 my $subsub = $self -> $method;
1037 1310 my $range = $self -> values_range;
1038 1310 my $subpat = "[$range]*(?<$cell>[$range])[$range]*";
1039  
1040 1310 map {$_ . $SENTINEL} $subsub, $subpat;
1041 }
1042  
1043 1179 ▶sub make_any ($self, $cell) {$self -> make_empty ($cell, "values")}
Subroutine make_any: called CC=1 SCAR=0.0
Pod undocumented
1044 70 ▶sub make_even ($self, $cell) {$self -> make_empty ($cell, "evens")}
Subroutine make_even: called CC=1 SCAR=0.0
Pod undocumented
1045 61 ▶sub make_odd ($self, $cell) {$self -> make_empty ($cell, "odds")}
Subroutine make_odd: called CC=1 SCAR=0.0
Pod undocumented
1046  
1047  
1048 ################################################################################
1049 #
1050 # make_cell ($cell)
1051 #
1052 # Given a cell name, return a subsubject and subpattern to set a value for
1053 # this cell. Either the cell has a clue (and we dispatch to make_clue),
1054 # or not (and we dispatch to make_empty).
1055 #
1056 # TESTS: 120-make_cell.t
1057 #
1058 ################################################################################
1059  
1060 ▶sub make_cell ($self, $cell) {
Subroutine make_cell: called CC=4 SCAR=13.9
Pod undocumented
1061 1886 my $clue = $self -> clue ($cell);
1062  
1063 1886 ▶ $self -> clue ($cell) ? $self -> make_clue ($cell)
Branch: 3 branches
BranchTrueFalseTotal
$self->clue($cell) ? : 585 1301 1886
$self->is_even($cell) ? : 67 1234 1301
$self->is_odd($cell) ? : 58 1176 1234
1064 : $self -> is_even ($cell) ? $self -> make_even ($cell)
1065 : $self -> is_odd ($cell) ? $self -> make_odd ($cell)
1066 : $self -> make_any ($cell)
1067 }
1068  
1069  
1070  
1071 ################################################################################
1072 #
1073 # semi_debruijn_seq
1074 #
1075 # Return, for the given values, a De Bruijn sequence of size 2 with
1076 # 1) Duplicates removed and
1077 # 2) The first character copied to the end
1078 #
1079 # TESTS: 130-semi_debruijn_seq.t
1080 #
1081 ################################################################################
1082  
1083 16990 ▶sub semi_debruijn_seq ($self, $values = $values {$self}) {
Subroutine semi_debruijn_seq: called CC=3 SCAR=11.0
Pod undocumented
1084 16990 state $cache;
1085 16990 ▶ $$cache {$values} //= do {
Condition66%2/3
$cache->{$values} //= do { my $seq = debruijn($values, 2); $seq .= substr($seq, 0, 1); $seq =~ s/(.)\g{1}/$1/gu; $seq }
l!l&&r!l&&!r
16965250
MC/DC0%0/2
$cache->{$values} //= do { my $seq = debruijn($values, 2); $seq .= substr($seq, 0, 1); $seq =~ s/(.)\g{1}/$1/gu; $seq }
$cache->{$values} do { my $seq = debruijn($values, 2); $seq .= substr($seq, 0, 1); $seq =~ s/(.)\g{1}/$1/gu; $seq }
Truth table66%2/3
A //= B
A= $cache->{$values}B= do { my $seq = debruijn($values, 2); $seq .= substr($seq, 0, 1); $seq =~ s/(.)\g{1}/$1/gu; $seq }
ABresult
1X1
011
000
1086 25 my $seq = debruijn ($values, 2);
1087 25 $seq .= substr $seq, 0, 1; # Copy first char to the end.
1088 25 $seq =~ s/(.)\g{1}/$1/g; # Remove duplicates.
1089 25 $seq;
1090 };
1091 }
1092  
1093  
1094  
1095 ################################################################################
1096 #
1097 # make_diff_clause ($self, $cell1, $cell2)
1098 #
1099 # Given two cell names, return a sub subject and a sub pattern which matches
1100 # iff the values in the cell differ.
1101 #
1102 # TESTS: 140-make_diff_clause.t
1103 #
1104 ################################################################################
1105  
1106 ▶sub make_diff_clause ($self, $cell1, $cell2) {
Subroutine make_diff_clause: called CC=1 SCAR=0.0
Pod undocumented
1107 16987 my $subsub = "";
1108 16987 my @values = $self -> values;
1109 16987 my $range = $self -> values_range;
1110  
1111 16987 my $seq = $self -> semi_debruijn_seq;
1112 16987 my $pat = "[$range]*\\g{$cell1}\\g{$cell2}[$range]*";
1113  
1114 16987 map {$_ . $SENTINEL} $seq, $pat;
1115 }
1116  
1117  
1118 ################################################################################
1119 #
1120 # must_differ ($self, $cell1, $cell2)
1121 #
1122 # Returns a true value if the two given cells must have different values.
1123 #
1124 # TESTS: 150-must_differ.t
1125 # 151-must_differ.t
1126 #
1127 ################################################################################
1128  
1129 ▶sub must_differ ($self, $cell1, $cell2) {
Subroutine must_differ: called CC=11 SCAR=24.0
Pod undocumented
1130 145850 my %seen;
1131 145850 $seen {$_} ++ for $self -> cell2houses ($cell1),
1132 $self -> cell2houses ($cell2);
1133  
1134 145850 my $same_house = grep {$_ > 1} values %seen;
1135  
1136 145850 my ($r1, $c1) = cell_row_column ($cell1);
1137 145850 my ($r2, $c2) = cell_row_column ($cell2);
1138 145850 my $constraints = $self -> constraints;
1139  
1140 145850 my $d_rows = abs ($r1 - $r2);
1141 145850 my $d_cols = abs ($c1 - $c2);
1142  
1143 145850 ▶ return $same_house
Branch: 1 branch
BranchTrueFalseTotal
$same_house || has_bit($constraints &. $Regexp::Sudoku::ANTI_KNIGHT) && ($d_rows == 1 && $d_cols == 2 || $d_rows == 2 && $d_cols == 1) || has_bit($constraints &. $Regexp::Sudoku::ANTI_KING) && $d_rows == 1 && $d_cols == 1 ? : 34773 111077 145850
Condition100%24/24
$same_house or has_bit $constraints &. $Regexp::Sudoku::ANTI_KNIGHT and $d_rows == 1 && $d_cols == 2 || $d_rows == 2 && $d_cols == 1 or has_bit $constraints &. $Regexp::Sudoku::ANTI_KING and $d_rows == 1 and $d_cols == 1
l!l&&r!l&&!r
34561212111077
$same_house or has_bit $constraints &. $Regexp::Sudoku::ANTI_KNIGHT and $d_rows == 1 && $d_cols == 2 || $d_rows == 2 && $d_cols == 1
l!l&&r!l&&!r
33977298111575
has_bit $constraints &. $Regexp::Sudoku::ANTI_KNIGHT and $d_rows == 1 && $d_cols == 2 || $d_rows == 2 && $d_cols == 1
!ll&&!rl&&r
1029038672298
$d_rows == 1 and $d_cols == 2 or $d_rows == 2 and $d_cols == 1
l!l&&r!l&&!r
2882968386
$d_rows == 1 and $d_cols == 2
!ll&&!rl&&r
72391443288
$d_rows == 2 and $d_cols == 1
!ll&&!rl&&r
69931393296
has_bit $constraints &. $Regexp::Sudoku::ANTI_KING and $d_rows == 1 and $d_cols == 1
!ll&&!rl&&r
1095391538212
has_bit $constraints &. $Regexp::Sudoku::ANTI_KING and $d_rows == 1
!ll&&!rl&&r
10233172081750
MC/DC100%9/9
$same_house or has_bit $constraints &. $Regexp::Sudoku::ANTI_KNIGHT and $d_rows == 1 && $d_cols == 2 || $d_rows == 2 && $d_cols == 1 or has_bit $constraints &. $Regexp::Sudoku::ANTI_KING and $d_rows == 1 and $d_cols == 1
$same_house has_bit $constraints &. $Regexp::Sudoku::ANTI_KNIGHT $d_rows == 1 $d_cols == 2 $d_rows == 2 $d_cols == 1 has_bit $constraints &. $Regexp::Sudoku::ANTI_KING $d_rows == 1 $d_cols == 1
Truth table41%10/24
A or B and C and D or E and F or G and H and I
A= $same_houseB= has_bit $constraints &. $Regexp::Sudoku::ANTI_KNIGHTC= $d_rows == 1D= $d_cols == 2E= $d_rows == 2F= $d_cols == 1G= has_bit $constraints &. $Regexp::Sudoku::ANTI_KINGH= $d_rows == 1I= $d_cols == 1
ABCDEFGHIresult
1XXXXXXXX1
0111XXXXX1
010X11XXX1
011011XXX1
00XXXX1111
010X0X1111
010X101111
01100X1111
0110101111
00XXXX0XX0
00XXXX10X0
00XXXX1100
010X0X0XX0
010X0X10X0
010X0X1100
010X100XX0
010X1010X0
010X101100
01100X0XX0
01100X10X0
01100X1100
0110100XX0
01101010X0
0110101100
1144 || has_bit ($constraints &. $ANTI_KNIGHT) &&
1145 (($d_rows == 1 && $d_cols == 2) ||
1146 ($d_rows == 2 && $d_cols == 1))
1147 || has_bit ($constraints &. $ANTI_KING) &&
1148 $d_rows == 1 && $d_cols == 1
1149 ? 1 : 0;
1150 }
1151  
1152  
1153 ################################################################################
1154 #
1155 # init_subject_and_pattern ($self)
1156 #
1157 # Create the subject we're going to match against, and the pattern
1158 # we use to match.
1159 #
1160 # TESTS: TODO
1161 #
1162 ################################################################################
1163  
1164 ▶sub init_subject_and_pattern ($self) {
Subroutine init_subject_and_pattern: called CC=8 SCAR=20.8
Pod undocumented
1165 50 ▶ return $self if $subject {$self} && $pattern {$self};
Branch: 1 branch
BranchTrueFalseTotal
if $subject{$self} and $pattern{$self} 25 25 50
Condition66%2/3
$subject{$self} and $pattern{$self}
!ll&&!rl&&r
25025
MC/DC50%1/2
$subject{$self} and $pattern{$self}
$subject{$self} $pattern{$self}
Truth table66%2/3
A and B
A= $subject{$self}B= $pattern{$self}
ABresult
0X0
100
111
1166  
1167 25 my $subject = "";
1168 25 my $pattern = "";
1169  
1170 25 my @cells = $self -> cells (1);
1171  
1172 25 for my $i (keys @cells) {
1173 #
1174 # First the part which picks up a value for this cell
1175 #
1176 1805 my $cell1 = $cells [$i];
1177  
1178 1805 my ($subsub, $subpat) = $self -> make_cell ($cell1);
1179 1805 $subject .= $subsub;
1180 1805 $pattern .= $subpat;
1181  
1182 #
1183 # Now, for all the previous cells, if they must differ,
1184 # add a clause for that.
1185 #
1186 1805 for my $j (0 .. $i - 1) {
1187 69540 my $cell2 = $cells [$j];
1188 #
1189 # If both cells are a clue, we don't need a restriction
1190 # between the cells.
1191 #
1192 69540 ▶ next if $self -> clue ($cell1) && $self -> clue ($cell2);
Branch: 1 branch
BranchTrueFalseTotal
if $self->clue($cell1) and $self->clue($cell2) 6597 62943 69540
Condition66%2/3
$self->clue($cell1) and $self->clue($cell2)
!ll&&!rl&&r
6294306597
MC/DC50%1/2
$self->clue($cell1) and $self->clue($cell2)
$self->clue($cell1) $self->clue($cell2)
Truth table66%2/3
A and B
A= $self->clue($cell1)B= $self->clue($cell2)
ABresult
0X0
100
111
1193  
1194 62943 ▶ if ($self -> must_differ ($cell1, $cell2)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->must_differ($cell1, $cell2)) 16978 45965 62943
1195 16978 my ($subsub, $subpat) =
1196 $self -> make_diff_clause ($cell1, $cell2);
1197 16978 $subject .= $subsub;
1198 16978 $pattern .= $subpat;
1199 }
1200 }
1201 }
1202  
1203 25 $subject {$self} = $subject;
1204 25 $pattern {$self} = "^" . $pattern . '$';
1205  
1206 25 $self;
1207 }
1208  
1209  
1210 ################################################################################
1211 #
1212 # subject ($self)
1213 #
1214 # Return the subject we're matching against.
1215 #
1216 # TESTS: Test.pm
1217 #
1218 ################################################################################
1219  
1220 ▶sub subject ($self) {
Subroutine subject: called CC=1 SCAR=0.0
Pod undocumented
1221 25 $self -> init_subject_and_pattern;
1222 25 $subject {$self}
1223 }
1224  
1225  
1226 ################################################################################
1227 #
1228 # pattern ($self)
1229 #
1230 # Return the pattern we're matching with.
1231 #
1232 # TESTS: Test.pm
1233 #
1234 ################################################################################
1235  
1236 ▶sub pattern ($self) {
Subroutine pattern: called CC=1 SCAR=0.0
Pod undocumented
1237 25 $self -> init_subject_and_pattern;
1238 25 $pattern {$self}
1239 }
1240  
1241 __END__
↑ Summary Sudoku/Constants.pm »