File Coverage

blib/lib/Game/Cribbage/Round.pm
Criterion Covered Total %
statement 146 167 87.4
branch 21 40 52.5
condition 8 15 53.3
subroutine 31 34 91.1
pod 0 29 0.0
total 206 285 72.2


line stmt bran cond sub pod time code
1             package Game::Cribbage::Round;
2              
3 8     8   82459 use strict;
  8         9  
  8         302  
4 8     8   27 use warnings;
  8         13  
  8         371  
5              
6 8     8   456 use Object::Proto::Sugar -types;
  8         11840  
  8         45  
7              
8 8     8   8986 use Game::Cribbage::Round::Score;
  8         64  
  8         292  
9 8     8   4773 use Game::Cribbage::Hands;
  8         90  
  8         351  
10              
11             has id => (
12             is => 'rw',
13             isa => Int
14             );
15              
16             has number => (
17             is => 'ro',
18             isa => Int
19             );
20              
21             has [qw/score current_hands/] => (
22             is => 'rw',
23             isa => Object
24             );
25              
26             has complete => (
27             is => 'rw',
28             isa => Bool,
29             default => 0
30             );
31              
32             has history => (
33             is => 'rw',
34             isa => ArrayRef,
35             default => []
36             );
37              
38             sub init {
39 37     37 0 78 my ($self, $game) = @_;
40 37         54 my %score = ();
41 37         39 for (@{$game->players}) {
  37         72  
42 74         216 $score{$_->player} = {
43             current => 0,
44             last => 0
45             };
46             }
47 37         249 $self->score(Game::Cribbage::Round::Score->new(%score));
48 37         89 $self->next_hands($game);
49             }
50              
51             sub reset_hands {
52 1     1 0 2 my ($self, $game) = @_;
53 1         4 $self->history([]);
54 1         2 my %score = ();
55 1         2 for (@{$game->players}) {
  1         3  
56 2         5 $score{$_->player} = {
57             current => 0,
58             last => 0
59             };
60             }
61 1         6 $self->score(Game::Cribbage::Round::Score->new(%score));
62 1         3 $self->next_hands($game);
63             }
64              
65             sub next_hands {
66 41     41 0 77 my ($self, $game, %args) = @_;
67 41         108 $game->shuffle();
68 41         255 my $hands = Game::Cribbage::Hands->new(%args)->init($game);
69 41         128 $self->current_hands($hands);
70 41         66 push @{$self->history}, $hands;
  41         67  
71 41         127 return $self;
72             }
73              
74             sub end_hands {
75 3     3 0 6 my ($self, $game) = @_;
76 3         14 my $scored = $self->current_hands->score_hands();
77 3         4 for my $hand (keys %{$scored}) {
  3         8  
78 6         34 $self->score->$hand->{last} = $self->score->$hand->{current};
79 6         31 $self->score->$hand->{current} += $scored->{$hand};
80             }
81 3         12 $self->next_hands($game, crib_player => $self->next_crib_player($game));
82             }
83              
84             sub add_player_card_by_index {
85 1     1 0 5 my ($self, $index, $player, $card) = @_;
86 1 50       3 my $hand = ref $player ? $player->player : $player;
87 1         5 $self->current_hands->$hand->add_by_index($index, $card);
88             }
89              
90             sub add_player_card {
91 399     399 0 702 my ($self, $player, $card) = @_;
92 399 100       623 my $hand = ref $player ? $player->player : $player;
93 399         839 $self->current_hands->$hand->add($card);
94             }
95              
96             sub add_starter_card {
97 30     30 0 41 my ($self, $player, $card) = @_;
98 30         102 my $score = $self->current_hands->add_starter_card($player, $card);
99 30 50 33     57 if (ref $score && $score->score) {
100 0 0       0 my $hand = ref $player ? $player->player : $player;
101 0         0 $self->score->$hand->{last} = $self->score->$hand->{current};
102 0         0 $self->score->$hand->{current} += $score->score;
103 0         0 push @{$self->current_hands->$hand->play_scored}, $score;
  0         0  
104             }
105 30         53 return $score;
106             }
107              
108             sub validate_crib_cards {
109 1     1 0 2 my ($self, $cards) = @_;
110 1         3 my $crib = $self->current_hands->crib_player;
111 1         5 $self->current_hands->$crib->validate_crib_cards($cards);
112             }
113              
114             sub next_to_play {
115 2     2 0 3 my ($self, $game) = @_;
116 2         19 my $next = $self->current_hands->play->next_to_play;
117 2         3 my $player;
118 2         3 for (@{$game->players}) {
  2         4  
119 2 50       5 if ($next eq $_->player) {
120 2         2 $player = $_;
121 2         4 last;
122             }
123             }
124 2         3 return $player;
125             }
126              
127             sub next_to_play_id {
128 1     1 0 2 my ($self, $game) = @_;
129 1         3 my $next = $self->next_to_play($game);
130 1         4 return $next->id;
131             }
132              
133             sub get_crib_player {
134 11     11 0 16 my ($self, $game) = @_;
135 11         13 my $player;
136 11         16 for (@{$game->players}) {
  11         24  
137 13 100       43 if ($self->current_hands->crib_player eq $_->player) {
138 11         14 $player = $_;
139 11         15 last;
140             }
141             }
142 11         28 return $player;
143             }
144              
145             sub next_crib_player {
146 5     5 0 10 my ($self, $game) = @_;
147 5         12 my $current = $self->get_crib_player($game);
148 5         8 my $next;
149 5 100       4 if (scalar @{$game->players} == $current->number) {
  5         19  
150 2         4 $next = 'player1';
151             } else {
152 3         8 $next = 'player' . ($current->number + 1);
153             }
154 5         12 return $next;
155             }
156              
157             sub crib_player_id {
158 2     2 0 4 my ($self, $game) = @_;
159 2         6 my $player = $self->get_crib_player($game);
160 2 50       10 return $player ? $player->id : $player;
161             }
162              
163             sub crib_player_name {
164 2     2 0 5 my ($self, $game) = @_;
165 2         6 my $player = $self->get_crib_player($game);
166 2 50       17 return $player ? $player->name : $player;
167             }
168              
169             sub crib_player_number {
170 2     2 0 241 my ($self, $game) = @_;
171 2         4 my $player = $self->get_crib_player($game);
172 2 50       10 return $player ? $player->number : $player;
173             }
174              
175             sub crib_player_cards {
176 0     0 0 0 my ($self, $player, $cards) = @_;
177 0 0       0 my $hand = ref $player ? $player->player : $player;
178 0         0 my $crib = $self->current_hands->crib_player;
179 0         0 return $self->current_hands->$hand->discard_cards($cards, $self->current_hands->$crib);
180             }
181              
182             sub crib_player_card {
183 121     121 0 177 my ($self, $player, $card_index) = @_;
184 121         163 my $hand = $player->player;
185 121         251 my $crib = $self->current_hands->crib_player;
186 121         381 $self->current_hands->$hand->discard($card_index, $self->current_hands->$crib);
187             }
188              
189             sub force_play_card {
190 1     1 0 6 my ($self, $card) = @_;
191 1         3 my ($score, $player) = $self->current_hands->force_play_card($card);
192 1 50 33     4 if (ref $score && $score->score) {
193 0 0       0 my $hand = ref $player ? $player->player : $player;
194 0         0 $self->score->$hand->{last} = $self->score->$hand->{current};
195 0         0 $self->score->$hand->{current} += $score->score;
196 0         0 push @{$self->current_hands->$hand->play_scored}, $score;
  0         0  
197             }
198 1         2 return $score;
199             }
200              
201             sub play_player_card {
202 29     29 0 37 my ($self, $player, $card_index) = @_;
203 29         85 my $score = $self->current_hands->play_card($player, $card_index);
204 29 100 66     87 if (ref $score && $score->score) {
205 1 50       3 my $hand = ref $player ? $player->player : $player;
206 1         5 $self->score->$hand->{last} = $self->score->$hand->{current};
207 1         4 $self->score->$hand->{current} += $score->score;
208 1         1 push @{$self->current_hands->$hand->play_scored}, $score;
  1         4  
209             }
210 29         109 return $score;
211             }
212              
213             sub card_exists {
214 0     0 0 0 my ($self, $player, $card) = @_;
215 0         0 return $self->current_hands->card_exists($player, $card);
216             }
217              
218             sub get_player_card {
219 8     8 0 9 my ($self, $player, $card_index) = @_;
220 8         21 $self->current_hands->get_card($player, $card_index);
221             }
222              
223             sub current_play_score {
224 13     13 0 22 my ($self) = @_;
225 13         36 $self->current_hands->play_score();
226             }
227              
228             sub last_play_score {
229 3     3 0 6 my ($self) = @_;
230 3         13 $self->current_hands->last_play_score();
231             }
232              
233             sub cannot_play_a_card {
234 10     10 0 15 my ($self, $player) = @_;
235 10         26 $self->current_hands->cannot_play_a_card($player);
236             }
237              
238             sub next_play {
239 7     7 0 11 my ($self, $game) = @_;
240 7         20 my $score = $self->current_hands->next_play($game);
241 7 100 100     22 if (ref $score && $score->score) {
242 4 50       19 my $hand = ref $score->player ? $score->player->player : $score->player;
243 4         32 $self->score->$hand->{last} = $self->score->$hand->{current};
244 4         14 $self->score->$hand->{current} += $score->score;
245 4         7 push @{$self->current_hands->$hand->play_scored}, $score;
  4         18  
246             }
247 7         47 return $self->current_hands->play;
248             }
249              
250             sub end_play {
251 2     2 0 3 my ($self) = @_;
252 2         9 my $score = $self->current_hands->end_play();
253 2 50 33     11 if (ref $score && $score->score) {
254 2 50       18 my $hand = ref $score->player ? $score->player->player : $score->player;
255 2         12 $self->score->$hand->{last} = $self->score->$hand->{current};
256 2         10 $self->score->$hand->{current} += $score->score;
257 2         3 push @{$self->current_hands->$hand->play_scored}, $score;
  2         9  
258             }
259 2         6 return 1;
260             }
261              
262             sub identify_worst_cards {
263 0     0 0 0 my ($self, $player) = @_;
264 0 0       0 my $hand = ref $player ? $player->player : $player;
265 0         0 $self->current_hands->$hand->identify_worst_cards();
266             }
267              
268             sub best_run_play {
269 2     2 0 22 my ($self, $player) = @_;
270 2         9 $self->current_hands->best_run_play($player);
271             }
272              
273             sub hand_play_history {
274 2     2 0 4 my ($self, $player) = @_;
275 2         55 my @history = @{$self->current_hands->play_history};
  2         9  
276 2         3 my $current = pop @history;
277 2         4 my $count_cards = 0;
278 2         4 for (@history) {
279 0         0 $count_cards += scalar @{$_->cards};
  0         0  
280             }
281             return {
282 2         7 used_count => $count_cards,
283             current_play => $current
284             };
285             }
286              
287             1;