blib/lib/Games/Sudoku/General.pm

filter: Statementstmt 74.9% 690 / 921 Branchbran 55.9% 282 / 504 Conditioncond 50.8% 63 / 124 MC/DCmcdc 31.5% 24 / 76 Subroutinesub 89.8% 53 / 59 Podpod 100% 13 / 13 totaltotal 66.2% 1125 / 1697 CC 346 SCAR 45.5
CC
346
Cov
67%
_constraint_remove
534.4
generate
380.0
_constraint_backtrack
156.0
CRAP 94.3
↑ Summary Build »
1 =head1 NAME
2  
3 Games::Sudoku::General - Solve sudoku-like puzzles.
4  
5 =head1 SYNOPSIS
6  
7 $su = Games::Sudoku::General->new ();
8 print $su->problem(<<eod)->solution();
9 3 . . . . 8 . 2 .
10 . . . . . 9 . . .
11 . . 2 7 . 5 . . .
12 2 4 . 5 . . 8 . .
13 . 8 5 . 7 4 . . 6
14 . 3 . . . . 9 4 .
15 1 . 4 . . . . 7 2
16 . . 6 9 . . . 5 .
17 . 7 . 6 1 2 . . 9
18 eod
19  
20 =head1 DESCRIPTION
21  
22 This package solves puzzles that involve the allocation of symbols
23 among a number of sets, such that no set contains more than one of
24 any symbol. This class of problem includes the puzzles known as
25 'Sudoku', 'Number Place', and 'Wasabi'.
26  
27 Each Sudoku puzzle is considered to be made up of a number of cells,
28 each of which is a member of one or more sets, and each of which may
29 contain exactly one symbol. The contents of some of the cells are
30 given, and the problem is to deduce the contents of the rest of the
31 cells.
32  
33 Although such puzzles as Sudoku are presented on a square grid, this
34 package does not assume any particular geometry. Instead, the topology
35 of the puzzle is defined by the user in terms of a list of the sets
36 to which each cell belongs. Some topology generators are provided, but
37 the user has the option of hand-specifying an arbitrary topology.
38  
39 Even on the standard 9 x 9 Sudoku topology there are variants in which
40 unspecified cells are constrained in various ways (odd/even, high/low).
41 Such variants are accommodated by defining named sets of allowed
42 symbols, and then giving the set name for each unoccupied cell to which
43 it applies. See the C<allowed_symbols> attribute for more
44 information and an example.
45  
46 This module is able not only to solve a variety of Sudoku-like
47 puzzles, but to 'explain' how it arrived at its solution. The
48 steps() method, called after a solution is generated, lists in order
49 what solution constraints were applied, what cell each constraint
50 is applied to, and what symbol the cell was constrained to.
51  
52 Test script t/sudoku.t demonstrates these features. ActivePerl users
53 will have to download the kit from L<https://www.cpan.org/> or
54 L<https://metacpan.org/dist/Games-Sudoku-General> to get this
55 file.
56  
57 =head2 Exported symbols
58  
59 No symbols are exported by default, but the following things are
60 available for export:
61  
62 Status values exported by the :status tag
63 SUDOKU_SUCCESS
64 This means what you think it does.
65 SUDOKU_NO_SOLUTION
66 This means the method exhausted all possible
67 soltions without finding one
68 SUDOKU_TOO_HARD
69 This means the iteration_limit attribute was
70 set to a positive number and the solution()
71 method hit the limit without finding a solution.
72  
73 The :all tag is provided for convenience, but it exports the same
74 symbols as :status.
75  
76 =head2 Attributes
77  
78 Games::Sudoku::General objects have the following attributes, which may
79 normally be accessed by the get() method, and changed by the set()
80 method.
81  
82 In parentheses after the name of the attribute is the word "boolean",
83 "number" or "string", giving the data type of the attribute. Booleans
84 are interpreted in the Perl sense: undef, 0, and '' are false, and
85 anything else is true. The parentheses may also contain the words
86 "read-only" to denote a read-only attribute or "write-only" to denote
87 a write-only attribute.
88  
89 In general, the write-only attributes exist as a convenience to the
90 user, and provide a shorthand way to set a cluster of attributes at
91 the same time. At the moment all of them are concerned with generating
92 problem topologies, which are a real pain to specify by hand.
93  
94 =over
95  
96 =item allowed_symbols (string)
97  
98 This attribute names and defines sets of allowed symbols which may
99 appear in empty cells. The set definitions are whitespace-delimited
100 and each consists of a string of the form 'name=symbol,symbol...'
101 where the 'name' is the name of the set, and the symbols are a list
102 of the symbols valid in a cell to which that set applies.
103  
104 For example, if you have an odd/even puzzle (i.e. you are given that
105 at least some of the unoccupied cells are even or odd but not both),
106 you might want to
107  
108 $su->set (allowed_symbols => <<eod);
109 o=1,3,5,7,9
110 e=2,4,6,8
111 eod
112  
113 and then define the problem like this:
114  
115 $su->problem (<<eod);
116 1 o e o e e o e 3
117 o o e o 6 e o o e
118 e e 3 o o 1 o e e
119 e 7 o 1 o e e o e
120 o e 8 e e o 5 o o
121 o e o o e 3 e 4 o
122 e o o 8 o o 6 o e
123 o o o e 1 e e e o
124 6 e e e o o o o 7
125 eod
126  
127 To eliminate an individual allowed symbol set, set it to an empty
128 string (e.g. $su->set (allowed_symbols => 'o=');). To eliminate all
129 symbol sets, set the entire attribute to the empty string.
130  
131 Allowed symbol set names may not conflict with symbol names. If you set
132 the symbol attribute, all allowed symbol sets are deleted, because
133 that seemed to be the most expeditious way to enforce this restriction
134 across a symbol set change.
135  
136 Because symbol set names must be parsed like symbol names when a
137 problem is defined, they also affect the need for whitespace on
138 problem input. See the L<problem()|/problem> documentation for
139 full details.
140  
141 =item autocopy (boolean)
142  
143 If true, this attribute causes the generate() method to implicitly call
144 copy() to copy the generated problem to the clipboard.
145  
146 This attribute is false by default.
147  
148 =item brick (string, write-only)
149  
150 This "virtual" attribute is a convenience, which causes the object to be
151 configured with a topology of rows, columns, and rectangles. The value
152 set must be either a comma-separated list of two numbers (e.g. '3,2')
153 or a reference to a list containing two numbers (e.g. [3, 2]). Either
154 way, the numbers represent the horizontal dimension of the rectangle (in
155 columns) and the vertical dimension of the rectangle (in rows). The
156 overall size of the puzzle square is the product of these. For example,
157  
158 $su->set( brick => [ 3, 2 ] )
159  
160 generates a topology that looks like this
161  
162 +-------+-------+
163 | x x x | x x x |
164 | x x x | x x x |
165 +-------+-------+
166 | x x x | x x x |
167 | x x x | x x x |
168 +-------+-------+
169 | x x x | x x x |
170 | x x x | x x x |
171 +-------+-------+
172  
173 Originally there was a third argument giving the total size of the
174 puzzle. Beginning with version 0.006 this was deprecated, since it
175 appeared to me to be redundant. As of version 0.021, all uses of this
176 argument resulted in a warning. As of version 0.022, use of the third
177 argument will become fatal.
178  
179 Setting this attribute modifies the following "real" attributes:
180  
181 columns is set to the size of the big square;
182 symbols is set to "." and the numbers "1", "2",
183 and so on, up to the size of the big square;
184 topology is set to represent the rows, columns,
185 and small rectangles in the big square, with row
186 sets named "r0", "r1", and so on, column sets
187 named "c0", "c1", and so on, and small
188 rectangle sets named "s0", "s1", and so on for
189 historical reasons.
190  
191 =item columns (number)
192  
193 This attribute defines the number of columns of data to present in a
194 line of output when formatting the topology attribute, or the solution
195 to a puzzle.
196  
197 =item corresponding (number, write-only)
198  
199 This "virtual" attribute is a convenience, which causes the object
200 to be configured for "corresponding-cell" Sudoku. The topology is
201 the same as C<set( sudoku => ... )>, but in addition corresponding
202 cells in the small squares must have different values. The extra set
203 names are "u0", "u1", and so on.
204  
205 This kind of puzzle is also called "disjoint groups."
206  
207 =item cube (string, write-only)
208  
209 This "virtual" attribute is a convenience, which causes the object to
210 be configured for cubical sudoku. The string is either a number, or
211 'full', or 'half'.
212  
213 * a number sets the topology to a Dion cube of the given order.
214 That is,
215  
216 sudokug> set cube 3
217  
218 generates a 9 x 9 x 9 Dion cube, with the small squares being 3 x 3.
219 The problem is entered in plane, row, and column order, as though you
220 were entering the required number of normal Sudoku puzzles
221 back-to-back.
222  
223 * 'full' generates a topology that includes all faces of the cube. The
224 sets are the faces of the cube, and the rows, columns, and (for lack
225 of a better word) planes of cells that circle the cube.
226  
227 To enter the problem, imagine the cube unfolded to make a Latin cross.
228 Then, enter the problem in order by faces, rows, and columns, top to
229 bottom and left to right. The order of entry is actually by cell
230 number, as given below.
231  
232 +-------------+
233 | 0 1 2 3 |
234 | 4 5 6 7 |
235 | 8 9 10 11 |
236 | 12 13 14 15 |
237 +-------------+-------------+-------------+
238 | 16 17 18 19 | 32 33 34 35 | 48 49 50 51 |
239 | 20 21 22 23 | 36 37 38 39 | 52 53 54 55 |
240 | 24 25 26 27 | 40 41 42 43 | 56 57 58 59 |
241 | 28 29 30 31 | 44 45 46 47 | 60 61 62 63 |
242 +-------------+-------------+-------------+
243 | 64 65 66 67 |
244 | 68 69 70 71 |
245 | 72 73 74 75 |
246 | 76 77 78 79 |
247 +-------------+
248 | 80 81 82 83 |
249 | 84 85 86 87 |
250 | 88 89 90 91 |
251 | 92 93 94 95 |
252 +-------------+
253  
254 The solution will be displayed in order by cell number, with line
255 breaks controlled by the C<columns> attribute, just
256 like any other solution presented by this package.
257  
258 I have seen such puzzles presented with the bottom square placed to the
259 right and rotated counterclockwise 90 degrees. You will need to perform
260 the opposite rotation when you enter the problem.
261  
262 * 'half' generates a topology that looks like an isometric view of a
263 cube, with the puzzle on the visible faces. The faces are divided in
264 half, since the set size here is 8, not 16. Imagine the isometric
265 unfolded to make an L-shape. Then, enter the problem in order by faces,
266 rows, and columns, top to bottom and left to right. The order of entry
267 is actually in order by cell number, as given below.
268  
269 +-------------------+
270 | 0 1 2 3 |
271 | |
272 | 4 5 6 7 |
273 +-------------------+
274 | 8 9 10 11 |
275 | |
276 | 12 13 14 15 |
277 +---------+---------+-------------------+
278 | 16 17 | 18 19 | 32 33 34 35 |
279 | | | |
280 | 20 21 | 22 23 | 36 37 38 39 |
281 | | +-------------------+
282 | 24 25 | 26 27 | 40 41 42 43 |
283 | | | |
284 | 28 29 | 30 31 | 44 45 46 47 |
285 +---------+---------+-------------------+
286  
287 The solution will be displayed in order by cell number, with line
288 breaks controlled by the C<columns> attribute, just
289 like any other solution presented by this package.
290  
291 For the 'full' and 'half' cube puzzles, the C<columns> attribute is
292 set to 4, and the C<symbols> attribute to the numbers 1
293 to the size of the largest set (16 for the full cube, 8 for the half
294 or isometric cube). I have seen full cube puzzles done with hex digits
295 0 to F; these are handled most easily by setting the
296 C<symbols> attribute appropriately:
297  
298 $su->set (cube => 'full', symbols => <<eod);
299 . 0 1 2 3 4 5 6 7 8 9 A B C D E F
300 eod
301  
302 =item debug (number)
303  
304 This attribute, if not 0, causes debugging information to be displayed.
305 Values other than 0 are not supported, in the sense that the author
306 makes no commitment what will happen when a non-zero value is set, and
307 further reserves the right to change this behavior without notice of
308 any sort, and without documenting the changes.
309  
310 =item generation_limit (number)
311  
312 This attribute governs how hard the generate() method tries to generate
313 a problem. If generate() cannot generate a problem after this number of
314 tries, it gives up.
315  
316 The default is 30.
317  
318 =item iteration_limit (number)
319  
320 This attribute governs how hard the solution() method tries to solve
321 a problem. An iteration is an attempt to use the backtrack constraint.
322 Since what this really counts is the number of times we place a
323 backtrack constraint on the stack, not the number of values generated
324 from that constraint, I suspect 10 to 20 is reasonable for a "normal"
325 sudoku problem.
326  
327 The default is 0, which imposes no limit.
328  
329 =item largest_set (number, read-only)
330  
331 This read-only attribute returns the size of the largest set defined by
332 the current topology.
333  
334 =item latin (number, write-only)
335  
336 This "virtual" attribute is a convenience, which causes the object to
337 be configured to handle a Latin square. The value gives the size of
338 the square. Setting this modifies the following "real" attributes:
339  
340 columns is set to the size of the square;
341 symbols is set to "." and the letters "A", "B",
342 and so on, up to the size of the square;
343 topology is set to represent the rows and columns
344 of a square, with row sets named "r0", "r1",
345 and so on, and the column sets named "c0",
346 "c1", and so on.
347  
348 =item max_tuple (number)
349  
350 This attribute represents the maximum-sized tuple to consider for the
351 tuple constraint. It is possible that one might want to modify this
352 upward for large puzzles, or downward for small ones.
353  
354 The default is 4, meaning that the solution considers doubles, triples,
355 and quads only.
356  
357 =item name (string)
358  
359 This attribute is for information, and is not used by the class.
360  
361 =item null (string, write-only)
362  
363 This "virtual" attribute is a convenience, which causes the object to
364 be configured with the given number of cells, but no topology. The
365 topology must be added later using the add_set method once for each
366 set of cells to be created.
367  
368 The value must be either a comma-separated list of one to three numbers
369 (e.g. '81,9,9') or a reference to a list containing one to three
370 numbers (e.g. [81, 9, 9]). The first (and only required) number gives the
371 number of cells. The second, if supplied, sets the 'columns' attribute,
372 and the third, if supplied, sets the 'rows' attribute. For example,
373  
374 $su->set (null => [36, 6]);
375 $su->add_set (r0 => 0, 1, 2, 3, 4, 5);
376 $su->add_set (r1 => 6, 7, 8, 9, 10, 11);
377 ...
378 $su->add_set (c0 => 0, 6, 12, 18, 24, 30);
379 $su->add_set (c1 => 1, 7, 13, 19, 25, 31);
380 ...
381 $su->add_set (s0 => 0, 1, 2, 6, 7, 8);
382 $su->add_set (s1 => 3, 4, 5, 9, 10, 11);
383 ...
384  
385 Generates the topology equivalent to
386  
387 $su->set (brick => [3, 2])
388  
389 =item output_delimiter (string)
390  
391 This attribute specifies the delimiter to be used between cell values
392 on output. The default is a single space.
393  
394 =item quincunx (text, write-only)
395  
396 This "virtual" attribute is a convenience, which causes the object to be
397 configured as a quincunx (a. k. a. 'Samurai Sudoku' at
398 L<https://www.samurai-sudoku.com/>). The value must be
399 either a comma-separated list of one to two numbers (e.g. '3,1') or a
400 reference to a list of one to two numbers (e.g. [3, 1]). In either case,
401 the numbers are the order of the quincunx (3 corresponding to the usual
402 'Samurai Sudoku' configuration), and the gap between the arms of the
403 quincunx, in small squares. The gap must be strictly less than the
404 order, and the same parity (odd or even) as the order. If the gap is not
405 specified, it defaults to the smallest possible.
406  
407 To be specific,
408  
409 $su->set(quincunx => 3)
410  
411 is equivalent to
412  
413 $su->set(quincunx => [3, 1])
414  
415 and both specify the 'Samurai Sudoku' configuration.
416  
417 The actual topology is set up as a square of (2 * order + gap) * order
418 cells on a side, with the cells in the gap being unused. The sets used
419 are the same as for sudoku of the same order, but with 'g0' through 'g4'
420 prepended to their names, with g0 being the top left sudoku grid, g1 the
421 top right, g2 the middle, g3 the bottom left, and g4 the bottom right.
422  
423 In the case of the 's' sets, this would result in duplicate sets being
424 generated in the overlap area, so the 's' set from the higher-numbered
425 grid is suppressed. For example, in the 'Samurai Sudoku' configuration,
426 sets g0s8, g1s6, g2s6, and g2s8 contain exactly the same cells as g2s0,
427 g2s2, g3s2, and g4s0 respectively, so the latter are suppressed, and
428 only the former appear in the topology.
429  
430 Problems are specified left-to-right by rows. The cells in the gaps are
431 unused, and are not specified. For example, the May 2, 2008 'Samurai
432 Sudoku' problem could be specified as
433  
434 . . . . . 1 . . . . . . 4 . . . . .
435 . . . . 3 . 6 . . . . 7 . 2 . . . .
436 . . . 7 . . . 5 . . 4 . . . 5 . . .
437  
438 . . 6 9 . . . . 7 6 . . . . 9 1 . .
439 . 5 . . 2 . . 4 . . 2 . . 5 . . 9 .
440 4 . . . . 5 2 . . . . 8 1 . . . . 7
441  
442 . 2 . . . 4 . . . . 8 . . . . 3 . . . 2 .
443 . . 5 . 6 . . . . 4 . 5 . . . . 8 . 4 . .
444 . . . 1 . . . . . . 7 . . . . . . 7 . . .
445  
446 . 4 . . 6 . . 2 .
447 6 . 7 8 . 9 4 . 1
448 . 1 . . 4 . . 3 .
449  
450 . . . 7 . . . . . . 9 . . . . . . 6 . . .
451 . . 8 . 2 . . . . 2 . 8 . . . . 8 . 5 . .
452 . 4 . . . 3 . . . . 5 . . . . 3 . . . 2 .
453  
454 2 . . . . 7 8 . . . . 4 1 . . . . 6
455 . 3 . . 5 . . 4 . . 3 . . 2 . . 4 .
456 . . 4 8 . . . . 7 2 . . . . 3 1 . .
457  
458 . . . 9 . . . 1 . . 5 . . . 8 . . .
459 . . . . 6 . 9 . . . . 7 . 4 . . . .
460 . . . . . 4 . . . . . . 2 . . . . .
461  
462 Setting this attribute causes the rows and columns attributes to be set
463 to (2 * order + gap) * order. The symbols attribute is set to '.' and
464 the numbers 1, 2, ... up to order * order.
465  
466 =item rows (number)
467  
468 This attribute defines the number of lines of output to present before
469 inserting a blank line (for readability) when formatting the topology
470 attribute, or the solution to a puzzle.
471  
472 =item status_text (text, read-only)
473  
474 This attribute is a short piece of text corresponding to the
475 status_value.
476  
477 =item status_value (number)
478  
479 The solution() method sets a status, which can be retrieved via this
480 attribute. The retrieved value is one of
481  
482 SUDOKU_SUCCESS
483 This means what you think it does.
484 SUDOKU_NO_SOLUTION
485 This means the method exhausted all possible
486 soltions without finding one
487 SUDOKU_TOO_HARD
488 This means the iteration_limit attribute was
489 set to a positive number and the solution()
490 method hit the limit without finding a solution.
491  
492 =item sudoku (number, write-only)
493  
494 This attribute is a convenience, which causes the object to be
495 configured to handle a standard Sudoku square. The value gives the size
496 of the small squares into which the big square is divided. The big
497 square's side is the square of the value.
498  
499 For example, the customary Sudoku topology is set by
500  
501 $su->set (sudoku => 3);
502  
503 This attribute is implemented in terms of C<set( brick => ... )>,
504 and modifies the same "real" attributes. See the C<brick> attribute for
505 the details.
506  
507 =item sudokux (number, write-only)
508  
509 This attribute is a convenience. It is similar to the 'sudoku'
510 attribute, but the topology includes both main diagonals (set names
511 'd0' and 'd1') in addition to the standard sets. See the
512 C<brick> attribute for the details, since that's ultimately how this
513 attribute is implemented.
514  
515 =item symbols (string)
516  
517 This attribute defines the symbols to be used in the puzzle. Any
518 printing characters may be used except ",". Multi-character symbols
519 are supported. The value of the attribute is a whitespace-delimited
520 list of the symbols, though the whitespace is optional if all symbols
521 (and symbol constraints if any) are a single character. See the
522 L<problem()|/problem> documentation for full details.
523  
524 The first symbol in the list is the one that represents an empty cell.
525 Except for this, the order of the symbols is immaterial.
526  
527 The symbols defined here are used only for input or output. It is
528 perfectly legitimate to set symbols, call the problem() method, and
529 then change the symbols. The solution() method will return solutions
530 in the new symbol set. I have no idea why you would want to do this.
531  
532 =item topology (string)
533  
534 This attribute defines the topology of the puzzle, in terms of what
535 sets each cell belongs to. Each cell is defined in terms of a
536 comma-delimited list of the names of the sets it belongs to, and
537 the string is a whitespace-delimited list of cell definitions. For
538 example, a three-by-three grid with diagonals can be defined as
539 follows in terms of sets r1, r2, and r3 for the rows, c1, c2, and
540 c3 for the columns, and d1 and d2 for the diagonals:
541  
542 r1,c1,d1 r1,c2 r1,c3,d2
543 r2,c1 r2,c2,d1,d2 r2,c3
544 r3,c1,d2 r3,c2 r3,c3,d1
545  
546 The parser treats line breaks as whitespace. That is to say, the
547 above definition would be the same if it were all on one line.
548  
549 You do not need to define the sets themselves anywhere. The
550 package defines each set as it encounters it in the topology
551 definition.
552  
553 For certain topologies (e.g. the London Times Quincunx) it may be
554 convenient to include in the definition cells that are not part of the
555 puzzle. Such unused cells are defined by specifying just a comma,
556 without any set names.
557  
558 Setting the topology invalidates any currently-set-up problem.
559  
560 =back
561  
562 =head2 Methods
563  
564 This package provides the following public methods:
565  
566 =cut
567  
568 package Games::Sudoku::General;
569  
570 2 ▶use 5.006002; # For 'our', at least.
Subroutine BEGIN: called CC=1 SCAR=0.0
571  
572 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
573 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
574  
575 2 ▶use Exporter qw{ import };
Subroutine BEGIN: called CC=1 SCAR=0.0
576  
577 2 our $VERSION = '0.027_01';
578 2 our @EXPORT_OK = qw{
579 SUDOKU_SUCCESS
580 SUDOKU_NO_SOLUTION
581 SUDOKU_TOO_HARD
582 SUDOKU_MULTIPLE_SOLUTIONS
583 };
584 2 our %EXPORT_TAGS = (
585 all => \@EXPORT_OK,
586 status => \@EXPORT_OK,
587 );
588 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
589 2 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
590 2 ▶use List::Util qw{first max reduce};
Subroutine BEGIN: called CC=1 SCAR=0.0
591 2 ▶use POSIX qw{floor};
Subroutine BEGIN: called CC=1 SCAR=0.0
592  
593 2 ▶use constant SUDOKU_SUCCESS => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
594 2 ▶use constant SUDOKU_NO_SOLUTION => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
595 2 ▶use constant SUDOKU_TOO_HARD => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
596 2 ▶use constant SUDOKU_MULTIPLE_SOLUTIONS => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
597  
598 2 my @status_values = (
599 'Success',
600 'No solution found',
601 'No solution found before exceeding iteration limit',
602 'Multiple solutions found',
603 );
604  
605 2 ▶use constant HASH_REF => ref {};
Subroutine BEGIN: called CC=1 SCAR=0.0
606  
607 =head2 new
608  
609 $su = Games::Sudoku::General->new ()
610  
611 This method instantiates a new Games::Sudoku::General object. Any
612 arguments are passed to the set() method. If, after processing
613 the arguments, the object does not have a topology,
614  
615 $self->set (sudoku => 3)
616  
617 is called. If there is no symbols setting (which could happen
618 if the user passed an explicit topology),
619  
620 $self->set (symbols => join ' ', '.',
621 1 .. $self->get ('largest_set'))
622  
623 is called. If, after all this, there is still no columns setting,
624 the number of columns is set to the number of symbols, excluding
625 the "empty cell" symbol.
626  
627 The newly-instantiated object is returned.
628  
629 =cut
630  
631 sub new {
632 2 ▶ my ($class, @args) = @_;
Subroutine new: called CC=8 SCAR=23.1
Pod documented
633 2 ▶ ref $class and $class = ref $class;
Branch: 1 branch
BranchTrueFalseTotal
ref $class and $class = ref $class 0 2 2
634 2 my $self = bless {
635 debug => 0,
636 generation_limit => 30,
637 iteration_limit => 0,
638 output_delimiter => ' ',
639 }, $class;
640 2 ▶ @args and $self->set (@args);
Branch: 1 branch
BranchTrueFalseTotal
@args and $self->set(@args) 0 2 2
641 2 ▶ $self->{cell} or $self->set (sudoku => 3);
Branch: 1 branch
BranchTrueFalseTotal
$self->{"cell"} or $self->set("sudoku", 3) 2 0 2
642 $self->{symbol_list}
643 2 ▶ or $self->set (symbols => join ' ', '.', 1 .. $self->{largest_set});
Branch: 1 branch
BranchTrueFalseTotal
$self->{"symbol_list"} or $self->set("symbols", join(" ", ".", 1 .. $self->{"largest_set"})) 0 2 2
644 defined $self->{columns}
645 2 ▶ or $self->set (columns => @{$self->{symbol_list}} - 1);
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{"columns"} or $self->set("columns", @{$self->{"symbol_list"};} - 1) 0 2 2
646 defined $self->{status_value}
647 2 ▶ or $self->set (status_value => SUDOKU_SUCCESS);
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{"status_value"} or $self->set("status_value", 0) 2 0 2
648 defined $self->{max_tuple}
649 2 ▶ or $self->set (max_tuple => 4);
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{"max_tuple"} or $self->set("max_tuple", 4) 2 0 2
650 2 return $self;
651 }
652  
653 =head2 add_set
654  
655 $su->add_set ($name => $cell ...)
656  
657 This method adds to the current topology a new set with the given name,
658 and consisting of the given cells. The set name must not already
659 exist, but the cells must already exist. In other words, you can't
660 modify an existing set with this method, nor can you add new cells.
661  
662 =cut
663  
664 sub add_set {
665 21 ▶ my ($self, $name, @cells) = @_;
Subroutine add_set: called CC=8 SCAR=20.9
Pod documented
666 21 ▶ $self->{set}{$name} and croak <<eod;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"set"}{$name} and croak("Error - Set '${name}' already exists.\n") 0 21 21
667 Error - Set '$name' already exists.
668 eod
669 21 foreach my $inx (@cells) {
670 184 ▶ $self->{cell}[$inx] or croak <<eod
Branch: 1 branch
BranchTrueFalseTotal
$self->{"cell"}[$inx] or croak("Error - Cell $inx does not exist.\n") 0 184 184
671 Error - Cell $inx does not exist.
672 eod
673 }
674 21 foreach my $inx (@cells) {
675 184 my $cell = $self->{cell}[$inx];
676 184 ▶ @{$cell->{membership}} or --$self->{cells_unused};
Branch: 1 branch
BranchTrueFalseTotal
@{$cell->{"membership"};} or --$self->{"cells_unused"} 0 184 184
677 184 foreach my $other (@{$cell->{membership}}) {
678 468 my $int = join ',', sort $other, $name;
679 468 ▶ $self->{intersection}{$int} ||= [];
Condition100%2/2
$self->{"intersection"}{$int} ||= []
l!l
192276
MC/DC100%1/1
$self->{"intersection"}{$int} ||= []
$self->{"intersection"}{$int}
Truth table100%2/2
A
A= $self->{"intersection"}{$int}
Aresult
11
00
680 468 push @{$self->{intersection}{$int}}, $inx;
681 }
682 184 @{$cell->{membership}} = sort $name, @{$cell->{membership}};
683 }
684 21 $self->{set}{$name} = {
685 name => $name,
686 membership => [sort @cells],
687 };
688 $self->{largest_set} = max ($self->{largest_set},
689 21 scalar @{$self->{set}{$name}{membership}});
690 21 delete $self->{backtrack_stack}; # Force setting of new problem.
691 21 return $self;
692 }
693  
694 =head2 constraints_used
695  
696 %constraints_used = $su->constraints_used;
697  
698 This method returns a hash containing the constraints used in the most
699 recent call to solution(), and the number of times each was used. The
700 constraint codes are the same as for the steps() method. If called in
701 scalar context it returns a string representing the constraints used
702 at least once, in canonical order (i.e. in the order documented in the
703 steps() method).
704  
705 B<Note:> As of version 0.002, the string returned by the scalar has
706 spaces delimiting the constraint names. They were not delimited in
707 version 0.001
708  
709 =cut
710  
711 sub constraints_used {
712 6 ▶ my ( $self ) = @_;
Subroutine constraints_used: called CC=4 SCAR=15.5
Pod documented
713 6 ▶ return unless $self->{constraints_used} && defined wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"constraints_used"} and defined wantarray 0 6 6
Condition33%1/3
$self->{"constraints_used"} and defined wantarray
!ll&&!rl&&r
006
MC/DC0%0/2
$self->{"constraints_used"} and defined wantarray
$self->{"constraints_used"} defined wantarray
Truth table33%1/3
A and B
A= $self->{"constraints_used"}B= defined wantarray
ABresult
0X0
100
111
714 6 ▶ return %{$self->{constraints_used}} if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 0 6 6
715 my $rslt = join ' ', grep {
716 6 $self->{constraints_used}{$_}} qw{F N B T X Y W ?};
717 6 return $rslt;
718 }
719  
720 =head2 copy
721  
722 $su->copy ()
723  
724 This method copies the current problem to the clipboard. If solution()
725 has been called, the current solution goes on the clipboard.
726  
727 See L<CLIPBOARD SUPPORT|/CLIPBOARD SUPPORT> for what is needed for this
728 to work.
729  
730 =cut
731  
732 { # Local symbol block.
733 2 my $copier;
734 sub copy {
735 1 ▶ my ( $self ) = @_;
Subroutine copy: called CC=3 SCAR=11.3
Pod documented
736 1 ▶ ( $copier ||= eval {
Branch: 1 branch
BranchTrueFalseTotal
$copier ||= eval { do { require Clipboard; "Clipboard"->import; sub { "Clipboard"->copy(join("", @_)); return; } } } or croak("copy() unavailable; can not load Clipboard") 0 1 1
Condition33%1/3
$copier ||= eval { do { require Clipboard; "Clipboard"->import; sub { "Clipboard"->copy(join("", @_)); return; } } }
l!l&&r!l&&!r
010
MC/DC0%0/2
$copier ||= eval { do { require Clipboard; "Clipboard"->import; sub { "Clipboard"->copy(join("", @_)); return; } } }
$copier eval { do { require Clipboard; "Clipboard"->import; sub { "Clipboard"->copy(join("", @_)); return; } } }
Truth table33%1/3
A ||= B
A= $copierB= eval { do { require Clipboard; "Clipboard"->import; sub { "Clipboard"->copy(join("", @_)); return; } } }
ABresult
1X1
011
000
737 1 require Clipboard;
738 1 Clipboard->import();
739 sub {
740 1 ▶ Clipboard->copy( join '', @_ );
Subroutine __ANON__: called CC=1 SCAR=0.0
741 1 return;
742 1 };
743 }
744 ) or croak 'copy() unavailable; can not load Clipboard';
745 1 $copier->( $self->_unload() );
746 1 return $self;
747 }
748 }
749  
750 =head2 drop_set
751  
752 $su->drop_set( $name )
753  
754 This method removes from the current topology the set with the given
755 name. The set must exist, or an exception is raised.
756  
757 =cut
758  
759 2 sub drop_set {
760 1 ▶ my ($self, $name) = @_;
Subroutine drop_set: called CC=7 SCAR=19.7
Pod documented
761 1 ▶ $self->{set}{$name} or croak <<eod;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"set"}{$name} or croak("Error - Set '${name}' not defined.\n") 0 1 1
762 Error - Set '$name' not defined.
763 eod
764 1 foreach my $inx (@{$self->{set}{$name}{membership}}) {
765 4 my $cell = $self->{cell}[$inx];
766 4 my @mbr;
767 4 foreach my $other (@{$cell->{membership}}) {
768 12 ▶ if ($other ne $name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($other ne $name) 8 4 12
769 8 push @mbr, $other;
770 8 my $int = join ',', sort $other, $name;
771 8 delete $self->{intersection}{$int};
772 }
773 }
774 4 ▶ if (@mbr) {
Branch: 1 branch
BranchTrueFalseTotal
if (@mbr) { } 4 0 4
775 4 @{$cell->{membership}} = sort @mbr;
776 } else {
777 0 @{$cell->{membership}} = ();
778 0 $self->{cells_unused}++;
779 }
780 }
781 1 delete $self->{set}{$name};
782 1 $self->{largest_set} = 0;
783 1 foreach (keys %{$self->{set}}) {
784 $self->{largest_set} = max ($self->{largest_set},
785 8 scalar @{$self->{set}{$_}{membership}});
786 }
787 1 delete $self->{backtrack_stack}; # Force setting of new problem.
788 1 return $self;
789 }
790  
791 =head2 generate
792  
793 $problem = $su->generate( $min, $max, $const );
794  
795 This method generates a problem and returns it.
796  
797 The $min argument is the minimum number of givens in the puzzle. You
798 may (and probably will) get more. The default is the number of cells
799 in the puzzle divided by the number of sets a cell belongs to.
800  
801 The value of this argument is critical to getting a puzzle: too large
802 and you generate puzzles with no solution; too small and you spend all
803 your time backtracking. There is no science behind the default, just an
804 attempt to make a rational heuristic based on the number of degrees of
805 freedom and the observation that about a third of the cells are given
806 in a typical Sudoku puzzle. My experience with the default is:
807  
808 topology comment
809 brick 3,2 default is OK
810 corresponding 3 default is OK
811 cube 3 default is too large
812 cube half default is OK
813 cube full default is OK
814 quincunx 3 default is too large
815 sudoku 3 default is OK
816 sudoku 4 default is OK
817 sudokux 3 default is OK
818  
819 Typically when I take the defaults I get a puzzle in anywhere from
820 a few seconds (most of the listed topologies) to a couple minutes
821 (sudoku 4) on an 800 Mhz G4. But I have never successfully generated
822 a Dion cube (cube 3). C<Caveat user.>
823  
824 The $max argument is the maximum number of givens in the puzzle. You
825 may get less. The default is 1.5 times the minimum.
826  
827 The $const argument specifies the constraints to be used in the
828 generated puzzle. This may be specified either as a string or as a hash
829 reference. If specified as a string, it is a whitespace-delimited list,
830 with each constraint name possibly followed by an equals sign and a
831 number to specify that that constraint can be used only a certain
832 number of times. For example, 'F N ?=1' specifies a puzzle to be
833 solved by use of any number of applications of the F and N constraints,
834 and at most one guessed cell. If specified as a hash reference, the
835 keys are the constraint names, and the values are the usage counts,
836 with undef meaning no limit. The hash reference corresponding to
837 'F N ?=1' is {F => undef, N => undef, '?' => 1}. The default for this
838 argument is to allow all known constraints except '?'.
839  
840 In practice, the generator usually generates puzzles solvable using
841 only the F constraint, or the F and N constraints.
842  
843 The algorithm used is to generate a puzzle with the minimum number of
844 cells selected at random, and then solve it. If a solution does not
845 exist, we try again until we have tried
846 C<generation_limit> times, then we return undef.
847 B<This means generate() is not guaranteed to generate a puzzle.>
848  
849 If we get a solution, we remove allowed constraints. If we run into
850 a constraint that is not allowed, we either stop (if we're below the
851 maximum number of givens) or turn it into a given value (if we're
852 above the maximum). We stop unconditionally if we get down to the
853 minimum number of givens. As a side effect, the generated puzzle is
854 set up as a problem.
855  
856 Note that if you allow guesses you may get puzzles with more than
857 one solution.
858  
859 =cut
860  
861 sub generate {
862 0 ▶ my ( $self, $min, $max, $const ) = @_;
Subroutine generate: not called CC=19 SCAR=59.4
Pod documented
863 0 my $size = @{$self->{cell}} - $self->{cells_unused};
864 0 ▶ $min ||= do {
Condition0%0/3
$min ||= do { floor($size * $size / ($self->{"largest_set"} * keys(%{$$self{"set"};}))) }
l!l&&r!l&&!r
000
MC/DC0%0/2
$min ||= do { floor($size * $size / ($self->{"largest_set"} * keys(%{$$self{"set"};}))) }
$min do { floor($size * $size / ($self->{"largest_set"} * keys(%{$$self{"set"};}))) }
Truth table0%0/3
A ||= B
A= $minB= do { floor($size * $size / ($self->{"largest_set"} * keys(%{$$self{"set"};}))) }
ABresult
1X1
011
000
865 floor( $size * $size /
866 0 ( $self->{largest_set} * keys %{ $self->{set} } ) );
867 };
868 0 ▶ $max ||= floor( $min * 1.5 );
Condition0%0/3
$max ||= floor($min * "1.5")
l!l&&r!l&&!r
000
MC/DC0%0/2
$max ||= floor($min * "1.5")
$max floor($min * "1.5")
Truth table0%0/3
A ||= B
A= $maxB= floor($min * "1.5")
ABresult
1X1
011
000
869 0 ▶ $const ||= 'F N B T';
Condition0%0/2
$const ||= "F N B T"
l!l
00
MC/DC0%0/1
$const ||= "F N B T"
$const
Truth table0%0/2
A
A= $const
Aresult
11
00
870 0 ▶ croak <<"EOD" if ref $const && HASH_REF ne ref $const;
Branch: 1 branch
BranchTrueFalseTotal
if ref $const and "HASH" ne ref $const 0 0 0
Condition0%0/3
ref $const and "HASH" ne ref $const
!ll&&!rl&&r
000
MC/DC0%0/2
ref $const and "HASH" ne ref $const
ref $const "HASH" ne ref $const
Truth table0%0/3
A and B
A= ref $constB= "HASH" ne ref $const
ABresult
0X0
100
111
871 Error - The constraints argument must be a string or a hash reference,
872 0 not a @{[ref $const]} reference.
873 EOD
874 0 ▶ $const = {map {my @ret; $_ and do {
Branch: 2 branches
BranchTrueFalseTotal
unless "HASH" eq ref $const 0 0 0
$_ and do { @ret = split(/=/, $_, 2); push @ret, undef while @ret < 2 } 0 0 0
875 0 ▶ @ret = split '=', $_, 2; push @ret, undef while @ret < 2}; @ret}
Branch: 1 branch
BranchTrueFalseTotal
@ret < 2 and do { push @ret, undef } 0 0 0
876 split '\s+', $const}
877 unless HASH_REF eq ref $const;
878 0 ▶ $self->{debug} and do {
Branch: 1 branch
BranchTrueFalseTotal
$self->{"debug"} and do { local $Data::Dumper::Terse = 1; print "Debug generate ($min, $max, @{[Dumper($const)];})\n" } 0 0 0
879 0 local $Data::Dumper::Terse = 1;
880 0 print <<eod;
881 0 Debug generate ($min, $max, @{[Dumper $const]})
882 eod
883 };
884 0 my $syms = @{$self->{symbol_list}} - 1;
885 0 ▶ croak <<eod if $min > $size;
Branch: 1 branch
BranchTrueFalseTotal
if $min > $size 0 0 0
886 Error - You specified a minimum of $min given values, but the puzzle
887 only contains $size cells.
888 eod
889 0 my $tries = $self->{generation_limit};
890 0 $size = @{$self->{cell}}; # Note equivocation on $size.
891 0 local $Data::Dumper::Terse = 1;
892 my @universe = $self->{cells_unused} ?
893 0 grep {@{$self->{cell}[$_]{membership}}} (0 .. @{$self->{cell}} - 1) :
894 0 ▶ (0 .. @{$self->{cell}} - 1);
Branch: 1 branch
BranchTrueFalseTotal
$self->{'cells_unused'} ? : 0 0 0
895 0 ▶ while (--$tries >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (--$tries >= 0) 0 0 0
896 0 $self->problem (); # We rely on this specifying an empty problem.
897 ## my @ix = (0 .. $size - 1);
898 0 my @ix = @universe;
899 0 my $gen = 0;
900 0 ▶ while ($gen++ < $min) {
Branch: 1 branch
BranchTrueFalseTotal
if ($gen++ < $min) 0 0 0
901 0 my ($inx) = splice @ix, floor (rand scalar @ix), 1;
902 0 my $cell = $self->{cell}[$inx];
903 ## @{$cell->{membership}} or redo; # Ignore unused cells.
904 0 ▶ my @pos = grep {!$cell->{possible}{$_}} 1 .. $syms or next;
Branch: 1 branch
BranchTrueFalseTotal
my(@pos) = grep({not $cell->{"possible"}{$_};} 1 .. $syms) or next 0 0 0
905 0 my $val = $pos[floor (rand scalar @pos)];
906 0 ▶ defined $val or confess <<eod, Dumper ($cell->{possible});
Branch: 1 branch
BranchTrueFalseTotal
defined $val or confess("Programming error - generate() selected an undefined value for cell $inx.\n Possible values hash is:\n", Dumper($cell->{"possible"})) 0 0 0
907 Programming error - generate() selected an undefined value for cell $inx.
908 Possible values hash is:
909 eod
910 $self->_try ($cell, $val)
911 0 ▶ and confess <<eod, Dumper ($cell->{possible});
Branch: 1 branch
BranchTrueFalseTotal
$self->_try($cell, $val) and confess("Programming error - generate() tried to assign $val to cell $inx,\n but it was rejected. Possible values hash is:\n", Dumper($cell->{"possible"})) 0 0 0
912 Programming error - generate() tried to assign $val to cell $inx,
913 but it was rejected. Possible values hash is:
914 eod
915 }
916 0 ▶ $self->solution () or next;
Branch: 1 branch
BranchTrueFalseTotal
$self->solution or next 0 0 0
917 0 $self->_constraint_remove ($min, $max, $const);
918 0 my $prob = $self->_unload ('', SUDOKU_SUCCESS);
919 0 $self->problem ($prob);
920 0 ▶ $self->copy ($prob) if $self->{autocopy};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"autocopy"} 0 0 0
921 0 return $prob;
922 }
923 0 return;
924 }
925  
926 2 my %accessor = (
927 allowed_symbols => \&_get_allowed_symbols,
928 autocopy => \&_get_value,
929 columns => \&_get_value,
930 debug => \&_get_value,
931 generation_limit => \&_get_value,
932 ## ignore_unused => \&_get_value,
933 iteration_limit => \&_get_value,
934 largest_set => \&_get_value,
935 name => \&_get_value,
936 output_delimiter => \&_get_value,
937 rows => \&_get_value,
938 status_text => \&_get_value,
939 status_value => \&_get_value,
940 symbols => \&_get_symbols,
941 topology => \&_get_topology,
942 );
943  
944 =head2 get
945  
946 $value = $su->get( $name );
947  
948 This method returns the value of the named attribute. An exception
949 is thrown if the given name does not correspond to an attribute that
950 can be read. That is, the given name must appear on the list of
951 attributes above, and not be marked "write-only".
952  
953 If called in list context, you can pass multiple attribute names,
954 and get back a list of their values. If called in scalar context,
955 attribute names after the first are ignored.
956  
957 =cut
958  
959 sub get {
960 21 ▶ my ($self, @args) = @_;
Subroutine get: called CC=5 SCAR=16.7
Pod documented
961 21 my @rslt;
962 21 ▶ wantarray or @args = ($args[0]);
Branch: 1 branch
BranchTrueFalseTotal
wantarray or @args = $args[0] 21 0 21
963 21 foreach my $name (@args) {
964 21 ▶ exists $accessor{$name} or croak <<eod;
Branch: 1 branch
BranchTrueFalseTotal
exists $accessor{$name} or croak("Error - Attribute $name does not exist, or is write-only.\n") 0 21 21
965 Error - Attribute $name does not exist, or is write-only.
966 eod
967 21 push @rslt, $accessor{$name}->($self, $name);
968 }
969 21 ▶ return wantarray ? @rslt : $rslt[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 21 21
970 }
971  
972 sub _get_allowed_symbols {
973 2 ▶ my ( $self ) = @_;
Subroutine _get_allowed_symbols: called CC=4 SCAR=13.9
974 2 my $rslt = '';
975 2 my $syms = @{$self->{symbol_list}};
976 2 foreach (sort keys %{$self->{allowed_symbols}}) {
977 6 my @symlst;
978 6 ▶ for (my $val = 1; $val < $syms; $val++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val < $syms) 54 6 60
979 push @symlst, $self->{symbol_list}[$val]
980 54 ▶ if $self->{allowed_symbols}{$_}[$val];
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"allowed_symbols"}{$_}[$val] 27 27 54
981 }
982 6 $rslt .= "$_=@{[join ',', @symlst]}\n";
983 }
984 2 return $rslt;
985 }
986  
987 sub _get_symbols {
988 5 ▶ my ( $self ) = @_;
Subroutine _get_symbols: called CC=1 SCAR=0.0
989 5 return join ' ', @{$self->{symbol_list}};
990 }
991  
992 sub _get_topology {
993 8 ▶ my ( $self ) = @_;
Subroutine _get_topology: called CC=7 SCAR=19.6
994 8 my $rslt = '';
995 8 my $col = $self->{columns};
996 8 ▶ my $row = $self->{rows} ||= floor (@{$self->{cell}} / $col);
Condition33%1/3
$self->{"rows"} ||= floor(@{$self->{"cell"};} / $col)
l!l&&r!l&&!r
800
MC/DC0%0/2
$self->{"rows"} ||= floor(@{$self->{"cell"};} / $col)
$self->{"rows"} floor(@{$self->{"cell"};} / $col)
Truth table33%1/3
A ||= B
A= $self->{"rows"}B= floor(@{$self->{"cell"};} / $col)
ABresult
1X1
011
000
997 8 ▶ foreach (map {join (',', @{$_->{membership}}) || ','} @{$self->{cell}}) {
Branch: 1 branch
BranchTrueFalseTotal
join ",", @{$_->{"membership"};} or "," 0 583 583
998 583 $rslt .= $_;
999 583 ▶ if (--$col > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (--$col > 0) { } 522 61 583
1000 522 $rslt .= ' '
1001 } else {
1002 61 $rslt .= "\n";
1003 61 $col = $self->{columns};
1004 61 ▶ if (--$row <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (--$row <= 0) 8 53 61
1005 8 $rslt .= "\n";
1006 8 $row = $self->{rows};
1007 }
1008 }
1009 }
1010 8 ▶ 0 while chomp $rslt;
Branch: 1 branch
BranchTrueFalseTotal
chomp $rslt and do { '???' } 16 8 24
1011 8 $rslt .= "\n";
1012 8 return $rslt;
1013 }
1014  
1015 6 ▶sub _get_value {return $_[0]->{$_[1]}}
Subroutine _get_value: called CC=1 SCAR=0.0
1016  
1017 =head2 paste
1018  
1019 $su->paste()
1020  
1021 This method pastes a problem from the clipboard.
1022  
1023 See L<CLIPBOARD SUPPORT|/CLIPBOARD SUPPORT> for what is needed for this
1024 to work.
1025  
1026 =cut
1027  
1028 { # Begin local symbol block
1029  
1030 2 my $paster;
1031 sub paste {
1032 1 ▶ my ( $self ) = @_;
Subroutine paste: called CC=3 SCAR=11.3
Pod documented
1033 1 ▶ ( $paster ||= eval {
Branch: 1 branch
BranchTrueFalseTotal
$paster ||= eval { do { require Clipboard; "Clipboard"->import; return sub { return "Clipboard"->paste; } } } or croak("paste() unavailable; can not load Clipboard") 0 1 1
Condition33%1/3
$paster ||= eval { do { require Clipboard; "Clipboard"->import; return sub { return "Clipboard"->paste; } } }
l!l&&r!l&&!r
010
MC/DC0%0/2
$paster ||= eval { do { require Clipboard; "Clipboard"->import; return sub { return "Clipboard"->paste; } } }
$paster eval { do { require Clipboard; "Clipboard"->import; return sub { return "Clipboard"->paste; } } }
Truth table33%1/3
A ||= B
A= $pasterB= eval { do { require Clipboard; "Clipboard"->import; return sub { return "Clipboard"->paste; } } }
ABresult
1X1
011
000
1034 1 require Clipboard;
1035 1 Clipboard->import();
1036 return sub {
1037 1 ▶ return Clipboard->paste();
Subroutine __ANON__: called CC=1 SCAR=0.0
1038 1 };
1039 }
1040 ) or croak 'paste() unavailable; can not load Clipboard';
1041  
1042 1 $self->problem( $paster->() );
1043 1 $self->_unload();
1044 1 return $self;
1045 }
1046  
1047 } # End local symbol block
1048  
1049 =head2 problem
1050  
1051 $su->problem( $string );
1052  
1053 This method specifies the problem to be solved, and sets the object
1054 up to solve the problem.
1055  
1056 The problem is specified by a whitespace-delimited list of the symbols
1057 contained by each cell. You can format the puzzle definition into a
1058 square grid (e.g. the SYNOPSIS section), but to the parser a line
1059 break is no different than spaces. If you pass an empty string, an
1060 empty problem will be set up - that is, one in which all cells are
1061 empty.
1062  
1063 An exception will be thrown if:
1064  
1065 * The puzzle definition uses an unknown symbol;
1066 * The puzzle definition has a different number
1067 of cells from the topology definition;
1068 * There exists a set with more members than the
1069 number of symbols, excluding the "empty"
1070 symbol.
1071  
1072 The whitespace delimiter is optional, provided that all symbol names
1073 are exactly one character long, B<and> that you have not defined any
1074 symbol constraint names more than one character long since the last
1075 time you set the symbol names.
1076  
1077 =cut
1078  
1079 2 sub problem {
1080 19 ▶ my ( $self, $val ) = @_;
Subroutine problem: called CC=29 SCAR=38.2
Pod documented
1081 19 ▶ $val ||= '';
Condition50%1/2
$val ||= ""
l!l
190
MC/DC0%0/1
$val ||= ""
$val
Truth table50%1/2
A
A= $val
Aresult
11
00
1082 $val =~ m/\S/ or
1083 $val = "$self->{symbol_list}[0] " x
1084 19 ▶ (scalar @{$self->{cell}} - $self->{cells_unused});
Branch: 1 branch
BranchTrueFalseTotal
$val =~ /\S/ or $val = (qq[$self->{"symbol_list"}[0] ]) x scalar(@{$self->{"cell"};} - $self->{"cells_unused"}) 0 19 19
1085 19 ▶ $val =~ s/\s+//g unless $self->{biggest_spec} > 1;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"biggest_spec"} > 1 17 2 19
1086 19 $val =~ s/^\s+//;
1087 19 $val =~ s/\s+$//;
1088 19 ▶ $self->{debug} and print <<eod;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"debug"} and print "Debug problem - Called with $val\n" 0 19 19
1089 Debug problem - Called with $val
1090 eod
1091  
1092 19 local $Data::Dumper::Terse = 1;
1093 19 ▶ $self->{largest_set} >= @{$self->{symbol_list}} and croak <<eod;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"largest_set"} >= @{$self->{"symbol_list"};} and croak(qq[Error - The largest set has $self->{"largest_set"} cells, but there are only @{[@{$self->{"symbol_list"};} - 1];} symbols.\n Either the set definition is in error or the list of symbols is\n incomplete.\n]) 0 19 19
1094 Error - The largest set has $self->{largest_set} cells, but there are only @{[
1095 0 @{$self->{symbol_list}} - 1]} symbols.
1096 Either the set definition is in error or the list of symbols is
1097 incomplete.
1098 eod
1099  
1100 19 my $syms = @{$self->{symbol_list}};
1101 19 foreach (@{$self->{cell}}) {
1102 1651 $_->{content} = $_->{chosen} = 0;
1103 1651 $_->{possible} = {map {$_ => 0} (1 .. $syms - 1)};
1104 }
1105 19 foreach (values %{$self->{set}}) {
1106 511 $_->{free} = @{$_->{membership}};
1107 511 $_->{content} = [$_->{free}];
1108 }
1109 19 $self->{cells_unassigned} = scalar @{$self->{cell}} - $self->{cells_unused};
1110  
1111 19 my $hash = $self->{symbol_hash};
1112 19 my $inx = 0;
1113 19 my $max = @{$self->{cell}};
1114 19 ▶ foreach (split (($self->{biggest_spec} > 1 ? '\s+' : ''), $val)) {
Branch: 1 branch
BranchTrueFalseTotal
$self->{'biggest_spec'} > 1 ? : 2 17 19
1115 1651 ▶ $inx >= $max and croak <<eod;
Branch: 1 branch
BranchTrueFalseTotal
$inx >= $max and croak("Error - Too many cell specifications. The topology allows only $max.\n") 0 1651 1651
1116 Error - Too many cell specifications. The topology allows only $max.
1117 eod
1118 1651 ▶ next unless defined $_;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_ 0 1651 1651
1119 # was $self->{ignore_unused}
1120 0 ($self->{cells_unused} && !@{$self->{cell}[$inx]{membership}})
1121 1651 ▶ and do {$inx++; redo};
Branch: 1 branch
BranchTrueFalseTotal
$self->{"cells_unused"} and not @{$self->{"cell"}[$inx]{"membership"};} and do { ++$inx; redo } 0 1651 1651
Condition33%1/3
$self->{"cells_unused"} and not @{$self->{"cell"}[$inx]{"membership"};}
!ll&&!rl&&r
165100
MC/DC0%0/2
$self->{"cells_unused"} and not @{$self->{"cell"}[$inx]{"membership"};}
$self->{"cells_unused"} not @{$self->{"cell"}[$inx]{"membership"};}
Truth table33%1/3
A and not(B)
A= $self->{"cells_unused"}B= not @{$self->{"cell"}[$inx]{"membership"};}
ABresult
0X0
100
111
1122 1651 ▶ $self->{allowed_symbols}{$_} and do {
Branch: 1 branch
BranchTrueFalseTotal
$self->{"allowed_symbols"}{$_} and do { $self->{"debug"} > 1 and print "Debug problem - Cell $inx allows symbol set $_\n"; my $cell = $self->{"cell"}[$inx]; @{$cell->{"membership"};} or croak("Error - Cell $inx is unused, and must be specified as empty.\n"); for (my $val = 1; $val < $syms; ++$val) { next if $self->{"allowed_symbols"}{$_}[$val]; $cell->{"possible"}{$val} = 1; } } 195 1456 1651
1123 195 ▶ $self->{debug} > 1 and print <<eod;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"debug"} > 1 and print "Debug problem - Cell $inx allows symbol set $_\n" 0 195 195
1124 Debug problem - Cell $inx allows symbol set $_
1125 eod
1126 195 my $cell = $self->{cell}[$inx];
1127 195 ▶ @{$cell->{membership}} or croak <<eod;
Branch: 1 branch
BranchTrueFalseTotal
@{$cell->{"membership"};} or croak("Error - Cell $inx is unused, and must be specified as empty.\n") 0 195 195
1128 Error - Cell $inx is unused, and must be specified as empty.
1129 eod
1130 195 ▶ for (my $val = 1; $val < $syms; $val++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val < $syms) 1692 195 1887
1131 1692 ▶ next if $self->{allowed_symbols}{$_}[$val];
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"allowed_symbols"}{$_}[$val] 943 749 1692
1132 749 $cell->{possible}{$val} = 1;
1133 }
1134 };
1135 1651 ▶ defined $hash->{$_} or $_ = $self->{symbol_list}[0];
Branch: 1 branch
BranchTrueFalseTotal
defined $hash->{$_} or $_ = $self->{"symbol_list"}[0] 365 1286 1651
1136 1651 (@{$self->{cell}[$inx]{membership}} ||
1137 1651 ▶ $_ eq $self->{symbol_list}[0])
Branch: 1 branch
BranchTrueFalseTotal
@{$self->{"cell"}[$inx]{"membership"};} or $_ eq $self->{"symbol_list"}[0] or croak("Error - Cell $inx is unused, and must be specified as empty.\n") 0 1651 1651
Condition33%1/3
@{$self->{"cell"}[$inx]{"membership"};} or $_ eq $self->{"symbol_list"}[0]
l!l&&r!l&&!r
165100
MC/DC0%0/2
@{$self->{"cell"}[$inx]{"membership"};} or $_ eq $self->{"symbol_list"}[0]
@{$self->{"cell"}[$inx]{"membership"};} $_ eq $self->{"symbol_list"}[0]
Truth table33%1/3
A or B
A= @{$self->{"cell"}[$inx]{"membership"};}B= $_ eq $self->{"symbol_list"}[0]
ABresult
1X1
011
000
1138 or croak <<eod;
1139 Error - Cell $inx is unused, and must be specified as empty.
1140 eod
1141 1651 ▶ $self->{debug} > 1 and print <<eod;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"debug"} > 1 and print "Debug problem - Cell $inx specifies symbol $_\n" 0 1651 1651
1142 Debug problem - Cell $inx specifies symbol $_
1143 eod
1144 1651 ▶ $self->_try ($inx, $hash->{$_}) and croak <<eod;
Branch: 1 branch
BranchTrueFalseTotal
$self->_try($inx, $hash->{$_}) and croak(qq[Error - Symbol '${_}' appears more than once in a set.\n The problem loaded thus far is:\n@{[$self->_unload(" ")];}\n]) 0 1651 1651
1145 Error - Symbol '$_' appears more than once in a set.
1146 The problem loaded thus far is:
1147 0 @{[$self->_unload (' ')]}
1148 eod
1149 1651 ▶ $self->{cell}[$inx]{chosen} = $hash->{$_} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$hash->{$_} ? : 480 1171 1651
1150 } continue {
1151 1651 $inx++;
1152 }
1153  
1154 19 ▶ unless ($inx == $max) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($inx == $max) 0 19 19
1155 # was $self->{ignore_unused}
1156 0 ▶ $self->{cells_unused} and do {
Branch: 1 branch
BranchTrueFalseTotal
$self->{"cells_unused"} and do { $inx -= $self->{"cells_unused"}; $max -= $self->{"cells_unused"} } 0 0 0
1157 0 $inx -= $self->{cells_unused};
1158 0 $max -= $self->{cells_unused};
1159 };
1160 0 croak <<eod;
1161 Error - Not enough cell specifications. you gave $inx but the topology
1162 defined $max.
1163 eod
1164 }
1165  
1166 19 $self->{constraints_used} = {};
1167  
1168 19 ▶ $self->{debug} and print <<eod;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"debug"} and print "Debug problem - problem loaded.\n" 0 19 19
1169 Debug problem - problem loaded.
1170 eod
1171  
1172 19 $self->{backtrack_stack} = [];
1173 19 $self->{cell_order} = [];
1174 19 delete $self->{no_more_solutions};
1175  
1176 19 ▶ $self->{debug} > 1 and print " object = ", Dumper ($self);
Branch: 1 branch
BranchTrueFalseTotal
$self->{"debug"} > 1 and print " object = ", Dumper($self) 0 19 19
1177  
1178 19 return $self;
1179 }
1180  
1181 2 my %mutator = (
1182 allowed_symbols => \&_set_allowed_symbols,
1183 autocopy => \&_set_value,
1184 brick => \&_set_brick,
1185 columns => \&_set_number,
1186 debug => \&_set_number,
1187 corresponding => \&_set_corresponding,
1188 cube => \&_set_cube,
1189 generation_limit => \&_set_number,
1190 ## ignore_unused => \&_set_value,
1191 iteration_limit => \&_set_number,
1192 latin => \&_set_latin,
1193 max_tuple => \&_set_number,
1194 name => \&_set_value,
1195 null => \&_set_null,
1196 output_delimiter => \&_set_value,
1197 quincunx => \&_set_quincunx,
1198 rows => \&_set_number,
1199 status_value => \&_set_status_value,
1200 sudoku => \&_set_sudoku,
1201 sudokux => \&_set_sudokux,
1202 symbols => \&_set_symbols,
1203 topology => \&_set_topology,
1204 );
1205  
1206 =head2 set
1207  
1208 $su->set( $name => $value );
1209  
1210 This method sets the value of the named attribute. An exception
1211 is thrown if the given name does not correspond to an attribute that
1212 can be written. That is, the given name must appear on the list of
1213 attributes above, and not be marked "read-only". An exception is
1214 also thrown if the value is invalid, e.g. a non-numeric value for
1215 an attribute marked "number".
1216  
1217 You can pass multiple name-value pairs. If an exception is thrown,
1218 all settings before the exception will be made, and all settings
1219 after the exception will not be made.
1220  
1221 The object itself is returned.
1222  
1223 =cut
1224  
1225 sub set {
1226 62 ▶ my ( $self, @args ) = @_;
Subroutine set: called CC=3 SCAR=11.0
Pod documented
1227 62 ▶ while ( @args ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args) 90 62 152
1228 90 my ( $name, $val ) = splice @args, 0, 2;
1229 90 ▶ exists $mutator{$name} or croak <<eod;
Branch: 1 branch
BranchTrueFalseTotal
exists $mutator{$name} or croak("Error - Attribute $name does not exist, or is read-only.\n") 0 90 90
1230 Error - Attribute $name does not exist, or is read-only.
1231 eod
1232 90 $mutator{$name}->($self, $name, $val );
1233 }
1234 62 return $self;
1235 }
1236  
1237 sub _set_allowed_symbols {
1238 ## my ( $self, $name, $value ) = @_;
1239 4 ▶ my ( $self, undef, $value ) = @_; # Name unused
Subroutine _set_allowed_symbols: called CC=12 SCAR=26.1
1240 4 ▶ defined $value or $value = '';
Branch: 1 branch
BranchTrueFalseTotal
defined $value or $value = "" 0 4 4
1241 4 my $maxlen = 0;
1242 4 ▶ $self->{debug} and print <<eod;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"debug"} and print "Debug allowed_symbols being set to '${value}'\n" 0 4 4
1243 Debug allowed_symbols being set to '$value'
1244 eod
1245 4 ▶ if ($value) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value) { } 4 0 4
1246 4 foreach (split '\s+', $value) {
1247 22 my ($name, $value) = split '=', $_, 2;
1248 22 ▶ croak <<eod if $self->{symbol_hash}{$name};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"symbol_hash"}{$name} 0 22 22
1249 Error - You can not use '$name' as a symbol constraint name, because
1250 it is a valid symbol name.
1251 eod
1252 22 ▶ $value or do {delete $self->{allowed_symbols}{$name}; next};
Branch: 1 branch
BranchTrueFalseTotal
$value or do { delete $self->{"allowed_symbols"}{$name}; next } 2 20 22
1253 20 $maxlen = max ($maxlen, length ($name));
1254 20 ▶ $self->{debug} > 1 and print <<eod;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"debug"} > 1 and print "Debug allowed_symbols - $_\n set name '${name}' has length @{[length $name];}. Maxlen now $maxlen.\n" 0 20 20
1255 Debug allowed_symbols - $_
1256 0 set name '$name' has length @{[length ($name)]}. Maxlen now $maxlen.
1257 eod
1258 20 my $const = $self->{allowed_symbols}{$name} = [];
1259 20 foreach (split ',', $value) {
1260 93 ▶ $self->{debug} > 1 and print <<eod;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"debug"} > 1 and print "Debug allowed_symbols - Adding symbol '${_}' to set '${name}'.\n" 0 93 93
1261 Debug allowed_symbols - Adding symbol '$_' to set '$name'.
1262 eod
1263 93 ▶ $self->{symbol_hash}{$_} or croak <<eod;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"symbol_hash"}{$_} or croak("Error - '${_}' is not a valid symbol.\n") 0 93 93
1264 Error - '$_' is not a valid symbol.
1265 eod
1266 93 $const->[$self->{symbol_hash}{$_}] = 1;
1267 }
1268 }
1269 } else {
1270 0 $self->{allowed_symbols} = {};
1271 }
1272 4 ▶ $self->{biggest_spec} = $maxlen if $maxlen > $self->{biggest_spec};
Branch: 1 branch
BranchTrueFalseTotal
if $maxlen > $self->{"biggest_spec"} 1 3 4
1273 4 return;
1274 }
1275  
1276 sub _set_brick {
1277 6 ▶ my ( $self, undef, $value ) = @_; # $name unused
Subroutine _set_brick: called CC=8 SCAR=21.3
1278 6 ▶ my ($horiz, $vert, $size) = ref $value ? @$value : split ',', $value;
Branch: 1 branch
BranchTrueFalseTotal
ref $value ? : 5 1 6
1279 6 ▶ defined $size
Branch: 1 branch
BranchTrueFalseTotal
defined $size and $self->_deprecation_notice("brick_third_argument") 0 6 6
1280 and $self->_deprecation_notice( 'brick_third_argument' );
1281 6 ▶ $size ||= $horiz * $vert;
Condition33%1/3
$size ||= $horiz * $vert
l!l&&r!l&&!r
060
MC/DC0%0/2
$size ||= $horiz * $vert
$size $horiz * $vert
Truth table33%1/3
A ||= B
A= $sizeB= $horiz * $vert
ABresult
1X1
011
000
1282 6 ▶ ($size % $horiz || $size % $vert) and croak <<eod;
Branch: 1 branch
BranchTrueFalseTotal
$size % $horiz || $size % $vert and croak("Error - The puzzle size $size must be a multiple of both the horizontal\n brick size $horiz and the vertical brick size $vert.\n") 0 6 6
Condition33%1/3
$size % $horiz or $size % $vert
l!l&&r!l&&!r
006
MC/DC0%0/2
$size % $horiz or $size % $vert
$size % $horiz $size % $vert
Truth table33%1/3
A or B
A= $size % $horizB= $size % $vert
ABresult
1X1
011
000
1283 Error - The puzzle size $size must be a multiple of both the horizontal
1284 brick size $horiz and the vertical brick size $vert.
1285 eod
1286 6 my $rowmul = floor ($size / $horiz);
1287 6 my $syms = '.';
1288 6 my $topo = '';
1289 6 ▶ for (my $row = 0; $row < $size; $row++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row < $size) 58 6 64
1290 58 $syms .= " @{[$row + 1]}";
1291 58 ▶ for (my $col = 0; $col < $size; $col++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($col < $size) 616 58 674
1292 616 $topo .= sprintf ' r%d,c%d,s%d', $row, $col,
1293 floor ($row / $vert) * $rowmul + floor ($col / $horiz);
1294 }
1295 }
1296 6 substr ($topo, 0, 1, '');
1297 6 $self->set (columns => $size, rows => $size, symbols => $syms,
1298 topology => $topo);
1299 6 return;
1300 }
1301  
1302 sub _set_corresponding {
1303 ## my ( $self, $name, $order ) = @_;
1304 1 ▶ my ( $self, undef, $order ) = @_; # Name unused
Subroutine _set_corresponding: called CC=2 SCAR=6.9
1305 1 my $size = $order * $order;
1306 1 $self->set (sudoku => $order);
1307 1 my $order_minus_1 = $order - 1;
1308 1 my $offset = $size * $order;
1309 1 ▶ for (my $inx = 0; $inx < $size; $inx++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($inx < $size) 9 1 10
1310 9 my $base = floor ($inx / $order) * $size + $inx % $order;
1311 $self->add_set ("u$inx", map {
1312 9 my $g = $_ * $offset + $base;
1313 27 (map {$_ * $order + $g} 0 .. $order_minus_1)} 0 .. $order_minus_1);
1314 }
1315 1 return;
1316 }
1317  
1318 2 my %cube = (
1319 full => <<eod,
1320 c0,r0,s0 c1,r0,s0 c2,r0,s0 c3,r0,s0
1321 c0,r1,s0 c1,r1,s0 c2,r1,s0 c3,r1,s0
1322 c0,r2,s0 c1,r2,s0 c2,r2,s0 c3,r2,s0
1323 c0,r3,s0 c1,r3,s0 c2,r3,s0 c3,r3,s0
1324 p0,r0,s1 p0,r1,s1 p0,r2,s1 p0,r3,s1
1325 p1,r0,s1 p1,r1,s1 p1,r2,s1 p1,r3,s1
1326 p2,r0,s1 p2,r1,s1 p2,r2,s1 p2,r3,s1
1327 p3,r0,s1 p3,r1,s1 p3,r2,s1 p3,r3,s1
1328 c0,p0,s2 c1,p0,s2 c2,p0,s2 c3,p0,s2
1329 c0,p1,s2 c1,p1,s2 c2,p1,s2 c3,p1,s2
1330 c0,p2,s2 c1,p2,s2 c2,p2,s2 c3,p2,s2
1331 c0,p3,s2 c1,p3,s2 c2,p3,s2 c3,p3,s2
1332 p0,r3,s3 p0,r2,s3 p0,r1,s3 p0,r0,s3
1333 p1,r3,s3 p1,r2,s3 p1,r1,s3 p1,r0,s3
1334 p2,r3,s3 p2,r2,s3 p2,r1,s3 p2,r0,s3
1335 p3,r3,s3 p3,r2,s3 p3,r1,s3 p3,r0,s3
1336 c0,r3,s4 c1,r3,s4 c2,r3,s4 c3,r3,s4
1337 c0,r2,s4 c1,r2,s4 c2,r2,s4 c3,r2,s4
1338 c0,r1,s4 c1,r1,s4 c2,r1,s4 c3,r1,s4
1339 c0,r0,s4 c1,r0,s4 c2,r0,s4 c3,r0,s4
1340 c0,p3,s5 c1,p3,s5 c2,p3,s5 c3,p3,s5
1341 c0,p2,s5 c1,p2,s5 c2,p2,s5 c3,p2,s5
1342 c0,p1,s5 c1,p1,s5 c2,p1,s5 c3,p1,s5
1343 c0,p0,s5 c1,p0,s5 c2,p0,s5 c3,p0,s5
1344 eod
1345 half => <<eod,
1346 r0,c0,s0 r0,c1,s0 r0,c2,s0 r0,c3,s0
1347 r1,c0,s0 r1,c1,s0 r1,c2,s0 r1,c3,s0
1348 r2,c0,s1 r2,c1,s1 r2,c2,s1 r2,c3,s1
1349 r3,c0,s1 r3,c1,s1 r3,c2,s1 r3,c3,s1
1350 p0,c0,s2 p0,c1,s2 p0,c2,s3 p0,c3,s3
1351 p1,c0,s2 p1,c1,s2 p1,c2,s3 p1,c3,s3
1352 p2,c0,s2 p2,c1,s2 p2,c2,s3 p2,c3,s3
1353 p3,c0,s2 p3,c1,s2 p3,c2,s3 p3,c3,s3
1354 p0,r3,s4 p0,r2,s4 p0,r1,s4 p0,r0,s4
1355 p1,r3,s4 p1,r2,s4 p1,r1,s4 p1,r0,s4
1356 p2,r3,s5 p2,r2,s5 p2,r1,s5 p2,r0,s5
1357 p3,r3,s5 p3,r2,s5 p3,r1,s5 p3,r0,s5
1358 eod
1359 );
1360  
1361 sub _set_cube {
1362 ## my ( $self, $name, $type ) = @_;
1363 2 ▶ my ( $self, undef, $type ) = @_; # Name unused
Subroutine _set_cube: called CC=6 SCAR=28.5
1364 2 ▶ if ($type =~ m/\D/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type =~ /\D/) { } 2 0 2
1365 2 ▶ $cube{$type} or croak <<eod;
Branch: 1 branch
BranchTrueFalseTotal
$cube{$type} or croak(qq[Error - Cube type '${type}' is not defined. Legal values are numeric (for\n Dion cube), or one of @{[join(", ", map({"'${_}'";} sort(keys %cube)))];}\n]) 0 2 2
1366 Error - Cube type '$type' is not defined. Legal values are numeric (for
1367 0 Dion cube), or one of @{[join ', ', map {"'$_'"} sort keys %cube]}
1368 eod
1369 2 $self->set (topology => $cube{$type}, columns => 4, rows => 4);
1370 } else {
1371 0 my $size = $type * $type;
1372 0 my $topo = '';
1373 0 ▶ for (my $x = 0; $x < $size; $x++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x < $size) 0 0 0
1374 0 ▶ for (my $y = 0; $y < $size; $y++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y < $size) 0 0 0
1375 0 ▶ for (my $z = 0; $z < $size; $z++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($z < $size) 0 0 0
1376 0 $topo .= join (',',
1377 _cube_set_names ($type, x => $x, $y, $z),
1378 _cube_set_names ($type, y => $y, $z, $x),
1379 _cube_set_names ($type, z => $z, $x, $y)) . ' ';
1380 }
1381 }
1382 }
1383 0 $self->set (topology => $topo, columns => $size, rows => $size);
1384 }
1385 2 $self->set (symbols => join ' ', '.', 1 .. $self->{largest_set});
1386 2 return;
1387 }
1388  
1389 sub _cube_set_names {
1390 0 ▶ my ( $order, $name, $x, $y, $z ) = @_;
Subroutine _cube_set_names: not called CC=1 SCAR=6.9
1391 0 my $tplt = sprintf '%s%d%%s%%d', $name, $x;
1392 0 return map {sprintf $tplt, @$_} [r => $y], [c => $z],
1393 [s => floor ($y / $order) * $order + floor ($z / $order)]
1394 }
1395  
1396 sub _set_latin {
1397 ## my ( $self, $name, $size ) = @_;
1398 2 ▶ my ( $self, undef, $size ) = @_; # Name unused
Subroutine _set_latin: called CC=3 SCAR=11.0
1399 2 my $syms = '.';
1400 2 my $topo = '';
1401 2 my $letter = 'A';
1402 2 ▶ for (my $row = 0; $row < $size; $row++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row < $size) 13 2 15
1403 13 $syms .= " @{[$letter++]}";
1404 13 ▶ for (my $col = 0; $col < $size; $col++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($col < $size) 97 13 110
1405 97 $topo .= sprintf ' r%d,c%d', $row, $col;
1406 }
1407 }
1408 2 substr ($topo, 0, 1, '');
1409 2 $self->set (columns => $size, rows => $size, symbols => $syms,
1410 topology => $topo);
1411 2 return;
1412 }
1413  
1414 sub _set_null {
1415 ## my ( $self, $name, $value ) = @_;
1416 0 ▶ my ( $self, undef, $value ) = @_; # Name unused
Subroutine _set_null: not called CC=5 SCAR=34.0
1417 0 ▶ my ($size, $columns, $rows) = ref $value ? @$value : split ',', $value;
Branch: 1 branch
BranchTrueFalseTotal
ref $value ? : 0 0 0
1418 0 $self->{cell} = []; # The cells themselves.
1419 0 $self->{set} = {}; # The sets themselves.
1420 0 $self->{largest_set} = 0;
1421 0 $self->{intersection} = {};
1422 0 $self->{cells_unused} = $size;
1423 0 foreach my $cell_inx (0 .. $size - 1) {
1424 0 my $cell = {membership => [], index => $cell_inx};
1425 0 push @{$self->{cell}}, $cell;
1426 }
1427 0 delete $self->{backtrack_stack}; # Force setting of new problem.
1428 0 ▶ defined $columns and $self->set (columns => $columns);
Branch: 1 branch
BranchTrueFalseTotal
defined $columns and $self->set("columns", $columns) 0 0 0
1429 0 ▶ defined $rows and $self->set (rows => $rows);
Branch: 1 branch
BranchTrueFalseTotal
defined $rows and $self->set("rows", $rows) 0 0 0
1430 0 return;
1431 }
1432  
1433 sub _set_number {
1434 23 ▶ my ( $self, $name, $value ) = @_;
Subroutine _set_number: called CC=2 SCAR=7.0
1435 23 ▶ _looks_like_number ($value) or croak <<eod;
Branch: 1 branch
BranchTrueFalseTotal
_looks_like_number($value) or croak("Error - Attribute $name must be numeric.\n") 0 23 23
1436 Error - Attribute $name must be numeric.
1437 eod
1438 23 $self->{$name} = $value;
1439 23 return;
1440 }
1441  
1442 sub _set_quincunx {
1443 ## my ( $self, $name, $value ) = @_;
1444 0 ▶ my ( $self, undef, $value ) = @_; # Name unused
Subroutine _set_quincunx: not called CC=10 SCAR=47.0
1445 0 ▶ my ($order, $gap) = ref $value ? @$value : split ',', $value;
Branch: 1 branch
BranchTrueFalseTotal
ref $value ? : 0 0 0
1446 0 ▶ $order =~ m/\D/ and croak <<eod;
Branch: 1 branch
BranchTrueFalseTotal
$order =~ /\D/ and croak("Error - The quincunx order must be an integer.\n") 0 0 0
1447 Error - The quincunx order must be an integer.
1448 eod
1449 0 ▶ if (defined $gap) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $gap) { } 0 0 0
1450 0 ▶ $gap =~ m/\D/ and croak <<eod;
Branch: 1 branch
BranchTrueFalseTotal
$gap =~ /\D/ and croak("Error - The quincunx gap must be an integer.\n") 0 0 0
1451 Error - The quincunx gap must be an integer.
1452 eod
1453 0 ▶ $gap > $order - 2 and croak <<eod;
Branch: 1 branch
BranchTrueFalseTotal
$gap > $order - 2 and croak("Error - The quincunx gap must not be greater than the order ($order) - 2.\n") 0 0 0
1454 Error - The quincunx gap must not be greater than the order ($order) - 2.
1455 eod
1456 0 ▶ $gap % 2 == $order % 2 or croak <<eod;
Branch: 1 branch
BranchTrueFalseTotal
$gap % 2 == $order % 2 or croak("Error - The gap must be the same parity (odd or even) as the order.\n") 0 0 0
1457 Error - The gap must be the same parity (odd or even) as the order.
1458 eod
1459 } else {
1460 0 $gap = $order % 2;
1461 }
1462 0 my $cols = ($order * 2 + $gap) * $order;
1463 0 $self->set(null => [$cols * $cols, $cols, $cols]);
1464 0 my $osq = $order * $order;
1465 0 $self->set(symbols => join (' ', '.', 1 .. $osq));
1466 0 my @squares = do { # Squares in terms of index of top left corner
1467 0 my $offset = ($order + $gap) * $order;
1468 0 my $inset = ($order - ($order - $gap) / 2) * $order;
1469 (
1470 0 0, # Top left square
1471 $offset, # Top right square
1472 $inset * $cols + $inset, # Middle square
1473 $offset * $cols, # Bottom left square
1474 $offset * ($cols + 1), # Bottom right square
1475 )
1476 };
1477 0 my $limit = $osq - 1;
1478 0 my @colinx = map {$_ * $cols} 0 .. $limit;
1479 0 my @sqinx = map {$_ .. $_ + $order - 1} map {$_ * $cols} 0 .. $order - 1;
1480 0 my @sqloc = map {$_ * $order} @sqinx;
1481 0 my @sqgened; # 's' sets generated, by origin cell.
1482 # Crete the row, column, and square sets. These have the same names
1483 # as those created by the corresponding 'sudoku' topology, but with
1484 # 'g0' .. 'g4' prepended, representing the five individual
1485 # 'standard' sudoku grids. For topology 'quincunx 3', the top left
1486 # cell is in sets g0c0,g0r0,g0s0, the top right in g1c8,g1r0,g1s2,
1487 # and so on. Because some of the 's' sets are duplicates, the
1488 # higher-numbered ones are supressed. In topology 'quincunx 3', set
1489 # g0s8 is the same as g2s0, so the latter is supressed.
1490 0 foreach my $grid (0 .. $#squares) {
1491 0 my $sqr = $squares[$grid];
1492 0 foreach my $inx (0 .. $limit) {
1493 0 my $offset = $inx * $cols;
1494 0 my $o1 = $offset + $sqr;
1495 0 $self->add_set("g${grid}r$inx" => $o1 .. $o1 + $limit);
1496 0 $self->add_set("g${grid}c$inx" => map {$_ + $inx + $sqr}
1497 @colinx);
1498 0 $o1 = $sqloc[$inx] + $sqr;
1499 $sqgened[$o1]++
1500 0 ▶ or $self->add_set("g${grid}s$inx" => map {$_ + $o1}
Branch: 1 branch
BranchTrueFalseTotal
$sqgened[$o1]++ or $self->add_set("g${grid}s$inx", map({$_ + $o1;} @sqinx)) 0 0 0
1501 @sqinx);
1502 }
1503 }
1504 0 return;
1505 }
1506  
1507 sub _set_status_value {
1508 21 ▶ my ( $self, $name, $value ) = @_;
Subroutine _set_status_value: called CC=4 SCAR=15.5
1509 21 ▶ _looks_like_number ($value) or croak <<eod;
Branch: 1 branch
BranchTrueFalseTotal
_looks_like_number($value) or croak("Error - Attribute $name must be numeric.\n") 0 21 21
1510 Error - Attribute $name must be numeric.
1511 eod
1512 21 ▶ ($value < 0 || $value >= @status_values) and croak <<eod;
Branch: 1 branch
BranchTrueFalseTotal
$value < 0 || $value >= @status_values and croak("Error - Attribute $name must be greater than or equal to 0 and\n less than @{[scalar @status_values];}\n") 0 21 21
Condition33%1/3
$value < 0 or $value >= @status_values
l!l&&r!l&&!r
0021
MC/DC0%0/2
$value < 0 or $value >= @status_values
$value < 0 $value >= @status_values
Truth table33%1/3
A or B
A= $value < 0B= $value >= @status_values
ABresult
1X1
011
000
1513 Error - Attribute $name must be greater than or equal to 0 and
1514 0 less than @{[scalar @status_values]}
1515 eod
1516 21 $self->{status_value} = $value;
1517 21 $self->{status_text} = $status_values[$value];
1518 21 return;
1519 }
1520  
1521 sub _set_sudoku {
1522 ## my ( $self, $name, $order ) = @_;
1523 5 ▶ my ( $self, undef, $order ) = @_; # Name unused
Subroutine _set_sudoku: called CC=1 SCAR=0.0
1524 5 $self->set( brick => [ $order, $order ] );
1525 5 return;
1526 }
1527  
1528 sub _set_sudokux {
1529 ## my ( $self, $name, $order ) = @_;
1530 1 ▶ my ( $self, undef, $order ) = @_; # Name unused
Subroutine _set_sudokux: called CC=1 SCAR=0.0
1531 1 $self->set (sudoku => $order);
1532 1 my $size = $order * $order;
1533 1 my $size_minus_1 = $size - 1;
1534 1 my $size_plus_1 = $size + 1;
1535 1 $self->add_set (d0 => map {$_ * $size_plus_1} 0 .. $size_minus_1);
1536 1 $self->add_set (d1 => map {$_ * $size_minus_1} 1 .. $size);
1537 1 return;
1538 }
1539  
1540 sub _set_symbols {
1541 ## my ( $self, $name, $value ) = @_;
1542 13 ▶ my ( $self, undef, $value ) = @_; # Name unused
Subroutine _set_symbols: called CC=5 SCAR=16.2
1543 13 my @lst = split '\s+', $value;
1544 13 my %hsh;
1545 13 my $inx = 0;
1546 13 my $maxlen = 0;
1547 13 foreach (@lst) {
1548 142 ▶ defined $_ or next;
Branch: 1 branch
BranchTrueFalseTotal
defined $_ or next 0 142 142
1549 142 ▶ m/,/ and croak <<eod;
Branch: 1 branch
BranchTrueFalseTotal
/,/ and croak("Error - Symbols may not contain commas.\n") 0 142 142
1550 Error - Symbols may not contain commas.
1551 eod
1552 142 ▶ exists $hsh{$_} and croak <<eod;
Branch: 1 branch
BranchTrueFalseTotal
exists $hsh{$_} and croak("Error - Symbol '${_}' specified more than once.\n") 0 142 142
1553 Error - Symbol '$_' specified more than once.
1554 eod
1555 142 $hsh{$_} = $inx++;
1556 142 $maxlen = max ($maxlen, length ($_));
1557 }
1558 13 $self->{symbol_list} = \@lst;
1559 13 $self->{symbol_hash} = \%hsh;
1560 13 $self->{symbol_number} = scalar @lst;
1561 13 $self->{biggest_spec} = $self->{biggest_symbol} = $maxlen;
1562 13 $self->{allowed_symbols} = {};
1563 13 return;
1564 }
1565  
1566 sub _set_topology {
1567 ## my ( $self, $name, @args ) = @_;
1568 11 ▶ my ( $self, undef, @args ) = @_; # Name unused
Subroutine _set_topology: called CC=7 SCAR=19.5
1569 11 $self->{cell} = []; # The cells themselves.
1570 11 $self->{set} = {}; # The sets themselves.
1571 11 $self->{largest_set} = 0;
1572 11 $self->{intersection} = {};
1573 11 $self->{cells_unused} = 0;
1574 11 my $cell_inx = 0;
1575 11 foreach my $cell_def (map {split '\s+', $_} @args) {
1576 938 my $cell = {membership => [], index => $cell_inx};
1577 938 push @{$self->{cell}}, $cell;
1578 938 foreach my $name (sort grep {$_ ne ''} split ',', $cell_def) {
1579 2717 foreach my $other (@{$cell->{membership}}) {
1580 2620 my $int = "$other,$name";
1581 2620 ▶ $self->{intersection}{$int} ||= [];
Condition100%2/2
$self->{"intersection"}{$int} ||= []
l!l
11991421
MC/DC100%1/1
$self->{"intersection"}{$int} ||= []
$self->{"intersection"}{$int}
Truth table100%2/2
A
A= $self->{"intersection"}{$int}
Aresult
11
00
1582 2620 push @{$self->{intersection}{$int}}, $cell_inx;
1583 }
1584 2717 push @{$cell->{membership}}, $name;
1585 2717 ▶ my $set = $self->{set}{$name} ||=
Condition100%2/2
$self->{"set"}{$name} ||= {"name", $name, "membership", []}
l!l
2454263
MC/DC100%1/1
$self->{"set"}{$name} ||= {"name", $name, "membership", []}
$self->{"set"}{$name}
Truth table100%2/2
A
A= $self->{"set"}{$name}
Aresult
11
00
1586 {name => $name, membership => []};
1587 2717 push @{$set->{membership}}, $cell_inx;
1588 $self->{largest_set} = max ($self->{largest_set},
1589 2717 scalar @{$set->{membership}});
1590 }
1591 938 ▶ @{$cell->{membership}} or $self->{cells_unused}++;
Branch: 1 branch
BranchTrueFalseTotal
@{$cell->{"membership"};} or ++$self->{"cells_unused"} 0 938 938
1592 938 $cell_inx++;
1593 }
1594 11 delete $self->{backtrack_stack}; # Force setting of new problem.
1595 11 return;
1596 }
1597  
1598 1 ▶sub _set_value {$_[0]->{$_[1]} = $_[2]; return;}
Subroutine _set_value: called CC=1 SCAR=0.0
1599  
1600 =head2 solution
1601  
1602 $string = $su->solution();
1603  
1604 This method returns the next solution to the problem, or undef if there
1605 are no further solutions. The solution is a blank-delimited list of the
1606 symbols each cell contains, with line breaks as specified by the
1607 'columns' attribute. If the problem() method has not been called,
1608 an exception is thrown.
1609  
1610 Status values set:
1611  
1612 SUDOKU_SUCCESS
1613 SUDOKU_NO_SOLUTION
1614 SUDOKU_TOO_HARD
1615  
1616 =cut
1617  
1618 sub solution {
1619 19 ▶ my ( $self ) = @_;
Subroutine solution: called CC=3 SCAR=12.7
Pod documented
1620  
1621 19 ▶ $self->{backtrack_stack} or croak <<eod;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"backtrack_stack"} or croak("Error - You cannot call the solution() method unless you have specified\n the problem via the problem() method.\n") 0 19 19
1622 Error - You cannot call the solution() method unless you have specified
1623 the problem via the problem() method.
1624 eod
1625  
1626 19 ▶ $self->{debug} and print <<eod;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"debug"} and print qq[Debug solution - entering method. Stack depth = @{[scalar @{$self->{"backtrack_stack"};}];}\n] 0 19 19
1627 Debug solution - entering method. Stack depth = @{[
1628 0 scalar @{$self->{backtrack_stack}}]}
1629 eod
1630  
1631 19 return $self->_constrain ();
1632 }
1633  
1634 =head2 steps
1635  
1636 $string = $su->steps();
1637  
1638 =for comment help syntax-highlighting editor "
1639  
1640 This method returns the steps taken to solve the problem. If no
1641 solution was found, it returns the steps taken to determine this. If
1642 called in list context, you get an actual copy of the list. The first
1643 element is the name of the constraint applied:
1644  
1645 F = forced: only one value works in this cell;
1646 N = numeration or necessary: this is the only cell
1647 that can supply the given value;
1648 B = box claim: if a candidate number appears in only
1649 one row or column of a given box, it can be
1650 eliminated as a candidate in that row or column
1651 but outside that box;
1652 T = tuple, which is a generalization of the concept
1653 pair, triple, and so on. These come in two
1654 varieties for a given size of the tuple N:
1655 naked: N cells contain among them N values, so
1656 no cells outside the tuple can supply those
1657 values.
1658 hidden: N cells contain N values which do not
1659 occur outside those cells, so any other values
1660 in the tuple are supressed.
1661 ? = no constraint: generated in backtrack mode.
1662  
1663 See C<http://www.research.att.com/~gsf/sudoku/> and
1664 L<https://www.angusj.com/sudoku/hints.php> for fuller
1665 definitions of the constraints and how they are applied. The
1666 L<ACKNOWLEDGMENTS|/ACKNOWLEDGMENTS> section addresses why the former
1667 URL is not an actual POD link.
1668  
1669 The second value is the cell number, as defined by the topology
1670 setting. For the 'sudoku' and 'latin' settings, the cells are
1671 numbered from zero, row-by-row. If you did your own topology, the
1672 first cell you defined is 0, the second is 1, and so on.
1673  
1674 The third value is the value assigned to the cell. If returned in
1675 list context, it is the number assigned to the cell's symbol. If
1676 in scalar context, it is the symbol itself.
1677  
1678 =for comment help syntax-highlighting editor "
1679  
1680 =cut
1681  
1682 sub steps {
1683 1 ▶ my ( $self ) = @_;
Subroutine steps: called CC=3 SCAR=12.5
Pod documented
1684 0 ▶ return wantarray ? (@{$self->{backtrack_stack}}) :
Branch: 1 branch
BranchTrueFalseTotal
defined wantarray ? : 1 0 1
1685 defined wantarray ?
1686 1 ▶ $self->_format_constraint (@{$self->{backtrack_stack}}) :
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 1 1
1687 undef;
1688 }
1689  
1690 =head2 unload
1691  
1692 $string = $su->unload();
1693  
1694 This method returns either the current puzzle or the current solution,
1695 depending on whether the solution() method has been called since the
1696 puzzle was loaded.
1697  
1698 =cut
1699  
1700 sub unload {
1701 2 ▶ my ( $self ) = @_;
Subroutine unload: called CC=1 SCAR=0.0
Pod documented
1702 2 return $self->_unload ()
1703 }
1704  
1705 ########################################################################
1706  
1707 # Private methods and subroutines.
1708  
1709 # $status_value = $su->_constrain ();
1710  
1711 # This method applies all possible constraints to the current
1712 # problem, placing them on the backtrack stack. The backtrack
1713 # algorithm needs to remove these when backtracking. The return
1714 # is false if we ran out of constraints, or true if we found
1715 # a constraint that could not be satisfied.
1716  
1717 2 my %constraint_method = (
1718 '?' => '_constraint_backtrack',
1719 );
1720  
1721 sub _constrain {
1722 19 ▶ my ( $self ) = @_;
Subroutine _constrain: called CC=17 SCAR=33.0
1723 19 ▶ my $stack = $self->{backtrack_stack} ||= []; # May hit this
Condition50%1/2
$self->{"backtrack_stack"} ||= []
l!l
190
MC/DC0%0/1
$self->{"backtrack_stack"} ||= []
$self->{"backtrack_stack"}
Truth table50%1/2
A
A= $self->{"backtrack_stack"}
Aresult
11
00
1724 # when initializing.
1725 19 ▶ my $used = $self->{constraints_used} ||= {};
Condition50%1/2
$self->{"constraints_used"} ||= {}
l!l
190
MC/DC0%0/1
$self->{"constraints_used"} ||= {}
$self->{"constraints_used"}
Truth table50%1/2
A
A= $self->{"constraints_used"}
Aresult
11
00
1726 19 my $iterations;
1727 $iterations = $self->{iteration_limit}
1728 19 ▶ if $self->{iteration_limit} > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"iteration_limit"} > 0 0 19 19
1729  
1730 $self->{no_more_solutions} and
1731 19 ▶ return $self->_unload (undef, SUDOKU_NO_SOLUTION);
Branch: 1 branch
BranchTrueFalseTotal
$self->{"no_more_solutions"} and return $self->_unload(undef, 1) 0 19 19
1732  
1733 19 ▶ @{$self->{backtrack_stack}} and do {
Branch: 1 branch
BranchTrueFalseTotal
@{$self->{"backtrack_stack"};} and do { $self->_constraint_remove and return $self->_unload(undef, 1) } 1 18 19
1734 1 ▶ $self->_constraint_remove and
Branch: 1 branch
BranchTrueFalseTotal
$self->_constraint_remove and return $self->_unload(undef, 1) 1 0 1
1735 return $self->_unload (undef, SUDOKU_NO_SOLUTION);
1736 };
1737  
1738 18 ▶ $self->{cells_unassigned} or do {
Branch: 1 branch
BranchTrueFalseTotal
$self->{"cells_unassigned"} or do { $self->{"no_more_solutions"} = 1; return $self->_unload("", 0) } 0 18 18
1739 0 $self->{no_more_solutions} = 1;
1740 0 return $self->_unload ('', SUDOKU_SUCCESS);
1741 };
1742  
1743 18 my $number_of_cells = @{$self->{cell}};
1744  
1745 constraint_loop:
1746 { # Begin outer constraint loop.
1747  
1748 18 foreach my $constraint (qw{F N B T ?}) {
1749 677 ▶ confess <<eod if @{$self->{cell}} != $number_of_cells;
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->{"cell"};} != $number_of_cells 0 677 677
1750 Programming error - Before trying $constraint constraint.
1751 We started with $number_of_cells cells, but now have @{[
1752 0 scalar @{$self->{cell}}]}.
1753 eod
1754 677 ▶ my $method = $constraint_method{$constraint} ||
Condition50%1/2
$constraint_method{$constraint} || "_constraint_$constraint"
l!l
0677
MC/DC0%0/1
$constraint_method{$constraint} || "_constraint_$constraint"
$constraint_method{$constraint}
Truth table50%1/2
A
A= $constraint_method{$constraint}
Aresult
11
00
1755 "_constraint_$constraint";
1756 677 ▶ my $rslt = $self->$method () or next;
Branch: 1 branch
BranchTrueFalseTotal
my $rslt = $self->$method or next 0 677 677
1757 677 ▶ @$rslt or next;
Branch: 1 branch
BranchTrueFalseTotal
@$rslt or next 328 349 677
1758 349 foreach my $constr (@$rslt) {
1759 1156 ▶ if (ref $constr) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $constr) { } 1156 0 1156
1760 1156 push @$stack, $constr;
1761 1156 $used->{$constr->[0]}++
1762 } else {
1763 0 ▶ my $rslt = $self->_constraint_remove or
Branch: 1 branch
BranchTrueFalseTotal
my $rslt = $self->_constraint_remove or redo constraint_loop 0 0 0
1764 redo constraint_loop;
1765 0 return $self->_unload ('', $rslt);
1766 }
1767 }
1768 $self->{cells_unassigned} or
1769 349 ▶ return $self->_unload ('', SUDOKU_SUCCESS);
Branch: 1 branch
BranchTrueFalseTotal
$self->{"cells_unassigned"} or return $self->_unload("", 0) 18 331 349
1770 331 redo constraint_loop;
1771 }
1772  
1773 } # end outer constraint loop.
1774  
1775 0 $self->set (status_value => SUDOKU_TOO_HARD);
1776 0 return;
1777 }
1778  
1779 # Constraint executors:
1780 # These all return a reference to the constraints to be stacked,
1781 # provided progress was made. Otherwise they return 0. At the
1782 # point a contradiction is found, they push 'backtrack' on the
1783 # end of the list to be returned, and return immediately.
1784  
1785 # F constraint - only one value possible. Unlike the other
1786 # constraints, we keep iterating this one until we make no
1787 # progress.
1788  
1789 sub _constraint_F {
1790 349 ▶ my ( $self ) = @_;
Subroutine _constraint_F: called CC=16 SCAR=29.7
1791 349 my @stack;
1792 349 my $done = 1;
1793  
1794 349 ▶ while ($done) {
Branch: 1 branch
BranchTrueFalseTotal
if ($done) 549 349 898
1795 549 $done = 0;
1796 549 my $inx = 0; # Cell index.
1797 549 foreach my $cell (@{$self->{cell}}) {
1798 65177 ▶ next if $cell->{content}; # Skip already-assigned cells.
Branch: 1 branch
BranchTrueFalseTotal
if $cell->{"content"} 34448 30729 65177
1799 30729 ▶ next unless @{$cell->{membership}}; # Skip unused cells.
Branch: 1 branch
BranchTrueFalseTotal
unless @{$cell->{"membership"};} 0 30729 30729
1800 30729 my $pos = 0;
1801 30729 ▶ foreach (values %{$cell->{possible}}) {$_ or $pos++};
Branch: 1 branch
BranchTrueFalseTotal
$_ or ++$pos 110571 278720 389291
1802 30729 ▶ if ($pos > 1) { # > 1 possibility. Can't apply.
Branch: 2 branches
BranchTrueFalseTotal
if ($pos > 1) { } 29856 873 30729
elsif ($pos == 1) { } 873 0 873
1803 } elsif ($pos == 1) { # Exactly 1 possibility. Apply.
1804 873 my $val;
1805 873 foreach (keys %{$cell->{possible}}) {
1806 4758 ▶ next if $cell->{possible}{$_};
Branch: 1 branch
BranchTrueFalseTotal
if $cell->{"possible"}{$_} 3885 873 4758
1807 873 $val = $_;
1808 873 last;
1809 }
1810 873 ▶ $self->_try ($cell, $val) and confess <<eod;
Branch: 1 branch
BranchTrueFalseTotal
$self->_try($cell, $val) and confess("Programming error - Passed 'F' constraint but _try failed.\n") 0 873 873
1811 Programming error - Passed 'F' constraint but _try failed.
1812 eod
1813 873 my $constraint = [F => [$inx, $val]];
1814 $self->{debug} and
1815 873 ▶ print '# ', $self->_format_constraint ($constraint);
Branch: 1 branch
BranchTrueFalseTotal
$self->{"debug"} and print "# ", $self->_format_constraint($constraint) 0 873 873
1816 873 $done++;
1817 873 push @stack, $constraint;
1818 873 ▶ $self->{cells_unassigned} or do {$done = 0; last};
Branch: 1 branch
BranchTrueFalseTotal
$self->{"cells_unassigned"} or do { $done = 0; last } 18 855 873
1819 } else { # No possibilities. Backtrack.
1820 0 ▶ $self->{debug} and print <<eod;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"debug"} and print "Debug - Cell $inx has no possible values. Backtracking.\n" 0 0 0
1821 Debug - Cell $inx has no possible values. Backtracking.
1822 eod
1823 0 ▶ $self->{debug} > 1 and do {
Branch: 1 branch
BranchTrueFalseTotal
$self->{"debug"} > 1 and do { local $Data::Dumper::Terse = 1; print Dumper($cell) } 0 0 0
1824 0 local $Data::Dumper::Terse = 1;
1825 0 print Dumper $cell;
1826 };
1827 0 push @stack, 'backtrack';
1828 0 $done = 0;
1829 0 last;
1830 }
1831 } continue {
1832 65159 $inx++;
1833 }
1834 }
1835 349 return \@stack;
1836 }
1837  
1838 # N constraint - the only cell which supplies a necessary value.
1839  
1840 sub _constraint_N {
1841 283 ▶ my ( $self ) = @_;
Subroutine _constraint_N: called CC=13 SCAR=25.9
1842 283 ▶ while (my ($name, $set) = each %{$self->{set}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$self{"set"};}) 3860 37 3897
1843 3860 my @suppliers;
1844 3860 foreach my $inx (@{$set->{membership}}) {
1845 49821 my $cell = $self->{cell}[$inx];
1846 49821 ▶ next if $cell->{content};
Branch: 1 branch
BranchTrueFalseTotal
if $cell->{"content"} 23686 26135 49821
1847 # No need to check @{$cell->{membership}}, since the cell is
1848 # known to be a member of set $name.
1849 26135 ▶ while (my ($val, $count) = each %{$cell->{possible}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$cell{"possible"};}) 358176 26135 384311
1850 358176 ▶ next if $count;
Branch: 1 branch
BranchTrueFalseTotal
if $count 253500 104676 358176
1851 104676 ▶ $suppliers[$val] ||= [];
Condition100%2/2
$suppliers[$val] ||= []
l!l
7854126135
MC/DC100%1/1
$suppliers[$val] ||= []
$suppliers[$val]
Truth table100%2/2
A
A= $suppliers[$val]
Aresult
11
00
1852 104676 push @{$suppliers[$val]}, $inx;
1853 }
1854 }
1855 3860 my $limit = @suppliers;
1856 3860 ▶ for (my $val = 1; $val < $limit; $val++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val < $limit) 42950 3614 46564
1857 42950 ▶ next unless $suppliers[$val] && @{$suppliers[$val]} == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless $suppliers[$val] and @{$suppliers[$val];} == 1 42704 246 42950
Condition100%3/3
$suppliers[$val] and @{$suppliers[$val];} == 1
!ll&&!rl&&r
1745925245246
MC/DC100%2/2
$suppliers[$val] and @{$suppliers[$val];} == 1
$suppliers[$val] @{$suppliers[$val];} == 1
Truth table100%3/3
A and B
A= $suppliers[$val]B= @{$suppliers[$val];} == 1
ABresult
0X0
100
111
1858 246 my $inx = $suppliers[$val][0];
1859 246 ▶ $self->_try ($inx, $val) and confess <<eod, $self->{debug} ? <<eod : ();
Branch: 2 branches
BranchTrueFalseTotal
$self->_try($inx, $val) and confess(qq[Programming error - Cell $inx passed 'N' constraint but try of\n $self->{"symbol_list"}[$val] failed.\n], $self->{"debug"} ? qq[@{[$self->_unload];} set: $name\n cell: @{[Dumper($self->{"cell"}[$inx])];}\n] : ()) 0 246 246
$self->{'debug'} ? : 0 0 0
1860 Programming error - Cell $inx passed 'N' constraint but try of
1861 $self->{symbol_list}[$val] failed.
1862 eod
1863 0 @{[$self->_unload
1864 ]} set: $name
1865 0 cell: @{[Dumper ($self->{cell}[$inx])]}
1866 eod
1867 246 my $constraint = [N => [$inx, $val]];
1868 $self->{debug} and
1869 246 ▶ print '# ', $self->_format_constraint ($constraint);
Branch: 1 branch
BranchTrueFalseTotal
$self->{"debug"} and print "# ", $self->_format_constraint($constraint) 0 246 246
1870 246 keys %{$self->{set}}; # Reset iterator.
1871 246 return [$constraint];
1872 }
1873 }
1874 37 return [];
1875 }
1876  
1877 # B constraint - "box claim". Given two sets whose intersection
1878 # contains more than one cell, if all cells which can contribute
1879 # a given value to one set are in the intersection, no cell in
1880 # the second set can contribute that value. Note that this
1881 # constraint does NOT actually assign a value to a cell, it just
1882 # eliminates possible values. The name is because on the
1883 # "standard" sudoku layout one of the sets is always a box; the
1884 # other can be a row or a column.
1885  
1886 sub _constraint_B {
1887 37 ▶ my ( $self ) = @_;
Subroutine _constraint_B: called CC=23 SCAR=31.4
1888 37 my $done = 0;
1889 37 ▶ while (my ($int, $cells) = each %{$self->{intersection}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$self{"intersection"};}) 6038 8 6046
1890 6038 ▶ next unless @$cells > 1;
Branch: 1 branch
BranchTrueFalseTotal
unless @$cells > 1 3917 2121 6038
1891 2121 my @int_supplies; # Values supplied by the intersection
1892 my %int_cells; # Cells in the intersection
1893 2121 foreach my $inx (@$cells) {
1894 7988 ▶ next if $self->{cell}[$inx]{content};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"cell"}[$inx]{"content"} 3879 4109 7988
1895 # No need to check @{$cell->{membership}}, since the cell is
1896 # known to be a member of at least two sets.
1897 4109 $int_cells{$inx} = 1;
1898 4109 ▶ while (my ($val, $imposs) = each %{
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$self{"cell"}[$inx]{"possible"};}) 61067 4109 65176
1899 65176 $self->{cell}[$inx]{possible}}) {
1900 61067 ▶ $int_supplies[$val] = 1 unless $imposs;
Branch: 1 branch
BranchTrueFalseTotal
unless $imposs 15950 45117 61067
1901 }
1902 }
1903 2121 my %ext_supplies; # Intersection values also supplied outside.
1904 my %ext_cells; # Cells not in the intersection.
1905 2121 my @set_names = split ',', $int;
1906 2121 foreach my $set (@set_names) {
1907 4242 $ext_supplies{$set} = [];
1908 4242 $ext_cells{$set} = [];
1909 4242 foreach my $inx (@{$self->{set}{$set}{membership}}) {
1910 60930 ▶ next if $int_cells{$inx}; # Skip cells in intersection.
Branch: 1 branch
BranchTrueFalseTotal
if $int_cells{$inx} 8218 52712 60930
1911 52712 ▶ next if $self->{cell}[$inx]{content};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"cell"}[$inx]{"content"} 29447 23265 52712
1912 23265 push @{$ext_cells{$set}}, $inx;
1913 23265 ▶ while (my ($val, $imposs) = each %{
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$self{"cell"}[$inx]{"possible"};}) 353632 23265 376897
1914 376897 $self->{cell}[$inx]{possible}}) {
1915 353632 ▶ $ext_supplies{$set}[$val] = 1
Branch: 1 branch
BranchTrueFalseTotal
if not $imposs and $int_supplies[$val] 46825 306807 353632
Condition100%3/3
not $imposs and $int_supplies[$val]
!ll&&!rl&&r
2607464606146825
MC/DC100%2/2
not $imposs and $int_supplies[$val]
not $imposs $int_supplies[$val]
Truth table100%3/3
not(A) and B
A= not $impossB= $int_supplies[$val]
ABresult
0X0
100
111
1916 if !$imposs && $int_supplies[$val];
1917 }
1918 }
1919 }
1920 2121 ▶ for (my $val = 1; $val < @int_supplies; $val++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val < @int_supplies) 22767 2092 24859
1921 22767 ▶ next unless $int_supplies[$val];
Branch: 1 branch
BranchTrueFalseTotal
unless $int_supplies[$val] 13526 9241 22767
1922 9241 my @occurs_in = grep {$ext_supplies{$_}[$val]} @set_names;
1923 9241 ▶ next unless @occurs_in && @occurs_in < @set_names;
Branch: 1 branch
BranchTrueFalseTotal
unless @occurs_in and @occurs_in < @set_names 9212 29 9241
Condition100%3/3
@occurs_in and @occurs_in < @set_names
!ll&&!rl&&r
496871629
MC/DC100%2/2
@occurs_in and @occurs_in < @set_names
@occurs_in @occurs_in < @set_names
Truth table100%3/3
A and B
A= @occurs_inB= @occurs_in < @set_names
ABresult
0X0
100
111
1924 29 my %cells_claimed;
1925 29 foreach my $set (@occurs_in) {
1926 29 foreach my $inx (@{$ext_cells{$set}}) {
1927 152 ▶ next if $self->{cell}[$inx]{possible}{$val};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"cell"}[$inx]{"possible"}{$val} 78 74 152
1928 74 $cells_claimed{$inx} = 1;
1929 74 $self->{cell}[$inx]{possible}{$val} = 1;
1930 74 $done++;
1931 }
1932 }
1933 29 ▶ next unless $done;
Branch: 1 branch
BranchTrueFalseTotal
unless $done 0 29 29
1934 29 my $constraint = [B => [[sort keys %cells_claimed], $val]];
1935 $self->{debug} and
1936 29 ▶ print '# ', $self->_format_constraint ($constraint);
Branch: 1 branch
BranchTrueFalseTotal
$self->{"debug"} and print "# ", $self->_format_constraint($constraint) 0 29 29
1937 29 keys %{$self->{intersection}}; # Reset iterator.
1938 29 return [$constraint];
1939 }
1940 }
1941 8 return []
1942 }
1943  
1944 # T constraint - "tuple" (double, triple, quad). These come in
1945 # two flavors, "naked" and "hidden". Considering only pairs for
1946 # the moment:
1947 # A "naked pair" is two cells in the same set which contain the same
1948 # pair of possibilities, and only those possibilities. These
1949 # possibilities are then excluded from other cells in the set.
1950 # A "hidden pair" is when there is a pair of values which can only
1951 # be contributed to the set by one or the other of a pair of
1952 # cells. These cells then must supply these values, and any other
1953 # values supplied by cells in the pair can be eliminated.
1954 # For higher groups (triples, quads ...) the rules generalize, except
1955 # that all of the candidate values need not be present in all of
1956 # the cells under consideration; it is only necessary that none
1957 # of the candidate values appears outside the cells under
1958 # consideration.
1959 #
1960 # Glenn Fowler of AT&T (http://www.research.att.com/~gsf/sudoku/)
1961 # lumps all these together. But he refers to Angus Johnson
1962 # (http://www.angusj.com/sudoku/hints.php) for the details, and
1963 # Angus separates naked and hidden tuples.
1964  
1965 sub _constraint_T {
1966 8 ▶ my ( $self ) = @_;
Subroutine _constraint_T: called CC=44 SCAR=37.9
1967 8 my @tuple; # Tuple indices
1968 my %vacant; # Empty cells by set. $vacant{$set} = [$cell ...]
1969 8 my %contributors; # Number of cells which can contrib value, by set.
1970 8 my $syms = @{$self->{symbol_list}};
1971  
1972 8 ▶ while (my ($name, $set) = each %{$self->{set}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$self{"set"};}) 302 8 310
1973 4062 my @open = grep {!$_->{content}}
1974 302 ▶ map {$self->{cell}[$_]} @{$set->{membership}}
Branch: 1 branch
BranchTrueFalseTotal
my(@open) = grep({not $_->{"content"};} map({$self->{"cell"}[$_];} @{$$set{"membership"};})) or next 24 278 302
1975 or next;
1976 # No need to check @{$_->{membership}} in the grep, since cell
1977 # $_ is known to be a member of set $name.
1978 278 foreach my $cell (@open) {
1979 1869 ▶ for (my $val = 1; $val < $syms; $val++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val < $syms) 27111 1869 28980
1980 27111 ▶ $cell->{possible}{$val} and next;
Branch: 1 branch
BranchTrueFalseTotal
$cell->{"possible"}{$val} and next 20422 6689 27111
1981 6689 ▶ $contributors{$name} ||= [];
Condition100%2/2
$contributors{$name} ||= []
l!l
6411278
MC/DC100%1/1
$contributors{$name} ||= []
$contributors{$name}
Truth table100%2/2
A
A= $contributors{$name}
Aresult
11
00
1982 6689 $contributors{$name}[$val]++;
1983 }
1984 }
1985 278 ▶ @{$contributors{$name}} = map {$_ || 0} @{$contributors{$name}};
Branch: 1 branch
BranchTrueFalseTotal
$_ or 0 1854 1869 3723
1986 278 $vacant{$name} = \@open;
1987 278 ▶ $tuple[scalar @open] ||= [map {[$_]} 0 .. $#open];
Condition100%2/2
$tuple[scalar @open] ||= [map({[$_];} 0 .. $#open)]
l!l
21563
MC/DC100%1/1
$tuple[scalar @open] ||= [map({[$_];} 0 .. $#open)]
$tuple[scalar @open]
Truth table100%2/2
A
A= $tuple[scalar @open]
Aresult
11
00
1988 }
1989  
1990 8 ▶ for (my $order = 2; $order <= $self->{max_tuple}; $order++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($order <= $self->{"max_tuple"}) 8 0 8
1991 8 ▶ for (my $inx = 1; $inx < @tuple; $inx++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($inx < @tuple) 78 8 86
1992 78 ▶ next unless $tuple[$inx];
Branch: 1 branch
BranchTrueFalseTotal
unless $tuple[$inx] 15 63 78
1993 63 my $max = $inx - 1;
1994 361 $tuple[$inx] = [map {my @tpl = @$_;
1995 361 map {[@tpl, $_]} $tpl[-1] + 1 .. $max}
1996 63 grep {$_->[-1] < $max} @{$tuple[$inx]}];
1997 63 ▶ $tuple[$inx] = undef unless @{$tuple[$inx]};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$tuple[$inx];} 0 63 63
1998 }
1999  
2000 # Okay, I have generated the blasted tuples. Now I need to take
2001 # the union of all values provided by the tuple of cells. If the
2002 # number of values in this union is equal to the current order, I
2003 # have potentially found a naked tuple, and if this lets me
2004 # eliminate any values outside the tuple I can apply the
2005 # constraint. If the number of values inside the union is greater
2006 # than the current order, I need to consider whether any tuple of
2007 # supplied values is not represented outside the cell tuple; if
2008 # so, I have a hidden tuple and can eliminate the superfluous
2009 # values.
2010  
2011 8 foreach my $name (keys %vacant) {
2012 87 my $open = $vacant{$name};
2013 87 ▶ next unless $tuple[@$open];
Branch: 1 branch
BranchTrueFalseTotal
unless $tuple[@$open] 0 87 87
2014 87 my $contributed = $contributors{$name};
2015 87 foreach my $tuple (@{$tuple[@$open]}) {
2016 1695 my @tcontr; # number of times each value
2017 # contributed by the tuple.
2018 1695 foreach my $inx (@$tuple) {
2019 3390 my $cell = $open->[$inx];
2020 3390 ▶ for (my $val = 1; $val < $syms; $val++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val < $syms) 51118 3390 54508
2021 51118 ▶ next if $cell->{possible}{$val};
Branch: 1 branch
BranchTrueFalseTotal
if $cell->{"possible"}{$val} 38541 12577 51118
2022 12577 $tcontr[$val]++;
2023 }
2024 }
2025 1695 ▶ @tcontr = map {$_ || 0} @tcontr;
Branch: 1 branch
BranchTrueFalseTotal
$_ or 0 14702 10017 24719
2026  
2027 # At this point, @tcontr contains how many cells in the tuple
2028 # contribute each value. Calculate the number of discrete values
2029 # the tuple can contribute.
2030  
2031 # If the number of discrete values contributed by the tuple is
2032 # equal to the current order, we have a naked tuple. We have an
2033 # "effective" naked tuple if at least one of the values
2034 # contributed by the tuple occurs outside the tuple. We can
2035 # determine this by subtracting the values in @tcontr from the
2036 # corresponding values in @$contributed; if we get a positive
2037 # result for any cell, we have an "effective" naked tuple.
2038  
2039 1695 my $discrete = grep {$_} @tcontr;
2040 1695 my $constraint;
2041 my @tuple_member;
2042 1695 ▶ if ($discrete == $order) {
Branch: 2 branches
BranchTrueFalseTotal
if ($discrete == $order) { } 29 1666 1695
elsif ($discrete > $order) { } 1666 0 1666
2043 29 ▶ for (my $val = 1; $val < @tcontr; $val++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val < @tcontr) 317 29 346
2044 317 ▶ next unless $tcontr[$val] &&
Branch: 1 branch
BranchTrueFalseTotal
unless $tcontr[$val] and $contributed->[$val] > $tcontr[$val] 308 9 317
Condition100%3/3
$tcontr[$val] and $contributed->[$val] > $tcontr[$val]
!ll&&!rl&&r
259499
MC/DC100%2/2
$tcontr[$val] and $contributed->[$val] > $tcontr[$val]
$tcontr[$val] $contributed->[$val] > $tcontr[$val]
Truth table100%3/3
A and B
A= $tcontr[$val]B= $contributed->[$val] > $tcontr[$val]
ABresult
0X0
100
111
2045 $contributed->[$val] > $tcontr[$val];
2046  
2047 # At this point we know we have an "effective" naked tuple.
2048  
2049 9 ▶ $constraint ||= ['T', 'naked', $order];
Condition100%2/2
$constraint ||= ["T", "naked", $order]
l!l
45
MC/DC100%1/1
$constraint ||= ["T", "naked", $order]
$constraint
Truth table100%2/2
A
A= $constraint
Aresult
11
00
2050 9 ▶ @tuple_member or map {$tuple_member[$_] = 1} @$tuple;
Branch: 1 branch
BranchTrueFalseTotal
@tuple_member or map {$tuple_member[$_] = 1;} @$tuple 5 4 9
2051 9 my @ccl;
2052 9 ▶ for (my $inx = 0; $inx < @$open; $inx++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($inx < @$open) 51 9 60
2053 next if $tuple_member[$inx] ||
2054 51 ▶ $open->[$inx]{possible}{$val};
Branch: 1 branch
BranchTrueFalseTotal
if $tuple_member[$inx] or $open->[$inx]{"possible"}{$val} 36 15 51
Condition100%3/3
$tuple_member[$inx] or $open->[$inx]{"possible"}{$val}
l!l&&r!l&&!r
181815
MC/DC100%2/2
$tuple_member[$inx] or $open->[$inx]{"possible"}{$val}
$tuple_member[$inx] $open->[$inx]{"possible"}{$val}
Truth table100%3/3
A or B
A= $tuple_member[$inx]B= $open->[$inx]{"possible"}{$val}
ABresult
1X1
011
000
2055 15 $open->[$inx]{possible}{$val} = 1;
2056 15 --$contributed->[$val];
2057 15 push @ccl, $open->[$inx]{index};
2058 }
2059 9 ▶ push @$constraint, [\@ccl, $val] if @ccl;
Branch: 1 branch
BranchTrueFalseTotal
if @ccl 9 0 9
2060 }
2061  
2062 # If the number of discrete values is greater than the current
2063 # order, we may have a hidden tuple. The test for an "effective"
2064 # hidden tuple involves massaging @tcontr against @$contributed in
2065 # some way to find a tuple of values within the tuple of cells
2066 # which do not occur outside it.
2067  
2068 } elsif ($discrete > $order) {
2069 1666 my $within = 0; # Number of values occuring only
2070 # within tuple.
2071 1666 ▶ for (my $val = 1; $val < @tcontr; $val++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val < @tcontr) 22707 1666 24373
2072 22707 ▶ $within++ if $tcontr[$val] &&
Branch: 1 branch
BranchTrueFalseTotal
if $tcontr[$val] and $contributed->[$val] == $tcontr[$val] 109 22598 22707
Condition100%3/3
$tcontr[$val] and $contributed->[$val] == $tcontr[$val]
!ll&&!rl&&r
127489850109
MC/DC100%2/2
$tcontr[$val] and $contributed->[$val] == $tcontr[$val]
$tcontr[$val] $contributed->[$val] == $tcontr[$val]
Truth table100%3/3
A and B
A= $tcontr[$val]B= $contributed->[$val] == $tcontr[$val]
ABresult
0X0
100
111
2073 $contributed->[$val] == $tcontr[$val];
2074 }
2075 1666 ▶ next unless $within >= $order;
Branch: 1 branch
BranchTrueFalseTotal
unless $within >= $order 1663 3 1666
2076 3 $constraint = ['T', 'hidden', $order];
2077 3 map {$tuple_member[$_] = 1} @$tuple;
2078 3 ▶ for (my $val = 1; $val < @tcontr; $val++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val < @tcontr) 31 3 34
2079 31 ▶ next unless $tcontr[$val] &&
Branch: 1 branch
BranchTrueFalseTotal
unless $tcontr[$val] and $contributed->[$val] > $tcontr[$val] 23 8 31
Condition100%3/3
$tcontr[$val] and $contributed->[$val] > $tcontr[$val]
!ll&&!rl&&r
1768
MC/DC100%2/2
$tcontr[$val] and $contributed->[$val] > $tcontr[$val]
$tcontr[$val] $contributed->[$val] > $tcontr[$val]
Truth table100%3/3
A and B
A= $tcontr[$val]B= $contributed->[$val] > $tcontr[$val]
ABresult
0X0
100
111
2080 $contributed->[$val] > $tcontr[$val];
2081 8 my @ccl;
2082 8 ▶ for (my $inx = 0; $inx < @$open; $inx++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($inx < @$open) 54 8 62
2083 next unless $tuple_member[$inx]
2084 54 ▶ && !$open->[$inx]{possible}{$val}
Branch: 1 branch
BranchTrueFalseTotal
unless $tuple_member[$inx] and not $open->[$inx]{"possible"}{$val} 44 10 54
Condition100%3/3
$tuple_member[$inx] and not $open->[$inx]{"possible"}{$val}
!ll&&!rl&&r
38610
MC/DC100%2/2
$tuple_member[$inx] and not $open->[$inx]{"possible"}{$val}
$tuple_member[$inx] not $open->[$inx]{"possible"}{$val}
Truth table100%3/3
A and not(B)
A= $tuple_member[$inx]B= not $open->[$inx]{"possible"}{$val}
ABresult
0X0
100
111
2085 ;
2086 10 $open->[$inx]{possible}{$val} = 1;
2087 10 --$contributed->[$val];
2088 10 --$tcontr[$val];
2089 10 push @ccl, $open->[$inx]{index};
2090 }
2091  
2092 8 ▶ push @$constraint, [\@ccl, $val] if @ccl;
Branch: 1 branch
BranchTrueFalseTotal
if @ccl 8 0 8
2093 }
2094 }
2095  
2096 32 ▶ next unless $constraint;
Branch: 1 branch
BranchTrueFalseTotal
unless $constraint 24 8 32
2097 $self->{debug} and
2098 8 ▶ print '# ', $self->_format_constraint ($constraint);
Branch: 1 branch
BranchTrueFalseTotal
$self->{"debug"} and print "# ", $self->_format_constraint($constraint) 0 8 8
2099 8 return [$constraint];
2100 } # Next tuple
2101 } # Next set containing vacant cells
2102 } # Next order
2103  
2104 0 return [];
2105 }
2106  
2107 # ? constraint - initiate backtracking.
2108  
2109 sub _constraint_backtrack {
2110 0 ▶ my ( $self ) = @_;
Subroutine _constraint_backtrack: not called CC=12 SCAR=50.5
2111 ## --$iterations >= 0 or return $self->_unload ('', SUDOKU_TOO_HARD)
2112 ## if defined $iterations;
2113 0 my @try;
2114 0 my $syms = @{$self->{symbol_list}};
2115 0 foreach my $cell (@{$self->{cell}}) {
2116 0 ▶ next if $cell->{content};
Branch: 1 branch
BranchTrueFalseTotal
if $cell->{"content"} 0 0 0
2117 0 ▶ next unless @{$cell->{membership}};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$cell->{"membership"};} 0 0 0
2118 0 my $possible = 0;
2119 0 ▶ for (my $val = 1; $val < $syms; $val++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val < $syms) 0 0 0
2120 0 ▶ $possible++ unless $cell->{possible}{$val};
Branch: 1 branch
BranchTrueFalseTotal
unless $cell->{"possible"}{$val} 0 0 0
2121 }
2122 0 ▶ $possible or return ['backtrack'];
Branch: 1 branch
BranchTrueFalseTotal
$possible or return ["backtrack"] 0 0 0
2123 0 push @try, [$cell, $possible];
2124 }
2125 0 @try = map {$_->[0]} sort {
2126 0 ▶ $a->[1] <=> $b->[1] || $a->[0]{index} <=> $b->[0]{index}} @try;
Branch: 1 branch
BranchTrueFalseTotal
$Games::Sudoku::General::a->[1] <=> $Games::Sudoku::General::b->[1] or $Games::Sudoku::General::a->[0]{"index"} <=> $Games::Sudoku::General::b->[0]{"index"} 0 0 0
2127 0 my $cell = $try[0];
2128 0 ▶ for (my $val = 1; $val < $syms; $val++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val < $syms) 0 0 0
2129 0 ▶ next if $cell->{possible}{$val};
Branch: 1 branch
BranchTrueFalseTotal
if $cell->{"possible"}{$val} 0 0 0
2130 0 ▶ $self->_try ($cell, $val) and confess <<eod;
Branch: 1 branch
BranchTrueFalseTotal
$self->_try($cell, $val) and confess(qq[Programming error - Value $val illegal in cell $cell->{"index"} for ? constraint, but\n \$self->{possible}{$val} = $self->{"possible"}{$val}\n]) 0 0 0
2131 Programming error - Value $val illegal in cell $cell->{index} for ? constraint, but
2132 \$self->{possible}{$val} = $self->{possible}{$val}
2133 eod
2134 0 my $constraint = ['?' => [$cell->{index}, $val]];
2135 $self->{debug}
2136 0 ▶ and print '# ', $self->_format_constraint ($constraint);
Branch: 1 branch
BranchTrueFalseTotal
$self->{"debug"} and print "# ", $self->_format_constraint($constraint) 0 0 0
2137 0 return [$constraint];
2138 }
2139 0 return [];
2140 }
2141  
2142 # $status_value = $su->_constraint_remove ();
2143  
2144 # This method removes the topmost constraints from the backtrack
2145 # stack. It continues until the next item is a backtrack item or
2146 # the stack is empty. It returns true (SUDOKU_NO_SOLUTION,
2147 # actually) if the stack is emptied, or false (SUDOKU_SUCCESS,
2148 # actually) if it stops because it found a backtrack item.
2149  
2150 # The following arguments may be passed, for use in preparing
2151 # a generated problem:
2152 # - minimum number of cells to leave occupied (no lower limit
2153 # if this is undefined);
2154 # - maximum number of cells to leave occupied (no upper limit
2155 # if this is undefined);
2156 # - a reference to a hash of constraints that it is legal to
2157 # remove. The hash value is the number of times it is
2158 # legal to remove that constraint, or undef if it can
2159 # be removed any number of times.
2160  
2161 sub _constraint_remove {
2162 1 ▶ my ( $self, $min, $max, $removal_ok ) = @_;
Subroutine _constraint_remove: called CC=40 SCAR=62.8
2163 1 ▶ $min and $min = @{$self->{cell}} - $min;
Branch: 1 branch
BranchTrueFalseTotal
$min and $min = @{$self->{"cell"};} - $min 0 1 1
2164 1 ▶ $max and $max = @{$self->{cell}} - $max;
Branch: 1 branch
BranchTrueFalseTotal
$max and $max = @{$self->{"cell"};} - $max 0 1 1
2165 1 ▶ $self->{no_more_solutions} and return SUDOKU_NO_SOLUTION;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"no_more_solutions"} and return 1 0 1 1
2166 1 ▶ my $stack = $self->{backtrack_stack} or return SUDOKU_NO_SOLUTION;
Branch: 1 branch
BranchTrueFalseTotal
my $stack = $self->{"backtrack_stack"} or return 1 0 1 1
2167 1 ▶ my $used = $self->{constraints_used} ||= {};
Condition50%1/2
$self->{"constraints_used"} ||= {}
l!l
10
MC/DC0%0/1
$self->{"constraints_used"} ||= {}
$self->{"constraints_used"}
Truth table50%1/2
A
A= $self->{"constraints_used"}
Aresult
11
00
2168 1 my $inx = @$stack;
2169 1 my $syms = @{$self->{symbol_list}};
2170 1 ▶ ($self->{debug} && $inx) and print <<eod;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"debug"} and $inx and print "# Debug - Backtracking\n" 0 1 1
Condition33%1/3
$self->{"debug"} and $inx
!ll&&!rl&&r
100
MC/DC0%0/2
$self->{"debug"} and $inx
$self->{"debug"} $inx
Truth table33%1/3
A and B
A= $self->{"debug"}B= $inx
ABresult
0X0
100
111
2171 # Debug - Backtracking
2172 eod
2173 1 my $old = $inx;
2174 1 ▶ while (--$inx >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (--$inx >= 0) 45 1 46
2175 45 ▶ ($min && $self->{cells_unassigned} >= $min) and do {
Branch: 1 branch
BranchTrueFalseTotal
$min and $self->{"cells_unassigned"} >= $min and do { $self->{"debug"} and print "Debug - Hit minimum occupied cells - returning.\n"; return 0 } 0 45 45
Condition33%1/3
$min and $self->{"cells_unassigned"} >= $min
!ll&&!rl&&r
4500
MC/DC0%0/2
$min and $self->{"cells_unassigned"} >= $min
$min $self->{"cells_unassigned"} >= $min
Truth table33%1/3
A and B
A= $minB= $self->{"cells_unassigned"} >= $min
ABresult
0X0
100
111
2176 0 ▶ $self->{debug} and print <<eod;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"debug"} and print "Debug - Hit minimum occupied cells - returning.\n" 0 0 0
2177 Debug - Hit minimum occupied cells - returning.
2178 eod
2179 0 return SUDOKU_SUCCESS;
2180 };
2181 45 my $constraint = $stack->[$inx][0];
2182 45 ▶ if ($removal_ok) {
Branch: 1 branch
BranchTrueFalseTotal
if ($removal_ok) { } 0 45 45
2183 ($max && $self->{cells_unassigned} <= $max &&
2184 ## !$removal_ok->{$constraint} and next;
2185 0 ▶ !exists $removal_ok->{$constraint}) and next;
Branch: 1 branch
BranchTrueFalseTotal
$max and $self->{"cells_unassigned"} <= $max and not exists $removal_ok->{$constraint} and next 0 0 0
Condition0%0/6
$max and $self->{"cells_unassigned"} <= $max and not exists $removal_ok->{$constraint}
!ll&&!rl&&r
000
$max and $self->{"cells_unassigned"} <= $max
!ll&&!rl&&r
000
MC/DC0%0/3
$max and $self->{"cells_unassigned"} <= $max and not exists $removal_ok->{$constraint}
$max $self->{"cells_unassigned"} <= $max not exists $removal_ok->{$constraint}
Truth table0%0/4
A and B and not(C)
A= $maxB= $self->{"cells_unassigned"} <= $maxC= not exists $removal_ok->{$constraint}
ABCresult
0XX0
10X0
1100
1111
2186  
2187 0 ▶ if (!exists $removal_ok->{$constraint}) {
Branch: 2 branches
BranchTrueFalseTotal
if (not exists $removal_ok->{$constraint}) { } 0 0 0
elsif (defined $removal_ok->{$constraint} and --$removal_ok->{$constraint}) { } 0 0 0
2188 0 ▶ $self->{debug} and print <<eod;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"debug"} and print "Debug - Encountered constraint $constraint - returning.\n" 0 0 0
2189 Debug - Encountered constraint $constraint - returning.
2190 eod
2191 0 return SUDOKU_SUCCESS;
2192 ▶ } elsif (defined $removal_ok->{$constraint} &&
Condition0%0/3
defined $removal_ok->{$constraint} and --$removal_ok->{$constraint}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $removal_ok->{$constraint} and --$removal_ok->{$constraint}
defined $removal_ok->{$constraint} --$removal_ok->{$constraint}
Truth table0%0/3
A and B
A= defined $removal_ok->{$constraint}B= --$removal_ok->{$constraint}
ABresult
0X0
100
111
2193 --$removal_ok->{$constraint}) {
2194 0 ▶ $self->{debug} and print <<eod;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"debug"} and print "Debug - Reached usage limit on $constraint - returning.\n" 0 0 0
2195 Debug - Reached usage limit on $constraint - returning.
2196 eod
2197 0 return SUDOKU_SUCCESS;
2198 }
2199 } else {
2200 45 ▶ ($max && $self->{cells_unassigned} <= $max &&
Branch: 1 branch
BranchTrueFalseTotal
$max and $self->{"cells_unassigned"} <= $max and $constraint eq "?" and next 0 45 45
Condition33%2/6
$max and $self->{"cells_unassigned"} <= $max and $constraint eq "?"
!ll&&!rl&&r
4500
$max and $self->{"cells_unassigned"} <= $max
!ll&&!rl&&r
4500
MC/DC0%0/3
$max and $self->{"cells_unassigned"} <= $max and $constraint eq "?"
$max $self->{"cells_unassigned"} <= $max $constraint eq "?"
Truth table25%1/4
A and B and C
A= $maxB= $self->{"cells_unassigned"} <= $maxC= $constraint eq "?"
ABCresult
0XX0
10X0
1100
1111
2201 $constraint eq '?')
2202 and next;
2203 }
2204 45 --$used->{$constraint};
2205 45 ▶ if ($constraint eq 'F' || $constraint eq 'N') {
Branch: 3 branches
BranchTrueFalseTotal
if ($constraint eq 'F' or $constraint eq 'N') { } 45 0 45
elsif ($constraint eq 'B' or $constraint eq 'T') { } 0 0 0
elsif ($constraint eq '?') { } 0 0 0
Condition66%2/3
$constraint eq "F" or $constraint eq "N"
l!l&&r!l&&!r
4050
MC/DC0%0/2
$constraint eq "F" or $constraint eq "N"
$constraint eq "F" $constraint eq "N"
Truth table66%2/3
A or B
A= $constraint eq "F"B= $constraint eq "N"
ABresult
1X1
011
000
2206 45 foreach my $ref (reverse @{$stack->[$inx]}) {
2207 90 ▶ $self->_try ($ref->[0], 0) if ref $ref;
Branch: 1 branch
BranchTrueFalseTotal
if ref $ref 45 45 90
2208 }
2209 ▶ } elsif ($constraint eq 'B' || $constraint eq 'T') {
Condition0%0/3
$constraint eq "B" or $constraint eq "T"
l!l&&r!l&&!r
000
MC/DC0%0/2
$constraint eq "B" or $constraint eq "T"
$constraint eq "B" $constraint eq "T"
Truth table0%0/3
A or B
A= $constraint eq "B"B= $constraint eq "T"
ABresult
1X1
011
000
2210 0 foreach my $ref (reverse @{$stack->[$inx]}) {
2211 0 ▶ next unless ref $ref;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $ref 0 0 0
2212 0 my $val = $ref->[1];
2213 0 foreach my $inx (@{$ref->[0]}) {
2214 0 $self->{cell}[$inx]{possible}{$val} = 0;
2215 }
2216 }
2217 } elsif ($constraint eq '?') {
2218 0 my $start = $stack->[$inx][1][1] + 1;
2219 0 my $cell = $self->{cell}[$stack->[$inx][1][0]];
2220 0 $self->_try ($cell, 0);
2221 0 ▶ next if $removal_ok;
Branch: 1 branch
BranchTrueFalseTotal
if $removal_ok 0 0 0
2222 0 ▶ for (my $val = $start; $val < $syms; $val++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val < $syms) 0 0 0
2223 0 ▶ next if $cell->{possible}{$val};
Branch: 1 branch
BranchTrueFalseTotal
if $cell->{"possible"}{$val} 0 0 0
2224 0 ▶ $self->_try ($cell, $val) and confess <<eod;
Branch: 1 branch
BranchTrueFalseTotal
$self->_try($cell, $val) and confess(qq[Programming error - Try of $val in cell $cell->{"index"} failed, but\n \$cell->{possible}[$inx] = $cell->{"possible"}[$inx]\n]) 0 0 0
2225 Programming error - Try of $val in cell $cell->{index} failed, but
2226 \$cell->{possible}[$inx] = $cell->{possible}[$inx]
2227 eod
2228 0 $used->{$constraint}++;
2229 0 $stack->[$inx][1][0] = $cell->{index};
2230 0 $stack->[$inx][1][1] = $val;
2231 0 ▶ $self->{debug} and do {
Branch: 1 branch
BranchTrueFalseTotal
$self->{"debug"} and do { my $x = $self->_format_constraint($stack->[$inx]); chomp $x; print qq[# Debug - Backtrack complete. @{[$old - @$stack];} constraints removed.\n# Resuming puzzle at stack depth @{[$inx + 1];} with\n# $self->{"cells_unassigned"} unassigned cells, guessing\n# $x\n] } 0 0 0
2232 0 my $x = $self->_format_constraint ($stack->[$inx]);
2233 0 chomp $x;
2234 0 print <<eod;
2235 0 # Debug - Backtrack complete. @{[$old - @$stack]} constraints removed.
2236 0 # Resuming puzzle at stack depth @{[$inx + 1]} with
2237 # $self->{cells_unassigned} unassigned cells, guessing
2238 # $x
2239 eod
2240 };
2241 0 return SUDOKU_SUCCESS;
2242 }
2243 0 } else {confess <<eod
2244 Programming Error - No code provided to remove constraint '$constraint' from stack.
2245 eod
2246 }
2247 45 pop @$stack;
2248 }
2249 1 ▶ $self->{debug} and print <<eod;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"debug"} and print "# Debug - Backtrack complete. @{[$old - @$stack];} constraints removed.\n# No more solutions to the puzzle exist.\n" 0 1 1
2250 0 # Debug - Backtrack complete. @{[$old - @$stack]} constraints removed.
2251 # No more solutions to the puzzle exist.
2252 eod
2253 1 $self->{no_more_solutions} = 1;
2254 1 return SUDOKU_NO_SOLUTION;
2255 }
2256  
2257 # $self->_deprecation_notice( $name );
2258 #
2259 # This method centralizes deprecation. Deprecation is driven of
2260 # the %deprecate hash. Level values are:
2261 # false - no warning
2262 # 1 - warn on first use
2263 # 2 - warn on each use
2264 # 3 - die on each use.
2265  
2266 {
2267  
2268 2 my %deprecate = (
2269 brick_third_argument => {
2270 message => 'Specifying 3 values for set( brick => ... ) is no longer allowed',
2271 level => 3,
2272 },
2273 );
2274  
2275 sub _deprecation_notice {
2276 0 ▶ my ( undef, $name ) = @_; # Invocant unused
Subroutine _deprecation_notice: not called CC=6 SCAR=37.4
2277 0 ▶ my $info = $deprecate{$name}
Branch: 1 branch
BranchTrueFalseTotal
my $info = $deprecate{$name} or return 0 0 0
2278 or return;
2279 $info->{level}
2280 0 ▶ or return;
Branch: 1 branch
BranchTrueFalseTotal
$info->{"level"} or return 0 0 0
2281 $info->{level} >= 3
2282 0 ▶ and croak $info->{message};
Branch: 1 branch
BranchTrueFalseTotal
$info->{"level"} >= 3 and croak($info->{"message"}) 0 0 0
2283 warnings::enabled( 'deprecated' )
2284 0 ▶ and carp $info->{message};
Branch: 1 branch
BranchTrueFalseTotal
warnings::enabled("deprecated") and carp($info->{"message"}) 0 0 0
2285 $info->{level} == 1
2286 0 ▶ and $info->{level} = 0;
Branch: 1 branch
BranchTrueFalseTotal
$info->{"level"} == 1 and $info->{"level"} = 0 0 0 0
2287 0 return;
2288 }
2289  
2290 }
2291  
2292 # _format_constraint formats the given constraint for output.
2293  
2294 2 sub _format_constraint {
2295 1 ▶ my ($self, @args) = @_;
Subroutine _format_constraint: called CC=7 SCAR=21.2
2296 1 my @steps;
2297 1 foreach (@args) {
2298 52 my @stuff;
2299 52 foreach (@$_) {
2300 104 ▶ last unless $_;
Branch: 1 branch
BranchTrueFalseTotal
unless $_ 0 104 104
2301 push @stuff, ref $_ ?
2302 '[' . join (' ',
2303 0 ▶ ref $_->[0] ? '[' . join (', ', @{$_->[0]}) . ']' : $_->[0],
Branch: 1 branch
BranchTrueFalseTotal
ref $_->[1] ? : 0 52 52
2304 ref $_->[1] ? '[' . join (', ',
2305 0 map {$self->{symbol_list}[$_]} @{$_->[1]}) . ']' :
2306 104 ▶ $self->{symbol_list}[$_->[1]],
Branch: 2 branches
BranchTrueFalseTotal
ref $_ ? : 52 52 104
ref $_->[0] ? : 0 52 52
2307 ) . ']' :
2308 $_;
2309 }
2310 52 push @steps, join (' ', @stuff) . "\n";
2311 }
2312 1 return join '', @steps;
2313 }
2314  
2315 # _looks_like_number is cribbed heavily from
2316 # Scalar::Util::looks_like_number by Graham Barr. This version
2317 # only accepts integers, but it is really here because
2318 # ActivePerl's Scalar::Util is too ancient to export
2319 # looks_like_number.
2320  
2321 sub _looks_like_number {
2322 44 ▶ ( local $_ ) = @_;
Subroutine _looks_like_number: called CC=4 SCAR=17.1
2323 44 ▶ return 0 if !defined ($_) || ref ($_);
Branch: 1 branch
BranchTrueFalseTotal
if not defined $_ or ref $_ 0 44 44
Condition33%1/3
not defined $_ or ref $_
l!l&&r!l&&!r
0044
MC/DC0%0/2
not defined $_ or ref $_
not defined $_ ref $_
Truth table33%1/3
not(A) or B
A= not defined $_B= ref $_
ABresult
1X1
011
000
2324 44 ▶ return 1 if m/^[+-]?\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
if /^[+-]?\d+$/ 44 0 44
2325 0 return 0;
2326 }
2327  
2328 # _set_* subroutines are found right after the set() method.
2329  
2330 # $su->_try ($cell, $value)
2331  
2332 # This method inserts the given value in the given cell,
2333 # replacing the previous value if any, and doing all the
2334 # bookkeeping. If the given value is legal (meaning, if
2335 # it is zero or if it is unique in all sets the cell
2336 # belongs to), it returns 0. If not, it returns 1, but
2337 # does not undo the trial.
2338  
2339 sub _try {
2340 2815 ▶ my ( $self, $cell, $new ) = @_;
Subroutine _try: called CC=16 SCAR=27.8
2341 2815 ▶ $cell = $self->{cell}[$cell] unless ref $cell;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $cell 1942 873 2815
2342 2815 ▶ defined $new
Branch: 1 branch
BranchTrueFalseTotal
defined $new or _fatal(qq[_try called for cell $cell->{"index"} with new value undefined]) 0 2815 2815
2343 or _fatal (
2344 "_try called for cell $cell->{index} with new value undefined");
2345 2815 ▶ defined (my $old = $cell->{content}) or _fatal (
Branch: 1 branch
BranchTrueFalseTotal
defined(my $old = $cell->{"content"}) or _fatal(qq[_try called with old cell $cell->{"index"} value undefined]) 0 2815 2815
2346 "_try called with old cell $cell->{index} value undefined");
2347 2815 my $rslt = eval {
2348 2815 ▶ return 0 if $old == $new;
Branch: 1 branch
BranchTrueFalseTotal
if $old == $new 1171 1644 2815
2349 1644 ▶ if ($new) {
Branch: 1 branch
BranchTrueFalseTotal
if ($new) 1599 45 1644
2350 1599 foreach my $set (@{$cell->{membership}}) {
2351 4833 ▶ return 1 if $self->{set}{$set}{content}[$new];
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"set"}{$set}{"content"}[$new] 0 4833 4833
2352 }
2353 }
2354 1644 $cell->{content} = $new;
2355 1644 ▶ $old and $self->{cells_unassigned}++;
Branch: 1 branch
BranchTrueFalseTotal
$old and ++$self->{"cells_unassigned"} 45 1599 1644
2356 1644 ▶ $new and --$self->{cells_unassigned};
Branch: 1 branch
BranchTrueFalseTotal
$new and --$self->{"cells_unassigned"} 1599 45 1644
2357 1644 foreach my $name (@{$cell->{membership}}) {
2358 4968 my $set = $self->{set}{$name};
2359 4968 --$set->{content}[$old];
2360 4968 ▶ $old and do {
Branch: 1 branch
BranchTrueFalseTotal
$old and do { ++$set->{"free"}; foreach $_ (@{$$set{"membership"};}) { --$self->{"cell"}[$_]{"possible"}{$old}; } } 135 4833 4968
2361 135 $set->{free}++;
2362 135 foreach (@{$set->{membership}}) {
2363 1215 --$self->{cell}[$_]{possible}{$old};
2364 }
2365 };
2366 4968 $set->{content}[$new]++;
2367 4968 ▶ $new and do {
Branch: 1 branch
BranchTrueFalseTotal
$new and do { --$set->{"free"}; foreach $_ (@{$$set{"membership"};}) { ++$self->{"cell"}[$_]{"possible"}{$new}; } } 4833 135 4968
2368 4833 --$set->{free};
2369 4833 foreach (@{$set->{membership}}) {
2370 50421 $self->{cell}[$_]{possible}{$new}++;
2371 }
2372 };
2373 }
2374 1644 return 0;
2375 };
2376 2815 ▶ $@ and _fatal ("Eval failed in _try", $@);
Branch: 1 branch
BranchTrueFalseTotal
$@ and _fatal("Eval failed in _try", $@) 0 2815 2815
2377 2815 return $rslt;
2378 }
2379  
2380 # $string = $self->_unload (prefix, status_value)
2381  
2382 # This method unloads the current cell contents into a string.
2383 # The prefix is prefixed to the string, and defaults to ''.
2384 # If status_value is specified, it is set. If status_value is
2385 # specified and it is a failure status, undef is returned, and
2386 # the current cell contents are ignored.
2387  
2388 sub _unload {
2389 23 ▶ my ($self, $prefix, @args) = @_;
Subroutine _unload: called CC=14 SCAR=26.9
2390 23 ▶ defined $prefix or $prefix = '';
Branch: 1 branch
BranchTrueFalseTotal
defined $prefix or $prefix = "" 5 18 23
2391 23 ▶ @args and do {
Branch: 1 branch
BranchTrueFalseTotal
@args and do { $self->set("status_value", $args[0]); $args[0] and return } 19 4 23
2392 19 $self->set (status_value => $args[0]);
2393 19 ▶ $args[0] and return;
Branch: 1 branch
BranchTrueFalseTotal
$args[0] and return 1 18 19
2394 };
2395 22 my $rslt = '';
2396 22 my $col = $self->{columns};
2397 22 ▶ my $row = $self->{rows} ||= floor (@{$self->{cell}} / $col);
Condition33%1/3
$self->{"rows"} ||= floor(@{$self->{"cell"};} / $col)
l!l&&r!l&&!r
2200
MC/DC0%0/2
$self->{"rows"} ||= floor(@{$self->{"cell"};} / $col)
$self->{"rows"} floor(@{$self->{"cell"};} / $col)
Truth table33%1/3
A ||= B
A= $self->{"rows"}B= floor(@{$self->{"cell"};} / $col)
ABresult
1X1
011
000
2398 22 my $fmt = "%$self->{biggest_symbol}s";
2399 22 foreach (@{$self->{cell}}) {
2400 1894 ▶ $col == $self->{columns} and $rslt .= $prefix;
Branch: 1 branch
BranchTrueFalseTotal
$col == $self->{"columns"} and $rslt .= $prefix 220 1674 1894
2401 # was $self->{ignore_unused}
2402 0 ▶ $rslt .= ($self->{cells_unused} && !@{$_->{membership}}) ?
Condition100%2/2
$_->{'content'} || 0
l!l
1686208
MC/DC100%1/1
$_->{'content'} || 0
$_->{'content'}
Truth table100%2/2
A
A= $_->{'content'}
Aresult
11
00
2403 sprintf ($fmt, ' ') :
2404 1894 ▶ sprintf ($fmt, $self->{symbol_list}[$_->{content} || 0]);
Branch: 1 branch
BranchTrueFalseTotal
$self->{'cells_unused'} && !@{$_->{'membership'};} ? : 0 1894 1894
Condition33%1/3
$self->{"cells_unused"} and not @{$_->{"membership"};}
!ll&&!rl&&r
189400
MC/DC0%0/2
$self->{"cells_unused"} and not @{$_->{"membership"};}
$self->{"cells_unused"} not @{$_->{"membership"};}
Truth table33%1/3
A and not(B)
A= $self->{"cells_unused"}B= not @{$_->{"membership"};}
ABresult
0X0
100
111
2405 1894 ▶ if (--$col > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (--$col > 0) { } 1674 220 1894
2406 $rslt .= $self->{output_delimiter}
2407 1674 } else {
2408 # was $self->{ignore_unused}
2409 220 ▶ $self->{cells_unused} and $rslt =~ s/\s+$//m;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"cells_unused"} and $rslt =~ s/\s+$//m 0 220 220
2410 220 $rslt .= "\n";
2411 220 $col = $self->{columns};
2412 220 ▶ if (--$row <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (--$row <= 0) 29 191 220
2413 29 $rslt .= "\n";
2414 29 $row = $self->{rows};
2415 }
2416 }
2417 }
2418 22 ▶ 0 while chomp $rslt;
Branch: 1 branch
BranchTrueFalseTotal
chomp $rslt and do { '???' } 44 22 66
2419 22 $rslt .= "\n";
2420 22 return $rslt;
2421 }
2422  
2423 2 1;
2424  
2425 __END__
↑ Summary Build »