File Coverage

blib/lib/Game/Cribbage/Board.pm
Criterion Covered Total %
statement 21 21 100.0
branch n/a
condition n/a
subroutine 7 7 100.0
pod n/a
total 28 28 100.0


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