File Coverage

blib/lib/Game/Cribbage/Board.pm
Criterion Covered Total %
statement 144 175 82.2
branch 12 26 46.1
condition 2 5 40.0
subroutine 53 62 85.4
pod 56 56 100.0
total 267 324 82.4


line stmt bran cond sub pod time code
1             package Game::Cribbage::Board;
2              
3 8     8   342241 use strict;
  8         15  
  8         287  
4 8     8   32 use warnings;
  8         28  
  8         389  
5              
6 8     8   1882 use Object::Proto::Sugar -types;
  8         49888  
  8         43  
7 8     8   9591 use Game::Cribbage::Player;
  8         76  
  8         308  
8 8     8   3000 use Game::Cribbage::Deck;
  8         62  
  8         311  
9 8     8   2858 use Game::Cribbage::Rounds;
  8         84  
  8         436  
10              
11             has players => (
12             is => 'rw',
13             isa => ArrayRef,
14             default => [],
15             );
16              
17             has [qw/deck rounds/] => (
18             is => 'rw',
19             isa => Object,
20             );
21              
22             sub add_player {
23 77     77 1 455146 my ($self, %player) = @_;
24 77         160 push @{$self->players}, Game::Cribbage::Player->new(
25 77         119 number => scalar @{$self->players} + 1,
  77         338  
26             %player
27             );
28 77         149 return $self;
29             }
30              
31             sub add_player_if_not_exists {
32 2     2 1 1117 my ($self, %player) = @_;
33 2         7 my $exists = $self->get_player(%player);
34 2 100       7 if (!$exists) {
35 1         4 $self->add_player(%player);
36 1         3 $exists = $self->get_player(%player);
37 1         4 return ($exists, 0);
38             }
39 1         3 return ($exists, 1);
40             }
41              
42             sub get_player {
43 5     5 1 811 my ($self, %player) = @_;
44 5         8 my $exists = 0;
45 5         5 for (@{$self->players}) {
  5         11  
46 6 100       84 if ($_->name =~ m/^$player{name}$/) {
47 3         8 return $_;
48             }
49             }
50             }
51              
52             sub build_deck {
53 37     37 1 491632 $_[0]->deck(Game::Cribbage::Deck->new());
54             }
55              
56             sub build_rounds {
57 34     34 1 162 $_[0]->rounds(Game::Cribbage::Rounds->new(
58             number => $_[1]
59             ));
60             }
61              
62             sub start_game {
63 34     34 1 2029 my ($self, %args) = @_;
64 34 50       136 $self->build_deck() if (!$self->deck);
65 34 50 50     189 $self->build_rounds($args{rounds} || 1) if (!$self->rounds);
66 34 50 33     251 $self->rounds->next_round($self, ($args{id} ? (id => $args{id}) : ())) if (!$self->rounds->current_round || $self->rounds->current_round->complete);
    50          
67             }
68              
69             sub next_hands {
70 0     0 1 0 my ($self, %args) = @_;
71 0         0 $self->rounds->current_round->next_hands(%args);
72             }
73              
74             sub force_to_crib {
75 0     0 1 0 my ($self, $card) = @_;
76              
77 0         0 my $crib = $self->crib_player_identifier;
78              
79 0         0 my $round = $self->rounds->current_round;
80              
81 0 0       0 if ($self->deck->card_exists($card)) {
82 0         0 $round->add_crib_card(
83             $self->deck->force_draw($card)
84             );
85 0         0 return 1;
86             }
87              
88             # confirm it exists in the players hand
89 0 0       0 if ($round->card_exists($crib, $card)) {
90 0         0 return 1;
91             }
92              
93             $round->add_crib_card(
94 0         0 $self->deck->generate_card($card)
95             );
96              
97 0         0 return 1;
98             }
99              
100             sub force_draw_card {
101 1     1 1 3 my ($self, $player, $index, $card) = @_;
102              
103 1         3 my $round = $self->rounds->current_round;
104              
105 1 50       4 if ($self->deck->card_exists($card)) {
106 1         4 $round->add_player_card(
107             $player,
108             $self->deck->force_draw($card)
109             );
110 1         3 return 1;
111             }
112              
113             # confirm it exists in the players hand
114 0 0       0 if ($round->card_exists($player, $card)) {
115 0         0 return 1;
116             }
117              
118             $round->add_player_card_by_index(
119 0         0 $index,
120             $player,
121             $self->deck->generate_card($card)
122             );
123              
124 0         0 return 1;
125             }
126              
127             sub draw_hands {
128 1     1 1 2 my ($self) = @_;
129              
130 1         3 my $round = $self->rounds->current_round;
131              
132 1         3 for (0 .. 5) {
133 6         12 for (@{$self->players}) {
  6         8  
134 12         25 $round->add_player_card(
135             $_,
136             $self->deck->draw
137             );
138             }
139             }
140             }
141              
142             sub add_starter_card {
143 0     0 1 0 my ($self, $player, $card) = @_;
144 0         0 $self->rounds->current_round->current_hands->add_starter_card($player, $card);
145             }
146              
147             sub get_hands {
148 0     0 1 0 my ($self) = @_;
149 0         0 return $self->rounds->current_round->current_hands;
150             }
151              
152             sub identify_worst_cards {
153 0     0 1 0 my ($self, $player) = @_;
154 0         0 $self->rounds->current_round->identify_worst_cards($player);
155             }
156              
157             sub validate_crib_cards {
158 1     1 1 6 my ($self, $cards) = @_;
159 1         6 $self->rounds->current_round->validate_crib_cards($cards);
160             }
161              
162             sub crib_player_id {
163 1     1 1 3 my ($self) = @_;
164 1         5 $self->rounds->current_round->crib_player_id($self);
165             }
166              
167             sub crib_player_identifier {
168 1     1 1 2 my ($self) = @_;
169 1         7 $self->rounds->current_round->current_hands->crib_player;
170             }
171              
172             sub crib_player_name {
173 1     1 1 243 my ($self) = @_;
174 1         6 $self->rounds->current_round->crib_player_name($self);
175             }
176              
177             sub crib_player_number {
178 1     1 1 2 my ($self) = @_;
179 1         5 $self->rounds->current_round->crib_player_number($self);
180             }
181              
182             sub crib_player_cards {
183 0     0 1 0 my ($self, $player, $cards) = @_;
184 0         0 $self->rounds->current_round->crib_player_cards($player, $cards);
185             }
186              
187             sub cribbed_card {
188 120     120 1 142 my ($self, $player, $index) = @_;
189 120         291 $self->rounds->current_round->crib_player_card($player, $index);
190             }
191              
192             sub cribbed_cards {
193 60     60 1 1874 my ($self, $player, @card_indexes) = @_;
194              
195 60         136 @card_indexes = sort { $b <=> $a } @card_indexes;
  60         117  
196 60         160 $self->cribbed_card($player, $_) for @card_indexes;
197 60         93 return 1;
198             }
199              
200             sub force_play_card {
201 0     0 1 0 my ($self, $card) = @_;
202 0         0 $self->rounds->current_round->force_play_card($card);
203             }
204              
205             sub play_card {
206 29     29 1 52 my ($self, $player, $index) = @_;
207 29         111 $self->rounds->current_round->play_player_card($player, $index);
208             }
209              
210             sub get_card {
211 8     8 1 9 my ($self, $player, $index) = @_;
212 8         27 $self->rounds->current_round->get_player_card($player, $index);
213             }
214              
215             sub current_play {
216 3     3 1 5 my ($self) = @_;
217 3         68 return $self->rounds->current_round->current_hands->play;
218             }
219              
220             sub current_play_cards {
221 1     1 1 2 my ($self) = @_;
222 1         3 return [map { $_->card } @{$self->rounds->current_round->current_hands->play->cards}];
  0         0  
  1         11  
223             }
224              
225             sub current_play_score {
226 13     13 1 25 my ($self) = @_;
227 13         56 $self->rounds->current_round->current_play_score();
228             }
229              
230             sub last_play_score {
231 2     2 1 4 my ($self) = @_;
232 2         11 $self->rounds->current_round->last_play_score();
233             }
234              
235             sub score {
236 14     14 1 23 my ($self) = @_;
237 14         133 $self->rounds->current_round->score;
238             }
239              
240             sub cannot_play {
241 10     10 1 41 my ($self, $player) = @_;
242 10         41 $self->rounds->current_round->cannot_play_a_card($player);
243             }
244              
245             sub player_cannot_play {
246 2     2 1 4 my ($self, $player) = @_;
247 2         13 return exists $self->rounds->current_round->current_hands->cannot_play->{$player};
248             }
249              
250             sub no_player_can_play {
251 1     1 1 2 my ($self) = @_;
252 1         1 return scalar(keys(%{$self->rounds->current_round->current_hands->cannot_play})) == scalar(@{$self->players});
  1         5  
  1         6  
253             }
254              
255             sub next_play {
256 7     7 1 13 my ($self) = @_;
257 7         26 $self->rounds->current_round->next_play($self);
258             }
259              
260             sub end_play {
261 2     2 1 4 my ($self) = @_;
262 2         13 $self->rounds->current_round->end_play();
263             }
264              
265             sub end_hands {
266 3     3 1 6 my ($self) = @_;
267 3         18 $self->rounds->current_round->end_hands($self);
268             }
269              
270             sub shuffle {
271 41     41 1 55 my ($self) = @_;
272 41         98 $self->deck->shuffle();
273             }
274              
275             sub get_round_id {
276 1     1 1 2 my ($self) = @_;
277 1         6 return $self->rounds->current_round->id;
278             }
279              
280             sub set_round_id {
281 1     1 1 2 my ($self, $id) = @_;
282 1         4 $self->rounds->current_round->id($id);
283 1         1 return $id;
284             }
285              
286             sub get_hands_id {
287 1     1 1 1 my ($self) = @_;
288 1         28 return $self->rounds->current_round->current_hands->id;
289             }
290              
291             sub set_hands_id {
292 1     1 1 3 my ($self, $id) = @_;
293 1         7 $self->rounds->current_round->current_hands->id($id);
294 1         2 return $id;
295             }
296              
297             sub get_play_id {
298 1     1 1 2 my ($self) = @_;
299 1         7 return $self->rounds->current_round->current_hands->play->id;
300             }
301              
302             sub set_play_id {
303 1     1 1 3 my ($self, $id) = @_;
304 1         9 $self->rounds->current_round->current_hands->play->id($id);
305 1         1 return $id;
306             }
307              
308             sub get_crib_player_hand_id {
309 1     1 1 3 my ($self) = @_;
310 1         7 return $self->rounds->current_round->current_hands->get_crib_player_hand_id();
311             }
312              
313             sub set_player_hand_id {
314 1     1 1 3 my ($self, $player, $id) = @_;
315 1         8 return $self->rounds->current_round->current_hands->set_player_hand_id($player, $id);
316             }
317              
318             sub get_player_hand_id {
319 1     1 1 2 my ($self, $player) = @_;
320 1         5 return $self->rounds->current_round->current_hands->get_player_hand_id($player);
321             }
322              
323             sub set_crib_complete {
324 1     1 1 4 my ($self, $player, $id) = @_;
325 1         6 return $self->rounds->current_round->current_hands->set_crib_complete($player, $id);
326             }
327              
328             sub crib_complete {
329 2     2 1 5 my ($self, $player) = @_;
330 2         29 return $self->rounds->current_round->current_hands->crib_complete;
331             }
332              
333             sub best_run_play {
334 1     1 1 3 my ($self, $player) = @_;
335 1         5 return $self->rounds->current_round->best_run_play($player);
336             }
337              
338             sub total_player_score {
339 1     1 1 3 my ($self, $player) = @_;
340 1 50       3 $player = ref $player ? $player->player : $player;
341 1         5 return $self->rounds->current_round->score->$player;
342             }
343              
344             sub set_crib_player {
345 35     35 1 102 my ($self, $player) = @_;
346 35 50       61 $player = ref $player ? $player->player : $player;
347 35         105 $self->rounds->current_round->current_hands->crib_player($player);
348 35 50       110 if ($self->rounds->current_round->current_hands->play) {
349 35         137 $self->rounds->current_round->current_hands->play->next_to_play($player);
350             }
351             }
352              
353             sub next_to_play_id {
354 1     1 1 268 my ($self) = @_;
355 1         6 $self->rounds->current_round->next_to_play_id($self);
356             }
357              
358             sub next_to_play {
359 1     1 1 1 my ($self) = @_;
360 1         6 $self->rounds->current_round->next_to_play($self);
361             }
362              
363             sub set_next_to_play {
364 0     0 1 0 my ($self, $player) = @_;
365 0         0 $self->rounds->current_round->current_hands->play->next_to_play($player);
366             }
367              
368             sub hand_play_history {
369 1     1 1 2 my ($self) = @_;
370 1         5 $self->rounds->current_round->hand_play_history();
371             }
372              
373             sub reset_hands {
374 0     0 1 0 my ($self) = @_;
375 0         0 $self->rounds->current_round->reset_hands();
376             }
377              
378             sub last_round_hands {
379 1     1 1 3 my ($self) = @_;
380 1         4 return $self->rounds->current_round->history->[-2];
381             }
382              
383             1;
384              
385             __END__