blib/lib/Dreamhack/Solitaire.pm

filter: Statementstmt 16.5% 18 / 109 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 45.4% 5 / 11 Podpod 0.0% 0 / 6 totaltotal 12.9% 23 / 178 CC 26 SCAR 42.3
CC
26
Cov
12%
new
132.0
init_layout
90.0
format
30.0
CRAP 68.5
↑ Summary
1 package Dreamhack::Solitaire;
2 1 ▶use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use List::Util qw(shuffle);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use List::MoreUtils 0.413 qw(uniq singleton);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 our $VERSION = "0.01";
10  
11 1 our @suits = ('s','c','d','h');
12 1 our @valence = ('A','K','Q','J','10','9','8','7','6');
13  
14 0 ▶sub new {
Subroutine new: not called CC=11 SCAR=48.8
Pod undocumented
15 0 my $class = shift;
16 0 my %args = @_;
17 0 my @check = ();
18 0 my @errors = ();
19  
20 0 ▶ if (exists $args{'suits'} && (ref $args{'suits'} eq 'ARRAY')) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $args{'suits'} and ref $args{'suits'} eq 'ARRAY') { } 0 0 0
elsif (exists $args{'lang'}) { } 0 0 0
Condition0%0/3
exists $args{"suits"} and ref $args{"suits"} eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
exists $args{"suits"} and ref $args{"suits"} eq "ARRAY"
exists $args{"suits"} ref $args{"suits"} eq "ARRAY"
Truth table0%0/3
A and B
A= exists $args{"suits"}B= ref $args{"suits"} eq "ARRAY"
ABresult
0X0
100
111
21 0 @suits = @{$args{'suits'}}
22 }
23 elsif (exists $args{'lang'}) {
24 0 ▶ if (lc($args{'lang'}) eq 'ru_ru.utf8') {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $args{"lang"} eq "ru_ru.utf8") 0 0 0
25 0 @suits = ('п','к','б','ч');
26 0 @valence = ('Т','К','Д','В','10','9','8','7','6');
27 }
28 }
29  
30 0 @check = uniq @suits;
31 0 ▶ unless ($#check == $#suits) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($#check == $#Dreamhack::Solitaire::suits) 0 0 0
32 0 die 'Duplicate suits';
33 }
34  
35 0 ▶ if (exists $args{'valence'} && (ref $args{'valence'} eq 'ARRAY')) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"valence"} and ref $args{"valence"} eq "ARRAY") 0 0 0
Condition0%0/3
exists $args{"valence"} and ref $args{"valence"} eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
exists $args{"valence"} and ref $args{"valence"} eq "ARRAY"
exists $args{"valence"} ref $args{"valence"} eq "ARRAY"
Truth table0%0/3
A and B
A= exists $args{"valence"}B= ref $args{"valence"} eq "ARRAY"
ABresult
0X0
100
111
36 0 @valence = @{$args{'valence'}}
37 }
38  
39 0 @check = uniq @valence;
40 0 ▶ unless ($#check == $#valence) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($#check == $#Dreamhack::Solitaire::valence) 0 0 0
41 0 die 'Duplicate valence';
42 }
43  
44 0 my @deck = ();
45 0 my %complex = ();
46  
47 0 for my $suit (@suits) {
48 0 for my $valence (@valence) {
49 0 push @deck, $valence.$suit;
50 0 $complex{$valence.$suit} = [$valence,$suit];
51 }
52 }
53  
54 0 my $self = {
55 suits => \@suits,
56 valence => \@valence,
57 deck => \@deck,
58 layout => [],
59 leftcards => \@deck,
60 complex => \%complex,
61 convolution => [],
62 attempts => undef,
63 errors => \@errors,
64 };
65  
66 0 bless $self, $class;
67 0 return $self
68 }
69  
70 0 ▶sub init_layout {
Subroutine init_layout: not called CC=9 SCAR=45.0
Pod undocumented
71 0 my ($self, $layout) = @_;
72  
73 0 ▶ unless ($layout && (ref $layout eq 'ARRAY')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($layout and ref $layout eq "ARRAY") 0 0 0
Condition0%0/3
$layout and ref $layout eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
$layout and ref $layout eq "ARRAY"
$layout ref $layout eq "ARRAY"
Truth table0%0/3
A and B
A= $layoutB= ref $layout eq "ARRAY"
ABresult
0X0
100
111
74 0 die 'Not an arrayref for layout';
75 }
76  
77 0 ▶ if ($#$layout > $#{$self->{deck}}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($layout->$#* > $#{$$self{"deck"};}) 0 0 0
78 0 die 'Initial layout too long: ' . (1 + $#$layout) . ' cards';
79 }
80  
81 0 my @cards = ();
82 0 map {s/\?//} @$layout;
83 0 for (@$layout) {
84 0 ▶ if ($_) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_) 0 0 0
85 0 push @cards, $_;
86 }
87 }
88  
89 0 my @test = uniq @cards;
90 0 ▶ unless ($#test == $#cards) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($#test == $#cards) 0 0 0
91 0 die 'Duplicate cards in layout';
92 }
93  
94 0 for my $card (@cards) {
95 0 ▶ unless (grep {$card eq $_} @{$self->{'deck'}}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$card eq $_;} @{$$self{"deck"};}) 0 0 0
96 0 die "Bad card in layout: $card";
97 }
98 }
99  
100 0 $self->{'layout'} = $layout;
101  
102 0 my @diff = singleton(@{$self->{'deck'}}, @{$self->{'layout'}});
103 0 $self->{'leftcards'} = \@diff;
104  
105 0 return $self
106 }
107  
108 0 ▶sub parse_init_string {
Subroutine parse_init_string: not called CC=2 SCAR=17.9
Pod undocumented
109 0 my ($self, $string) = @_;
110 0 my @layout = split /[\[\]\s\n]+/, $string;
111 0 @layout = grep {$_} @layout;
112 0 ▶ return wantarray ? @layout : \@layout;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
113 }
114  
115 0 ▶sub extract {
Subroutine extract: not called CC=1 SCAR=6.9
Pod undocumented
116 0 my ($self, $str) = @_;
117 0 my ($valence, $suit) = @{${$self->{'complex'}}{$str}};
118 0 return ($valence, $suit)
119 }
120  
121 0 ▶sub add_rnd_layout {
Subroutine add_rnd_layout: not called CC=3 SCAR=24.8
Pod undocumented
122 0 my ($self, ) = @_;
123 0 my @cards = shuffle @{$self->{'leftcards'}};
124  
125 0 my @layout = ();
126 0 for my $card (@{$self->{'layout'}}) {
127 0 ▶ if ($card) {
Branch: 1 branch
BranchTrueFalseTotal
if ($card) { } 0 0 0
128 0 push @layout, $card;
129 }
130 else {
131 0 push @layout, shift @cards;
132 }
133 }
134 0 @layout = (@layout, @cards);
135  
136 return @layout
137 0 }
138  
139 0 ▶sub format {
Subroutine format: not called CC=5 SCAR=34.0
Pod undocumented
140 0 my ($self, ) = @_;
141 0 my $string = '[';
142 0 my @layout = @{$self->{'layout'}};
143 0 my @convolution = @{$self->{'convolution'}};
144  
145 0 my $index = 0;
146 0 my $convolution = shift @convolution;
147 0 ▶ while (my $card = shift @layout) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $card = shift @layout) 0 0 0
148 0 $string .= $card;
149 0 ▶ if ((defined $convolution) && ($convolution == $index++)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $convolution and $convolution == $index++) { } 0 0 0
Condition0%0/3
defined $convolution and $convolution == $index++
!ll&&!rl&&r
000
MC/DC0%0/2
defined $convolution and $convolution == $index++
defined $convolution $convolution == $index++
Truth table0%0/3
A and B
A= defined $convolutionB= $convolution == $index++
ABresult
0X0
100
111
150 0 $string .= "]\n";
151 0 $convolution = shift @convolution;
152 0 ▶ $string .= '[' if $convolution;
Branch: 1 branch
BranchTrueFalseTotal
if $convolution 0 0 0
153 }
154 else {
155 0 $string .= " ";
156 }
157 }
158 0 return $string
159 }
160  
161 1 1;
162 __END__
↑ Summary