Cribbage/Play.pm

filter: Statementstmt 99.1% 112 / 113 Branchbran 93.7% 30 / 32 Conditioncond 88.8% 8 / 9 MC/DCmcdc 83.3% 5 / 6 Subroutinesub 100% 16 / 16 Podpod 100% 9 / 9 totaltotal 97.3% 180 / 185 CC 23 SCAR 13.0
CC
23
Cov
97%
calculate_pair
7.0
calculate_run
6.0
force_card
3.0
CRAP 3.7
« Cribbage/Hands.pm ↑ Summary Cribbage/Deck.pm »
1 package Game::Cribbage::Play;
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 14 ▶use Game::Cribbage::Play::Card;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 14 ▶use Game::Cribbage::Play::Score;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 14 ▶use Game::Cribbage::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 14 ▶use ntheory qw/forcomb vecsum/;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 has [qw/id next_to_play/] => (
13 is => 'rw',
14 14 );
15  
16 has total => (
17 is => 'rw',
18 isa => Int,
19 default => 0,
20 14 );
21  
22 has [qw/cards scored/] => (
23 is => 'rw',
24 isa => ArrayRef,
25 default => []
26 14 );
27  
28 75 ▶sub test_card_score {
Subroutine test_card_score: called CC=2 SCAR=6.9
Pod documented
29 75 my ($self, $player, $card) = @_;
30  
31 75 my $total = $self->calculate_total([@{$self->cards}, $card], 0);
32  
33 75 ▶ if ($total > 31) {
Branch: 1 branch
BranchTrueFalseTotal
if ($total > 31) 26 49 75
34 26 return Game::Cribbage::Error->new(
35 message => 'Total count would be greater than 31 if ' . $card->stringify . ' was to be played',
36 go => 1
37 );
38 }
39  
40 49 my $score = Game::Cribbage::Play::Score->new(
41 player => $player,
42 card => $card
43 );
44  
45 49 $self->calculate_pair($score, $card);
46  
47 49 $self->calculate_run($score, $card);
48  
49 49 $self->calculate_hits($score, $total);
50  
51 49 $score->score;
52  
53 49 return $score;
54 }
55  
56 57 ▶sub test_card {
Subroutine test_card: called CC=2 SCAR=6.9
Pod documented
57 57 my ($self, $player, $card) = @_;
58  
59 57 my $score = $self->test_card_score($player, $card);
60  
61 57 ▶ return $score if ref $score eq 'Game::Cribbage::Error';
Branch: 1 branch
BranchTrueFalseTotal
if ref $score eq "Game::Cribbage::Error" 24 33 57
62  
63 33 return $score->total_score;
64 }
65  
66 101 ▶sub card {
Subroutine card: called CC=3 SCAR=11.0
Pod documented
67 101 my ($self, $player, $card) = @_;
68  
69 101 my $total = $self->calculate_total([@{$self->cards}, $card], 0);
70  
71 101 ▶ if ($total > 31) {
Branch: 1 branch
BranchTrueFalseTotal
if ($total > 31) 0 101 101
72 0 return Game::Cribbage::Error->new(
73 message => 'Total count would be greater than 31 if ' . $card->stringify . ' was to be played',
74 go => 1
75 );
76 }
77  
78 101 my $score = Game::Cribbage::Play::Score->new(
79 player => $player,
80 card => $card
81 );
82  
83 101 $self->calculate_pair($score, $card);
84  
85 101 $self->calculate_run($score, $card);
86  
87 101 push @{$self->cards}, Game::Cribbage::Play::Card->new(
88 player => $player,
89 card => $card
90 );
91  
92 101 $total = $self->calculate_total($self->cards, 1);
93  
94 101 $self->calculate_hits($score, $total);
95  
96 101 ▶ if ($score->score) {
Branch: 1 branch
BranchTrueFalseTotal
if ($score->score) 23 78 101
97 23 push @{$self->scored}, $score;
98 }
99 101 return $score;
100 }
101  
102 4 ▶sub force_card {
Subroutine force_card: called CC=3 SCAR=11.0
Pod documented
103 4 my ($self, $player, $card) = @_;
104  
105 4 for (@{$self->cards}) {
106 1 ▶ if ($_->card->match($card)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_->card->match($card)) 1 0 1
107 1 return 0;
108 }
109 }
110  
111 3 return $self->card($player, $card);
112 }
113  
114  
115 8 ▶sub end_play {
Subroutine end_play: called CC=2 SCAR=6.9
Pod documented
116 8 my ($self) = @_;
117  
118 8 ▶ return unless $self->cards->[0];
Branch: 1 branch
BranchTrueFalseTotal
unless $self->cards->[0] 1 7 8
119  
120 7 my $score = Game::Cribbage::Play::Score->new(
121 player => $self->cards->[-1]->player,
122 card => $self->cards->[-1],
123 go => 1
124 );
125  
126 7 push @{$self->scored}, $score;
127  
128 7 return $score;
129 }
130  
131 150 ▶sub calculate_pair {
Subroutine calculate_pair: called CC=7 SCAR=19.5
Pod documented
132 150 my ($self, $score, $card) = @_;
133  
134 150 ▶ if ($self->cards->[-1] && $self->cards->[-1]->symbol eq $card->symbol) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->cards->[-1] and $self->cards->[-1]->symbol eq $card->symbol) 29 121 150
Condition100%3/3
$self->cards->[-1] and $self->cards->[-1]->symbol eq $card->symbol
!ll&&!rl&&r
576429
MC/DC100%2/2
$self->cards->[-1] and $self->cards->[-1]->symbol eq $card->symbol
$self->cards->[-1] $self->cards->[-1]->symbol eq $card->symbol
Truth table100%3/3
A and B
A= $self->cards->[-1]B= $self->cards->[-1]->symbol eq $card->symbol
ABresult
0X0
100
111
135 29 ▶ if ($self->cards->[-2] && $self->cards->[-2]->symbol eq $card->symbol) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->cards->[-2] and $self->cards->[-2]->symbol eq $card->symbol) { } 6 23 29
Condition100%3/3
$self->cards->[-2] and $self->cards->[-2]->symbol eq $card->symbol
!ll&&!rl&&r
12116
MC/DC100%2/2
$self->cards->[-2] and $self->cards->[-2]->symbol eq $card->symbol
$self->cards->[-2] $self->cards->[-2]->symbol eq $card->symbol
Truth table100%3/3
A and B
A= $self->cards->[-2]B= $self->cards->[-2]->symbol eq $card->symbol
ABresult
0X0
100
111
136 6 ▶ if ($self->cards->[-3] && $self->cards->[-3]->symbol eq $card->symbol) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->cards->[-3] and $self->cards->[-3]->symbol eq $card->symbol) { } 1 5 6
Condition66%2/3
$self->cards->[-3] and $self->cards->[-3]->symbol eq $card->symbol
!ll&&!rl&&r
501
MC/DC50%1/2
$self->cards->[-3] and $self->cards->[-3]->symbol eq $card->symbol
$self->cards->[-3] $self->cards->[-3]->symbol eq $card->symbol
Truth table66%2/3
A and B
A= $self->cards->[-3]B= $self->cards->[-3]->symbol eq $card->symbol
ABresult
0X0
100
111
137 1 $score->pair(3);
138 } else {
139 5 $score->pair(2);
140 }
141 } else {
142 23 $score->pair(1);
143 }
144 }
145  
146 150 return 1;
147 }
148  
149 150 ▶sub calculate_run {
Subroutine calculate_run: called CC=6 SCAR=17.9
Pod documented
150 150 my ($self, $score, $card) = @_;
151 150 my @cards = map { $_->card } @{$self->cards};
152 150 my @values = map { $_->run_value } (@cards, $card);
153 150 my $length = scalar @values - 1;
154 150 my $set = 5;
155 150 for my $n (qw/6 5 4 3 2/) {
156 749 my $match = $set--;
157 749 ▶ next unless $n <= $length;
Branch: 1 branch
BranchTrueFalseTotal
unless $n <= $length 658 91 749
158 91 my @new = sort { $a <=> $b } @values[$length - $n .. $length];
159 91 my $first = $new[0];
160 91 ▶ for (my $i = 1; $i <= $n; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $n) 98 91 189
161 98 $first = $first + 1;
162 98 ▶ if ($first != $new[$i]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($first != $new[$i]) 87 11 98
163 87 $match = 0;
164 87 $i = $n + 1;
165 }
166 }
167 91 ▶ if ($match) {
Branch: 1 branch
BranchTrueFalseTotal
if ($match) 4 87 91
168 4 $score->run($match);
169 4 last;
170 }
171 }
172  
173 150 return 1;
174 }
175  
176 279 ▶sub calculate_total {
Subroutine calculate_total: called CC=3 SCAR=11.0
Pod documented
177 279 my ($self, $cards, $set) = @_;
178  
179 279 my $total = 0;
180 279 for (@{ $cards }) {
181 634 $total += $_->value;
182 }
183 279 ▶ if ($set) {
Branch: 1 branch
BranchTrueFalseTotal
if ($set) 102 177 279
184 102 $self->total($total);
185 }
186  
187 279 return $total;
188 }
189  
190 150 ▶sub calculate_hits {
Subroutine calculate_hits: called CC=3 SCAR=11.0
Pod documented
191 150 my ($self, $score, $total) = @_;
192 150 ▶ if ($total == 15) {
Branch: 2 branches
BranchTrueFalseTotal
if ($total == 15) { } 5 145 150
elsif ($total == 31) { } 14 131 145
193 5 $score->fifteen(1);
194 } elsif ($total == 31) {
195 14 $score->pegged(1);
196 14 $score->go(1);
197 }
198  
199 150 return 1;
200 }
201  
202 14 1;
203  
204 __END__
« Cribbage/Hands.pm ↑ Summary Cribbage/Deck.pm »