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

filter: Statementstmt 95.6% 66 / 69 Branchbran 83.3% 15 / 18 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 17 / 17 Podpod 100% 8 / 8 totaltotal 94.6% 106 / 112 CC 13 SCAR 9.0
CC
13
Cov
93%
_input_from_string
5.4
_init
3.0
num_empty
3.0
CRAP 2.5
« blib/lib/Games/Solitaire/Verify/State.pm ↑ Summary blib/lib/Games/Solitaire/Verify/Card.pm »
1 package Games::Solitaire::Verify::Freecells;
2 19 $Games::Solitaire::Verify::Freecells::VERSION = '0.2301';
3 19 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 19 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6  
7 19 ▶use parent 'Games::Solitaire::Verify::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 19 ▶use Games::Solitaire::Verify::Exception ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 19 ▶use Games::Solitaire::Verify::Card ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 19 ▶use List::Util qw(first);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 # _s is the string.
15 19 __PACKAGE__->mk_acc_ref(
16 [
17 qw(
18 _count
19 _cells
20 _s
21 )
22 ]
23 );
24  
25  
26 sub _input_from_string
27 {
28 270 ▶ my $self = shift;
Subroutine _input_from_string: called CC=5 SCAR=16.8
29 270 my $str = shift;
30  
31 270 ▶ if ( $str !~ m{\AFreecells:}gms )
Branch: 1 branch
BranchTrueFalseTotal
unless ($str =~ /\AFreecells:/gms) 0 270 270
32 {
33 0 Games::Solitaire::Verify::Exception::Parse::State::Freecells->throw(
34 error => "Wrong Freecell String", );
35 }
36  
37 POS:
38 270 for my $pos ( 0 .. ( $self->count() - 1 ) )
39 {
40 930 ▶ if ( $str =~ m{\G\z}cg )
Branch: 2 branches
BranchTrueFalseTotal
if ($str =~ /\G\z/cg) { } 129 801 930
elsif ($str =~ /\G (..)/gms) { } 801 0 801
41 {
42 129 last POS;
43 }
44 elsif ( $str =~ m{\G (..)}gms )
45 {
46 801 my $card_s = $1;
47 801 $self->assign( $pos, $self->_parse_freecell_card($card_s) );
48 }
49 else
50 {
51 0 Games::Solitaire::Verify::Exception::Parse::State::Freecells
52 ->throw( error => "Wrong Freecell String", );
53 }
54 }
55 }
56  
57 sub _init
58 {
59 284 ▶ my ( $self, $args ) = @_;
Subroutine _init: called CC=3 SCAR=11.1
60  
61 284 ▶ if ( !exists( $args->{count} ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $args->{"count"}) 0 284 284
62 {
63 0 die "The count was not specified for the freecells";
64 }
65  
66 284 $self->_count( $args->{count} );
67  
68 284 $self->_s( 'Freecells:' . ( ' ' x $self->_count ) );
69  
70 284 $self->_cells( [ (undef) x $self->_count() ] );
71  
72 284 ▶ if ( exists( $args->{string} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args->{"string"}) 270 14 284
73 {
74 270 return $self->_input_from_string( $args->{string} );
75 }
76  
77 14 return;
78 }
79  
80 sub _parse_freecell_card
81 {
82 801 ▶ my ( $self, $s ) = @_;
Subroutine _parse_freecell_card: called CC=2 SCAR=6.9
83  
84 return (
85 801 ▶ ( $s eq q{ } )
Branch: 1 branch
BranchTrueFalseTotal
$s eq ' ' ? : 174 627 801
86 ? undef()
87 : Games::Solitaire::Verify::Card->new(
88 {
89 string => $s,
90 }
91 )
92 );
93 }
94  
95  
96 sub count
97 {
98 735 ▶ my $self = shift;
Subroutine count: called CC=1 SCAR=0.0
Pod documented
99  
100 735 return $self->_count();
101 }
102  
103  
104 sub cell
105 {
106 3450 ▶ my ( $self, $idx ) = @_;
Subroutine cell: called CC=1 SCAR=0.0
Pod documented
107  
108 3450 return $self->_cells()->[$idx];
109 }
110  
111  
112 sub assign
113 {
114 2192 ▶ my ( $self, $idx, $card ) = @_;
Subroutine assign: called CC=2 SCAR=6.9
Pod documented
115  
116 2192 $self->_cells()->[$idx] = $card;
117 substr(
118 2192 ▶ $self->{_s}, length('Freecells:') + ( $idx << 2 ) + 2,
Branch: 1 branch
BranchTrueFalseTotal
defined $card ? : 1442 750 2192
119 2, ( defined($card) ? $card->fast_s : ' ' )
120 );
121  
122 2192 return;
123 }
124  
125  
126 sub to_string
127 {
128 2055 ▶ ( my $r = $_[0]->{_s} ) =~ s# +\z##;
Subroutine to_string: called CC=1 SCAR=0.0
Pod documented
129 2055 return $r;
130 }
131  
132  
133 sub cell_clone
134 {
135 26 ▶ my ( $self, $pos ) = @_;
Subroutine cell_clone: called CC=2 SCAR=6.9
Pod documented
136  
137 26 my $card = $self->cell($pos);
138  
139 26 ▶ return defined($card) ? $card->clone() : undef();
Branch: 1 branch
BranchTrueFalseTotal
defined $card ? : 6 20 26
140 }
141  
142  
143 sub clear
144 {
145 556 ▶ my ( $self, $pos ) = @_;
Subroutine clear: called CC=1 SCAR=0.0
Pod documented
146  
147 556 $self->assign( $pos, undef() );
148  
149 556 return;
150 }
151  
152  
153 sub clone
154 {
155 7 ▶ my $self = shift;
Subroutine clone: called CC=2 SCAR=6.9
Pod documented
156  
157 7 my $copy = __PACKAGE__->new(
158 {
159 count => $self->count(),
160 }
161 );
162  
163 7 foreach my $pos ( 0 .. ( $self->count() - 1 ) )
164 {
165 26 $copy->assign( $pos, $self->cell_clone($pos) );
166 }
167  
168 7 return $copy;
169 }
170  
171  
172 sub num_empty
173 {
174 451 ▶ my $self = shift;
Subroutine num_empty: called CC=3 SCAR=11.0
Pod documented
175  
176 451 my $count = 0;
177  
178 451 foreach my $fc_idx ( 0 .. ( $self->count() - 1 ) )
179 {
180 1540 ▶ if ( !defined( $self->cell($fc_idx) ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->cell($fc_idx)) 181 1359 1540
181 {
182 181 ++$count;
183 }
184 }
185 451 return $count;
186 }
187  
188 19 1; # End of Games::Solitaire::Verify::Freecells
189  
190 __END__
« blib/lib/Games/Solitaire/Verify/State.pm ↑ Summary blib/lib/Games/Solitaire/Verify/Card.pm »