blib/lib/Regexp/N_Queens.pm

filter: Statementstmt 40.2% 31 / 77 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 17 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 44.4% 8 / 18 Podpod 20.0% 1 / 5 totaltotal 29.8% 40 / 134 CC 14 SCAR 29.4
CC
14
Cov
30%
init_subject_and_pattern
56.0
init
20.0
attacks
20.0
CRAP 18.8
↑ Summary
1 package Regexp::N_Queens;
2  
3 1 ▶use 5.028;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶no warnings 'syntax';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use experimental 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use experimental 'lexical_subs';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 ▶use Hash::Util::FieldHash qw [fieldhash];
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 ▶use lib qw [lib];
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 1 our $VERSION = '2023021701';
16  
17 1 fieldhash my %size;
18 1 fieldhash my %pattern;
19 1 fieldhash my %subject;
20  
21 1 my $queen = "Q";
22 1 my $prefix = "Q";
23 1 my $sep = ";";
24  
25 my sub init_size;
26 my sub init_subject_and_pattern;
27 my sub name;
28 my sub all_groups;
29 my sub attacks;
30  
31  
32 ################################################################################
33 #
34 # sub new ($class)
35 #
36 # Create a new, uninitialized object.
37 #
38 # IN: $class: Package name
39 #
40 # OUT: Blessed object, uninitialized object
41 #
42 ################################################################################
43  
44 0 ▶sub new ($class) {bless \do {my $v => $class}}
Subroutine new: not called CC=1 SCAR=6.9
Pod undocumented
45  
46  
47 ################################################################################
48 #
49 # sub init ($self, @args)
50 #
51 # Initialize the object.
52 #
53 # IN: $self: Uninitialized object.
54 # @args: Set of named parameters used to initialized the object.
55 # We accept the following options:
56 # - size => N: The size of the chess board (number of rows/columns)
57 # If no size parameter is given, the board will
58 # be assumed to have size 8, like a standard
59 # chess board.
60 #
61 # As a special feature, the set of named parameter may also
62 # be passed in as a hashref.
63 #
64 # OUT: Initialized object.
65 #
66 ################################################################################
67  
68 0 ▶sub init ($self, @args) {
Subroutine init: not called CC=4 SCAR=30.0
Pod undocumented
69 0 ▶ my $args = @args == 1 && ref $args [0] eq "HASH" ? $args [0] : {@args};
Branch: 1 branch
BranchTrueFalseTotal
@args == 1 && ref $args[0] eq 'HASH' ? : 0 0 0
Condition0%0/3
@args == 1 and ref $args[0] eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
@args == 1 and ref $args[0] eq "HASH"
@args == 1 ref $args[0] eq "HASH"
Truth table0%0/3
A and B
A= @args == 1B= ref $args[0] eq "HASH"
ABresult
0X0
100
111
70  
71 0 init_size ($self, $args);
72  
73 0 ▶ if (keys %$args) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %$args) 0 0 0
74 0 die "Unknown parameter(s) to init: " . join (", " => keys %$args)
75 . "\n";
76 }
77  
78 0 $self;
79 }
80  
81  
82 ################################################################################
83 #
84 # my sub init_size ($self, $args)
85 #
86 # Initialize the size of the board. This subroutine is called from init ().
87 # This is subroutine is lexical, and cannot be called from the outside.
88 #
89 # IN: $self: Current object
90 # $args: Hashref with parameters. Used (and then deleted) parameters:
91 # - size => N: The size of the chess board (number of rows/columns)
92 #
93 # OUT: Current object
94 #
95 ################################################################################
96  
97 0 ▶sub init_size ($self, $args) {
Subroutine init_size: not called CC=2 SCAR=17.9
98 0 ▶ $size {$self} = delete $$args {size} || 8;
Condition0%0/2
delete $args->{'size'} || 8
l!l
00
MC/DC0%0/1
delete $args->{'size'} || 8
delete $args->{'size'}
Truth table0%0/2
A
A= delete $args->{'size'}
Aresult
11
00
99 0 $self;
100 }
101  
102  
103 ################################################################################
104 #
105 # sub size ($self)
106 #
107 # Return the size of the board.
108 #
109 # IN: $self: Current object
110 #
111 # OUT: Size of the board.
112 #
113 ################################################################################
114  
115 0 ▶sub size ($self) {
Subroutine size: not called CC=1 SCAR=6.9
Pod documented
116 0 $size {$self}
117 }
118  
119  
120 ################################################################################
121 #
122 # sub subject ($self)
123 #
124 # Return the subject to be matched against.
125 #
126 # IN: $self: Current object
127 #
128 # OUT: Subject
129 #
130 ################################################################################
131  
132 0 ▶sub subject ($self) {
Subroutine subject: not called CC=1 SCAR=6.9
Pod undocumented
133 0 init_subject_and_pattern ($self);
134 0 $subject {$self};
135 }
136  
137  
138 ################################################################################
139 #
140 # sub pattern ($self)
141 #
142 # Return the pattern to match against the subject.
143 #
144 # IN: $self: Current object
145 #
146 # OUT: Pattern
147 #
148 ################################################################################
149  
150 0 ▶sub pattern ($self) {
Subroutine pattern: not called CC=1 SCAR=6.9
Pod undocumented
151 0 init_subject_and_pattern ($self);
152 0 $pattern {$self};
153 }
154  
155 ################################################################################
156 #
157 # my sub name ($square)
158 #
159 # Return the name of the capture which captures the state of a square.
160 # This is a lexical sub, and not callable from the outside.
161 #
162 # IN: $square: The square for which we want the name. A square is represented
163 # as an 2-element array(ref), with an x and a y coordinate.
164 #
165 # OUT: Name of the capture.
166 #
167 ################################################################################
168  
169 0 ▶sub name ($square) {
Subroutine name: not called CC=1 SCAR=6.9
170 0 join "_" => $prefix, @$square
171 }
172  
173  
174 ################################################################################
175 #
176 # my sub all_groups (@squares)
177 #
178 # Give a list of squares, return a sub pattern, where we refer back to
179 # each capture group of said square. The sub pattern is terminated with
180 # a semi-colon.
181 #
182 # IN: @squares: List of squares, where each square is represented by a
183 # two element array, with an x and a y coordinate.
184 #
185 # OUT: A string with back references (\g{name}), terminated by a semi colon.
186 #
187 ################################################################################
188  
189 0 ▶sub all_groups (@squares) {
Subroutine all_groups: not called CC=1 SCAR=6.9
190 0 (join "" => map {my $name = name $_; "\\g{$name}"} @squares) . $sep
191 }
192  
193  
194 ################################################################################
195 #
196 # my sub attacks ($sq1, $sq2)
197 #
198 # Returns true iff the two squares are a Queens move away from each other.
199 # That is, if both squares would contain a Queen, they will attack each other.
200 # This is a lexical sub, and not callable from the outside.
201 #
202 # IN: $sq1, $sq2: The two squares of which we want to know whether they
203 # are in attacking range. Both squares are represented
204 # as 2-element arrays, with an x and a y coordinate.
205 #
206 # OUT: True if the squares are in attacking range, false otherwise.
207 #
208 ################################################################################
209  
210 0 ▶sub attacks ($sq1, $sq2) {
Subroutine attacks: not called CC=4 SCAR=30.0
211 0 state $X = 0;
212 0 state $Y = 1;
213 0 ▶ return $$sq1 [$X] == $$sq2 [$X] || # Same column
Condition0%0/9
$sq1->[$X] == $sq2->[$X] || $sq1->[$Y] == $sq2->[$Y] || $sq1->[$X] - $sq2->[$X] == $sq1->[$Y] - $sq2->[$Y] || $sq1->[$X] - $sq2->[$X] == $sq2->[$Y] - $sq1->[$Y]
l!l&&r!l&&!r
000
$sq1->[$X] == $sq2->[$X] or $sq1->[$Y] == $sq2->[$Y] or $sq1->[$X] - $sq2->[$X] == $sq1->[$Y] - $sq2->[$Y]
l!l&&r!l&&!r
000
$sq1->[$X] == $sq2->[$X] or $sq1->[$Y] == $sq2->[$Y]
l!l&&r!l&&!r
000
MC/DC0%0/4
$sq1->[$X] == $sq2->[$X] || $sq1->[$Y] == $sq2->[$Y] || $sq1->[$X] - $sq2->[$X] == $sq1->[$Y] - $sq2->[$Y] || $sq1->[$X] - $sq2->[$X] == $sq2->[$Y] - $sq1->[$Y]
$sq1->[$X] == $sq2->[$X] $sq1->[$Y] == $sq2->[$Y] $sq1->[$X] - $sq2->[$X] == $sq1->[$Y] - $sq2->[$Y] $sq1->[$X] - $sq2->[$X] == $sq2->[$Y] - $sq1->[$Y]
Truth table0%0/5
A or B or C || D
A= $sq1->[$X] == $sq2->[$X]B= $sq1->[$Y] == $sq2->[$Y]C= $sq1->[$X] - $sq2->[$X] == $sq1->[$Y] - $sq2->[$Y]D= $sq1->[$X] - $sq2->[$X] == $sq2->[$Y] - $sq1->[$Y]
ABCDresult
1XXX1
01XX1
001X1
00011
00000
214 $$sq1 [$Y] == $$sq2 [$Y] || # Same row
215 $$sq1 [$X] - $$sq2 [$X] == $$sq1 [$Y] - $$sq2 [$Y] || # Same diagonal
216 $$sq1 [$X] - $$sq2 [$X] == $$sq2 [$Y] - $$sq1 [$Y] # Same anti-diagonal
217 }
218  
219  
220 ################################################################################
221 #
222 # my sub init_subject_and_pattern ($self)
223 #
224 # Calculate the subject and pattern. This method is called when the object
225 # is queried for either the subject or pattern. Once called, a second call
226 # will immediately return. As the subroutine is lexical, it cannot be
227 # called from the outside.
228 #
229 # IN: $self: Current object.
230 #
231 # OUT: Current object.
232 #
233 ################################################################################
234  
235 0 ▶sub init_subject_and_pattern ($self) {
Subroutine init_subject_and_pattern: not called CC=7 SCAR=40.3
236 0 ▶ return if $subject {$self} && $pattern {$self};
Branch: 1 branch
BranchTrueFalseTotal
if $subject{$self} and $pattern{$self} 0 0 0
Condition0%0/3
$subject{$self} and $pattern{$self}
!ll&&!rl&&r
000
MC/DC0%0/2
$subject{$self} and $pattern{$self}
$subject{$self} $pattern{$self}
Truth table0%0/3
A and B
A= $subject{$self}B= $pattern{$self}
ABresult
0X0
100
111
237  
238 0 my $subject = "";
239 0 my $pattern = "";
240 0 my $size = $self -> size;
241  
242 #
243 # Process each of the squares
244 #
245 0 my @previous_squares;
246 0 foreach my $x (1 .. $size) {
247 0 my @this_row;
248 0 foreach my $y (1 .. $size) {
249 0 my $this_square = [$x, $y];
250 #
251 # First, decide whether the square gets a Queen or not.
252 # We capture this in a capture group ("Q_$x_$y"). If
253 # we capture a 'Q', there is a Queen on the square, else
254 # there is no Queen on the square.
255 #
256 0 my $this_group = name $this_square;
257 0 $subject .= "$queen$sep";
258 0 $pattern .= "(?<$this_group>$queen?)$queen?$sep";
259  
260 #
261 # Now we compare this cell with each of the previous squares.
262 # If they are a Queens move away (they can attack each other
263 # if both of them have a Queen), the two squares may have at
264 # most one Queen among them.
265 #
266 0 foreach my $previous_square (@previous_squares) {
267 0 ▶ next unless attacks $this_square, $previous_square;
Branch: 1 branch
BranchTrueFalseTotal
unless attacks $this_square, $previous_square 0 0 0
268 0 my $prev_group = name $previous_square;
269 0 $subject .= "$queen$sep";
270 0 $pattern .= "\\g{$prev_group}\\g{$this_group}$queen?$sep";
271 }
272 0 push @previous_squares => $this_square;
273 0 push @this_row => $this_square;
274 }
275 #
276 # We know there has to be exactly one Queen on each row.
277 # Previous constraints already made sure we cannot have
278 # more than one Queen, but it could have left us with
279 # no Queens at all. So, we add a constraint.
280 #
281 0 $subject .= "$queen$sep";
282 0 $pattern .= all_groups @this_row;
283 0 @this_row = ();
284 }
285  
286 0 $subject {$self} = $subject;
287 0 $pattern {$self} = '^' . $pattern . '$';
288  
289 0 $self;
290 }
291  
292  
293 1 1;
294  
295 __END__
↑ Summary