Cribbage/Hands.pm

filter: Statementstmt 96.6% 172 / 178 Branchbran 84.8% 56 / 66 Conditioncond 62.5% 5 / 8 MC/DCmcdc 20.0% 1 / 5 Subroutinesub 93.1% 27 / 29 Podpod 95.6% 22 / 23 totaltotal 91.5% 283 / 309 CC 45 SCAR 15.0
CC
45
Cov
92%
next_play
10.0
play_card
7.0
cannot_play_a_card
7.0
CRAP 4.5
« Cribbage/Score.pm ↑ Summary Cribbage/Play.pm »
1 package Game::Cribbage::Hands;
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::Player::Hand;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 11 ▶use Game::Cribbage::Play;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 11 ▶use Game::Cribbage::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 has id => (
12 is => 'rw',
13 isa => Int
14 11 );
15  
16 has number => (
17 is => 'ro',
18 isa => Int
19 11 );
20  
21 has crib_player => (
22 is => 'rw',
23 isa => Str
24 11 );
25  
26 has crib_complete => (
27 is => 'rw',
28 isa => Bool
29 11 );
30  
31 has [qw/starter play player1 player2 player3 player4/] => (
32 is => 'rw',
33 isa => Object
34 11 );
35  
36 has cannot_play => (
37 is => 'rw',
38 isa => HashRef,
39 default => {}
40 11 );
41  
42 has play_history => (
43 is => 'rw',
44 isa => ArrayRef,
45 default => []
46 11 );
47  
48  
49 68 ▶sub BUILD {
Subroutine BUILD: called CC=2 SCAR=6.9
Pod undocumented
50 68 my ($self, $options) = @_;
51 68 ▶ $self->crib_player('player1') if !$self->crib_player;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->crib_player 65 3 68
52 68 return $self;
53 }
54  
55 68 ▶sub init {
Subroutine init: called CC=2 SCAR=6.9
Pod documented
56 68 my ($self, $game) = @_;
57 68 for (@{$game->players}) {
58 136 my $player = $_->player;
59 136 $self->$player(Game::Cribbage::Player::Hand->new(
60 player => $player
61 ));
62 }
63 68 $self->new_play();
64 68 $self;
65 }
66  
67 36 ▶sub add_starter_card {
Subroutine add_starter_card: called CC=4 SCAR=13.9
Pod documented
68 36 my ($self, $player, $card) = @_;
69 36 $self->starter($card);
70  
71 36 my $scored = 0;
72 36 ▶ if ($card->symbol =~ m/^J$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($card->symbol =~ /^J$/) 2 34 36
73 2 $scored = Game::Cribbage::Play::Score->new(
74 player => $player,
75 card => $card,
76 flipped => 1
77 );
78 2 push @{$self->play->scored}, $scored;
79 }
80  
81 36 for (qw/player1 player2 player3 player4/) {
82 144 ▶ if ($self->$_) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->$_) 72 72 144
83 72 $self->$_->starter($card);
84 }
85 }
86  
87 36 return $scored;
88 }
89  
90 3 ▶sub find_player_card {
Subroutine find_player_card: called CC=5 SCAR=16.1
Pod documented
91 3 my ($self, $card) = @_;
92 3 my $player;
93 3 for my $p (qw/player1 player2 player3 player4/) {
94 12 ▶ if ($self->$p) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->$p) 6 6 12
95 6 for (@{$self->$p->cards}) {
96 15 ▶ if ($_->match($card)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_->match($card)) 3 12 15
97 3 $card = $_;
98 3 $player = $p;
99 3 last;
100 }
101 }
102 }
103 }
104 3 return ($card, $player);
105 }
106  
107 2 ▶sub force_play_card {
Subroutine force_play_card: called CC=1 SCAR=0.0
Pod documented
108 2 my ($self, $card) = @_;
109 2 my $player;
110 2 ($card, $player) = $self->find_player_card($card);
111 2 $card->used(1);
112 2 my $scored = $self->play->force_card($player, $card);
113 2 $self->set_next_to_play();
114 2 return ($scored, $player);
115 }
116  
117 37 ▶sub play_card {
Subroutine play_card: called CC=7 SCAR=19.5
Pod documented
118 37 my ($self, $player, $card_index) = @_;
119 37 ▶ my $hand = ref $player ? $player->player : $player;
Branch: 1 branch
BranchTrueFalseTotal
ref $player ? : 37 0 37
120 37 ▶ if ($self->play->next_to_play ne $hand) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->play->next_to_play ne $hand) 5 32 37
121 5 return Game::Cribbage::Error->new( message => 'It is not the turn of ' . $hand );
122 }
123 32 ▶ my $card = ref $card_index ? $self->$hand->match($card_index) : $self->$hand->cards->[$card_index];
Branch: 1 branch
BranchTrueFalseTotal
ref $card_index ? : 3 29 32
124 32 ▶ if (!$card) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($card) 1 31 32
125 1 return Game::Cribbage::Error->new( message => 'That card is not in the hand of ' . $hand );
126 }
127 31 ▶ if ($card->used) {
Branch: 1 branch
BranchTrueFalseTotal
if ($card->used) 1 30 31
128 1 return Game::Cribbage::Error->new( message => 'That card has already been played this hand' );
129 }
130  
131 30 my $total = $self->play->total;
132 30 ▶ if ($card->value + $total > 31) {
Branch: 1 branch
BranchTrueFalseTotal
if ($card->value + $total > 31) 2 28 30
133 2 return Game::Cribbage::Error->new( over => 1, message => 'Playing this card will make the score greater than 31');
134 }
135 28 $card->used(1);
136 28 my $scored = $self->play->card($player, $card);
137 28 $self->set_next_to_play();
138 28 return $scored;
139 }
140  
141 12 ▶sub cannot_play_a_card {
Subroutine cannot_play_a_card: called CC=7 SCAR=19.5
Pod documented
142 12 my ($self, $player) = @_;
143  
144 12 ▶ my $hand = ref $player ? $player->player : $player;
Branch: 1 branch
BranchTrueFalseTotal
ref $player ? : 11 1 12
145 12 ▶ if ($self->play->next_to_play ne $hand) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->play->next_to_play ne $hand) 1 11 12
146 1 return Game::Cribbage::Error->new( message => 'It is not the turn of ' . $hand );
147 }
148  
149 11 my $current_total = $self->play->total;
150  
151 11 my @can_be_played;
152 11 for (@{$self->$hand->cards}) {
153 44 ▶ next if $_->used;
Branch: 1 branch
BranchTrueFalseTotal
if $_->used 18 26 44
154 26 ▶ if ( ($current_total + $_->value) <= 31 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($current_total + $_->value <= 31) 11 15 26
155 11 push @can_be_played, $_;
156 }
157 }
158  
159 11 ▶ return \@can_be_played if scalar @can_be_played;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @can_be_played 4 7 11
160  
161 7 $self->set_next_to_play();
162  
163 7 $self->cannot_play->{$hand} = 1;
164  
165 7 return 1;
166 }
167  
168 37 ▶sub set_next_to_play {
Subroutine set_next_to_play: called CC=1 SCAR=0.0
Pod documented
169 37 my ($self) = @_;
170 37 my $next = $self->parse_next_to_play($self->play->next_to_play);
171 37 $self->play->next_to_play($next);
172 }
173  
174 41 ▶sub parse_next_to_play {
Subroutine parse_next_to_play: called CC=2 SCAR=6.9
Pod documented
175 41 my ($self, $player_string) = @_;
176 41 $player_string =~ m/player(\d)/;
177 41 my $index = $1;
178 41 $index++;
179 41 my $next = 'player' . $index;
180 41 ▶ if ($self->$next) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->$next) { } 25 16 41
181 25 return $next;
182 } else {
183 16 return 'player1';
184 }
185 }
186  
187 3 ▶sub set_player_hand_id {
Subroutine set_player_hand_id: called CC=2 SCAR=7.0
Pod documented
188 3 my ($self, $player, $id) = @_;
189 3 ▶ my $hand = ref $player ? $player->player : $player;
Branch: 1 branch
BranchTrueFalseTotal
ref $player ? : 0 3 3
190 3 $self->$hand->id($id);
191 3 return $id;
192 }
193  
194 2 ▶sub get_player_hand_id {
Subroutine get_player_hand_id: called CC=2 SCAR=7.1
Pod documented
195 2 my ($self, $player) = @_;
196 2 ▶ my $hand = ref $player ? $player->player : $player;
Branch: 1 branch
BranchTrueFalseTotal
ref $player ? : 0 2 2
197 2 return $self->$hand->id;
198 }
199  
200 2 ▶sub get_crib_player_hand_id {
Subroutine get_crib_player_hand_id: called CC=1 SCAR=0.0
Pod documented
201 2 my ($self) = @_;
202 2 my $hand = $self->crib_player;
203 2 return $self->$hand->id;
204 }
205  
206 8 ▶sub get_card {
Subroutine get_card: called CC=2 SCAR=7.0
Pod documented
207 8 my ($self, $player, $card_index) = @_;
208 8 ▶ my $hand = ref $player ? $player->player : $player;
Branch: 1 branch
BranchTrueFalseTotal
ref $player ? : 8 0 8
209 8 my $card = $self->$hand->get($card_index);
210 8 return $card;
211 }
212  
213 52 ▶sub play_score {
Subroutine play_score: called CC=2 SCAR=6.9
Pod documented
214 52 my ($self) = @_;
215 52 ▶ return $self->play->total || 0;
Condition100%2/2
$self->play->total || 0
l!l
493
MC/DC100%1/1
$self->play->total || 0
$self->play->total
Truth table100%2/2
A
A= $self->play->total
Aresult
11
00
216 }
217  
218 3 ▶sub last_play_score {
Subroutine last_play_score: called CC=2 SCAR=6.9
Pod documented
219 3 my ($self) = @_;
220 3 my $prev = $self->play_history->[-2];
221 3 ▶ return $prev ? $prev->total : 'This is the first play';
Branch: 1 branch
BranchTrueFalseTotal
$prev ? : 2 1 3
222 }
223  
224 72 ▶sub new_play {
Subroutine new_play: called CC=3 SCAR=11.0
Pod documented
225 72 my ($self) = @_;
226 72 my $next_to_play = $self->crib_player;
227 72 ▶ if ($self->play) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->play) 4 68 72
228 4 ▶ $next_to_play = $self->parse_next_to_play(ref $self->play->cards->[-1]->player ? $self->play->cards->[-1]->player->player : $self->play->cards->[-1]->player);
Branch: 1 branch
BranchTrueFalseTotal
ref $self->play->cards->[-1]->player ? : 4 0 4
229 }
230 72 $self->play(Game::Cribbage::Play->new(
231 next_to_play => $next_to_play
232 ));
233 72 push @{$self->play_history}, $self->play;
234 }
235  
236 7 ▶sub next_play {
Subroutine next_play: called CC=10 SCAR=23.0
Pod documented
237 7 my ($self, $game) = @_;
238  
239 # first check whether any players can play on the current 'play'
240 # if they can they must use those cards first.
241 7 my $current_total = $self->play->total;
242  
243 7 my $available_cards = 0;
244 7 for my $hand (qw/player1 player2 player3 player4/) {
245 22 ▶ if ($self->$hand) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->$hand) 12 10 22
246 12 my @can_be_played;
247 12 for (@{$self->$hand->cards}) {
248 48 ▶ next if $_->used;
Branch: 1 branch
BranchTrueFalseTotal
if $_->used 27 21 48
249 21 $available_cards = 1;
250 21 ▶ if ( $current_total + $_->value <= 31 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($current_total + $_->value <= 31) 6 15 21
251 6 push @can_be_played, $_;
252 }
253 }
254 12 ▶ if (scalar @can_be_played) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @can_be_played) 2 10 12
255 2 return Game::Cribbage::Error->new(
256 message => 'Cards can be played',
257 player => $hand,
258 cards => \@can_be_played
259 );
260 }
261 }
262 }
263  
264 5 $self->cannot_play({});
265 # now we know cards can't be played confirm we have cards left to Play another 'play'.
266 5 ▶ if (!$available_cards) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($available_cards) 1 4 5
267 1 $game->end_hands();
268 1 return undef;
269 }
270  
271 4 my $scored;
272 4 ▶ if (!$self->play->scored->[-1] || !$self->play->scored->[-1]->go) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->play->scored->[-1] and $self->play->scored->[-1]->go) 4 0 4
Condition66%2/3
$self->play->scored->[-1] and $self->play->scored->[-1]->go
!ll&&!rl&&r
310
MC/DC0%0/2
$self->play->scored->[-1] and $self->play->scored->[-1]->go
$self->play->scored->[-1] $self->play->scored->[-1]->go
Truth table66%2/3
A and B
A= $self->play->scored->[-1]B= $self->play->scored->[-1]->go
ABresult
0X0
100
111
273 4 $scored = $self->play->end_play();
274 }
275  
276 4 $self->new_play();
277  
278 4 return $scored;
279 }
280  
281 2 ▶sub end_play {
Subroutine end_play: called CC=3 SCAR=11.6
Pod documented
282 2 my ($self) = @_;
283  
284 2 my $scored;
285  
286 2 ▶ if (!$self->play->scored->[-1] || !$self->play->scored->[-1]->go) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->play->scored->[-1] and $self->play->scored->[-1]->go) 2 0 2
Condition33%1/3
$self->play->scored->[-1] and $self->play->scored->[-1]->go
!ll&&!rl&&r
200
MC/DC0%0/2
$self->play->scored->[-1] and $self->play->scored->[-1]->go
$self->play->scored->[-1] $self->play->scored->[-1]->go
Truth table33%1/3
A and B
A= $self->play->scored->[-1]B= $self->play->scored->[-1]->go
ABresult
0X0
100
111
287 2 $scored = $self->play->end_play();
288 }
289  
290 2 $self->play(undef);
291  
292 2 return $scored;
293 }
294  
295 3 ▶sub score_hands {
Subroutine score_hands: called CC=3 SCAR=11.0
Pod documented
296 3 my ($self) = @_;
297 3 my %scored;
298 3 for (qw/player1 player2 player3 player4/) {
299 12 ▶ $scored{$_} = $self->$_->calculate_score()
Branch: 1 branch
BranchTrueFalseTotal
if $self->$_ 6 6 12
300 if ($self->$_);
301 }
302 3 return \%scored;
303 }
304  
305 0 ▶sub card_exists {
Subroutine card_exists: not called CC=2 SCAR=17.9
Pod documented
306 0 my ($self, $player, $card) = @_;
307 0 ▶ $player = ref $player ? $player->player : $player;
Branch: 1 branch
BranchTrueFalseTotal
ref $player ? : 0 0 0
308 0 return $self->$player->card_exists($card);
309 }
310  
311 2 ▶sub set_crib_complete {
Subroutine set_crib_complete: called CC=1 SCAR=0.0
Pod documented
312 2 my ($self) = @_;
313 2 $self->crib_complete(1);
314 }
315  
316 2 ▶sub best_run_play {
Subroutine best_run_play: called CC=2 SCAR=7.1
Pod documented
317 2 my ($self, $player) = @_;
318 2 ▶ $player = ref $player ? $player->player : $player;
Branch: 1 branch
BranchTrueFalseTotal
ref $player ? : 0 2 2
319 2 return $self->$player->best_run_play($self->play);
320 }
321  
322 0 ▶sub next_to_play_id {
Subroutine next_to_play_id: not called CC=1 SCAR=6.9
Pod documented
323 0 my ($self, $game) = @_;
324 0 my $hand = $self->play->next_to_play;
325 0 return $hand;
326 }
327  
328 11 1;
329  
330 __END__
« Cribbage/Score.pm ↑ Summary Cribbage/Play.pm »