Sudoku/Diagonal.pm

filter: Statementstmt 92.8% 65 / 70 Branchbran 95.4% 21 / 22 Conditioncond 88.8% 8 / 9 MC/DCmcdc 75.0% 3 / 4 Subroutinesub 76.1% 16 / 21 Podpod 0.0% 0 / 6 totaltotal 85.6% 113 / 132 CC 16 SCAR 20.4
CC
16
Cov
93%
init_diagonal
16.0
set_diagonal_triple
2.0
__ANON__
1.1
CRAP 7.7
« Sudoku.pm ↑ Summary Sudoku/Quadruple.pm »
1 package Regexp::Sudoku::Diagonal;
2  
3 36 ▶use 5.028;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 36 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 36 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 36 ▶no warnings 'syntax';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 36 ▶use experimental 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 36 ▶use experimental 'lexical_subs';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 36 our $VERSION = '2022030401';
13  
14 36 ▶use Hash::Util::FieldHash qw [fieldhash];
Subroutine BEGIN: called CC=1 SCAR=0.0
15 36 ▶use Regexp::Sudoku::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 ################################################################################
18 #
19 # sub init_diagonal ($self, $args)
20 #
21 # If we have diagonals, it means cells on one or more diagonals
22 # should differ. This method initializes the houses for that.
23 #
24 # The main diagonal for a 9 x 9 sudoku is defined as follows:
25 #
26 # * . . . . . . . .
27 # . * . . . . . . .
28 # . . * . . . . . .
29 #
30 # . . . * . . . . .
31 # . . . . * . . . .
32 # . . . . . * . . .
33 #
34 # . . . . . . * . .
35 # . . . . . . . * .
36 # . . . . . . . . *
37 #
38 # The minor diagonal for a 9 x 9 sudoku is defined as follows:
39 #
40 # . . . . . . . . *
41 # . . . . . . . * .
42 # . . . . . . * . .
43 #
44 # . . . . . * . . .
45 # . . . . * . . . .
46 # . . . * . . . . .
47 #
48 # . . * . . . . . .
49 # . * . . . . . . .
50 # * . . . . . . . .
51 #
52 # TESTS: Diagonal/100-set_diagonals.t
53 # Diagonal/101-set_diagonals.t
54 # Diagonal/102-set_diagonals.t
55 #
56 ################################################################################
57
58 24 ▶my sub init_diagonal ($self, $type, $offset = 0) {
Subroutine init_diagonal: called CC=16 SCAR=27.7
59 24 my $size = $self -> size;
60  
61 24 ▶ return $self if $offset >= $size;
Branch: 1 branch
BranchTrueFalseTotal
if $offset >= $size 0 24 24
62  
63 24 my @cells;
64 24 ▶ for (my ($r, $c) = $type == $MAIN_DIAGONAL
Branch: 4 branches
BranchTrueFalseTotal
$type == $Regexp::Sudoku::Diagonal::MAIN_DIAGONAL ? : 12 12 24
$offset >= 0 ? : 7 5 12
$offset >= 0 ? : 7 5 12
if (0 < $r and $r <= $size and 0 < $c and $c <= $size) 162 24 186
Condition88%8/9
0 < $r and $r <= $size and 0 < $c and $c <= $size
!ll&&!rl&&r
1410162
0 < $r and $r <= $size and 0 < $c
!ll&&!rl&&r
140172
0 < $r and $r <= $size
!ll&&!rl&&r
77172
MC/DC75%3/4
0 < $r and $r <= $size and 0 < $c and $c <= $size
0 < $r $r <= $size 0 < $c $c <= $size
Truth table80%4/5
A and B and C and D
A= 0 < $rB= $r <= $sizeC= 0 < $cD= $c <= $size
ABCDresult
0XXX0
10XX0
110X0
11100
11111
65 ? ($offset >= 0 ? (1, 1 + $offset)
66 : (1 - $offset, 1))
67 : ($offset >= 0 ? ($size, 1 + $offset)
68 : ($size + $offset, 1));
69 0 < $r && $r <= $size && 0 < $c && $c <= $size;
70 ($r, $c) = $type == $MAIN_DIAGONAL ? ($r + 1, $c + 1)
71 : ($r - 1, $c + 1)) {
72 162 ▶ push @cells => cell_name ($r, $c);
Branch: 1 branch
BranchTrueFalseTotal
$type == $Regexp::Sudoku::Diagonal::MAIN_DIAGONAL ? : 81 81 162
73 }
74  
75 24 my $name;
76 24 ▶ if ($type == $MAIN_DIAGONAL) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type == $Regexp::Sudoku::Diagonal::MAIN_DIAGONAL) { } 12 12 24
77 12 $name = "DM";
78 12 ▶ if ($offset) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offset) 10 2 12
79 10 ▶ $name .= $offset > 0 ? "S" : "s";
Branch: 1 branch
BranchTrueFalseTotal
$offset > 0 ? : 5 5 10
80 10 $name .= "-" . abs ($offset);
81 }
82 }
83 else {
84 12 $name = "Dm";
85 12 ▶ if ($offset) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offset) 10 2 12
86 10 ▶ $name .= $offset < 0 ? "S" : "s";
Branch: 1 branch
BranchTrueFalseTotal
$offset < 0 ? : 5 5 10
87 10 $name .= "-" . abs ($offset);
88 }
89 }
90  
91 24 $self -> create_house ($name => @cells);
92 }
93  
94 2 ▶sub set_diagonal_main ($self) {
Subroutine set_diagonal_main: called CC=1 SCAR=0.0
Pod undocumented
95 2 init_diagonal ($self, $MAIN_DIAGONAL);
96 }
97 2 ▶sub set_diagonal_minor ($self) {
Subroutine set_diagonal_minor: called CC=1 SCAR=0.0
Pod undocumented
98 2 init_diagonal ($self, $MINOR_DIAGONAL);
99 }
100 2 ▶sub set_cross ($self) {
Subroutine set_cross: called CC=1 SCAR=0.0
Pod undocumented
101 2 $self -> set_diagonal_main
102 -> set_diagonal_minor
103 }
104 1 ▶sub set_diagonal_double ($self) {
Subroutine set_diagonal_double: called CC=1 SCAR=0.0
Pod undocumented
105 1 $self -> set_cross_1
106 }
107 0 ▶sub set_diagonal_triple ($self) {
Subroutine set_diagonal_triple: not called CC=1 SCAR=6.9
Pod undocumented
108 0 $self -> set_cross_1
109 -> set_cross
110 }
111 2 ▶sub set_argyle ($self) {
Subroutine set_argyle: called CC=1 SCAR=0.0
Pod undocumented
112 2 $self -> set_cross_1
113 -> set_cross_4
114 }
115
116
117 36 foreach my $offset (1 .. $NR_OF_SYMBOLS - 1) {
118 36 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
119
120 1224 ▶ *{"set_diagonal_main_super_$offset"} = sub ($self) {
Subroutine __ANON__: not called CC=1 SCAR=1.2
121 0 init_diagonal ($self, $MAIN_DIAGONAL, $offset);
122 1224 };
123
124 1224 ▶ *{"set_diagonal_main_sub_$offset"} = sub ($self) {
Subroutine __ANON__: not called CC=1 SCAR=1.2
125 0 init_diagonal ($self, $MAIN_DIAGONAL, - $offset);
126 1224 };
127
128 1224 ▶ *{"set_diagonal_minor_super_$offset"} = sub ($self) {
Subroutine __ANON__: not called CC=1 SCAR=1.2
129 0 init_diagonal ($self, $MINOR_DIAGONAL, - $offset);
130 1224 };
131
132 1224 ▶ *{"set_diagonal_minor_sub_$offset"} = sub ($self) {
Subroutine __ANON__: not called CC=1 SCAR=1.2
133 0 init_diagonal ($self, $MINOR_DIAGONAL, $offset);
134 1224 };
135
136 1224 ▶ *{"set_cross_$offset"} = sub ($self) {
Subroutine __ANON__: called CC=1 SCAR=0.0
137 5 init_diagonal ($self, $MAIN_DIAGONAL, $offset);
138 5 init_diagonal ($self, $MAIN_DIAGONAL, - $offset);
139 5 init_diagonal ($self, $MINOR_DIAGONAL, - $offset);
140 5 init_diagonal ($self, $MINOR_DIAGONAL, $offset);
141 1224 };
142 }
143
144 36 1;
145  
146 __END__
« Sudoku.pm ↑ Summary Sudoku/Quadruple.pm »