Cribbage/Score.pm

filter: Statementstmt 100% 114 / 114 Branchbran 94.0% 47 / 50 Conditioncond 100% 6 / 6 MC/DCmcdc 100% 4 / 4 Subroutinesub 100% 14 / 14 Podpod 85.7% 6 / 7 totaltotal 97.9% 191 / 195 CC 34 SCAR 16.3
CC
34
Cov
98%
calculate_of_a_kind
7.0
calculate_flush
7.0
__ANON__
7.0
CRAP 5.1
« Cribbage/Player/Hand.pm ↑ Summary Cribbage/Hands.pm »
1 package Game::Cribbage::Score;
2  
3 14 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 14 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 14 ▶use Object::Proto::Sugar -types;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 14 ▶use ntheory qw/forcomb vecsum/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 has total_score => (
11 is => 'rw',
12 isa => Int
13 14 );
14  
15 has scored => (
16 is => 'ro',
17 builder => 1
18 14 );
19  
20 57 ▶sub _build_scored {
Subroutine _build_scored: called CC=1 SCAR=0.0
21 return {
22 57 fifteen => 2,
23 pair => 2,
24 three_of_a_kind => 6,
25 four_of_a_kind => 12,
26 run => [3, 4, 5],
27 four_flush => 4,
28 five_flush => 5,
29 nobs => 1
30 }
31 }
32  
33 has [qw/fifteen pair three_of_a_kind four_of_a_kind run four_flush five_flush nobs cards/] => (
34 is => 'rw',
35 isa => ArrayRef,
36 default => []
37 14 );
38  
39 has with_starter => (
40 is => 'ro',
41 isa => Bool
42 14 );
43  
44 has crib => (
45 is => 'ro',
46 isa => Bool
47 14 );
48  
49 57 ▶sub BUILD {
Subroutine BUILD: called CC=2 SCAR=6.9
Pod undocumented
50 57 my ($self, $params) = @_;
51 57 my $starter = $self->cards->[-1];
52 57 my @cards = sort { $b->value <=> $a->value } @{$self->cards};
53 57 $self->calculate_fifteen(@cards);
54 57 $self->calculate_of_a_kind(@cards);
55 57 $self->calculate_run(@cards);
56 57 $self->calculate_flush(@cards);
57 57 ▶ $self->calculate_nob($starter, @cards) if $self->with_starter;
Branch: 1 branch
BranchTrueFalseTotal
if $self->with_starter 24 33 57
58 57 $self->calculate_total();
59 57 return $self;
60 }
61  
62 24 ▶sub calculate_nob {
Subroutine calculate_nob: called CC=5 SCAR=16.1
Pod documented
63 24 my ($self, $starter, @cards) = @_;
64  
65 24 ▶ return if ($starter->symbol eq 'J');
Branch: 1 branch
BranchTrueFalseTotal
if $starter->symbol eq "J" 1 23 24
66  
67 23 for (@cards) {
68 91 ▶ if ($_->symbol eq 'J' && $starter->suit eq $_->suit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_->symbol eq "J" and $starter->suit eq $_->suit) 6 85 91
Condition100%3/3
$_->symbol eq "J" and $starter->suit eq $_->suit
!ll&&!rl&&r
8416
MC/DC100%2/2
$_->symbol eq "J" and $starter->suit eq $_->suit
$_->symbol eq "J" $starter->suit eq $_->suit
Truth table100%3/3
A and B
A= $_->symbol eq "J"B= $starter->suit eq $_->suit
ABresult
0X0
100
111
69 6 push @{$self->nobs}, $_;
70 6 last;
71 }
72 }
73 };
74  
75 57 ▶sub calculate_run {
Subroutine calculate_run: called CC=5 SCAR=16.1
Pod documented
76 57 my ($self, @cards) = @_;
77  
78 # By run value, not pip value: J, Q and K are all worth ten, so the
79 # caller's sort leaves them in whatever order they arrived, and a run
80 # is only found when each card is one below the last.
81 57 @cards = sort { $b->run_value <=> $a->run_value } @cards;
82 57 my @values = map { $_->run_value } @cards;
83  
84 57 my %map;
85 57 foreach my $n (1 .. @values) {
86 1239 ▶ forcomb {
Subroutine __ANON__: called CC=7 SCAR=19.5
87 1239 my $first = $values[$_[0]];
88 1239 my $match = 1;
89 1239 ▶ return if scalar @_ < 3;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ < 3 690 549 1239
90 549 ▶ for (my $i = 1; $i < scalar(@_); $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @_) 1299 549 1848
91 1299 $first = $first - 1;
92 1299 ▶ if ($first != $values[$_[$i]]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($first != $values[$_[$i]]) 1188 111 1299
93 1188 $match = 0;
94 }
95 }
96 549 ▶ if ($match) {
Branch: 1 branch
BranchTrueFalseTotal
if ($match) 27 522 549
97 27 ▶ if (scalar(@_) == 3) {
Branch: 2 branches
BranchTrueFalseTotal
if (scalar @_ == 3) { } 19 8 27
elsif (scalar @_ == 4) { } 7 1 8
98 19 push @{$map{three}}, [@cards[@_]];
99 } elsif (scalar(@_) == 4) {
100 7 push @{$map{four}}, [@cards[@_]];
101 } else {
102 1 push @{$map{five}}, [@cards[@_]];
103 }
104 }
105 252 } @values, $n;
106 }
107  
108 57 ▶ if ($map{five}) {
Branch: 3 branches
BranchTrueFalseTotal
if ($map{'five'}) { } 1 56 57
elsif ($map{'four'}) { } 5 51 56
elsif ($map{'three'}) { } 6 45 51
109 1 $self->run($map{five});
110 } elsif ($map{four}) {
111 5 $self->run($map{four});
112 } elsif ($map{three}) {
113 6 $self->run($map{three});
114 }
115 }
116  
117 57 ▶sub calculate_flush {
Subroutine calculate_flush: called CC=7 SCAR=19.5
Pod documented
118 57 my ($self) = @_;
119  
120 # A flush is the four cards in the HAND sharing a suit; the starter can
121 # only extend it to five. Four of a suit made up with the starter is
122 # nothing, and a crib flush needs all five.
123 57 my @all = @{$self->cards};
124 57 ▶ my $starter = $self->with_starter ? pop @all : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->with_starter ? : 24 33 57
125 57 ▶ return unless @all == 4;
Branch: 1 branch
BranchTrueFalseTotal
unless @all == 4 0 57 57
126 57 my $suit = $all[0]->suit;
127 57 ▶ return if grep { $_->suit ne $suit } @all;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_->suit ne $suit;} @all 50 7 57
128  
129 7 ▶ if ($starter && $starter->suit eq $suit) {
Branch: 2 branches
BranchTrueFalseTotal
if ($starter and $starter->suit eq $suit) { } 3 4 7
elsif (not $self->crib) { } 3 1 4
Condition100%3/3
$starter and $starter->suit eq $suit
!ll&&!rl&&r
133
MC/DC100%2/2
$starter and $starter->suit eq $suit
$starter $starter->suit eq $suit
Truth table100%3/3
A and B
A= $starterB= $starter->suit eq $suit
ABresult
0X0
100
111
130 3 push @{$self->five_flush}, [@all, $starter];
131 } elsif (!$self->crib) {
132 3 push @{$self->four_flush}, [@all];
133 }
134 }
135  
136 57 ▶sub calculate_fifteen {
Subroutine calculate_fifteen: called CC=2 SCAR=6.9
Pod documented
137 57 my ($self, @cards) = @_;
138 57 my @values = map { $_->value } @cards;
139 57 foreach my $n (1 .. @values) {
140 1239 ▶ forcomb {
Subroutine __ANON__: called CC=2 SCAR=6.9
141 1239 ▶ push @{$self->fifteen}, [@cards[@_]] if vecsum(@values[@_]) == 15;
Branch: 1 branch
BranchTrueFalseTotal
if &vecsum(@values[@_]) == 15 83 1156 1239
142 252 } @values, $n
143 }
144 }
145  
146 57 ▶sub calculate_of_a_kind {
Subroutine calculate_of_a_kind: called CC=7 SCAR=19.5
Pod documented
147 57 my ($self, @cards) = @_;
148 57 my %map = ();
149 57 push @{$map{$_->symbol}}, $_ for (@cards);
150 57 for (keys %map) {
151 217 my $c = scalar @{$map{$_}};
152 217 ▶ next if ($c == 1);
Branch: 1 branch
BranchTrueFalseTotal
if $c == 1 193 24 217
153 24 ▶ if ($c == 2) {
Branch: 3 branches
BranchTrueFalseTotal
if ($c == 2) { } 16 8 24
elsif ($c == 3) { } 5 3 8
elsif ($c == 4) { } 3 0 3
154 16 push @{$self->pair}, $map{$_};
155 } elsif ($c == 3) {
156 5 push @{$self->three_of_a_kind}, $map{$_};
157 } elsif ($c == 4) {
158 3 push @{$self->four_of_a_kind}, $map{$_};
159 }
160 }
161 }
162  
163 57 ▶sub calculate_total {
Subroutine calculate_total: called CC=5 SCAR=16.1
Pod documented
164 57 my ($self) = @_;
165 57 my $scored = $self->scored;
166 57 my $score = 0;
167 57 for (keys %{$scored}) {
168 456 ▶ if (scalar @{$self->$_}) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$self->$_;}) 81 375 456
169 81 ▶ if ($_ eq 'run') {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ eq 'run') { } 12 69 81
170 12 ▶ $score += scalar @{$self->$_} * scalar @{$self->$_->[0]}
Branch: 1 branch
BranchTrueFalseTotal
if $self->$_->[0] 12 0 12
171 if $self->$_->[0];
172 } else {
173 69 $score += $scored->{$_} * scalar @{$self->$_};
174 }
175 }
176 }
177 57 $self->total_score($score);
178 }
179  
180 14 1;
181  
182 __END__
« Cribbage/Player/Hand.pm ↑ Summary Cribbage/Hands.pm »