Verify/Golf.pm

filter: Statementstmt 92.5% 162 / 175 Branchbran 72.7% 48 / 66 Conditioncond 82.1% 23 / 28 MC/DCmcdc 57.1% 8 / 14 Subroutinesub 100% 17 / 17 Podpod 100% 1 / 1 totaltotal 86.0% 259 / 301 CC 47 SCAR 31.4
CC
47
Cov
87%
process_solution
38.7
_init
10.2
_read_foundation_line
5.5
CRAP 23.1
↑ Summary Verify/VariantParams.pm »
1 package Games::Solitaire::Verify::Golf;
2 3 $Games::Solitaire::Verify::Golf::VERSION = '0.2601';
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use autodie;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8  
9 3 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use List::Util qw/ sum /;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 3 ▶use Games::Solitaire::Verify::Card ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use Games::Solitaire::Verify::Column ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use Games::Solitaire::Verify::Freecells ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use Games::Solitaire::Verify::LinesIter ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 3 ▶use parent 'Games::Solitaire::Verify::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 3 __PACKAGE__->mk_acc_ref(
20 [
21 qw(
22 _columns
23 _foundation
24 _num_foundations
25 _place_queens_on_kings
26 _talon
27 _variant
28 _wrap_ranks
29 )
30 ]
31 );
32  
33 3 my $MAX_RANK = 13;
34 3 my $NUM_SUITS = 4;
35 3 my $CARD_RE = qr/[A23456789TJQK][HCDS]/;
36  
37 3 my %_VARIANTS = (
38 all_in_a_row => 1,
39 binary_star => 1,
40 black_hole => 1,
41 golf => 1,
42 );
43  
44 sub _is_binary_star
45 12 ▶{
Subroutine _is_binary_star: called CC=1 SCAR=0.0
46 12 my $self = shift;
47  
48 12 return $self->_variant eq 'binary_star';
49 }
50  
51 sub _is_golf
52 6 ▶{
Subroutine _is_golf: called CC=1 SCAR=0.0
53 6 my $self = shift;
54  
55 6 return $self->_variant eq 'golf';
56 }
57  
58 sub _read_foundation_line
59 5 ▶{
Subroutine _read_foundation_line: called CC=5 SCAR=17.1
60 5 my ( $self, $foundation_str ) = @_;
61 5 my $num_foundations = $self->_num_foundations();
62  
63 5 ▶ if ( my ($card_s) = $foundation_str =~
Branch: 1 branch
BranchTrueFalseTotal
if (my($card_s) = $foundation_str =~ /\AFoundations:((?: $CARD_RE){$num_foundations})\z/u) { } 5 0 5
64 m#\AFoundations:((?: $CARD_RE){$num_foundations})\z# )
65 {
66 5 ▶ $card_s =~ s/\A //ms
Branch: 1 branch
BranchTrueFalseTotal
$card_s =~ s/\A //msu or Carp::confess("_set_found_line: no leading space") 5 0 5
67 or Carp::confess("_set_found_line: no leading space");
68 5 my @c = split( / /, $card_s );
69 5 ▶ if ( @c != $num_foundations )
Branch: 1 branch
BranchTrueFalseTotal
if (@c != $num_foundations) 0 5 5
70 {
71 0 Carp::confess( "num_foundations is "
72 . scalar(@c)
73 . " rather than $num_foundations" );
74 }
75 5 for my $i ( keys @c )
76 {
77 6 my $s = $c[$i];
78 6 $self->_set_found( $i,
79 Games::Solitaire::Verify::Card->new( { string => $s } ) );
80 }
81 }
82 else
83 {
84 0 Carp::confess("Foundations str is '$foundation_str'");
85 }
86 5 return;
87 }
88  
89 sub _init
90 6 ▶{
Subroutine _init: called CC=10 SCAR=23.2
91 6 my ( $self, $args ) = @_;
92  
93 6 my $variant = $self->_variant( $args->{variant} );
94 6 ▶ if ( not exists $_VARIANTS{$variant} )
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $_VARIANTS{$variant}) 0 6 6
95 {
96 0 Carp::confess("Unknown variant '$variant'!");
97 }
98 6 my $IS_BINARY_STAR = $self->_is_binary_star;
99 6 ▶ $self->_place_queens_on_kings( $args->{queens_on_kings} // '' );
Condition100%2/2
$args->{"queens_on_kings"} // ""
l!l
24
MC/DC100%1/1
$args->{"queens_on_kings"} // ""
$args->{"queens_on_kings"}
Truth table100%2/2
A
A= $args->{"queens_on_kings"}
Aresult
11
00
100 6 ▶ $self->_wrap_ranks( $args->{wrap_ranks} // '' );
Condition100%2/2
$args->{"wrap_ranks"} // ""
l!l
33
MC/DC100%1/1
$args->{"wrap_ranks"} // ""
$args->{"wrap_ranks"}
Truth table100%2/2
A
A= $args->{"wrap_ranks"}
Aresult
11
00
101 6 ▶ my $num_foundations = ( $IS_BINARY_STAR ? 2 : 1 );
Branch: 1 branch
BranchTrueFalseTotal
$IS_BINARY_STAR ? : 1 5 6
102 6 $self->_num_foundations($num_foundations);
103 6 $self->_foundation(
104 Games::Solitaire::Verify::Freecells->new(
105 { count => $num_foundations, }
106 )
107 );
108 6 my $board_string = $args->{board_string};
109  
110 6 my @lines = split( /\n/, $board_string );
111 6 my $foundation_str = shift(@lines);
112 6 ▶ if ( $self->_variant eq 'golf' )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_variant eq 'golf') { } 3 3 6
113 {
114 3 ▶ if ( $foundation_str !~ s#\ATalon: ((?:$CARD_RE ){15}$CARD_RE)#$1# )
Branch: 1 branch
BranchTrueFalseTotal
unless ($foundation_str =~ s/\ATalon: ((?:$CARD_RE ){15}$CARD_RE)/$1/u) 0 3 3
115 {
116 0 Carp::confess("improper talon line <$foundation_str>!");
117 }
118 $self->_talon(
119 [
120 3 map { Games::Solitaire::Verify::Card->new( { string => $_ } ) }
121 split / /,
122 $foundation_str
123 ]
124 );
125  
126 3 $foundation_str = shift(@lines);
127 3 $self->_read_foundation_line($foundation_str);
128 }
129 else
130 {
131 3 $self->_talon( [] );
132 3 ▶ if ( $self->_variant eq "all_in_a_row" )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_variant eq 'all_in_a_row') { } 1 2 3
133 {
134 1 ▶ if ( $foundation_str ne "Foundations: -" )
Branch: 1 branch
BranchTrueFalseTotal
if ($foundation_str ne "Foundations: -") 0 1 1
135 {
136 0 Carp::confess("Foundations str is '$foundation_str'");
137 }
138 }
139 else
140 {
141 2 $self->_read_foundation_line($foundation_str);
142 }
143 }
144  
145 $self->_columns(
146 [
147 map {
148 6 Games::Solitaire::Verify::Column->new(
149 {
150 string => ": $_",
151 }
152 )
153 } @lines
154 ]
155 );
156 6 ▶ if ( $self->_wrap_ranks )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_wrap_ranks) 1 5 6
157 {
158 1 $self->_place_queens_on_kings(1);
159 }
160  
161 6 return;
162 }
163  
164 sub _set_found
165 345 ▶{
Subroutine _set_found: called CC=1 SCAR=0.0
166 345 my ( $self, $i, $card ) = @_;
167 345 $self->_foundation->assign( $i, $card, );
168 345 return;
169 }
170  
171 sub process_solution
172 6 ▶{
Subroutine process_solution: called CC=34 SCAR=36.5
Pod documented
173 6 my ( $self, $next_line_iter ) = @_;
174 6 my $columns = $self->_columns;
175 6 my $NUM_COLUMNS = @$columns;
176 6 my $it = Games::Solitaire::Verify::LinesIter->new(
177 { _get => $next_line_iter, } );
178 6 my $remaining_cards = sum( map { $_->len } @$columns );
179  
180 6 $it->_compare_line( "Solved!", "First line" );
181  
182 6 my $IS_BINARY_STAR = $self->_is_binary_star;
183 6 my $IS_GOLF = $self->_is_golf;
184 6 ▶ my $CHECK_EMPTY = ( $IS_GOLF or $self->_variant eq "black_hole" );
Condition100%3/3
$IS_GOLF || $self->_variant eq 'black_hole'
l!l&&r!l&&!r
312
MC/DC100%2/2
$IS_GOLF || $self->_variant eq 'black_hole'
$IS_GOLF $self->_variant eq 'black_hole'
Truth table100%3/3
A || B
A= $IS_GOLFB= $self->_variant eq 'black_hole'
ABresult
1X1
011
000
185 6 my $IS_DETAILED_MOVE = $IS_BINARY_STAR;
186 6 my $IS_DISPLAYED_BOARD = $IS_BINARY_STAR;
187 6 my $num_decks = $self->_num_foundations();
188 6 my $num_foundations = $self->_num_foundations();
189  
190 # As many moves as the number of cards.
191 MOVES:
192 6 for my $move_idx (
193 0 .. (
194 $num_decks * $MAX_RANK * $NUM_SUITS -
195 $num_foundations -
196 ( $num_foundations > 1 )
197 )
198 )
199 {
200 340 my ( $move_line, $move_line_idx ) = $it->_get_line;
201  
202 340 my $card;
203 my $col_idx;
204 340 my $foundation_idx = 0;
205 340 my $moved_card_str;
206 340 ▶ if ( $IS_GOLF
Branch: 1 branch
BranchTrueFalseTotal
if ($IS_GOLF and $move_line =~ /\ADeal talon\z/u) { } 39 301 340
Condition100%3/3
$IS_GOLF and $move_line =~ /\ADeal talon\z/u
!ll&&!rl&&r
2059639
MC/DC100%2/2
$IS_GOLF and $move_line =~ /\ADeal talon\z/u
$IS_GOLF $move_line =~ /\ADeal talon\z/u
Truth table100%3/3
A and B
A= $IS_GOLFB= $move_line =~ /\ADeal talon\z/u
ABresult
0X0
100
111
207 and $move_line =~ m/\ADeal talon\z/ )
208 {
209 39 ▶ if ( !@{ $self->_talon } )
Branch: 1 branch
BranchTrueFalseTotal
unless (@{$self->_talon;}) 0 39 39
210 {
211 0 Carp::confess("Talon is empty on line no. $move_line_idx");
212 }
213 39 $card = shift @{ $self->_talon };
214 }
215 else
216 {
217 301 ▶ if (
Branch: 2 branches
BranchTrueFalseTotal
unless ($IS_DETAILED_MOVE ? ($moved_card_str, $col_idx, $foundation_idx) = $move_line =~ /\AMove ($CARD_RE) from stack ([0-9]+) to foundations ([0-9]+)\z/u : (($col_idx) = $move_line =~ /\AMove a card from stack ([0-9]+) to the foundations\z/u)) 0 301 301
$IS_DETAILED_MOVE ? : 102 199 301
218 not(
219 $IS_DETAILED_MOVE
220 ? ( ( $moved_card_str, $col_idx, $foundation_idx ) =
221 $move_line =~
222 m/\AMove ($CARD_RE) from stack ([0-9]+) to foundations ([0-9]+)\z/
223 )
224 : ( ($col_idx) =
225 $move_line =~
226 m/\AMove a card from stack ([0-9]+) to the foundations\z/
227 )
228 )
229 )
230 {
231 0 Carp::confess(
232 "Incorrect format for move line no. $move_line_idx - '$move_line'"
233 );
234 }
235 }
236  
237 340 ▶ if ( !defined $card )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $card) 301 39 340
238 {
239 301 ▶ if ( ( $col_idx < 0 ) or ( $col_idx >= $NUM_COLUMNS ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($col_idx < 0 or $col_idx >= $NUM_COLUMNS) 0 301 301
Condition33%1/3
$col_idx < 0 or $col_idx >= $NUM_COLUMNS
l!l&&r!l&&!r
00301
MC/DC0%0/2
$col_idx < 0 or $col_idx >= $NUM_COLUMNS
$col_idx < 0 $col_idx >= $NUM_COLUMNS
Truth table33%1/3
A or B
A= $col_idx < 0B= $col_idx >= $NUM_COLUMNS
ABresult
1X1
011
000
240 {
241 0 Carp::confess(
242 "Invalid column index '$col_idx' at line no. $move_line_idx"
243 );
244 }
245 }
246  
247 340 $it->_assert_empty_line();
248 340 my ( $info_line, $info_line_idx );
249 340 ▶ if ( not $IS_DETAILED_MOVE )
Branch: 1 branch
BranchTrueFalseTotal
unless ($IS_DETAILED_MOVE) 238 102 340
250 {
251 238 ( $info_line, $info_line_idx ) = $it->_get_line;
252 238 ▶ if ( $info_line !~ m/\AInfo: Card moved is ($CARD_RE)\z/ )
Branch: 1 branch
BranchTrueFalseTotal
unless ($info_line =~ /\AInfo: Card moved is ($CARD_RE)\z/u) 0 238 238
253 {
254 0 Carp::confess(
255 "Invalid format for info line no. $info_line_idx - '$info_line'"
256 );
257 }
258  
259 238 $moved_card_str = $1;
260  
261 238 $it->_assert_empty_line();
262 238 $it->_assert_empty_line();
263  
264 238 my ( $sep_line, $sep_line_idx ) = $it->_get_line;
265  
266 238 ▶ if ( $sep_line !~ m/\A=+\z/ )
Branch: 1 branch
BranchTrueFalseTotal
unless ($sep_line =~ /\A=+\z/u) 0 238 238
267 {
268 0 Carp::confess(
269 "Invalid format for separator line no. $sep_line_idx - '$sep_line'"
270 );
271 }
272  
273 238 $it->_assert_empty_line();
274 }
275  
276 340 ▶ if ( defined $card )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $card) { } 39 301 340
277 {
278 39 my $top_card_moved_str = $card->to_string();
279 39 ▶ if ( $top_card_moved_str ne $moved_card_str )
Branch: 1 branch
BranchTrueFalseTotal
if ($top_card_moved_str ne $moved_card_str) 0 39 39
280 {
281 0 Carp::confess(
282 "Card moved should be '$top_card_moved_str', but the info says it is '$moved_card_str' at line $info_line_idx"
283 );
284 }
285 }
286 else
287 {
288 301 my $col = $columns->[$col_idx];
289 301 my $top_card = $col->top();
290 301 my $top_card_moved_str = $top_card->to_string();
291  
292 301 ▶ if ( $top_card_moved_str ne $moved_card_str )
Branch: 1 branch
BranchTrueFalseTotal
if ($top_card_moved_str ne $moved_card_str) 0 301 301
293 {
294 0 Carp::confess(
295 "Card moved should be '$top_card_moved_str', but the info says it is '$moved_card_str' at line $info_line_idx"
296 );
297 }
298  
299 301 my $found_card = $self->_foundation->cell($foundation_idx);
300 301 ▶ if ( defined($found_card) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $found_card) 300 1 301
301 {
302 300 my $found_rank = $found_card->rank();
303 300 my $src_rank = $top_card->rank();
304  
305 300 my $delta = abs( $src_rank - $found_rank );
306 300 ▶ if (
Branch: 2 branches
BranchTrueFalseTotal
if (not $delta == 1 || $delta == $MAX_RANK - 1 or $IS_GOLF and not $self->_wrap_ranks and ($self->_place_queens_on_kings ? $found_rank == $MAX_RANK : 0) || $delta != 1) 1 299 300
$self->_place_queens_on_kings ? : 0 61 61
Condition80%12/15
not $delta == 1 || $delta == $MAX_RANK - 1 or $IS_GOLF and not $self->_wrap_ranks and ($self->_place_queens_on_kings ? $found_rank == $MAX_RANK : 0) || $delta != 1
l!l&&r!l&&!r
01299
$delta == 1 || $delta == $MAX_RANK - 1
l!l&&r!l&&!r
283170
$IS_GOLF and not $self->_wrap_ranks and ($self->_place_queens_on_kings ? $found_rank == $MAX_RANK : 0) || $delta != 1
!ll&&!rl&&r
239601
$IS_GOLF and not $self->_wrap_ranks
!ll&&!rl&&r
2043561
$self->_place_queens_on_kings ? $found_rank == $MAX_RANK : 0 or $delta != 1
l!l&&r!l&&!r
0160
MC/DC33%2/6
not $delta == 1 || $delta == $MAX_RANK - 1 or $IS_GOLF and not $self->_wrap_ranks and ($self->_place_queens_on_kings ? $found_rank == $MAX_RANK : 0) || $delta != 1
$delta == 1 $delta == $MAX_RANK - 1 $IS_GOLF not $self->_wrap_ranks $self->_place_queens_on_kings ? $found_rank == $MAX_RANK : 0 $delta != 1
Truth table54%6/11
not(A || B) or C and not(D) and E or F
A= $delta == 1B= $delta == $MAX_RANK - 1C= $IS_GOLFD= not $self->_wrap_ranksE= $self->_place_queens_on_kings ? $found_rank == $MAX_RANK : 0F= $delta != 1
ABCDEFresult
00XXXX1
1X111X1
1X11011
01111X1
0111011
1X0XXX0
1X10XX0
1X11000
010XXX0
0110XX0
0111000
307 not( $delta == 1 or $delta == ( $MAX_RANK - 1 ) )
308 or (
309 $IS_GOLF
310 and ( !$self->_wrap_ranks )
311 and (
312 (
313 $self->_place_queens_on_kings
314 ? ( $found_rank == $MAX_RANK )
315 : 0
316 )
317 or $delta != 1
318 )
319 )
320 )
321 {
322 1 Carp::confess(
323 "Cannot put $top_card_moved_str in the foundations that contain "
324 . $found_card->to_string() );
325 }
326 299 ▶ if ($IS_DISPLAYED_BOARD)
Branch: 1 branch
BranchTrueFalseTotal
if ($IS_DISPLAYED_BOARD) 102 197 299
327 {
328 102 my $wanted_line = $self->_foundation->to_string();
329 102 ▶ $wanted_line =~ s#\AFreecells:#Foundations:#
Branch: 1 branch
BranchTrueFalseTotal
$wanted_line =~ s/\AFreecells:/Foundations:/u or Carp::confess("Unimpl!") 102 0 102
330 or Carp::confess("Unimpl!");
331 102 $wanted_line =~ s# # #g;
332 102 my $fstr = $found_card->to_string();
333 102 my $tstr = $top_card->to_string();
334 102 ▶ $wanted_line =~
Branch: 1 branch
BranchTrueFalseTotal
$wanted_line =~ s/\AFoundations:(?: $CARD_RE){$foundation_idx} \K(\Q$fstr\E)/my $c = $1; "[ $c -> $tstr ]";/eu or Carp::confess("Failed substitute! foundation_idx=$foundation_idx wanted_line=$wanted_line fstr='${fstr}'") 102 0 102
335 102 s#\AFoundations:(?: $CARD_RE){$foundation_idx} \K(\Q$fstr\E)#my$c=$1;"[ $c -> $tstr ]"#e
336 or Carp::confess(
337 "Failed substitute! foundation_idx=$foundation_idx wanted_line=$wanted_line fstr='$fstr'"
338 );
339 102 $it->_compare_line( $wanted_line, "Foundations" );
340 102 for my $i ( keys @$columns )
341 {
342 1734 my $col = $columns->[$i];
343 1734 my $wanted_line = $col->to_string();
344 1734 ▶ if ( $i == $col_idx )
Branch: 1 branch
BranchTrueFalseTotal
if ($i == $col_idx) 102 1632 1734
345 {
346 102 ▶ $wanted_line =~
Branch: 1 branch
BranchTrueFalseTotal
$wanted_line =~ s/ \K(\Q$tstr\E)\z/my $c = $1; "[ $c -> ]";/eu or Carp::confess("Failed column substitute! foundation_idx=$foundation_idx wanted_line=$wanted_line tstr='${tstr}'") 102 0 102
347 102 s# \K(\Q$tstr\E)\z#my$c=$1;"[ $c -> ]"#e
348 or Carp::confess(
349 "Failed column substitute! foundation_idx=$foundation_idx wanted_line=$wanted_line tstr='$tstr'"
350 );
351 }
352 1734 $it->_compare_line( $wanted_line, "Column $i" );
353 }
354 102 $it->_assert_empty_line();
355 }
356 }
357 300 $card = $col->pop;
358 300 --$remaining_cards;
359 }
360 339 ▶ if ( not defined $foundation_idx )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $foundation_idx) 0 339 339
361 {
362 0 Carp::confess("\$foundation_idx not set");
363 }
364 339 $self->_set_found( $foundation_idx, $card, );
365 339 ▶ if ($CHECK_EMPTY)
Branch: 1 branch
BranchTrueFalseTotal
if ($CHECK_EMPTY) 185 154 339
366 {
367 185 ▶ if ( $remaining_cards == 0 )
Branch: 1 branch
BranchTrueFalseTotal
if ($remaining_cards == 0) 3 182 185
368 {
369 3 last MOVES;
370 }
371 }
372 }
373 5 return;
374 }
375  
376 3 1;
377  
378 __END__
↑ Summary Verify/VariantParams.pm »