File Coverage

blib/lib/Game/Cribbage/Round.pm
Criterion Covered Total %
statement 18 18 100.0
branch n/a
condition n/a
subroutine 6 6 100.0
pod n/a
total 24 24 100.0


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