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 29 29 100.0
total 235 285 82.4


line stmt bran cond sub pod time code
1             package Game::Cribbage::Round;
2              
3 8     8   81681 use strict;
  8         12  
  8         280  
4 8     8   27 use warnings;
  8         12  
  8         407  
5              
6 8     8   477 use Object::Proto::Sugar -types;
  8         11529  
  8         43  
7              
8 8     8   9599 use Game::Cribbage::Round::Score;
  8         66  
  8         308  
9 8     8   4891 use Game::Cribbage::Hands;
  8         91  
  8         347  
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 1 80 my ($self, $game) = @_;
40 37         47 my %score = ();
41 37         80 for (@{$game->players}) {
  37         91  
42 74         220 $score{$_->player} = {
43             current => 0,
44             last => 0
45             };
46             }
47 37         213 $self->score(Game::Cribbage::Round::Score->new(%score));
48 37         101 $self->next_hands($game);
49             }
50              
51             sub reset_hands {
52 1     1 1 2 my ($self, $game) = @_;
53 1         3 $self->history([]);
54 1         2 my %score = ();
55 1         1 for (@{$game->players}) {
  1         3  
56 2         6 $score{$_->player} = {
57             current => 0,
58             last => 0
59             };
60             }
61 1         6 $self->score(Game::Cribbage::Round::Score->new(%score));
62 1         2 $self->next_hands($game);
63             }
64              
65             sub next_hands {
66 41     41 1 71 my ($self, $game, %args) = @_;
67 41         103 $game->shuffle();
68 41         289 my $hands = Game::Cribbage::Hands->new(%args)->init($game);
69 41         135 $self->current_hands($hands);
70 41         41 push @{$self->history}, $hands;
  41         69  
71 41         110 return $self;
72             }
73              
74             sub end_hands {
75 3     3 1 7 my ($self, $game) = @_;
76 3         17 my $scored = $self->current_hands->score_hands();
77 3         5 for my $hand (keys %{$scored}) {
  3         7  
78 6         26 $self->score->$hand->{last} = $self->score->$hand->{current};
79 6         24 $self->score->$hand->{current} += $scored->{$hand};
80             }
81 3         14 $self->next_hands($game, crib_player => $self->next_crib_player($game));
82             }
83              
84             sub add_player_card_by_index {
85 1     1 1 5 my ($self, $index, $player, $card) = @_;
86 1 50       3 my $hand = ref $player ? $player->player : $player;
87 1         40 $self->current_hands->$hand->add_by_index($index, $card);
88             }
89              
90             sub add_player_card {
91 399     399 1 691 my ($self, $player, $card) = @_;
92 399 100       581 my $hand = ref $player ? $player->player : $player;
93 399         793 $self->current_hands->$hand->add($card);
94             }
95              
96             sub add_starter_card {
97 30     30 1 37 my ($self, $player, $card) = @_;
98 30         75 my $score = $self->current_hands->add_starter_card($player, $card);
99 30 50 33     69 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         49 return $score;
106             }
107              
108             sub validate_crib_cards {
109 1     1 1 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 1 2 my ($self, $game) = @_;
116 2         7 my $next = $self->current_hands->play->next_to_play;
117 2         3 my $player;
118 2         2 for (@{$game->players}) {
  2         5  
119 2 50       4 if ($next eq $_->player) {
120 2         3 $player = $_;
121 2         4 last;
122             }
123             }
124 2         3 return $player;
125             }
126              
127             sub next_to_play_id {
128 1     1 1 2 my ($self, $game) = @_;
129 1         3 my $next = $self->next_to_play($game);
130 1         3 return $next->id;
131             }
132              
133             sub get_crib_player {
134 11     11 1 18 my ($self, $game) = @_;
135 11         12 my $player;
136 11         13 for (@{$game->players}) {
  11         37  
137 13 100       82 if ($self->current_hands->crib_player eq $_->player) {
138 11         14 $player = $_;
139 11         17 last;
140             }
141             }
142 11         14 return $player;
143             }
144              
145             sub next_crib_player {
146 5     5 1 9 my ($self, $game) = @_;
147 5         12 my $current = $self->get_crib_player($game);
148 5         8 my $next;
149 5 100       5 if (scalar @{$game->players} == $current->number) {
  5         18  
150 2         4 $next = 'player1';
151             } else {
152 3         9 $next = 'player' . ($current->number + 1);
153             }
154 5         13 return $next;
155             }
156              
157             sub crib_player_id {
158 2     2 1 5 my ($self, $game) = @_;
159 2         5 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 1 5 my ($self, $game) = @_;
165 2         6 my $player = $self->get_crib_player($game);
166 2 50       12 return $player ? $player->name : $player;
167             }
168              
169             sub crib_player_number {
170 2     2 1 229 my ($self, $game) = @_;
171 2         6 my $player = $self->get_crib_player($game);
172 2 50       12 return $player ? $player->number : $player;
173             }
174              
175             sub crib_player_cards {
176 0     0 1 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 1 140 my ($self, $player, $card_index) = @_;
184 121         204 my $hand = $player->player;
185 121         204 my $crib = $self->current_hands->crib_player;
186 121         344 $self->current_hands->$hand->discard($card_index, $self->current_hands->$crib);
187             }
188              
189             sub force_play_card {
190 1     1 1 6 my ($self, $card) = @_;
191 1         4 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 1 53 my ($self, $player, $card_index) = @_;
203 29         85 my $score = $self->current_hands->play_card($player, $card_index);
204 29 100 66     91 if (ref $score && $score->score) {
205 1 50       4 my $hand = ref $player ? $player->player : $player;
206 1         7 $self->score->$hand->{last} = $self->score->$hand->{current};
207 1         5 $self->score->$hand->{current} += $score->score;
208 1         2 push @{$self->current_hands->$hand->play_scored}, $score;
  1         4  
209             }
210 29         108 return $score;
211             }
212              
213             sub card_exists {
214 0     0 1 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 1 9 my ($self, $player, $card_index) = @_;
220 8         25 $self->current_hands->get_card($player, $card_index);
221             }
222              
223             sub current_play_score {
224 13     13 1 18 my ($self) = @_;
225 13         42 $self->current_hands->play_score();
226             }
227              
228             sub last_play_score {
229 3     3 1 6 my ($self) = @_;
230 3         12 $self->current_hands->last_play_score();
231             }
232              
233             sub cannot_play_a_card {
234 10     10 1 13 my ($self, $player) = @_;
235 10         29 $self->current_hands->cannot_play_a_card($player);
236             }
237              
238             sub next_play {
239 7     7 1 12 my ($self, $game) = @_;
240 7         21 my $score = $self->current_hands->next_play($game);
241 7 100 100     24 if (ref $score && $score->score) {
242 4 50       40 my $hand = ref $score->player ? $score->player->player : $score->player;
243 4         26 $self->score->$hand->{last} = $self->score->$hand->{current};
244 4         14 $self->score->$hand->{current} += $score->score;
245 4         6 push @{$self->current_hands->$hand->play_scored}, $score;
  4         17  
246             }
247 7         32 return $self->current_hands->play;
248             }
249              
250             sub end_play {
251 2     2 1 4 my ($self) = @_;
252 2         8 my $score = $self->current_hands->end_play();
253 2 50 33     9 if (ref $score && $score->score) {
254 2 50       12 my $hand = ref $score->player ? $score->player->player : $score->player;
255 2         11 $self->score->$hand->{last} = $self->score->$hand->{current};
256 2         9 $self->score->$hand->{current} += $score->score;
257 2         4 push @{$self->current_hands->$hand->play_scored}, $score;
  2         8  
258             }
259 2         7 return 1;
260             }
261              
262             sub identify_worst_cards {
263 0     0 1 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 1 6 my ($self, $player) = @_;
270 2         7 $self->current_hands->best_run_play($player);
271             }
272              
273             sub hand_play_history {
274 2     2 1 75 my ($self, $player) = @_;
275 2         3 my @history = @{$self->current_hands->play_history};
  2         8  
276 2         4 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         16 used_count => $count_cards,
283             current_play => $current
284             };
285             }
286              
287             1;
288              
289             __END__