Cribbage/Play.pm

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