Sudoku.pm

filter: Statementstmt 96.1% 320 / 333 Branchbran 86.1% 62 / 72 Conditioncond 83.9% 68 / 81 MC/DCmcdc 50.0% 20 / 40 Subroutinesub 100% 55 / 55 Podpod 25.6% 10 / 39 totaltotal 86.2% 535 / 620 CC 101 SCAR 21.4
CC
101
Cov
93%
cells
22.2
init_subject_and_pattern
20.2
must_differ
11.0
CRAP 8.5
« Sudoku/German_Whisper.pm ↑ Summary Sudoku/Diagonal.pm »
1 package Regexp::Sudoku;
2  
3 36 ▶use 5.028;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 36 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 36 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 36 ▶no warnings 'syntax';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 36 ▶use experimental 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 36 ▶use experimental 'lexical_subs';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 36 our $VERSION = '2022062101';
12  
13 36 ▶use lib qw [lib];
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 36 ▶use Hash::Util::FieldHash qw [fieldhash];
Subroutine BEGIN: called CC=1 SCAR=0.0
16 36 ▶use List::Util qw [min max];
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 36 ▶use Regexp::Sudoku::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 36 ▶use Regexp::Sudoku::Battenburg;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 36 ▶use Regexp::Sudoku::Quadruple;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 36 ▶use Regexp::Sudoku::Diagonal;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 36 ▶use Regexp::Sudoku::Parity;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 36 ▶use Regexp::Sudoku::Renban;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 36 ▶use Regexp::Sudoku::German_Whisper;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 36 our @ISA = qw [
27 Regexp::Sudoku::Battenburg
28 Regexp::Sudoku::Quadruple
29 Regexp::Sudoku::Diagonal
30 Regexp::Sudoku::Parity
31 Regexp::Sudoku::Renban
32 Regexp::Sudoku::German_Whisper
33 ];
34  
35  
36 36 fieldhash my %size;
37 36 fieldhash my %values;
38 36 fieldhash my %box_width;
39 36 fieldhash my %box_height;
40 36 fieldhash my %values_range;
41 36 fieldhash my %cell2houses;
42 36 fieldhash my %house2cells;
43 36 fieldhash my %clues;
44 36 fieldhash my %subject;
45 36 fieldhash my %pattern;
46 36 fieldhash my %constraints;
47  
48  
49 ################################################################################
50 #
51 # new ($class)
52 #
53 # Create an uninitialized object.
54 #
55 ################################################################################
56  
57 95 ▶sub new ($class) {bless \do {my $v} => $class}
Subroutine new: called CC=1 SCAR=0.0
Pod documented
58  
59  
60 ################################################################################
61 #
62 # init_sizes ($self, $size)
63 #
64 # Initialize the sizes of the soduko.
65 #
66 # Calls init_size () and init_box () doing the work.
67 #
68 # TESTS: 010_size.t
69 #
70 ################################################################################
71  
72 93 ▶sub init_sizes ($self, $args) {
Subroutine init_sizes: called CC=1 SCAR=0.0
Pod undocumented
73 93 $self -> init_size ($args)
74 -> init_box ($args);
75 }
76  
77  
78 ################################################################################
79 #
80 # init_size ($self, $size)
81 #
82 # Initialize the size of a sudoku. If the size is not given, use the default.
83 #
84 # TESTS: 010_size.t
85 #
86 ################################################################################
87  
88 93 ▶sub init_size ($self, $args = {}) {
Subroutine init_size: called CC=4 SCAR=13.9
Pod undocumented
89 93 ▶ $size {$self} = delete $$args {size} || $DEFAULT_SIZE;
Condition66%2/3
delete $args->{'size'} || $Regexp::Sudoku::DEFAULT_SIZE
l!l&&r!l&&!r
64290
MC/DC0%0/2
delete $args->{'size'} || $Regexp::Sudoku::DEFAULT_SIZE
delete $args->{'size'} $Regexp::Sudoku::DEFAULT_SIZE
Truth table66%2/3
A || B
A= delete $args->{'size'}B= $Regexp::Sudoku::DEFAULT_SIZE
ABresult
1X1
011
000
90 die "Size should not exceed $NR_OF_SYMBOLS\n"
91 93 ▶ if $size {$self} > $NR_OF_SYMBOLS;
Branch: 1 branch
BranchTrueFalseTotal
if $size{$self} > $Regexp::Sudoku::NR_OF_SYMBOLS 1 92 93
92 92 $self;
93 }
94  
95  
96 ################################################################################
97 #
98 # size ($self)
99 #
100 # Returns the size of the sudoku.
101 #
102 # TESTS: 010_size.t
103 #
104 ################################################################################
105  
106 2535 ▶sub size ($self) {
Subroutine size: called CC=1 SCAR=0.0
Pod documented
107 2535 $size {$self}
108 }
109  
110  
111 ################################################################################
112 #
113 # init_values ($self, $args)
114 #
115 # Initializes the values. We calculate them from the range (1-9, A-Z),
116 # as many as needed.
117 #
118 # TESTS: 020_values.t
119 #
120 ################################################################################
121  
122 77 ▶sub init_values ($self, $args = {}) {
Subroutine init_values: called CC=3 SCAR=11.0
Pod undocumented
123 77 my $size = $self -> size;
124 77 my $values = join "" => 1 .. min $size, $NR_OF_DIGITS;
125 77 ▶ if ($size > $NR_OF_DIGITS) {
Branch: 1 branch
BranchTrueFalseTotal
if ($size > $Regexp::Sudoku::NR_OF_DIGITS) 17 60 77
126 $values .= join "" =>
127 17 map {chr (ord ('A') + $_ - $NR_OF_DIGITS - 1)}
128 ($NR_OF_DIGITS + 1) .. min $size, $NR_OF_SYMBOLS;
129 }
130  
131 77 $values {$self} = $values;
132 77 $self -> init_values_range ($args);
133 77 $self;
134 }
135  
136  
137 ################################################################################
138 #
139 # init_value_ranges ($self, $args)
140 #
141 # Processes the values to turn them into ranges for use in a character class
142 #
143 # TESTS: 020_values.t
144 #
145 ################################################################################
146  
147 77 ▶sub init_values_range ($self, $args = {}) {
Subroutine init_values_range: called CC=4 SCAR=14.0
Pod undocumented
148 77 my @values = sort {$a cmp $b} $self -> values;
149 77 my $size = $self -> size;
150 77 my $range = "1-";
151  
152 77 ▶ if ($size < 10) {$range .= $values [-1];}
Branch: 2 branches
BranchTrueFalseTotal
if ($size < 10) { } 60 17 77
elsif ($size == 10) { } 0 17 17
153 0 elsif ($size == 10) {$range .= "9A"; }
154 17 else {$range .= "9A-" . $values [-1];}
155  
156 77 $values_range {$self} = $range;
157  
158 77 $self;
159 }
160  
161  
162 ################################################################################
163 #
164 # values ($self)
165 #
166 # Return the set of values used in the sudoku. In list context, this will
167 # be an array of characters; in scalar context, a string.
168 #
169 # TESTS: 020_values.t
170 #
171 ################################################################################
172  
173 39892 ▶sub values ($self) {
Subroutine values: called CC=2 SCAR=6.9
Pod undocumented
174 39892 ▶ wantarray ? split // => $values {$self} : $values {$self};
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 19362 20530 39892
175 }
176  
177  
178 ################################################################################
179 #
180 # values_range ($self, $with_zero = 0)
181 #
182 # Return the set of values used in the sudoku, as ranges to be used in
183 # a character class. If $with_zero is true, we include '0' in the range
184 # as well. Note, '0' will *never* be an acceptable value.
185 #
186 # TESTS: 020_values.t
187 #
188 ################################################################################
189  
190 20812 ▶sub values_range ($self, $with_zero = 0) {
Subroutine values_range: called CC=3 SCAR=11.0
Pod undocumented
191 20812 my $range = $values_range {$self};
192 20812 ▶ $range =~ s/1/0/ if $with_zero;
Branch: 1 branch
BranchTrueFalseTotal
if $with_zero 27 20785 20812
193 20812 $range;
194 }
195  
196  
197 ################################################################################
198 #
199 # init_box ($self, $args)
200 #
201 # Find the width and height of a box. If the size of the sudoku is a square,
202 # the width and height of a box are equal, and the square root of the size
203 # of the sudoku. Else, we'll find the most squarish width and height (with
204 # the width larger than the height). The width and height are stored as
205 # attributes. If they are already set, the function immediately returns.
206 #
207 # TESTS: 030-box.t
208 #
209 ################################################################################
210  
211 92 ▶sub init_box ($self, $args = {}) {
Subroutine init_box: called CC=5 SCAR=16.4
Pod undocumented
212 92 ▶ return if $box_height {$self} && $box_width {$self};
Branch: 1 branch
BranchTrueFalseTotal
if $box_height{$self} and $box_width{$self} 0 92 92
Condition33%1/3
$box_height{$self} and $box_width{$self}
!ll&&!rl&&r
9200
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
213 92 my $size = $self -> size;
214 92 my $box_height = int sqrt $size;
215 92 ▶ $box_height -- while $size % $box_height;
Branch: 1 branch
BranchTrueFalseTotal
$size % $box_height and do { --$box_height } 3 92 95
216 92 my $box_width = $size / $box_height;
217  
218 92 $box_height {$self} = $box_height;
219 92 $box_width {$self} = $box_width;
220  
221 92 $self;
222 }
223  
224  
225 ################################################################################
226 #
227 # box_height ($self)
228 # box_width ($self)
229 #
230 # Return the height and width of a box in the sudoku. These methods will
231 # call $self -> box_init first, to calculate the values if necessary.
232 #
233 # TESTS: 030_values.t
234 #
235 ################################################################################
236  
237 96 ▶sub box_height ($self) {
Subroutine box_height: called CC=1 SCAR=0.0
Pod undocumented
238 96 $box_height {$self};
239 }
240  
241 96 ▶sub box_width ($self) {
Subroutine box_width: called CC=1 SCAR=0.0
Pod undocumented
242 96 $box_width {$self};
243 }
244  
245  
246 ################################################################################
247 #
248 # create_house ($self, $house_name, @cells)
249 #
250 # Create a house with the given name, containing the passed in cells.
251 #
252 # TESTS: 040-create_house.t
253 #
254 ################################################################################
255  
256 2460 ▶sub create_house ($self, $name, @cells) {
Subroutine create_house: called CC=2 SCAR=6.9
Pod undocumented
257 2460 for my $cell (@cells) {
258 30570 $cell2houses {$self} {$cell} {$name} = 1;
259 30570 $house2cells {$self} {$name} {$cell} = 1;
260 }
261 2460 $self;
262 }
263  
264  
265 ################################################################################
266 #
267 # init_rows ($self, $args)
268 #
269 # Initialize the rows in the sudoku. Calculates which cells belong to which
270 # rows, and calls create_house for each row. Called from init_houses.
271 # Rows are named "R1" .. "Rn", where n is the size of the sudoku.
272 #
273 # TESTS: 041-init_rows
274 #
275 ################################################################################
276  
277 82 ▶sub init_rows ($self, $args = {}) {
Subroutine init_rows: called CC=3 SCAR=11.0
Pod undocumented
278 82 my $size = $self -> size;
279 82 for my $r (1 .. $size) {
280 804 my $row_name = "R$r";
281 804 my @cells = map {cell_name $r, $_} 1 .. $size;
282 804 $self -> create_house ($row_name, @cells);
283 }
284 82 $self;
285 }
286  
287  
288 ################################################################################
289 #
290 # init_columns ($self, $args)
291 #
292 # Initialize the columns in the sudoku. Calculates which cells belong to which
293 # columns, and calls create_house for each column. Called from init_houses.
294 # Columns are named "C1" .. "Cn", where n is the size of the sudoku.
295 #
296 # TESTS: 042-init_columns
297 #
298 ################################################################################
299  
300 82 ▶sub init_columns ($self, $args = {}) {
Subroutine init_columns: called CC=3 SCAR=11.0
Pod undocumented
301 82 my $size = $self -> size;
302 82 for my $c (1 .. $size) {
303 804 my $col_name = "C$c";
304 804 my @cells = map {cell_name $_, $c} 1 .. $size;
305 804 $self -> create_house ($col_name, @cells);
306 }
307 82 $self;
308 }
309  
310  
311 ################################################################################
312 #
313 # init_boxes ($self, $args)
314 #
315 # Initialize the boxes in the sudoku. Calculates which cells belong to which
316 # boxes, and calls create_house for each box. Called from init_houses.
317 # Boxes are named "B1-1" .. "Bh-w" where we have h rows of w boxes.
318 #
319 # TESTS: 043-init_boxes
320 #
321 ################################################################################
322  
323 82 ▶sub init_boxes ($self, $args = {}) {
Subroutine init_boxes: called CC=6 SCAR=17.9
Pod undocumented
324 82 my $size = $self -> size;
325 82 my $box_width = $self -> box_width;
326 82 my $box_height = $self -> box_height;
327  
328 82 my $bc = $size / $box_width;
329 82 my $br = $size / $box_height;
330 82 for my $r (1 .. $br) {
331 275 for my $c (1 .. $bc) {
332 804 my $box_name = "B${r}-${c}";
333 804 my $tlr = 1 + ($r - 1) * $box_height;
334 804 my $tlc = 1 + ($c - 1) * $box_width;
335 804 my @cells;
336 804 for my $dr (1 .. $box_height) {
337 2544 for my $dc (1 .. $box_width) {
338 10062 my $cell = cell_name $tlr + $dr - 1, $tlc + $dc - 1;
339 10062 push @cells => $cell;
340 }
341 }
342 804 $self -> create_house ($box_name, @cells);
343 }
344 }
345 82 $self;
346 }
347  
348  
349 ################################################################################
350 #
351 # init_houses ($self, $args)
352 # init_rows ($self)
353 # init_columns ($self)
354 # init_boxes ($self)
355 #
356 # Calculate which cells go into which houses.
357 #
358 # Calls init_rows (), init_columns (), and init_boxes () to initialize
359 # the rows, columns and boxes.
360 #
361 # TESTS: 045-init_houses.t
362 #
363 ################################################################################
364  
365 77 ▶sub init_houses ($self, $args = {}) {
Subroutine init_houses: called CC=2 SCAR=6.9
Pod undocumented
366 77 $self -> init_rows ($args)
367 -> init_columns ($args)
368 -> init_boxes ($args)
369 }
370  
371  
372 ################################################################################
373 #
374 # set_nrc_houses ($self, $args)
375 #
376 # For NRC style puzzles, handle creating the houses.
377 #
378 # There are four NRC houses (9 x 9 Sudokus only):
379 #
380 # . . . . . . . . .
381 # . * * * . * * * .
382 # . * * * . * * * .
383 #
384 # . * * * . * * * .
385 # . . . . . . . . .
386 # . * * * . * * * .
387 #
388 # . * * * . * * * .
389 # . * * * . * * * .
390 # . . . . . . . . .
391 #
392 #
393 # TESTS: 046-set_nrc_houses.t
394 #
395 ################################################################################
396  
397 3 ▶sub set_nrc_houses ($self, $args = {}) {
Subroutine set_nrc_houses: called CC=6 SCAR=17.9
Pod documented
398 3 ▶ return $self unless $self -> size == $DEFAULT_SIZE;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->size == $Regexp::Sudoku::DEFAULT_SIZE 0 3 3
399  
400 3 my @top_left = ([2, 2], [2, 6], [6, 2], [6, 6]);
401 3 foreach my $i (keys @top_left) {
402 12 my $top_left = $top_left [$i];
403 12 my $house = "NRC" . ($i + 1);
404 12 my @cells;
405 12 foreach my $dr (0 .. 2) {
406 36 foreach my $dc (0 .. 2) {
407 108 my $cell = cell_name $$top_left [0] + $dr,
408 $$top_left [1] + $dc;
409 108 push @cells => $cell;
410 }
411 }
412 12 $self -> create_house ($house, @cells);
413 }
414  
415 3 $self;
416 }
417  
418  
419 ################################################################################
420 #
421 # sub set_asterisk_house ($self, $args)
422 #
423 # An asterisk sudoku has an additional house: one cell from each box.
424 # This method initializes that house.
425 #
426 # An asterisk is defined for a 9 x 9 sudoku as follows:
427 #
428 # . . . . . . . . .
429 # . . . . * . . . .
430 # . . * . . . * . .
431 #
432 # . . . . . . . . .
433 # . * . . * . . * .
434 # . . . . . . . . .
435 #
436 # . . * . . . * . .
437 # . . . . * . . . .
438 # . . . . . . . . .
439 #
440 # TESTS: 047-set_asterisk_house.t
441 #
442 ################################################################################
443  
444 4 ▶sub set_asterisk_house ($self, $args = {}) {
Subroutine set_asterisk_house: called CC=3 SCAR=11.1
Pod documented
445 4 ▶ return $self unless $self -> size == $DEFAULT_SIZE;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->size == $Regexp::Sudoku::DEFAULT_SIZE 0 4 4
446  
447 4 $self -> create_house ("AS" => map {cell_name @$_}
448 [3, 3], [2, 5], [3, 7],
449 [5, 2], [5, 5], [5, 8],
450 [7, 3], [8, 5], [7, 7]);
451 }
452  
453  
454 ################################################################################
455 #
456 # sub set_girandola_house ($self, $args)
457 #
458 # An girandola sudoku has an additional house: one cell from each box.
459 # This method initializes that house.
460 #
461 # An girandola is defined for a 9 x 9 sudoku as follows:
462 #
463 # * . . . . . . . *
464 # . . . . * . . . .
465 # . . . . . . . . .
466 #
467 # . . . . . . . . .
468 # . * . . * . . * .
469 # . . . . . . . . .
470 #
471 # . . . . . . . . .
472 # . . . . * . . . .
473 # * . . . . . . . *
474 #
475 # TESTS: 048-set_girandola_house.t
476 #
477 ################################################################################
478  
479 3 ▶sub set_girandola_house ($self) {
Subroutine set_girandola_house: called CC=2 SCAR=7.1
Pod documented
480 3 ▶ return $self unless $self -> size == $DEFAULT_SIZE;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->size == $Regexp::Sudoku::DEFAULT_SIZE 0 3 3
481  
482 3 $self -> create_house ("GR" => map {cell_name @$_}
483 [1, 1], [2, 5], [1, 9],
484 [5, 2], [5, 5], [5, 8],
485 [9, 1], [8, 5], [9, 9]);
486 }
487  
488 ################################################################################
489 #
490 # sub set_center_dot_house ($self, $args)
491 #
492 # An center dot sudoku has an additional house: one cell from each box.
493 # This method initializes that house.
494 #
495 # A center dot is defined for a 9 x 9 sudoku as follows:
496 #
497 # . . . . . . . . .
498 # . * . . * . . * .
499 # . . . . . . . . .
500 #
501 # . . . . . . . . .
502 # . * . . * . . * .
503 # . . . . . . . . .
504 #
505 # . . . . . . . . .
506 # . * . . * . . * .
507 # . . . . . . . . .
508 #
509 # TESTS: 049-set_center_dot_house.t
510 #
511 ################################################################################
512  
513 3 ▶sub set_center_dot_house ($self) {
Subroutine set_center_dot_house: called CC=5 SCAR=16.2
Pod documented
514 3 my $width = $self -> box_width;
515 3 my $height = $self -> box_height;
516 3 my $size = $self -> size;
517  
518 #
519 # We can only do center dots if boxes are odd sized width and heigth.
520 #
521 3 ▶ return $self unless $width % 2 && $height % 2;
Branch: 1 branch
BranchTrueFalseTotal
unless $width % 2 and $height % 2 0 3 3
Condition33%1/3
$width % 2 and $height % 2
!ll&&!rl&&r
003
MC/DC0%0/2
$width % 2 and $height % 2
$width % 2 $height % 2
Truth table33%1/3
A and B
A= $width % 2B= $height % 2
ABresult
0X0
100
111
522  
523 3 my $width_start = ($width + 1) / 2;
524 3 my $height_start = ($height + 1) / 2;
525  
526 3 my @center_cells;
527 3 ▶ for (my $x = $width_start; $x <= $size; $x += $width) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x <= $size) 9 3 12
528 9 ▶ for (my $y = $height_start; $y <= $size; $y += $height) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y <= $size) 33 9 42
529 33 push @center_cells => [$x, $y];
530 }
531 }
532  
533 3 $self -> create_house ("CD" => map {cell_name @$_} @center_cells);
534 }
535  
536  
537  
538  
539 ################################################################################
540 #
541 # cell2houses ($self, $cell)
542 #
543 # Give the name of a cell, return the names of all the houses this cell
544 # is part off.
545 #
546 # TESTS: 040-houses.t
547 #
548 ################################################################################
549  
550 322639 ▶sub cell2houses ($self, $cell) {
Subroutine cell2houses: called CC=2 SCAR=7.2
Pod undocumented
551 322639 ▶ keys %{$cell2houses {$self} {$cell} || {}}
Condition50%1/2
$cell2houses{$self}{$cell} || {}
l!l
3226390
MC/DC0%0/1
$cell2houses{$self}{$cell} || {}
$cell2houses{$self}{$cell}
Truth table50%1/2
A
A= $cell2houses{$self}{$cell}
Aresult
11
00
552 }
553  
554  
555 ################################################################################
556 #
557 # house2cells ($self, $house)
558 #
559 # Give the name of a house, return the names of all the cells in this house.
560 #
561 # TESTS: 040-houses.t
562 #
563 ################################################################################
564  
565 337 ▶sub house2cells ($self, $house) {
Subroutine house2cells: called CC=2 SCAR=7.2
Pod undocumented
566 337 ▶ keys %{$house2cells {$self} {$house} || {}}
Condition50%1/2
$house2cells{$self}{$house} || {}
l!l
3370
MC/DC0%0/1
$house2cells{$self}{$house} || {}
$house2cells{$self}{$house}
Truth table50%1/2
A
A= $house2cells{$self}{$house}
Aresult
11
00
567 }
568  
569  
570 ################################################################################
571 #
572 # cells ($self, $sorted = 0)
573 #
574 # Return the names of all the cells in the sudoku.
575 #
576 # If we want the cells to be sorted, then we use the following priorities:
577 # * Clues go first
578 # * Odd/Even cells go next
579 # * Then the other cells
580 # * Ties are broken in the following fashion:
581 # * The more clues seen, the better
582 # * The smallest Renban area the cell is in, the better
583 # * The more houses the cell is in, the better.
584 #
585 # TESTS: 040-houses.t
586 #
587 ################################################################################
588  
589 34 ▶sub cells ($self, $sorted = 0) {
Subroutine cells: called CC=22 SCAR=31.0
Pod undocumented
590 34 my @cells = sort keys %{$cell2houses {$self}};
591 34 ▶ if ($sorted) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sorted) 28 6 34
592 28 state $CELL_NAME = 0;
593 28 state $IS_CLUE = $CELL_NAME + 1;
594 28 state $EVEN_ODD = $IS_CLUE + 1;
595 28 state $CLUES_SEEN = $EVEN_ODD + 1;
596 28 state $NR_OF_HOUSES = $CLUES_SEEN + 1;
597 28 state $RENBAN = $NR_OF_HOUSES + 1;
598 28 state $GERMAN = $RENBAN + 1;
599 28 state $QUADRUPLE = $GERMAN + 1;
600  
601 #
602 # For each cell, determine how many different clues it sees.
603 #
604 28 my %sees;
605 28 foreach my $cell1 (@cells) {
606 2048 ▶ next if $self -> clue ($cell1); # Don't care about clues
Branch: 1 branch
BranchTrueFalseTotal
if $self->clue($cell1) 625 1423 2048
607 1423 foreach my $cell2 (@cells) {
608 111648 ▶ if ($self -> clue ($cell2) &&
Branch: 1 branch
BranchTrueFalseTotal
if ($self->clue($cell2) and $self->must_differ($cell1, $cell2)) 8881 102767 111648
Condition100%3/3
$self->clue($cell2) and $self->must_differ($cell1, $cell2)
!ll&&!rl&&r
78119246488881
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
609 $self -> must_differ ($cell1, $cell2)) {
610 8881 $sees {$cell1} {$self -> clue ($cell2)} = 1;
611 }
612 }
613 }
614  
615 2048 @cells = map {$$_ [$CELL_NAME]}
616 10166 ▶ sort {$$b [$IS_CLUE] <=> $$a [$IS_CLUE] ||
Condition85%18/21
$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::a->[$RENBAN] <=> $Regexp::Sudoku::b->[$RENBAN] || $Regexp::Sudoku::b->[$GERMAN] <=> $Regexp::Sudoku::a->[$GERMAN] || $Regexp::Sudoku::b->[$QUADRUPLE] <=> $Regexp::Sudoku::a->[$QUADRUPLE] || $Regexp::Sudoku::b->[$NR_OF_HOUSES] <=> $Regexp::Sudoku::a->[$NR_OF_HOUSES] || $Regexp::Sudoku::a->[$CELL_NAME] cmp $Regexp::Sudoku::b->[$CELL_NAME]
l!l&&r!l&&!r
624039260
$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::a->[$RENBAN] <=> $Regexp::Sudoku::b->[$RENBAN] or $Regexp::Sudoku::b->[$GERMAN] <=> $Regexp::Sudoku::a->[$GERMAN] or $Regexp::Sudoku::b->[$QUADRUPLE] <=> $Regexp::Sudoku::a->[$QUADRUPLE] or $Regexp::Sudoku::b->[$NR_OF_HOUSES] <=> $Regexp::Sudoku::a->[$NR_OF_HOUSES]
l!l&&r!l&&!r
56326083926
$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::a->[$RENBAN] <=> $Regexp::Sudoku::b->[$RENBAN] or $Regexp::Sudoku::b->[$GERMAN] <=> $Regexp::Sudoku::a->[$GERMAN] or $Regexp::Sudoku::b->[$QUADRUPLE] <=> $Regexp::Sudoku::a->[$QUADRUPLE]
l!l&&r!l&&!r
563204534
$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::a->[$RENBAN] <=> $Regexp::Sudoku::b->[$RENBAN] or $Regexp::Sudoku::b->[$GERMAN] <=> $Regexp::Sudoku::a->[$GERMAN]
l!l&&r!l&&!r
563204534
$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::a->[$RENBAN] <=> $Regexp::Sudoku::b->[$RENBAN]
l!l&&r!l&&!r
5539934534
$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
238931504627
$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
2290997777
MC/DC0%0/8
$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::a->[$RENBAN] <=> $Regexp::Sudoku::b->[$RENBAN] || $Regexp::Sudoku::b->[$GERMAN] <=> $Regexp::Sudoku::a->[$GERMAN] || $Regexp::Sudoku::b->[$QUADRUPLE] <=> $Regexp::Sudoku::a->[$QUADRUPLE] || $Regexp::Sudoku::b->[$NR_OF_HOUSES] <=> $Regexp::Sudoku::a->[$NR_OF_HOUSES] || $Regexp::Sudoku::a->[$CELL_NAME] cmp $Regexp::Sudoku::b->[$CELL_NAME]
$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::a->[$RENBAN] <=> $Regexp::Sudoku::b->[$RENBAN] $Regexp::Sudoku::b->[$GERMAN] <=> $Regexp::Sudoku::a->[$GERMAN] $Regexp::Sudoku::b->[$QUADRUPLE] <=> $Regexp::Sudoku::a->[$QUADRUPLE] $Regexp::Sudoku::b->[$NR_OF_HOUSES] <=> $Regexp::Sudoku::a->[$NR_OF_HOUSES] $Regexp::Sudoku::a->[$CELL_NAME] cmp $Regexp::Sudoku::b->[$CELL_NAME]
Truth table66%6/9
A or B or C or D or E or F or G || H
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::a->[$RENBAN] <=> $Regexp::Sudoku::b->[$RENBAN]E= $Regexp::Sudoku::b->[$GERMAN] <=> $Regexp::Sudoku::a->[$GERMAN]F= $Regexp::Sudoku::b->[$QUADRUPLE] <=> $Regexp::Sudoku::a->[$QUADRUPLE]G= $Regexp::Sudoku::b->[$NR_OF_HOUSES] <=> $Regexp::Sudoku::a->[$NR_OF_HOUSES]H= $Regexp::Sudoku::a->[$CELL_NAME] cmp $Regexp::Sudoku::b->[$CELL_NAME]
ABCDEFGHresult
1XXXXXXX1
01XXXXXX1
001XXXXX1
0001XXXX1
00001XXX1
000001XX1
0000001X1
000000011
000000000
617 $$b [$EVEN_ODD] <=> $$a [$EVEN_ODD] ||
618 $$b [$CLUES_SEEN] <=> $$a [$CLUES_SEEN] ||
619 $$a [$RENBAN] <=> $$b [$RENBAN] ||
620 $$b [$GERMAN] <=> $$a [$GERMAN] ||
621 $$b [$QUADRUPLE] <=> $$a [$QUADRUPLE] ||
622 $$b [$NR_OF_HOUSES] <=> $$a [$NR_OF_HOUSES] ||
623 $$a [$CELL_NAME] cmp $$b [$CELL_NAME]}
624 28 map {my $r = [];
625 2048 $$r [$CELL_NAME] = $_;
626 2048 ▶ $$r [$IS_CLUE] = $self -> clue ($_) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->clue($_) ? : 625 1423 2048
627 2048 ▶ $$r [$EVEN_ODD] = $self -> is_even ($_) ||
Branch: 1 branch
BranchTrueFalseTotal
$self->is_even($_) || $self->is_odd($_) ? : 91 1957 2048
Condition100%3/3
$self->is_even($_) or $self->is_odd($_)
l!l&&r!l&&!r
49421957
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
628 $self -> is_odd ($_) ? 1 : 0;
629 2048 ▶ $$r [$CLUES_SEEN] = keys (%{$sees {$_} || {}});
Condition100%2/2
$sees{$_} || {}
l!l
1423625
MC/DC100%1/1
$sees{$_} || {}
$sees{$_}
Truth table100%2/2
A
A= $sees{$_}
Aresult
11
00
630 2048 $$r [$NR_OF_HOUSES] = $self -> cell2houses ($_);
631 2048 $$r [$GERMAN] = $self -> cell2germans ($_);
632 #
633 # Find the *smallest* renban the cell is in
634 #
635 $$r [$RENBAN] =
636 2048 ▶ (min map {scalar $self -> renban2cells ($_)}
Condition66%2/3
&min(map({scalar $self->renban2cells($_);} $self->cell2renbans($_))) // $self->size
l!l&&r!l&&!r
4720010
MC/DC0%0/2
&min(map({scalar $self->renban2cells($_);} $self->cell2renbans($_))) // $self->size
&min(map({scalar $self->renban2cells($_);} $self->cell2renbans($_))) $self->size
Truth table66%2/3
A // B
A= &min(map({scalar $self->renban2cells($_);} $self->cell2renbans($_)))B= $self->size
ABresult
1X1
011
000
637 $self -> cell2renbans ($_)) // $self -> size;
638  
639 #
640 # Assign quadruple points for each cell. A cell
641 # gets points for each quadruple it is, and more
642 # points the more values are in the quadruple.
643 # More quadruples counts for more though.
644 #
645 2048 $$r [$QUADRUPLE] = 0;
646 2048 foreach my $quadruple ($self -> cell2quadruples ($_)) {
647 0 $$r [$QUADRUPLE] += 10;
648 0 $$r [$QUADRUPLE] ++ for
649 $self -> quadruple_values ($quadruple);
650 }
651  
652 2048 $r}
653 @cells;
654 }
655 34 @cells;
656 }
657  
658  
659 ################################################################################
660 #
661 # houses ($self)
662 #
663 # Return the names of all the houses in the sudoku.
664 #
665 # TESTS: 040-houses.t
666 #
667 ################################################################################
668  
669 6 ▶sub houses ($self) {
Subroutine houses: called CC=1 SCAR=0.0
Pod undocumented
670 6 keys %{$house2cells {$self}}
671 }
672  
673  
674 ################################################################################
675 #
676 # set_clues ($self, $args)
677 #
678 # Take the supplied clues (if any!), and return a structure which maps cell
679 # names to clue values.
680 #
681 # The clues could be one of:
682 # - A 2-d array, with false values indicating the cell doesn't have a clue.
683 # A "." will also be consider to be not a clue.
684 # - A string, newlines separating rows, and whitespace clues. A value
685 # of 0 or "." indicates no clue.
686 #
687 # We wil populate the clues attribute, mapping cell names to clue values.
688 # Cells without clues won't be set.
689 #
690 # TESTS: 080-set_clues.t
691 #
692 ################################################################################
693  
694 31 ▶sub set_clues ($self, $in_clues) {
Subroutine set_clues: called CC=8 SCAR=20.8
Pod documented
695 31 my $clues = {};
696 #
697 # Turn a string into an array
698 #
699 31 ▶ if (!ref $in_clues) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $in_clues) 30 1 31
700 30 my @rows = grep {/\S/} split /\n/ => $in_clues;
701 30 $in_clues = [map {[split]} @rows];
702 }
703 31 foreach my $r (keys @$in_clues) {
704 263 foreach my $c (keys @{$$in_clues [$r]}) {
705 2255 my $val = $$in_clues [$r] [$c];
706 2255 ▶ next if !$val || $val eq ".";
Branch: 1 branch
BranchTrueFalseTotal
if not $val or $val eq "." 1436 819 2255
Condition100%3/3
not $val or $val eq "."
l!l&&r!l&&!r
781358819
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
707 819 my $cell = cell_name $r + 1, $c + 1;
708 819 ▶ if ($val eq 'e') {$self -> set_is_even ($cell)}
Branch: 2 branches
BranchTrueFalseTotal
if ($val eq 'e') { } 67 752 819
elsif ($val eq 'o') { } 58 694 752
709 58 elsif ($val eq 'o') {$self -> set_is_odd ($cell)}
710 694 else {$$clues {$cell} = $val}
711 }
712 }
713 31 $clues {$self} = $clues;
714  
715 31 $self;
716 }
717  
718  
719 ################################################################################
720 #
721 # clues ($self)
722 #
723 # Return an hashref mapping cell names to clues.
724 #
725 # TESTS: 080-clues.t
726 #
727 ################################################################################
728  
729 1 ▶sub clues ($self) {
Subroutine clues: called CC=1 SCAR=0.0
Pod undocumented
730 1 $clues {$self};
731 }
732  
733  
734 ################################################################################
735 #
736 # clue ($self, $cell)
737 #
738 # Returns the clue in the given cell. If the cell does not have a clue,
739 # return false.
740 #
741 # TESTS: 080-clues.t
742 #
743 ################################################################################
744  
745 216271 ▶sub clue ($self , $cell) {
Subroutine clue: called CC=1 SCAR=0.0
Pod undocumented
746 216271 $clues {$self} {$cell}
747 }
748  
749  
750 ################################################################################
751 #
752 # set_anti_knight_constraint ($self)
753 # set_anti_king_constraint ($self)
754 #
755 # Set the anti knigt/anti king constraints for the sudoku.
756 #
757 # TESTS: 151-must-differ.t
758 #
759 ################################################################################
760  
761 3 ▶sub set_anti_knight_constraint ($self) {
Subroutine set_anti_knight_constraint: called CC=1 SCAR=0.0
Pod documented
762 3 $constraints {$self} {$ANTI_KNIGHT} = 1;
763 3 $self;
764 }
765  
766 3 ▶sub set_anti_king_constraint ($self) {
Subroutine set_anti_king_constraint: called CC=1 SCAR=0.0
Pod documented
767 3 $constraints {$self} {$ANTI_KING} = 1;
768 3 $self;
769 }
770  
771  
772  
773 ################################################################################
774 #
775 # init ($self, %args)
776 #
777 # Configure the Regexp::Sudoku object.
778 #
779 # TESTS: *.t
780 #
781 ################################################################################
782  
783  
784 78 ▶sub init ($self, %args) {
Subroutine init: called CC=2 SCAR=7.4
Pod documented
785 78 my $args = {%args};
786  
787 78 $self -> init_sizes ($args)
788 -> init_values ($args)
789 -> init_houses ($args);
790  
791 77 ▶ if (keys %$args) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %$args) 0 77 77
792 0 die "Unknown parameter(s) to init: " . join (", " => keys %$args)
793 . "\n";
794 }
795  
796 77 $self;
797 }
798  
799  
800 ################################################################################
801 #
802 # make_clue_statement ($self, $cell, $value)
803 #
804 # Given a cell name, and a value, return a sub subject, and sub pattern
805 # which sets the capture '$cell' to '$value'
806 #
807 # TESTS: 110-make_clue_statement.t
808 # 120-make_cell_statement.t
809 #
810 ################################################################################
811  
812 664 ▶sub make_clue_statement ($self, $cell) {
Subroutine make_clue_statement: called CC=1 SCAR=0.0
Pod undocumented
813 664 my $value = $self -> clue ($cell);
814 664 my $subsub = $value;
815 664 my $subpat = "(?<$cell>$value)";
816  
817 664 map {$_ . $SENTINEL} $subsub, $subpat;
818 }
819  
820  
821 ################################################################################
822 #
823 # make_empty_statement ($cell)
824 #
825 # Given a cell name, return a sub subject and a sub pattern allowing the
826 # cell to pick up one of the values in the sudoku.
827 #
828 # TESTS: 100-make_empty_statement.t
829 # 120-make_cell_statement.t
830 #
831 ################################################################################
832  
833 1477 ▶sub make_empty_statement ($self, $cell, $method = "values") {
Subroutine make_empty_statement: called CC=2 SCAR=6.9
Pod undocumented
834 1477 my $subsub = $self -> $method;
835 1477 my $range = $self -> values_range;
836 1477 my $subpat = "[$range]*(?<$cell>[$range])[$range]*";
837  
838 1477 map {$_ . $SENTINEL} $subsub, $subpat;
839 }
840  
841 1352 ▶sub make_any_statement ($self, $cell) {
Subroutine make_any_statement: called CC=1 SCAR=0.0
Pod undocumented
842 1352 $self -> make_empty_statement ($cell, "values")
843 }
844  
845  
846 ################################################################################
847 #
848 # make_cell_statement ($cell)
849 #
850 # Given a cell name, return a subsubject and subpattern to set a value for
851 # this cell. Either the cell has a clue (and we dispatch to make_clue),
852 # or not (and we dispatch to make_empty).
853 #
854 # TESTS: 120-make_cell_statement.t
855 #
856 ################################################################################
857  
858 2129 ▶sub make_cell_statement ($self, $cell) {
Subroutine make_cell_statement: called CC=4 SCAR=13.9
Pod undocumented
859 2129 my $clue = $self -> clue ($cell);
860  
861 2129 ▶ $self -> clue ($cell) ? $self -> make_clue_statement ($cell)
Branch: 3 branches
BranchTrueFalseTotal
$self->clue($cell) ? : 655 1474 2129
$self->is_even($cell) ? : 67 1407 1474
$self->is_odd($cell) ? : 58 1349 1407
862 : $self -> is_even ($cell) ? $self -> make_even_statement ($cell)
863 : $self -> is_odd ($cell) ? $self -> make_odd_statement ($cell)
864 : $self -> make_any_statement ($cell)
865 }
866  
867  
868  
869 ################################################################################
870 #
871 # make_diff_statement ($self, $cell1, $cell2)
872 #
873 # Given two cell names, return a sub subject and a sub pattern which matches
874 # iff the values in the cell differ.
875 #
876 # TESTS: 140-make_diff_statement.t
877 #
878 ################################################################################
879  
880 19168 ▶sub make_diff_statement ($self, $cell1, $cell2) {
Subroutine make_diff_statement: called CC=1 SCAR=0.0
Pod undocumented
881 19168 my $subsub = "";
882 19168 my @values = $self -> values;
883 19168 my $range = $self -> values_range;
884  
885 19168 my $seq = semi_debruijn_seq (scalar $self -> values);
886 19168 my $pat = "[$range]*\\g{$cell1}\\g{$cell2}[$range]*";
887  
888 19168 map {$_ . $SENTINEL} $seq, $pat;
889 }
890  
891  
892 ################################################################################
893 #
894 # must_differ ($self, $cell1, $cell2)
895 #
896 # Returns a true value if the two given cells must have different values.
897 #
898 # TESTS: 150-must_differ.t
899 # 151-must_differ.t
900 #
901 ################################################################################
902  
903 158712 ▶sub must_differ ($self, $cell1, $cell2) {
Subroutine must_differ: called CC=11 SCAR=24.0
Pod undocumented
904 158712 my %seen;
905 158712 $seen {$_} ++ for $self -> cell2houses ($cell1),
906 $self -> cell2houses ($cell2);
907  
908 158712 my $same_house = grep {$_ > 1} values %seen;
909  
910 158712 my ($r1, $c1) = cell_row_column ($cell1);
911 158712 my ($r2, $c2) = cell_row_column ($cell2);
912  
913 158712 my $d_rows = abs ($r1 - $r2);
914 158712 my $d_cols = abs ($c1 - $c2);
915  
916 158712 my $constraints = $constraints {$self};
917 return $same_house
918 || $$constraints {$ANTI_KNIGHT} && (($d_rows == 1 && $d_cols == 2) ||
919 ($d_rows == 2 && $d_cols == 1))
920 158712 ▶ || $$constraints {$ANTI_KING} && $d_rows == 1 && $d_cols == 1
Branch: 1 branch
BranchTrueFalseTotal
$same_house || $constraints->{$Regexp::Sudoku::ANTI_KNIGHT} && ($d_rows == 1 && $d_cols == 2 || $d_rows == 2 && $d_cols == 1) || $constraints->{$Regexp::Sudoku::ANTI_KING} && $d_rows == 1 && $d_cols == 1 ? : 37970 120742 158712
Condition100%24/24
$same_house or $constraints->{$Regexp::Sudoku::ANTI_KNIGHT} and $d_rows == 1 && $d_cols == 2 || $d_rows == 2 && $d_cols == 1 or $constraints->{$Regexp::Sudoku::ANTI_KING} and $d_rows == 1 and $d_cols == 1
l!l&&r!l&&!r
37758212120742
$same_house or $constraints->{$Regexp::Sudoku::ANTI_KNIGHT} and $d_rows == 1 && $d_cols == 2 || $d_rows == 2 && $d_cols == 1
l!l&&r!l&&!r
37174584120954
$constraints->{$Regexp::Sudoku::ANTI_KNIGHT} and $d_rows == 1 && $d_cols == 2 || $d_rows == 2 && $d_cols == 1
!ll&&!rl&&r
1125688386584
$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
$constraints->{$Regexp::Sudoku::ANTI_KING} and $d_rows == 1 and $d_cols == 1
!ll&&!rl&&r
1192041538212
$constraints->{$Regexp::Sudoku::ANTI_KING} and $d_rows == 1
!ll&&!rl&&r
11199672081750
MC/DC100%9/9
$same_house or $constraints->{$Regexp::Sudoku::ANTI_KNIGHT} and $d_rows == 1 && $d_cols == 2 || $d_rows == 2 && $d_cols == 1 or $constraints->{$Regexp::Sudoku::ANTI_KING} and $d_rows == 1 and $d_cols == 1
$same_house $constraints->{$Regexp::Sudoku::ANTI_KNIGHT} $d_rows == 1 $d_cols == 2 $d_rows == 2 $d_cols == 1 $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= $constraints->{$Regexp::Sudoku::ANTI_KNIGHT}C= $d_rows == 1D= $d_cols == 2E= $d_rows == 2F= $d_cols == 1G= $constraints->{$Regexp::Sudoku::ANTI_KING}H= $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
921 ? 1 : 0;
922 }
923  
924  
925 ################################################################################
926 #
927 # init_subject_and_pattern ($self)
928 #
929 # Create the subject we're going to match against, and the pattern
930 # we use to match.
931 #
932 # TESTS: TODO
933 #
934 ################################################################################
935  
936 56 ▶sub init_subject_and_pattern ($self) {
Subroutine init_subject_and_pattern: called CC=18 SCAR=30.1
Pod undocumented
937 56 ▶ return $self if $subject {$self} && $pattern {$self};
Branch: 1 branch
BranchTrueFalseTotal
if $subject{$self} and $pattern{$self} 28 28 56
Condition66%2/3
$subject{$self} and $pattern{$self}
!ll&&!rl&&r
28028
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
938  
939 28 my $subject = "";
940 28 my $pattern = "";
941  
942 28 my @cells = $self -> cells (1);
943  
944 28 my %seen;
945  
946 28 for my $i (keys @cells) {
947 #
948 # First the part which picks up a value for this cell
949 #
950 2048 my $cell1 = $cells [$i];
951  
952 2048 my ($subsub, $subpat) = $self -> make_cell_statement ($cell1);
953 2048 $subject .= $subsub;
954 2048 $pattern .= $subpat;
955  
956 2048 $seen {$cell1} = 1;
957  
958 #
959 # Now, for all the previous cells, if there are constraints
960 # between them, add statements for each constraint.
961 #
962 2048 for my $j (0 .. $i - 1) {
963 79260 my $cell2 = $cells [$j];
964 #
965 # If both cells are a clue, we don't need a restriction
966 # between the cells.
967 #
968 79260 ▶ next if $self -> clue ($cell1) && $self -> clue ($cell2);
Branch: 1 branch
BranchTrueFalseTotal
if $self->clue($cell1) and $self->clue($cell2) 7383 71877 79260
Condition66%2/3
$self->clue($cell1) and $self->clue($cell2)
!ll&&!rl&&r
7187707383
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
969  
970 71877 my ($subsub, $subpat) = ("", "");
971 71877 my $differs = 0;
972 71877 my @todo;
973  
974 71877 ▶ if (my @renbans = $self -> same_renban ($cell1, $cell2)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@renbans) = $self->same_renban($cell1, $cell2)) 112 71765 71877
975 112 push @todo => "make_renban_statement";
976 112 $differs = 1;
977 }
978  
979 71877 ▶ if ($self -> same_battenburg ($cell1, $cell2)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->same_battenburg($cell1, $cell2)) 35 71842 71877
980 35 push @todo => "make_battenburg_statement";
981 }
982  
983 71877 ▶ if ($self -> consecutive_in_german_whisper ($cell1, $cell2)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->consecutive_in_german_whisper($cell1, $cell2)) 0 71877 71877
984 0 push @todo => "make_german_whisper_statement";
985 0 $differs = 1;
986 }
987  
988 71877 ▶ if (!$differs && $self -> must_differ ($cell1, $cell2)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $differs and $self->must_differ($cell1, $cell2)) 19159 52718 71877
Condition100%3/3
not $differs and $self->must_differ($cell1, $cell2)
!ll&&!rl&&r
1125260619159
MC/DC100%2/2
not $differs and $self->must_differ($cell1, $cell2)
not $differs $self->must_differ($cell1, $cell2)
Truth table100%3/3
not(A) and B
A= not $differsB= $self->must_differ($cell1, $cell2)
ABresult
0X0
100
111
989 19159 push @todo => "make_diff_statement";
990 }
991  
992 71877 foreach my $method (@todo) {
993 19306 my ($subsub, $subpat) = $self -> $method ($cell1, $cell2);
994 19306 $subject .= $subsub;
995 19306 $pattern .= $subpat;
996 }
997 }
998  
999 #
1000 # If the cell is part of an anti-Battenburg, and if the cell
1001 # is the last cell seen of that anti-Battenburg, add the
1002 # contraints.
1003 #
1004 2048 foreach my $anti_battenburg ($self -> cell2anti_battenburgs ($cell1)) {
1005 40 my @cells = $self -> anti_battenburg2cells ($anti_battenburg);
1006 40 ▶ if (@cells == grep {$seen {$_}} @cells) {
Branch: 1 branch
BranchTrueFalseTotal
if (@cells == grep({$seen{$_};} @cells)) 10 30 40
1007 10 my ($subsub, $subpat) =
1008 $self -> make_anti_battenburg_statement ($anti_battenburg);
1009 10 $subject .= $subsub;
1010 10 $pattern .= $subpat;
1011 }
1012 }
1013  
1014 #
1015 # If the cell is part of a quadruple constraint, and if the cell
1016 # is the last cell seen of this quadruple, add the constraints
1017 # for the quadruple.
1018 #
1019 2048 foreach my $quadruple ($self -> cell2quadruples ($cell1)) {
1020 0 my @cells = $self -> quadruple2cells ($quadruple);
1021 0 ▶ if (@cells == grep {$seen {$_}} @cells) {
Branch: 1 branch
BranchTrueFalseTotal
if (@cells == grep({$seen{$_};} @cells)) 0 0 0
1022 0 my ($subsubs, $subpats) =
1023 $self -> make_quadruple_statements ($quadruple);
1024 0 foreach my $i (keys @$subsubs) {
1025 0 $subject .= $$subsubs [$i];
1026 0 $pattern .= $$subpats [$i];
1027 }
1028 }
1029 }
1030 }
1031  
1032 28 $subject {$self} = $subject;
1033 28 $pattern {$self} = "^" . $pattern . '$';
1034  
1035 28 $self;
1036 }
1037  
1038  
1039 ################################################################################
1040 #
1041 # subject ($self)
1042 #
1043 # Return the subject we're matching against.
1044 #
1045 # TESTS: Test.pm
1046 #
1047 ################################################################################
1048  
1049 28 ▶sub subject ($self) {
Subroutine subject: called CC=1 SCAR=0.0
Pod undocumented
1050 28 $self -> init_subject_and_pattern;
1051 28 $subject {$self}
1052 }
1053  
1054  
1055 ################################################################################
1056 #
1057 # pattern ($self)
1058 #
1059 # Return the pattern we're matching with.
1060 #
1061 # TESTS: Test.pm
1062 #
1063 ################################################################################
1064  
1065 28 ▶sub pattern ($self) {
Subroutine pattern: called CC=1 SCAR=0.0
Pod undocumented
1066 28 $self -> init_subject_and_pattern;
1067 28 $pattern {$self}
1068 }
1069  
1070 36 1;
1071  
1072  
1073 __END__
« Sudoku/German_Whisper.pm ↑ Summary Sudoku/Diagonal.pm »