blib/lib/Games/Solitaire/Verify/Golf.pm

filter: Statementstmt 90.5% 124 / 137 Branchbran 70.8% 34 / 48 Conditioncond 82.1% 23 / 28 MC/DCmcdc 42.8% 6 / 14 Subroutinesub 100% 16 / 16 Podpod 100% 1 / 1 totaltotal 83.6% 204 / 244 CC 36 SCAR 28.8
CC
36
Cov
85%
process_solution
30.8
_init
9.3
__ANON__
2.1
CRAP 17.7
↑ Summary blib/lib/Games/Solitaire/Verify/VariantParams.pm »
1 package Games::Solitaire::Verify::Golf;
2 3 $Games::Solitaire::Verify::Golf::VERSION = '0.2301';
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 autodie;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7  
8 3 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use List::Util qw/ sum /;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 3 ▶use Games::Solitaire::Verify::Card ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use Games::Solitaire::Verify::Column ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use Games::Solitaire::Verify::Freecells ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 3 ▶use parent 'Games::Solitaire::Verify::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 3 __PACKAGE__->mk_acc_ref(
18 [
19 qw(
20 _columns
21 _foundation
22 _place_queens_on_kings
23 _talon
24 _variant
25 _wrap_ranks
26 )
27 ]
28 );
29  
30 3 my $MAX_RANK = 13;
31 3 my $NUM_SUITS = 4;
32  
33 sub _is_golf
34 {
35 5 ▶ my $self = shift;
Subroutine _is_golf: called CC=1 SCAR=0.0
36  
37 5 return $self->_variant eq 'golf';
38 }
39  
40 sub _init
41 {
42 5 ▶ my ( $self, $args ) = @_;
Subroutine _init: called CC=9 SCAR=22.3
43  
44 5 my $variant = $self->_variant( $args->{variant} );
45 5 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
unless (exists {"golf", 1, "all_in_a_row", 1, "black_hole", 1}->{$variant}) 0 5 5
46 not
47 exists { golf => 1, all_in_a_row => 1, black_hole => 1, }->{$variant} )
48 {
49 0 Carp::confess("Unknown variant '$variant'!");
50 }
51 5 ▶ $self->_place_queens_on_kings( $args->{queens_on_kings} // '' );
Condition100%2/2
$args->{"queens_on_kings"} // ""
l!l
23
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
52 5 ▶ $self->_wrap_ranks( $args->{wrap_ranks} // '' );
Condition100%2/2
$args->{"wrap_ranks"} // ""
l!l
32
MC/DC100%1/1
$args->{"wrap_ranks"} // ""
$args->{"wrap_ranks"}
Truth table100%2/2
A
A= $args->{"wrap_ranks"}
Aresult
11
00
53 5 $self->_foundation(
54 Games::Solitaire::Verify::Freecells->new( { count => 1 } ) );
55 5 my $board_string = $args->{board_string};
56  
57 5 my @lines = split( /\n/, $board_string );
58  
59 my $_set_found_line = sub {
60 4 ▶ my $foundation_str = shift;
Subroutine __ANON__: called CC=2 SCAR=7.4
61 4 ▶ if ( my ($card_s) = $foundation_str =~ m#\AFoundations: (\S{2})\z# )
Branch: 1 branch
BranchTrueFalseTotal
if (my($card_s) = $foundation_str =~ /\AFoundations: (\S{2})\z/) { } 4 0 4
62 {
63 4 $self->_set_found(
64 Games::Solitaire::Verify::Card->new( { string => $card_s } ) );
65 }
66 else
67 {
68 0 Carp::confess("Foundations str is '$foundation_str'");
69 }
70 4 return;
71 5 };
72 5 my $foundation_str = shift(@lines);
73 5 ▶ if ( $self->_variant eq 'golf' )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_variant eq 'golf') { } 3 2 5
74 {
75 3 ▶ if ( $foundation_str !~ s#\ATalon: ((?:\S{2} ){15}\S{2})#$1# )
Branch: 1 branch
BranchTrueFalseTotal
unless ($foundation_str =~ s/\ATalon: ((?:\S{2} ){15}\S{2})/$1/) 0 3 3
76 {
77 0 die "improper talon line <$foundation_str>!";
78 }
79 $self->_talon(
80 [
81 3 map { Games::Solitaire::Verify::Card->new( { string => $_ } ) }
82 split / /,
83 $foundation_str
84 ]
85 );
86  
87 3 $foundation_str = shift(@lines);
88 3 $_set_found_line->($foundation_str);
89  
90 }
91 else
92 {
93 2 $self->_talon( [] );
94 2 ▶ if ( $self->_variant eq "all_in_a_row" )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_variant eq 'all_in_a_row') { } 1 1 2
95 {
96 1 ▶ if ( $foundation_str ne "Foundations: -" )
Branch: 1 branch
BranchTrueFalseTotal
if ($foundation_str ne "Foundations: -") 0 1 1
97 {
98 0 Carp::confess("Foundations str is '$foundation_str'");
99 }
100 }
101 else
102 {
103 1 $_set_found_line->($foundation_str);
104 }
105 }
106  
107 $self->_columns(
108 [
109 map {
110 5 Games::Solitaire::Verify::Column->new(
111 {
112 string => ": $_",
113 }
114 )
115 } @lines
116 ]
117 );
118 5 ▶ if ( $self->_wrap_ranks )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_wrap_ranks) 1 4 5
119 {
120 1 $self->_place_queens_on_kings(1);
121 }
122  
123 5 return;
124 }
125  
126 sub _set_found
127 {
128 241 ▶ my ( $self, $card ) = @_;
Subroutine _set_found: called CC=1 SCAR=0.0
129 241 $self->_foundation->assign( 0, $card );
130 241 return;
131 }
132  
133 sub process_solution
134 {
135 5 ▶ my ( $self, $next_line_iter ) = @_;
Subroutine process_solution: called CC=26 SCAR=34.3
Pod documented
136 5 my $columns = $self->_columns;
137 5 my $NUM_COLUMNS = @$columns;
138 5 my $line_num = 0;
139 5 my $remaining_cards = sum( map { $_->len } @$columns );
140  
141 my $get_line = sub {
142 1671 ▶ my $ret = $next_line_iter->();
Subroutine __ANON__: called CC=1 SCAR=0.0
143 1671 return ( $ret, ++$line_num );
144 5 };
145  
146 my $assert_empty_line = sub {
147 952 ▶ my ( $s, $line_idx ) = $get_line->();
Subroutine __ANON__: called CC=2 SCAR=7.6
148  
149 952 ▶ if ( $s ne '' )
Branch: 1 branch
BranchTrueFalseTotal
if ($s ne "") 0 952 952
150 {
151 0 die "Line '$line_idx' is not empty, but '$s'";
152 }
153  
154 952 return;
155 5 };
156  
157 5 my ( $l, $first_l ) = $get_line->();
158  
159 5 ▶ if ( $l ne "Solved!" )
Branch: 1 branch
BranchTrueFalseTotal
if ($l ne "Solved!") 0 5 5
160 {
161 0 die "First line is '$l' instead of 'Solved!'";
162 }
163 5 my $IS_GOLF = $self->_is_golf;
164 5 ▶ 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
311
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
165  
166 # As many moves as the number of cards.
167 MOVES:
168 5 for my $move_idx ( 0 .. ( $MAX_RANK * $NUM_SUITS - 1 ) )
169 {
170 238 my ( $move_line, $move_line_idx ) = $get_line->();
171  
172 238 my $card;
173 238 ▶ if ( $IS_GOLF
Branch: 2 branches
BranchTrueFalseTotal
if ($IS_GOLF and $move_line =~ /\ADeal talon\z/) { } 39 199 238
elsif (not $move_line =~ /\AMove a card from stack ([0-9]+) to the foundations\z/) { } 0 199 199
Condition100%3/3
$IS_GOLF and $move_line =~ /\ADeal talon\z/
!ll&&!rl&&r
1039639
MC/DC100%2/2
$IS_GOLF and $move_line =~ /\ADeal talon\z/
$IS_GOLF $move_line =~ /\ADeal talon\z/
Truth table100%3/3
A and B
A= $IS_GOLFB= $move_line =~ /\ADeal talon\z/
ABresult
0X0
100
111
174 and $move_line =~ m/\ADeal talon\z/ )
175 {
176 39 ▶ if ( !@{ $self->_talon } )
Branch: 1 branch
BranchTrueFalseTotal
unless (@{$self->_talon;}) 0 39 39
177 {
178 0 die "Talon is empty on line no. $move_line_idx";
179 }
180 39 $card = shift @{ $self->_talon };
181 }
182 elsif ( $move_line !~
183 m/\AMove a card from stack ([0-9]+) to the foundations\z/ )
184 {
185 0 die
186 "Incorrect format for move line no. $move_line_idx - '$move_line'";
187 }
188  
189 238 my $col_idx = $1;
190  
191 238 ▶ if ( !defined $card )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $card) 199 39 238
192 {
193 199 ▶ if ( ( $col_idx < 0 ) or ( $col_idx >= $NUM_COLUMNS ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($col_idx < 0 or $col_idx >= $NUM_COLUMNS) 0 199 199
Condition33%1/3
$col_idx < 0 or $col_idx >= $NUM_COLUMNS
l!l&&r!l&&!r
00199
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
194 {
195 0 die "Invalid column index '$col_idx' at $move_line_idx";
196 }
197 }
198  
199 238 $assert_empty_line->();
200  
201 238 my ( $info_line, $info_line_idx ) = $get_line->();
202  
203 238 ▶ if ( $info_line !~ m/\AInfo: Card moved is ([A23456789TJQK][HCDS])\z/ )
Branch: 1 branch
BranchTrueFalseTotal
unless ($info_line =~ /\AInfo: Card moved is ([A23456789TJQK][HCDS])\z/) 0 238 238
204 {
205 0 die
206 "Invalid format for info line no. $info_line_idx - '$info_line'";
207 }
208  
209 238 my $moved_card_str = $1;
210  
211 238 $assert_empty_line->();
212 238 $assert_empty_line->();
213  
214 238 my ( $sep_line, $sep_line_idx ) = $get_line->();
215  
216 238 ▶ if ( $sep_line !~ m/\A=+\z/ )
Branch: 1 branch
BranchTrueFalseTotal
unless ($sep_line =~ /\A=+\z/) 0 238 238
217 {
218 0 die
219 "Invalid format for separator line no. $sep_line_idx - '$sep_line'";
220 }
221  
222 238 $assert_empty_line->();
223  
224 238 ▶ if ( defined $card )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $card) { } 39 199 238
225 {
226 39 my $top_card_moved_str = $card->to_string();
227 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
228 {
229 0 die
230 "Card moved should be '$top_card_moved_str', but the info says it is '$moved_card_str' at line $info_line_idx";
231 }
232 }
233 else
234 {
235 199 my $col = $columns->[$col_idx];
236 199 my $top_card = $col->top();
237 199 my $top_card_moved_str = $top_card->to_string();
238  
239 199 ▶ if ( $top_card_moved_str ne $moved_card_str )
Branch: 1 branch
BranchTrueFalseTotal
if ($top_card_moved_str ne $moved_card_str) 0 199 199
240 {
241 0 die
242 "Card moved should be '$top_card_moved_str', but the info says it is '$moved_card_str' at line $info_line_idx";
243 }
244  
245 199 my $found_card = $self->_foundation->cell(0);
246 199 ▶ if ( defined($found_card) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $found_card) 198 1 199
247 {
248 198 my $found_rank = $found_card->rank();
249 198 my $src_rank = $top_card->rank();
250  
251 198 my $delta = abs( $src_rank - $found_rank );
252 198 ▶ 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 197 198
$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
01197
$delta == 1 || $delta == $MAX_RANK - 1
l!l&&r!l&&!r
188100
$IS_GOLF and not $self->_wrap_ranks and ($self->_place_queens_on_kings ? $found_rank == $MAX_RANK : 0) || $delta != 1
!ll&&!rl&&r
137601
$IS_GOLF and not $self->_wrap_ranks
!ll&&!rl&&r
1023561
$self->_place_queens_on_kings ? $found_rank == $MAX_RANK : 0 or $delta != 1
l!l&&r!l&&!r
0160
MC/DC0%0/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 table18%2/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
253 not( $delta == 1 or $delta == ( $MAX_RANK - 1 ) )
254 or (
255 $IS_GOLF
256 and ( !$self->_wrap_ranks )
257 and (
258 (
259 $self->_place_queens_on_kings
260 ? ( $found_rank == $MAX_RANK )
261 : 0
262 )
263 or $delta != 1
264 )
265 )
266 )
267 {
268 1 die
269 "Cannot put $top_card_moved_str in the foundations that contain "
270 . $found_card->to_string();
271 }
272 }
273 198 $card = $col->pop;
274 198 --$remaining_cards;
275 }
276  
277 237 $self->_set_found($card);
278 237 ▶ if ($CHECK_EMPTY)
Branch: 1 branch
BranchTrueFalseTotal
if ($CHECK_EMPTY) 185 52 237
279 {
280 185 ▶ if ( $remaining_cards == 0 )
Branch: 1 branch
BranchTrueFalseTotal
if ($remaining_cards == 0) 3 182 185
281 {
282 3 last MOVES;
283 }
284 }
285 }
286 4 return;
287 }
288  
289 3 1;
290  
291 __END__
↑ Summary blib/lib/Games/Solitaire/Verify/VariantParams.pm »