Cribbage/Terminal.pm

filter: Statementstmt 64.3% 387 / 601 Branchbran 45.5% 134 / 294 Conditioncond 37.9% 47 / 124 MC/DCmcdc 25.3% 18 / 71 Subroutinesub 80.3% 45 / 56 Podpod 100% 45 / 45 totaltotal 56.7% 676 / 1191 CC 203 SCAR 46.3
CC
203
Cov
56%
play_hand
600.0
init_draw
420.0
discard_cards
132.0
CRAP 102.1
↑ Summary Cribbage/Player/Hand.pm »
1 package Game::Cribbage::Terminal;
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 4 our $VERSION = "0.17";
7  
8 4 ▶use Object::Proto::Sugar -types;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use Game::Cribbage::Board;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use Getopt::Long ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 4 our %SEQUENCE = (
13 'A' => 'up', 'B' => 'down', 'C' => 'right', 'D' => 'left',
14 'H' => 'home', 'F' => 'end',
15 '1~' => 'home', '4~' => 'end', '5~' => 'page_up', '6~' => 'page_down',
16 '7~' => 'home', '8~' => 'end',
17 );
18  
19 4 our %CONTROL = (
20 "\r" => 'enter', "\n" => 'enter', "\t" => 'tab',
21 "\x7f" => 'backspace', "\x08" => 'backspace',
22 "\x03" => 'interrupt', "\x04" => 'eof',
23 );
24  
25 has board => (
26 is => 'rw',
27 isa => Object
28 4 );
29  
30 has in => (
31 is => 'rw',
32 37 ▶ default => sub { \*STDIN }
Subroutine __ANON__: called CC=1 SCAR=0.0
33 4 );
34  
35 has out => (
36 is => 'rw',
37 14 ▶ default => sub { \*STDOUT }
Subroutine __ANON__: called CC=1 SCAR=0.0
38 4 );
39  
40 has raw => (
41 is => 'rw',
42 isa => Any,
43 default => 0
44 4 );
45  
46 has keysource => (
47 is => 'rw',
48 isa => Any
49 4 );
50  
51 has pending => (
52 is => 'rw',
53 isa => ArrayRef,
54 40 ▶ default => sub { [] }
Subroutine __ANON__: called CC=1 SCAR=0.0
55 4 );
56  
57 has _picking => (
58 is => 'rw',
59 isa => Any,
60 init_arg => 'picking'
61 4 );
62  
63 has _colour => (
64 is => 'rw',
65 isa => Any,
66 init_arg => 'colour'
67 4 );
68  
69 has [qw/dealer crib_set/] => (
70 is => 'rw',
71 isa => Bool,
72 default => 0
73 4 );
74  
75 has starter_card => (
76 is => 'rw'
77 4 );
78  
79 3 ▶sub start {
Subroutine start: called CC=2 SCAR=10.3
Pod documented
80 3 my ($self, %params) = @_;
81  
82 3 $self->clear_screen;
83  
84 3 $self->print_header("Welcome to terminal Cribbage");
85  
86 3 $self->print_footer("Insert name: ");
87  
88 3 my $name = $self->read_line;
89  
90 3 ▶ return 'quit' unless defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 3 0 3
91  
92 0 chomp($name);
93  
94 0 my $board = Game::Cribbage::Board->new();
95  
96 0 $board->add_player(name => 'Bot');
97 0 $board->add_player(name => $name);
98  
99 0 $board->start_game();
100  
101 0 $self->board($board);
102  
103 0 my $status = $self->init_game();
104  
105 0 $self->leave_raw;
106  
107 0 return $status;
108 }
109  
110 0 ▶sub init_game {
Subroutine init_game: not called CC=4 SCAR=30.0
Pod documented
111 0 my ($self) = @_;
112  
113 0 $self->clear_screen;
114  
115 0 $self->reset_cursor();
116  
117 0 my $split = $self->split_cards();
118  
119 0 ▶ return 0 if defined $split && !ref $split && $split eq 'quit';
Branch: 1 branch
BranchTrueFalseTotal
if defined $split and not ref $split and $split eq "quit" 0 0 0
Condition0%0/6
defined $split and not ref $split and $split eq "quit"
!ll&&!rl&&r
000
defined $split and not ref $split
!ll&&!rl&&r
000
MC/DC0%0/3
defined $split and not ref $split and $split eq "quit"
defined $split not ref $split $split eq "quit"
Truth table0%0/4
A and not(B) and C
A= defined $splitB= not ref $splitC= $split eq "quit"
ABCresult
0XX0
10X0
1100
1111
120  
121 0 return $self->init_draw();
122 }
123  
124 0 ▶sub init_draw {
Subroutine init_draw: not called CC=20 SCAR=60.4
Pod documented
125 0 my ($self, $switch) = @_;
126  
127 0 $self->crib_set(0);
128 0 $self->starter_card(undef);
129 0 ▶ $self->dealer(!$self->dealer) if $switch;
Branch: 1 branch
BranchTrueFalseTotal
if $switch 0 0 0
130  
131 0 my $winner;
132 0 ▶ if ($self->board->score->player1->{current} > 120) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->board->score->player1->{'current'} > 120) { } 0 0 0
elsif ($self->board->score->player2->{'current'} > 120) { } 0 0 0
133 0 $winner = 'player1';
134 } elsif ($self->board->score->player2->{current} > 120) {
135 0 $winner = 'player2';
136 }
137 0 ▶ return $self->winner($winner) if $winner;
Branch: 1 branch
BranchTrueFalseTotal
if $winner 0 0 0
138  
139 0 $self->draw_cards();
140  
141 0 my $discard = $self->discard_cards();
142  
143 0 ▶ return 0 if defined $discard && !ref $discard && $discard eq 'quit';
Branch: 1 branch
BranchTrueFalseTotal
if defined $discard and not ref $discard and $discard eq "quit" 0 0 0
Condition0%0/6
defined $discard and not ref $discard and $discard eq "quit"
!ll&&!rl&&r
000
defined $discard and not ref $discard
!ll&&!rl&&r
000
MC/DC0%0/3
defined $discard and not ref $discard and $discard eq "quit"
defined $discard not ref $discard $discard eq "quit"
Truth table0%0/4
A and not(B) and C
A= defined $discardB= not ref $discardC= $discard eq "quit"
ABCresult
0XX0
10X0
1100
1111
144  
145 0 my $split = $self->starter();
146  
147 0 ▶ return 0 if defined $split && !ref $split && $split eq 'quit';
Branch: 1 branch
BranchTrueFalseTotal
if defined $split and not ref $split and $split eq "quit" 0 0 0
Condition0%0/6
defined $split and not ref $split and $split eq "quit"
!ll&&!rl&&r
000
defined $split and not ref $split
!ll&&!rl&&r
000
MC/DC0%0/3
defined $split and not ref $split and $split eq "quit"
defined $split not ref $split $split eq "quit"
Truth table0%0/4
A and not(B) and C
A= defined $splitB= not ref $splitC= $split eq "quit"
ABCresult
0XX0
10X0
1100
1111
148  
149 0 ▶ while (!$winner) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($winner) 0 0 0
150 0 $self->clear_screen();
151  
152 0 $self->print_header(q|It is your turn to play a card, type go if you're unable to play.|);
153  
154 0 my $played = eval { $self->play_hand(); };
155  
156 0 ▶ return 0 if defined $played && !ref $played && $played eq 'quit';
Branch: 1 branch
BranchTrueFalseTotal
if defined $played and not ref $played and $played eq "quit" 0 0 0
Condition0%0/6
defined $played and not ref $played and $played eq "quit"
!ll&&!rl&&r
000
defined $played and not ref $played
!ll&&!rl&&r
000
MC/DC0%0/3
defined $played and not ref $played and $played eq "quit"
defined $played not ref $played $played eq "quit"
Truth table0%0/4
A and not(B) and C
A= defined $playedB= not ref $playedC= $played eq "quit"
ABCresult
0XX0
10X0
1100
1111
157  
158 0 ▶ unless ($@) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($@) 0 0 0
159 0 ▶ if ($self->board->score->player1->{current} > 120) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->board->score->player1->{'current'} > 120) { } 0 0 0
elsif ($self->board->score->player2->{'current'} > 120) { } 0 0 0
160 0 $winner = 'player1';
161 0 next;
162 } elsif ($self->board->score->player2->{current} > 120) {
163 0 $winner = 'player2';
164 0 next;
165 }
166  
167  
168 0 my $hands = $self->board->get_hands;
169 0 ▶ unless (grep { !$_->used } @{$hands->player2->cards}, @{$hands->player1->cards}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {not $_->used;} @{$hands->player2->cards;}, @{$hands->player1->cards;}) 0 0 0
170 0 eval {
171 0 $self->end_hands();
172 };
173 0 return $self->init_draw(1);
174 }
175  
176 0 ▶ if ($self->board->no_player_can_play) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->board->no_player_can_play) 0 0 0
177 0 eval { $self->board->next_play(); };
178 }
179 }
180 }
181 0 $self->winner($winner);
182 }
183  
184 0 ▶sub end_hands {
Subroutine end_hands: not called CC=3 SCAR=24.8
Pod documented
185 0 my ($self) = @_;
186  
187 0 $self->board->end_play();
188  
189 0 my $hands = $self->board->get_hands;
190  
191 0 $self->board->end_hands();
192  
193 0 $self->crib_set(0);
194  
195 0 $self->clear_screen();
196  
197 0 my $last = $self->board->last_round_hands();
198 0 my $player1_score = $last->player1->score->total_score;
199 0 my $player2_score = $last->player2->score->total_score;
200 0 ▶ my $crib_player = $self->dealer ? $self->board->players->[-1]->name : 'Bot';
Branch: 1 branch
BranchTrueFalseTotal
$self->dealer ? : 0 0 0
201 0 ▶ my $player = $self->dealer ? 'player2' : 'player1';
Branch: 1 branch
BranchTrueFalseTotal
$self->dealer ? : 0 0 0
202 0 my $crib_score = $last->$player->crib_score->total_score;
203  
204 0 $self->print_header(sprintf "Bot scored: %s - %s scored: %s - %s crib scored: %s",
205 $player1_score,
206 $self->board->players->[-1]->name,
207 $player2_score,
208 $crib_player,
209 $crib_score
210 );
211  
212 0 $self->draw_scores();
213  
214 0 $self->render_player_cards($hands->player1->cards, 3, 20, 1);
215  
216 0 $crib_player = $hands->crib_player;
217 0 $self->render_player_cards($hands->$crib_player->crib, 15, 20, 1);
218  
219 0 $self->render_player_cards($hands->player2->cards, 26, 20, 1);
220  
221 0 $self->print_footer(q|Press enter to continue|);
222  
223 0 $self->read_line;
224 }
225  
226 0 ▶sub play_hand {
Subroutine play_hand: not called CC=24 SCAR=64.0
Pod documented
227 0 my ($self) = @_;
228  
229 0 my $hands = $self->board->get_hands;
230 0 ▶ if ($self->board->next_to_play->player eq 'player1') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->board->next_to_play->player eq "player1") 0 0 0
231 0 my $card = $self->board->best_run_play('player1');
232 0 ▶ if ($card->can('go') && $card->go) {
Branch: 1 branch
BranchTrueFalseTotal
if ($card->can('go') and $card->go) { } 0 0 0
Condition0%0/3
$card->can("go") and $card->go
!ll&&!rl&&r
000
MC/DC0%0/2
$card->can("go") and $card->go
$card->can("go") $card->go
Truth table0%0/3
A and B
A= $card->can("go")B= $card->go
ABresult
0X0
100
111
233 0 $self->board->cannot_play('player1');
234 0 $self->draw_go(6, 2);
235 } else {
236 0 $self->board->play_card('player1', $card);
237 0 ▶ if ($self->board->current_play_score == 31) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->board->current_play_score == 31) 0 0 0
238 0 $self->board->next_play();
239 0 return;
240 }
241  
242 0 ▶ if ($self->board->score->player1->{current} > 120) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->board->score->player1->{"current"} > 120) 0 0 0
243 0 return;
244 }
245 }
246 }
247  
248 0 ▶ if ($self->board->player_cannot_play('player2')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->board->player_cannot_play("player2")) 0 0 0
249 0 ▶ if (!$self->board->player_cannot_play('player1')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->board->player_cannot_play("player1")) 0 0 0
250 0 $self->board->set_next_to_play('player1');
251 }
252 0 return;
253 }
254  
255 0 $_[0]->draw_scores();
256  
257 0 $self->render_opponent_cards(scalar grep { !$_->used } @{$hands->player1->cards});
258  
259 0 $self->render_run_play();
260  
261 0 my $number;
262 0 ▶ if ($self->picking && $self->enter_raw) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->picking and $self->enter_raw) { } 0 0 0
Condition0%0/3
$self->picking and $self->enter_raw
!ll&&!rl&&r
000
MC/DC0%0/2
$self->picking and $self->enter_raw
$self->picking $self->enter_raw
Truth table0%0/3
A and B
A= $self->pickingB= $self->enter_raw
ABresult
0X0
100
111
263 0 $number = $self->pick_card('player2');
264 0 ▶ return 'quit' if $number eq 'quit';
Branch: 1 branch
BranchTrueFalseTotal
if $number eq "quit" 0 0 0
265 0 ▶ $number = 'go' if $number eq 'go';
Branch: 1 branch
BranchTrueFalseTotal
if $number eq "go" 0 0 0
266 0 ▶ $number = 'b' if $number eq 'bot';
Branch: 1 branch
BranchTrueFalseTotal
if $number eq "bot" 0 0 0
267 }
268 else {
269 0 $self->render_player_cards($hands->player2->cards, 26, 20);
270  
271 0 $self->print_footer(q|Pick a card: |);
272  
273 0 $number = $self->read_line;
274  
275 0 ▶ return 'quit' unless defined $number;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $number 0 0 0
276  
277 0 chomp($number);
278 }
279  
280 0 ▶ if ($number =~ m/go/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($number =~ /go/i) 0 0 0
281 0 my @can = $self->board->cannot_play('player2');
282 0 ▶ if (scalar @can && ref $can[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @can and ref $can[0]) 0 0 0
Condition0%0/3
scalar @can and ref $can[0]
!ll&&!rl&&r
000
MC/DC0%0/2
scalar @can and ref $can[0]
scalar @can ref $can[0]
Truth table0%0/3
A and B
A= scalar @canB= ref $can[0]
ABresult
0X0
100
111
283 0 return;
284 }
285 0 $self->draw_go(30);
286 0 return;
287 }
288  
289 0 ▶ if ($number eq 'b') {
Branch: 1 branch
BranchTrueFalseTotal
if ($number eq "b") 0 0 0
290 0 my $card = $self->board->best_run_play('player2');
291 0 ▶ if ($card->can('go') && $card->go) {
Branch: 1 branch
BranchTrueFalseTotal
if ($card->can('go') and $card->go) { } 0 0 0
Condition0%0/3
$card->can("go") and $card->go
!ll&&!rl&&r
000
MC/DC0%0/2
$card->can("go") and $card->go
$card->can("go") $card->go
Truth table0%0/3
A and B
A= $card->can("go")B= $card->go
ABresult
0X0
100
111
292 0 $self->board->cannot_play('player2');
293 0 $self->draw_go(6, 2);
294 } else {
295 0 $self->board->play_card('player2', $card);
296 0 ▶ if ($self->board->current_play_score == 31) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->board->current_play_score == 31) 0 0 0
297 0 $self->board->next_play();
298 }
299 }
300 0 return;
301 }
302  
303 0 ▶ if ($number !~ m/^\d+$/ || $number > scalar @{$hands->player2->cards}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $number =~ /^\d+$/ or $number > scalar @{$hands->player2->cards;}) 0 0 0
Condition0%0/3
not $number =~ /^\d+$/ or $number > scalar @{$hands->player2->cards;}
l!l&&r!l&&!r
000
MC/DC0%0/2
not $number =~ /^\d+$/ or $number > scalar @{$hands->player2->cards;}
not $number =~ /^\d+$/ $number > scalar @{$hands->player2->cards;}
Truth table0%0/3
not(A) or B
A= not $number =~ /^\d+$/B= $number > scalar @{$hands->player2->cards;}
ABresult
1X1
011
000
304 0 return;
305 }
306  
307 0 my $card = $self->board->get_card('player2', $number - 1);
308 0 $self->board->play_card('player2', $card);
309  
310 0 ▶ if ($self->board->current_play_score == 31) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->board->current_play_score == 31) 0 0 0
311 0 $self->board->next_play();
312 }
313  
314 0 return 1;
315 }
316  
317 10 ▶sub playable_cards {
Subroutine playable_cards: called CC=5 SCAR=16.1
Pod documented
318 10 my ($self, $player) = @_;
319 10 my $hands = $self->board->get_hands;
320 10 ▶ my $hand = $player eq 'player1' ? $hands->player1 : $hands->player2;
Branch: 1 branch
BranchTrueFalseTotal
$player eq 'player1' ? : 0 10 10
321 10 my $play = $self->board->current_play;
322  
323 10 my @can;
324 10 my $i = 0;
325 10 for my $card (@{ $hand->cards }) {
326 36 $i++;
327 36 ▶ next if $card->used;
Branch: 1 branch
BranchTrueFalseTotal
if $card->used 0 36 36
328 36 my $test = $play->test_card($player, $card);
329 36 ▶ next if ref $test;
Branch: 1 branch
BranchTrueFalseTotal
if ref $test 20 16 36
330 16 push @can, $i;
331 }
332 10 return \@can;
333 }
334  
335 16 ▶sub card_line {
Subroutine card_line: called CC=9 SCAR=22.0
Pod documented
336 16 my ($self, $player, $index) = @_;
337 16 my $hands = $self->board->get_hands;
338 16 ▶ my $hand = $player eq 'player1' ? $hands->player1 : $hands->player2;
Branch: 1 branch
BranchTrueFalseTotal
$player eq 'player1' ? : 0 16 16
339 16 my $card = $hand->cards->[ $index - 1 ];
340 16 my $play = $self->board->current_play;
341  
342 16 my $score = $play->test_card_score($player, $card);
343 16 ▶ return sprintf('%s cannot be played, it would take the count past 31',
Branch: 1 branch
BranchTrueFalseTotal
if ref $score eq "Game::Cribbage::Error" 1 15 16
344 $card->stringify) if ref $score eq 'Game::Cribbage::Error';
345  
346 15 my $count = $self->board->current_play_score + $card->value;
347  
348 # `run` AND `pair` ARE BOTH INDEXES INTO Play::Score's table, and only one
349 # of them happens to read as a count. The table is
350 # `run => [3, 4, 5, 6, 7]` and `pair => [2, 6, 12]`, so pair 1 is a pair
351 # and run 1 is a run of THREE: the run length is the index plus two, and
352 # printing the index gives "a run of 1" for three cards in sequence.
353 15 my @why;
354 15 ▶ push @why, 'fifteen' if $score->fifteen;
Branch: 1 branch
BranchTrueFalseTotal
if $score->fifteen 1 14 15
355 15 ▶ push @why, 'thirty-one' if $score->pegged;
Branch: 1 branch
BranchTrueFalseTotal
if $score->pegged 3 12 15
356 15 ▶ push @why, sprintf('a run of %d', $score->run + 2) if $score->run;
Branch: 1 branch
BranchTrueFalseTotal
if $score->run 1 14 15
357 15 ▶ push @why, ('a pair', 'three of a kind', 'four of a kind')[ $score->pair - 1 ]
Branch: 1 branch
BranchTrueFalseTotal
if $score->pair 3 12 15
358 if $score->pair;
359  
360 15 my $total = $score->total_score;
361 15 ▶ return sprintf '%s takes the count to %d%s%s',
Branch: 2 branches
BranchTrueFalseTotal
@why ? : 6 9 15
$total ? : 6 9 15
362 $card->stringify, $count,
363 (@why ? ' for ' . join(' and ', @why) : ''),
364 ($total ? sprintf(', pegging %d', $total) : ', pegging nothing');
365 }
366  
367 8 ▶sub pick_card {
Subroutine pick_card: called CC=11 SCAR=24.1
Pod documented
368 8 my ($self, $player) = @_;
369  
370 8 my $can = $self->playable_cards($player);
371 8 ▶ return 'go' unless @$can;
Branch: 1 branch
BranchTrueFalseTotal
unless @$can 1 7 8
372  
373 7 my $at = 0;
374 7 while (1) {
375 10 my $hands = $self->board->get_hands;
376 10 $self->clear_screen();
377 10 $self->print_header($self->card_line($player, $can->[$at]));
378 10 $self->draw_scores();
379 10 $self->render_opponent_cards(scalar grep { !$_->used } @{$hands->player1->cards});
380 10 $self->render_run_play();
381 10 $self->render_player_cards($hands->player2->cards, 26, 20, 0,
382 { $can->[$at] => 'double' });
383 10 $self->print_footer(
384 q|arrows to choose, enter or space to play, g for go, b for the bot's pick, q to quit|);
385  
386 10 my $key = $self->read_key;
387 10 ▶ return 'quit' unless defined $key;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key 0 10 10
388 10 ▶ return 'quit' if $key eq 'q' || $key eq 'interrupt' || $key eq 'eof';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "q" or $key eq "interrupt" or $key eq "eof" 1 9 10
Condition66%4/6
$key eq "q" or $key eq "interrupt" or $key eq "eof"
l!l&&r!l&&!r
109
$key eq "q" or $key eq "interrupt"
l!l&&r!l&&!r
109
MC/DC33%1/3
$key eq "q" or $key eq "interrupt" or $key eq "eof"
$key eq "q" $key eq "interrupt" $key eq "eof"
Truth table50%2/4
A or B or C
A= $key eq "q"B= $key eq "interrupt"C= $key eq "eof"
ABCresult
1XX1
01X1
0011
0000
389 9 ▶ return 'go' if $key eq 'g';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "g" 1 8 9
390 8 ▶ return 'bot' if $key eq 'b';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "b" 1 7 8
391 7 ▶ return $can->[$at] if $key eq 'enter' || $key eq ' ';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "enter" or $key eq " " 4 3 7
Condition100%3/3
$key eq "enter" or $key eq " "
l!l&&r!l&&!r
313
MC/DC100%2/2
$key eq "enter" or $key eq " "
$key eq "enter" $key eq " "
Truth table100%3/3
A or B
A= $key eq "enter"B= $key eq " "
ABresult
1X1
011
000
392 3 ▶ $at = $self->step($can, $at, $key) if $key =~ /\A(?:up|down|left|right|home|end|tab)\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $key =~ /\A(?:up|down|left|right|home|end|tab)\z/ 3 0 3
393 }
394 }
395  
396 5 ▶sub pick_discards {
Subroutine pick_discards: called CC=12 SCAR=25.0
Pod documented
397 5 my ($self) = @_;
398  
399 5 my $hands = $self->board->get_hands;
400 5 my $cards = $hands->player2->cards;
401 5 my @all = (1 .. scalar @$cards);
402  
403 5 my ($at, %held) = (0);
404 5 while (1) {
405 16 my %marks = map { $_ => 'heavy' } keys %held;
406 16 $marks{ $all[$at] } = 'double';
407  
408 16 $self->clear_screen();
409 16 $self->print_header(sprintf '%s - %d of 2 chosen for the crib',
410 $cards->[ $all[$at] - 1 ]->stringify, scalar keys %held);
411 16 $self->render_opponent_cards(6);
412 16 $self->render_player_cards($cards, 26, 5, 1, \%marks);
413 16 $self->print_footer(
414 q|arrows to choose, enter or space to take a card, b for the bot's pick, q to quit|);
415  
416 16 my $key = $self->read_key;
417 16 ▶ return 'quit' unless defined $key;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key 0 16 16
418 16 ▶ return 'quit' if $key eq 'q' || $key eq 'interrupt' || $key eq 'eof';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "q" or $key eq "interrupt" or $key eq "eof" 1 15 16
Condition66%4/6
$key eq "q" or $key eq "interrupt" or $key eq "eof"
l!l&&r!l&&!r
1015
$key eq "q" or $key eq "interrupt"
l!l&&r!l&&!r
1015
MC/DC33%1/3
$key eq "q" or $key eq "interrupt" or $key eq "eof"
$key eq "q" $key eq "interrupt" $key eq "eof"
Truth table50%2/4
A or B or C
A= $key eq "q"B= $key eq "interrupt"C= $key eq "eof"
ABCresult
1XX1
01X1
0011
0000
419 15 ▶ return 'bot' if $key eq 'b';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "b" 1 14 15
420  
421 14 ▶ if ($key eq 'enter' || $key eq ' ') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "enter" or $key eq " ") 8 6 14
Condition66%2/3
$key eq "enter" or $key eq " "
l!l&&r!l&&!r
806
MC/DC50%1/2
$key eq "enter" or $key eq " "
$key eq "enter" $key eq " "
Truth table66%2/3
A or B
A= $key eq "enter"B= $key eq " "
ABresult
1X1
011
000
422 8 my $here = $all[$at];
423 8 ▶ if ($held{$here}) { delete $held{$here} }
Branch: 2 branches
BranchTrueFalseTotal
if ($held{$here}) { } 1 7 8
elsif (scalar keys %held < 2) { } 7 0 7
424 7 elsif (scalar keys %held < 2) { $held{$here} = 1 }
425 8 ▶ return [ sort { $a <=> $b } keys %held ] if scalar keys %held == 2;
Branch: 1 branch
BranchTrueFalseTotal
if scalar keys %held == 2 3 5 8
426 5 next;
427 }
428 6 ▶ $at = $self->step(\@all, $at, $key) if $key =~ /\A(?:up|down|left|right|home|end|tab)\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $key =~ /\A(?:up|down|left|right|home|end|tab)\z/ 6 0 6
429 }
430 }
431  
432 0 ▶sub random_index {
Subroutine random_index: not called CC=1 SCAR=6.9
Pod documented
433 0 my ($self, $count) = @_;
434 0 return int(rand($count));
435 }
436  
437 17 ▶sub step {
Subroutine step: called CC=8 SCAR=21.0
Pod documented
438 17 my ($self, $items, $at, $key) = @_;
439 17 ▶ return 0 if $key eq 'home';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "home" 1 16 17
440 16 ▶ return $#$items if $key eq 'end';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "end" 2 14 16
441 14 ▶ return ($at + 1) % @$items if $key eq 'right' || $key eq 'down' || $key eq 'tab';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "right" or $key eq "down" or $key eq "tab" 12 2 14
Condition100%6/6
$key eq "right" or $key eq "down" or $key eq "tab"
l!l&&r!l&&!r
1112
$key eq "right" or $key eq "down"
l!l&&r!l&&!r
1013
MC/DC100%3/3
$key eq "right" or $key eq "down" or $key eq "tab"
$key eq "right" $key eq "down" $key eq "tab"
Truth table100%4/4
A or B or C
A= $key eq "right"B= $key eq "down"C= $key eq "tab"
ABCresult
1XX1
01X1
0011
0000
442 2 ▶ return ($at - 1) % @$items if $key eq 'left' || $key eq 'up';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "left" or $key eq "up" 2 0 2
Condition66%2/3
$key eq "left" or $key eq "up"
l!l&&r!l&&!r
110
MC/DC0%0/2
$key eq "left" or $key eq "up"
$key eq "left" $key eq "up"
Truth table66%2/3
A or B
A= $key eq "left"B= $key eq "up"
ABresult
1X1
011
000
443 0 return $at;
444 }
445  
446 1 ▶sub split_cards {
Subroutine split_cards: called CC=9 SCAR=24.2
Pod documented
447 1 my ($self) = @_;
448  
449 1 $self->print_header(q|Split cards lowest goes first|);
450  
451 1 $self->show_face_down_split(52);
452  
453 1 $self->print_footer(q|Pick a number: |);
454  
455 1 my $number = $self->read_line;
456  
457 1 ▶ return 'quit' unless defined $number;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $number 0 1 1
458  
459 1 chomp($number);
460  
461 1 ▶ if ($number !~ m/^\d+$/ || $number < 1 || $number > 52) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $number =~ /^\d+$/ or $number < 1 or $number > 52) 0 1 1
Condition33%2/6
not $number =~ /^\d+$/ or $number < 1 or $number > 52
l!l&&r!l&&!r
001
not $number =~ /^\d+$/ or $number < 1
l!l&&r!l&&!r
001
MC/DC0%0/3
not $number =~ /^\d+$/ or $number < 1 or $number > 52
not $number =~ /^\d+$/ $number < 1 $number > 52
Truth table25%1/4
not(A) or B or C
A= not $number =~ /^\d+$/B= $number < 1C= $number > 52
ABCresult
1XX1
01X1
0011
0000
462 0 $self->clear_screen();
463 0 return $self->split_cards();
464 }
465  
466 1 my $low = $self->board->deck->get($number - 1);
467  
468 # BOTH INDEXES ARE 0-BASED HERE. 0.16 drew the bot's card with
469 # `get($rand - 1)` against a 0-based `rand(52)`, so rand 0 reached
470 # `get(-1)`, the LAST card of the deck, and the "is it the same card"
471 # guard compared a 1-based number with a 0-based index and so let the
472 # pair collide whenever $number was $rand + 1.
473 1 my $mine = $number - 1;
474 1 my $rand = $self->random_index(52);
475  
476 1 ▶ while ($rand == $mine) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rand == $mine) 0 1 1
477 0 $rand = $self->random_index(52);
478 }
479  
480 1 my $bot = $self->board->deck->get($rand);
481  
482 1 $self->clear_screen();
483  
484 1 ▶ my $dealer = $low->value < $bot->value ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$low->value < $bot->value ? : 0 1 1
485  
486 1 ▶ $self->print_header(q|Low cards picked, | . ($dealer ? "you are the dealer" : "they are the dealer"));
Branch: 1 branch
BranchTrueFalseTotal
$dealer ? : 0 1 1
487  
488 1 $self->render_card($bot, 2, 45);
489  
490 1 $self->render_card($low, 27, 45);
491  
492 1 ▶ $self->board->set_crib_player($dealer ? 'player2' : 'player1');
Branch: 1 branch
BranchTrueFalseTotal
$dealer ? : 0 1 1
493  
494 1 $self->print_footer(q|Press enter to continue|);
495  
496 1 $self->dealer($dealer);
497  
498 1 $self->read_line;
499 }
500  
501 1 ▶sub draw_cards {
Subroutine draw_cards: called CC=1 SCAR=0.0
Pod documented
502 1 my ($self) = @_;
503  
504 1 $self->board->draw_hands();
505 }
506  
507 0 ▶sub discard_cards {
Subroutine discard_cards: not called CC=11 SCAR=48.8
Pod documented
508 0 my ($self) = @_;
509  
510 0 $self->clear_screen();
511  
512 0 $self->print_header(q|Discard two cards to the crib, seperate the indexes with a space.|);
513  
514 0 my $hands = $self->board->get_hands;
515  
516 0 my $cards_index;
517 0 ▶ if ($self->picking && $self->enter_raw) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->picking and $self->enter_raw) { } 0 0 0
Condition0%0/3
$self->picking and $self->enter_raw
!ll&&!rl&&r
000
MC/DC0%0/2
$self->picking and $self->enter_raw
$self->picking $self->enter_raw
Truth table0%0/3
A and B
A= $self->pickingB= $self->enter_raw
ABresult
0X0
100
111
518 0 my $picked = $self->pick_discards();
519 0 ▶ return 'quit' if !ref $picked && $picked eq 'quit';
Branch: 1 branch
BranchTrueFalseTotal
if not ref $picked and $picked eq "quit" 0 0 0
Condition0%0/3
not ref $picked and $picked eq "quit"
!ll&&!rl&&r
000
MC/DC0%0/2
not ref $picked and $picked eq "quit"
not ref $picked $picked eq "quit"
Truth table0%0/3
not(A) and B
A= not ref $pickedB= $picked eq "quit"
ABresult
0X0
100
111
520 0 ▶ $cards_index = ref $picked ? join(' ', @$picked) : 'b';
Branch: 1 branch
BranchTrueFalseTotal
ref $picked ? : 0 0 0
521 }
522 else {
523 0 $self->render_opponent_cards(6);
524  
525 0 $self->render_player_cards($hands->player2->cards, 26, 5);
526  
527 0 $self->print_footer(q|Discard cards: |);
528  
529 0 $cards_index = $self->read_line;
530  
531 0 ▶ return 'quit' unless defined $cards_index;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cards_index 0 0 0
532  
533 0 chomp($cards_index);
534 }
535  
536 0 my @cards;
537 0 ▶ if ($cards_index =~ m/b/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cards_index =~ /b/i) { } 0 0 0
538 0 my @worst = $self->board->identify_worst_cards('player2');
539 0 @cards = @{$worst[0]};
540 } else {
541 @cards = map {
542 0 $self->board->get_card('player2', $_ - 1);
543 0 ▶ } grep { $_ =~ m/^\d+$/ && $_ >= 1 && $_ <= 6 } split " ", $cards_index;
Condition0%0/6
$_ =~ /^\d+$/ && $_ >= 1 && $_ <= 6
!ll&&!rl&&r
000
$_ =~ /^\d+$/ and $_ >= 1
!ll&&!rl&&r
000
MC/DC0%0/3
$_ =~ /^\d+$/ && $_ >= 1 && $_ <= 6
$_ =~ /^\d+$/ $_ >= 1 $_ <= 6
Truth table0%0/4
A and B && C
A= $_ =~ /^\d+$/B= $_ >= 1C= $_ <= 6
ABCresult
0XX0
10X0
1100
1111
544 }
545  
546 0 ▶ unless (scalar @cards == 2) {
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @cards == 2) 0 0 0
547 0 $self->discard_cards();
548 0 return;
549 }
550  
551 0 $self->board->crib_player_cards('player2', \@cards);
552  
553 0 my @bot = $self->board->identify_worst_cards('player1');
554  
555 0 $self->board->crib_player_cards('player1', $bot[0]);
556  
557 0 $self->crib_set(1);
558 }
559  
560 4 ▶sub starter {
Subroutine starter: called CC=6 SCAR=18.0
Pod documented
561 4 my ($self) = @_;
562  
563 4 $self->clear_screen();
564  
565 4 my $count = scalar @{$self->board->deck->deck};
566 4 ▶ if ($self->dealer) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->dealer) 1 3 4
567 # WHAT IS LEFT, NOT A HARDCODED 39. The deal takes six cards a side
568 # and 0.16 wrote the remainder as a literal, so the bot's cut could
569 # only ever reach the first 39 of the 40 that are there.
570 1 my $card = $self->board->deck->get($self->random_index($count));
571 1 $self->starter_card($card);
572 1 $self->board->add_starter_card('player1', $card);
573 1 return;
574 }
575  
576 3 $self->print_header(q|Split deck for starter card.|);
577  
578 3 $self->show_face_down_split(40, 13);
579  
580 3 $self->print_footer(q|Split deck: |);
581  
582 3 my $number = $self->read_line;
583  
584 3 ▶ return 'quit' unless defined $number;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $number 0 3 3
585  
586 3 chomp($number);
587  
588 # THE DECK HERE IS WHAT IS LEFT, NOT 52. Six cards a side and four to the
589 # crib are already dealt, so the split is over `$count` cards and 0.16
590 # checked against 52: a number past the end reached `get` for a card that
591 # is not there, and the starter came back undef.
592 3 ▶ if ($number !~ m/^\d+$/ || $number < 1 || $number > $count) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $number =~ /^\d+$/ or $number < 1 or $number > $count) 1 2 3
Condition50%3/6
not $number =~ /^\d+$/ or $number < 1 or $number > $count
l!l&&r!l&&!r
012
not $number =~ /^\d+$/ or $number < 1
l!l&&r!l&&!r
003
MC/DC33%1/3
not $number =~ /^\d+$/ or $number < 1 or $number > $count
not $number =~ /^\d+$/ $number < 1 $number > $count
Truth table50%2/4
not(A) or B or C
A= not $number =~ /^\d+$/B= $number < 1C= $number > $count
ABCresult
1XX1
01X1
0011
0000
593 1 $self->clear_screen();
594 1 return $self->starter();
595 }
596  
597 2 my $card = $self->board->deck->get($number - 1);
598 2 $self->starter_card($card);
599 2 my $scored = $self->board->add_starter_card('player2', $card);
600 2 return;
601 }
602  
603 4 our %FRAME = (
604 light => [ '┌', '─', '┐', '│', '└', '┘' ],
605 double => [ '╔', '═', '╗', '║', '╚', '╝' ],
606 heavy => [ '┏', '━', '┓', '┃', '┗', '┛' ],
607 );
608  
609 194 ▶sub render_card {
Subroutine render_card: called CC=7 SCAR=19.5
Pod documented
610 194 my ($self, $card, $row, $col, $frame) = @_;
611  
612 194 $self->set_cursor_vertical($row);
613 194 $self->set_cursor_horizontal($col);
614 194 ▶ my $color = $card->suit =~ m/H|D/ ? 91 : 90;
Branch: 1 branch
BranchTrueFalseTotal
$card->suit =~ /H|D/ ? : 153 41 194
615  
616 194 my %suits = (
617 H => '♥',
618 D => '♦',
619 C => '♣',
620 S => '♠'
621 );
622  
623 194 ▶ my ($tl, $bar, $tr, $side, $bl, $br) = @{ $FRAME{ $frame || 'light' } || $FRAME{light} };
Condition60%3/5
$Game::Cribbage::Terminal::FRAME{$frame or 'light'} || $Game::Cribbage::Terminal::FRAME{'light'}
l!l&&r!l&&!r
19400
$frame || 'light'
l!l
31163
MC/DC0%0/2
$Game::Cribbage::Terminal::FRAME{$frame or 'light'} || $Game::Cribbage::Terminal::FRAME{'light'}
$Game::Cribbage::Terminal::FRAME{$frame or 'light'} $Game::Cribbage::Terminal::FRAME{'light'}
MC/DC100%1/1
$frame || 'light'
$frame
Truth table33%1/3
A || B
A= $Game::Cribbage::Terminal::FRAME{$frame or 'light'}B= $Game::Cribbage::Terminal::FRAME{'light'}
ABresult
1X1
011
000
Truth table100%2/2
A
A= $frame
Aresult
11
00
624 194 my $top = $tl . ($bar x 9) . $tr;
625 194 my $bot = $bl . ($bar x 9) . $br;
626  
627 my @card = (
628 $top,
629 sprintf("%s%s. . . .%s", $side, $card->symbol =~ m/10/ ? $card->symbol : $card->symbol . " ", $side),
630 sprintf("%s. . . . .%s", $side, $side),
631 sprintf("%s. . . . .%s", $side, $side),
632 194 ▶ sprintf("%s. . %s . .%s", $side, $suits{$card->suit}, $side),
Branch: 2 branches
BranchTrueFalseTotal
$card->symbol =~ /10/ ? : 14 180 194
$card->symbol =~ /10/ ? : 14 180 194
633 sprintf("%s. . . . .%s", $side, $side),
634 sprintf("%s. . . . .%s", $side, $side),
635 sprintf("%s. . . .%s%s", $side, $card->symbol =~ m/10/ ? $card->symbol : " " . $card->symbol, $side),
636 $bot
637 );
638  
639 194 for (@card) {
640 1746 $self->say($_, 1, 0, $color);
641 1746 $self->set_cursor_horizontal($col);
642 }
643 }
644  
645  
646 28 ▶sub render_opponent_cards {
Subroutine render_opponent_cards: called CC=3 SCAR=11.0
Pod documented
647 28 my ($self, $num) = @_;
648  
649 28 ▶ return unless $num;
Branch: 1 branch
BranchTrueFalseTotal
unless $num 11 17 28
650  
651 17 $self->set_cursor_vertical(2);
652 17 $self->set_cursor_horizontal((100 - ($num * 4)) / 2);
653  
654 17 my $string = "┌──" x ($num - 1);
655 17 $self->say($string);
656 17 $self->say("┌─────────┐", 1, 1);
657 17 $self->set_cursor_horizontal((100 - ($num * 4)) / 2);
658 17 for (0..6) {
659 119 $string = "│. " x ($num - 1);
660 119 $self->say($string);
661 119 $self->say("│. . . . .│", 1, 1);
662 119 $self->set_cursor_horizontal((100 - ($num * 4)) / 2);
663 }
664 17 $string = "└──" x ($num - 1);
665 17 $self->say($string);
666 17 $self->say("└─────────┘", 1, 1);
667 }
668  
669 29 ▶sub render_player_cards {
Subroutine render_player_cards: called CC=5 SCAR=16.1
Pod documented
670 29 my ($self, $cards, $top, $left, $all, $marks) = @_;
671 29 my $i = 1;
672 29 for (@{$cards}) {
673 145 ▶ if (!$all && $_->used) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $all and $_->used) 1 144 145
Condition100%3/3
not $all and $_->used
!ll&&!rl&&r
102421
MC/DC100%2/2
not $all and $_->used
not $all $_->used
Truth table100%3/3
not(A) and B
A= not $allB= $_->used
ABresult
0X0
100
111
674 1 $i++;
675 1 next;
676 }
677 144 $left += 11;
678 144 $self->set_cursor_vertical($top);
679 144 $self->set_cursor_horizontal($left + 5);
680 144 $self->say($i);
681 144 ▶ $self->render_card($_, $top + 1, $left, $marks ? $marks->{$i} : undef);
Branch: 1 branch
BranchTrueFalseTotal
$marks ? : 139 5 144
682 144 $i++;
683 }
684 }
685  
686 12 ▶sub render_run_play {
Subroutine render_run_play: called CC=3 SCAR=11.0
Pod documented
687 12 my ($self) = @_;
688  
689 12 my $cards = $self->board->current_play->cards;
690  
691 12 ▶ return unless (scalar @{$cards});
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @{$cards;} 1 11 12
692  
693 11 my $left = 25;
694 11 for (@{$cards}) {
695 31 $self->render_card($_->card, 15, $left);
696 31 $left += 10;
697 }
698 }
699  
700  
701 4 ▶sub show_face_down_split {
Subroutine show_face_down_split: called CC=10 SCAR=23.0
Pod documented
702 4 my ($self, $num, $vertical) = @_;
703  
704 4 ▶ $self->set_cursor_vertical($vertical || 10);
Condition100%2/2
$vertical || 10
l!l
31
MC/DC100%1/1
$vertical || 10
$vertical
Truth table100%2/2
A
A= $vertical
Aresult
11
00
705  
706 4 my $width = ($num / 2) - 1;
707  
708 4 for my $it (0 .. 1) {
709 8 my $string = "┌──" x $width;
710 8 $self->say($string);
711 8 $self->say("┌─────────┐", 1, 1);
712 8 $string = "";
713 8 for (1 .. $width) {
714 164 ▶ my $v = $it ? 26 + $_ : $_;
Branch: 1 branch
BranchTrueFalseTotal
$it ? : 82 82 164
715 164 ▶ $string .= sprintf("│%s", (!$it && $_ < 10 ? "$v " : $v));
Branch: 1 branch
BranchTrueFalseTotal
!$it && $_ < 10 ? : 36 128 164
Condition100%3/3
not $it and $_ < 10
!ll&&!rl&&r
824636
MC/DC100%2/2
not $it and $_ < 10
not $it $_ < 10
Truth table100%3/3
not(A) and B
A= not $itB= $_ < 10
ABresult
0X0
100
111
716 }
717 8 $self->say($string);
718 8 ▶ $self->say(sprintf("│%s. . . .│", $it ? ($width + 1) * 2 : $width + 1), 1, 1);
Branch: 1 branch
BranchTrueFalseTotal
$it ? : 4 4 8
719 8 for (0..4) {
720 40 $string = "│. " x $width;
721 40 $self->say($string);
722 40 $self->say("│. . . . .│", 1, 1);
723 }
724 8 $string = "│. " x $width;
725 8 $self->say($string);
726 8 ▶ $self->say(sprintf("│. . . .%s│", $it ? ($width + 1) * 2 : $width + 1), 1, 1);
Branch: 1 branch
BranchTrueFalseTotal
$it ? : 4 4 8
727 8 $string = "└──" x $width;
728 8 $self->say($string);
729 8 $self->say("└─────────┘", 1, 1);
730 }
731 }
732  
733 35 ▶sub draw_background {
Subroutine draw_background: called CC=4 SCAR=14.0
Pod documented
734 35 for (0 .. 35) {
735 1260 ▶ $_[0]->emit("\e[102;1m") if $_[0]->colour;
Branch: 1 branch
BranchTrueFalseTotal
if $_[0]->colour 0 1260 1260
736 1260 $_[0]->emit(pack("A100", " "));
737 1260 $_[0]->emit("\n");
738 1260 ▶ $_[0]->emit("\e[0m") if $_[0]->colour;
Branch: 1 branch
BranchTrueFalseTotal
if $_[0]->colour 0 1260 1260
739 }
740 35 $_[0]->draw_dealer();
741 35 $_[0]->draw_crib();
742 35 $_[0]->draw_starter();
743 35 $_[0]->reset_cursor();
744 }
745  
746 37 ▶sub draw_starter {
Subroutine draw_starter: called CC=2 SCAR=6.9
Pod documented
747 37 my ($self) = @_;
748  
749 37 ▶ return unless $self->starter_card;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->starter_card 36 1 37
750  
751 1 $self->render_card($self->starter_card, 15, 2);
752 }
753  
754  
755 38 ▶sub draw_crib {
Subroutine draw_crib: called CC=5 SCAR=16.1
Pod documented
756 38 my ($self) = shift;
757 38 my $dealer = $self->dealer;
758 38 ▶ return unless defined $dealer;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dealer 0 38 38
759 38 ▶ return unless $self->crib_set;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->crib_set 36 2 38
760 2 my @card = (
761 "┌─────────┐",
762 "│. . . . .│",
763 "│. . . . .│",
764 "│. . . . .│",
765 "│. . . . .│",
766 "│. . . . .│",
767 "│. . . . .│",
768 "│. . . . .│",
769 "└─────────┘"
770 );
771  
772 2 ▶ if ($dealer) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dealer) { } 1 1 2
773 1 $self->set_cursor_vertical(27);
774 } else {
775 1 $self->set_cursor_vertical(3);
776 }
777  
778 2 for (@card) {
779 18 $self->set_cursor_horizontal(10);
780 18 $self->say($_, 1, 0);
781 }
782 }
783  
784 1 ▶sub draw_go {
Subroutine draw_go: called CC=1 SCAR=0.0
Pod documented
785 1 my ($self, $top, $left) = @_;
786 1 my $message = ' GO ';
787 1 $self->set_cursor_vertical($top);
788 1 $self->say($message, 0, 0, 31, 40);
789 }
790  
791  
792 38 ▶sub draw_dealer {
Subroutine draw_dealer: called CC=3 SCAR=11.0
Pod documented
793 38 my ($self) = @_;
794  
795 38 my $dealer = $self->dealer;
796  
797 38 ▶ return unless defined $dealer;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dealer 0 38 38
798 38 my $message = ' Dealer ';
799 38 ▶ if ($dealer) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dealer) { } 2 36 38
800 2 $self->set_cursor_vertical(34);
801 2 $self->say($message, 0, 0, 31, 40);
802 } else {
803 36 $self->set_cursor_vertical(3);
804 36 $self->say($message, 0, 0, 31, 40);
805 }
806 }
807  
808 13 ▶sub draw_scores {
Subroutine draw_scores: called CC=4 SCAR=13.9
Pod documented
809 13 my ($self) = @_;
810 13 my $message;
811 13 ▶ return unless $self->board;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->board 1 12 13
812 12 ▶ if ($self->board && $self->board->current_play_score) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->board and $self->board->current_play_score) 11 1 12
Condition66%2/3
$self->board and $self->board->current_play_score
!ll&&!rl&&r
0111
MC/DC50%1/2
$self->board and $self->board->current_play_score
$self->board $self->board->current_play_score
Truth table66%2/3
A and B
A= $self->boardB= $self->board->current_play_score
ABresult
0X0
100
111
813 11 $message = pack("A2", $self->board->current_play_score);
814 11 $self->set_cursor_vertical(14);
815 11 $self->set_cursor_horizontal(49);
816 11 $self->say($message, 0, 0, 31, 40);
817 }
818 12 $message = ' Score: ' . $self->board->score->player2->{current} . ' ';
819 12 $self->set_cursor_vertical(34);
820 12 $self->set_cursor_horizontal(100 - (length($message) + 1));
821 12 $self->say($message, 0, 0, 31, 40);
822 12 $message = ' Score: ' . $self->board->score->player1->{current} . ' ';
823 12 $self->set_cursor_vertical(3);
824 12 $self->set_cursor_horizontal(100 - (length($message) + 1));
825 12 $self->say($message, 0, 0, 31, 40);
826 }
827  
828 0 ▶sub winner {
Subroutine winner: not called CC=6 SCAR=37.4
Pod documented
829 0 my ($self, $who) = @_;
830 0 $self->emit("\e[0m\e[2J\e[0;1H");
831 0 for (0 .. 35) {
832 0 ▶ $self->emit("\e[102;1m") if $self->colour;
Branch: 1 branch
BranchTrueFalseTotal
if $self->colour 0 0 0
833 0 $self->emit(pack("A100", " "));
834 0 $self->emit("\n");
835 0 ▶ $self->emit("\e[0m") if $self->colour;
Branch: 1 branch
BranchTrueFalseTotal
if $self->colour 0 0 0
836 }
837 0 $self->set_cursor_vertical(17);
838 0 my $player = $self->board->players->[1];
839 0 ▶ $self->say($who eq 'player2'
Branch: 1 branch
BranchTrueFalseTotal
$who eq 'player2' ? : 0 0 0
840 ? sprintf(q|Congratulations %s, you won the game!|, $player->name)
841 : sprintf(q|Unlucky %s, you lost this time!|, $player->name)
842 );
843 0 $self->read_line;
844 0 ▶ return $who eq 'player2' ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$who eq 'player2' ? : 0 0 0
845 }
846  
847  
848 0 ▶sub keys_available {
Subroutine keys_available: not called CC=4 SCAR=30.0
Pod documented
849 0 my ($self) = @_;
850 0 ▶ return 1 if $self->keysource;
Branch: 1 branch
BranchTrueFalseTotal
if $self->keysource 0 0 0
851 0 ▶ return 0 unless eval { -t $self->in };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { -t $self->in } } 0 0 0
852 0 ▶ return eval { require Term::ReadKey; 1 } ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require Term::ReadKey; 1 } } ? : 0 0 0
853 }
854  
855 0 ▶sub picking {
Subroutine picking: not called CC=5 SCAR=34.0
Pod documented
856 0 my ($self, @set) = @_;
857 0 ▶ $self->_picking($set[0] ? 1 : 0) if @set;
Branch: 2 branches
BranchTrueFalseTotal
if @set 0 0 0
$set[0] ? : 0 0 0
858 0 ▶ return $self->_picking ? 1 : 0 if defined $self->_picking;
Branch: 2 branches
BranchTrueFalseTotal
if defined $self->_picking 0 0 0
$self->_picking ? : 0 0 0
859 0 return $self->keys_available;
860 }
861  
862 7592 ▶sub colour {
Subroutine colour: called CC=8 SCAR=27.0
Pod documented
863 7592 my ($self, @set) = @_;
864 7592 ▶ $self->_colour($set[0] ? 1 : 0) if @set;
Branch: 2 branches
BranchTrueFalseTotal
if @set 0 7592 7592
$set[0] ? : 0 0 0
865 7592 ▶ return $self->_colour ? 1 : 0 if defined $self->_colour;
Branch: 2 branches
BranchTrueFalseTotal
if defined $self->_colour 0 7592 7592
$self->_colour ? : 0 0 0
866 7592 ▶ return 0 if defined $ENV{NO_COLOR} && length $ENV{NO_COLOR};
Branch: 1 branch
BranchTrueFalseTotal
if defined $ENV{"NO_COLOR"} and length $ENV{"NO_COLOR"} 0 7592 7592
Condition33%1/3
defined $ENV{"NO_COLOR"} and length $ENV{"NO_COLOR"}
!ll&&!rl&&r
759200
MC/DC0%0/2
defined $ENV{"NO_COLOR"} and length $ENV{"NO_COLOR"}
defined $ENV{"NO_COLOR"} length $ENV{"NO_COLOR"}
Truth table33%1/3
A and B
A= defined $ENV{"NO_COLOR"}B= length $ENV{"NO_COLOR"}
ABresult
0X0
100
111
867 7592 ▶ return eval { -t $self->out } ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { -t $self->out } } ? : 0 7592 7592
868 }
869  
870 0 ▶sub enter_raw {
Subroutine enter_raw: not called CC=5 SCAR=34.0
Pod documented
871 0 my ($self) = @_;
872 0 ▶ return $self if $self->raw;
Branch: 1 branch
BranchTrueFalseTotal
if $self->raw 0 0 0
873 0 ▶ if ($self->keysource) { $self->raw(1); return $self }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->keysource) 0 0 0
874 0 ▶ return undef unless $self->keys_available;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->keys_available 0 0 0
875 0 ▶ return undef unless eval { Term::ReadKey::ReadMode(3, $self->in); 1 };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { Term::ReadKey::ReadMode(3, $self->in); 1 } } 0 0 0
876 0 $self->raw(1);
877 0 return $self;
878 }
879  
880 3 ▶sub leave_raw {
Subroutine leave_raw: called CC=3 SCAR=18.1
Pod documented
881 3 my ($self) = @_;
882 3 ▶ return $self unless $self->raw;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->raw 3 0 3
883 0 ▶ eval { Term::ReadKey::ReadMode(0, $self->in) } unless $self->keysource;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->keysource 0 0 0
884 0 $self->raw(0);
885 0 return $self;
886 }
887  
888 44 ▶sub read_char {
Subroutine read_char: called CC=6 SCAR=28.6
Pod documented
889 44 my ($self, $wait) = @_;
890 44 my $pending = $self->pending;
891 44 ▶ return shift @$pending if @$pending;
Branch: 1 branch
BranchTrueFalseTotal
if @$pending 0 44 44
892 44 ▶ return $self->keysource->($wait) if $self->keysource;
Branch: 1 branch
BranchTrueFalseTotal
if $self->keysource 44 0 44
893 0 ▶ my $char = Term::ReadKey::ReadKey($wait ? 0 : -1, $self->in);
Branch: 1 branch
BranchTrueFalseTotal
$wait ? : 0 0 0
894 0 ▶ return $char if defined $char || $wait;
Branch: 1 branch
BranchTrueFalseTotal
if defined $char or $wait 0 0 0
Condition0%0/3
defined $char or $wait
l!l&&r!l&&!r
000
MC/DC0%0/2
defined $char or $wait
defined $char $wait
Truth table0%0/3
A or B
A= defined $charB= $wait
ABresult
1X1
011
000
895 0 select undef, undef, undef, 0.05;
896 0 return Term::ReadKey::ReadKey(-1, $self->in);
897 }
898  
899 26 ▶sub read_key {
Subroutine read_key: called CC=4 SCAR=13.9
Pod documented
900 26 my ($self) = @_;
901 26 my $char = $self->read_char(1);
902 26 ▶ return undef unless defined $char;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $char 0 26 26
903 26 ▶ return $CONTROL{$char} if $CONTROL{$char};
Branch: 1 branch
BranchTrueFalseTotal
if $Game::Cribbage::Terminal::CONTROL{$char} 11 15 26
904 15 ▶ return $self->read_sequence if $char eq "\e";
Branch: 1 branch
BranchTrueFalseTotal
if $char eq "\e" 9 6 15
905 6 return $char;
906 }
907  
908 9 ▶sub read_sequence {
Subroutine read_sequence: called CC=8 SCAR=24.4
Pod documented
909 9 my ($self) = @_;
910 9 my $opener = $self->read_char(0);
911 9 ▶ return 'escape' unless defined $opener;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opener 0 9 9
912 9 ▶ unless ($opener eq '[' || $opener eq 'O') {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opener eq "[" or $opener eq "O") 0 9 9
Condition33%1/3
$opener eq "[" or $opener eq "O"
l!l&&r!l&&!r
900
MC/DC0%0/2
$opener eq "[" or $opener eq "O"
$opener eq "[" $opener eq "O"
Truth table33%1/3
A or B
A= $opener eq "["B= $opener eq "O"
ABresult
1X1
011
000
913 0 unshift @{ $self->pending }, $opener;
914 0 return 'escape';
915 }
916 9 my $tail = '';
917 9 ▶ while (length $tail < 8) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $tail < 8) 9 0 9
918 9 my $char = $self->read_char(0);
919 9 ▶ last unless defined $char;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $char 0 9 9
920 9 $tail .= $char;
921 9 ▶ last if $char =~ /[A-Za-z~]/;
Branch: 1 branch
BranchTrueFalseTotal
if $char =~ /[A-Za-z~]/ 9 0 9
922 }
923 9 ▶ return $SEQUENCE{$tail} || 'escape';
Condition50%1/2
$Game::Cribbage::Terminal::SEQUENCE{$tail} || 'escape'
l!l
90
MC/DC0%0/1
$Game::Cribbage::Terminal::SEQUENCE{$tail} || 'escape'
$Game::Cribbage::Terminal::SEQUENCE{$tail}
Truth table50%1/2
A
A= $Game::Cribbage::Terminal::SEQUENCE{$tail}
Aresult
11
00
924 }
925  
926 10345 ▶sub emit {
Subroutine emit: called CC=1 SCAR=0.0
Pod documented
927 10345 my ($self, @text) = @_;
928 10345 my $fh = $self->out;
929 10345 print {$fh} @text;
930 10345 1;
931 }
932  
933 8 ▶sub read_line {
Subroutine read_line: called CC=1 SCAR=0.0
Pod documented
934 8 my ($self) = @_;
935 8 my $fh = $self->in;
936 8 my $line = <$fh>;
937 8 return $line;
938 }
939  
940 35 ▶sub clear_screen {
Subroutine clear_screen: called CC=1 SCAR=0.0
Pod documented
941 35 $_[0]->emit("\e[0m\e[2J\e[0;1H");
942 35 $_[0]->draw_background();
943 }
944  
945 36 ▶sub reset_cursor {
Subroutine reset_cursor: called CC=1 SCAR=0.0
Pod documented
946 36 $_[0]->emit("\e[;2H");
947 }
948  
949 504 ▶sub set_cursor_vertical {
Subroutine set_cursor_vertical: called CC=1 SCAR=0.0
Pod documented
950 504 $_[0]->emit("\e[$_[1];2H");
951 }
952  
953 2687 ▶sub set_cursor_horizontal {
Subroutine set_cursor_horizontal: called CC=1 SCAR=0.0
Pod documented
954 2687 $_[0]->emit("\e[$_[1]G");
955 }
956  
957 2502 ▶sub say {
Subroutine say: called CC=7 SCAR=19.5
Pod documented
958 2502 my ($self, $msg, $nl, $indent, $color, $back) = @_;
959 2502 ▶ $color ||= 90;
Condition100%2/2
$color ||= 90
l!l
1889613
MC/DC100%1/1
$color ||= 90
$color
Truth table100%2/2
A
A= $color
Aresult
11
00
960 2502 ▶ $back ||= 102;
Condition100%2/2
$back ||= 102
l!l
1432359
MC/DC100%1/1
$back ||= 102
$back
Truth table100%2/2
A
A= $back
Aresult
11
00
961 2502 ▶ $self->emit("\e[$color;1;1m\e[$back;1m") if $self->colour;
Branch: 1 branch
BranchTrueFalseTotal
if $self->colour 0 2502 2502
962 2502 $self->emit($msg);
963 2502 ▶ $self->emit("\n") if $nl;
Branch: 1 branch
BranchTrueFalseTotal
if $nl 1990 512 2502
964 2502 ▶ $self->set_cursor_horizontal(2) if $indent;
Branch: 1 branch
BranchTrueFalseTotal
if $indent 226 2276 2502
965 2502 ▶ $self->emit("\e[0m") if $self->colour;
Branch: 1 branch
BranchTrueFalseTotal
if $self->colour 0 2502 2502
966 2502 1;
967 }
968  
969 34 ▶sub print_header {
Subroutine print_header: called CC=2 SCAR=7.0
Pod documented
970 34 my ($self, $message) = @_;
971 34 $self->set_cursor_vertical(0);
972 34 $self->set_cursor_horizontal(0);
973 34 ▶ $self->emit("\e[40;1m") if $self->colour;
Branch: 1 branch
BranchTrueFalseTotal
if $self->colour 0 34 34
974 34 $self->emit(pack("A100", " "));
975 34 $self->set_cursor_horizontal(2);
976 34 $self->say($message, 0, 0, 31, 40);
977 }
978  
979 34 ▶sub print_footer {
Subroutine print_footer: called CC=2 SCAR=7.0
Pod documented
980 34 my ($self, $message) = @_;
981 34 $self->set_cursor_vertical(36);
982 34 $self->set_cursor_horizontal(0);
983 34 ▶ $self->emit("\e[40;1m") if $self->colour;
Branch: 1 branch
BranchTrueFalseTotal
if $self->colour 0 34 34
984 34 $self->emit(pack("A100", " "));
985 34 $self->set_cursor_horizontal(2);
986 34 $self->say($message, 0, 0, 31, 40);
987 34 $self->set_cursor_horizontal(length($message) + 2);
988 }
989  
990 3 ▶sub _usage {
Subroutine _usage: called CC=1 SCAR=0.0
991 3 return <<'USAGE';
992 cribbage - cribbage on a terminal
993  
994 usage: cribbage [options]
995  
996 --pick choose cards with the arrow keys; --no-pick to type numbers
997 --help this
998  
999 exit status:
1000 0 you won, or you quit
1001 1 you lost
1002 2 a bad option
1003  
1004 With a terminal and Term::ReadKey the arrow keys walk the cards you can play
1005 and the box round one says what it would peg. Without either, or with
1006 --no-pick, a card is chosen by typing its number. Colour is off when NO_COLOR
1007 is set or the output is not a terminal.
1008 USAGE
1009 }
1010  
1011 6 ▶sub run {
Subroutine run: called CC=8 SCAR=21.2
Pod documented
1012 6 my ($class, @argv) = @_;
1013 6 my %opt;
1014  
1015 6 my $ok = eval {
1016 6 ▶ local $SIG{__WARN__} = sub { print STDERR "cribbage: $_[0]" };
Subroutine __ANON__: called CC=1 SCAR=0.0
1017 6 Getopt::Long::GetOptionsFromArray(\@argv, \%opt, 'pick!', 'help');
1018 };
1019 6 ▶ if (!$ok) { print STDERR _usage(); return 2 }
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 1 5 6
1020 5 ▶ if ($opt{help}) { print _usage(); return 0 }
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"help"}) 1 4 5
1021 4 ▶ if (@argv) { print STDERR "cribbage: not an option: $argv[0]\n", _usage(); return 2 }
Branch: 1 branch
BranchTrueFalseTotal
if (@argv) 1 3 4
1022  
1023 3 my $terminal = $class->new(picking => $opt{pick});
1024  
1025 3 local @SIG{qw(INT TERM HUP)};
1026 3 for my $sig (qw(INT TERM HUP)) {
1027 9 ▶ $SIG{$sig} = sub { $terminal->leave_raw; $SIG{$sig} = 'DEFAULT'; kill $sig, $$ };
Subroutine __ANON__: not called CC=1 SCAR=3.5
1028 }
1029  
1030 3 my $status = eval { $terminal->start };
1031 3 my $died = $@;
1032 3 $terminal->leave_raw;
1033 3 $terminal->emit("\e[0m\n");
1034 3 ▶ die $died if $died;
Branch: 1 branch
BranchTrueFalseTotal
if $died 0 3 3
1035  
1036 3 ▶ return $status && $status =~ /^\d+$/ ? $status : 0;
Branch: 1 branch
BranchTrueFalseTotal
$status && $status =~ /^\d+$/ ? : 0 3 3
Condition33%1/3
$status and $status =~ /^\d+$/
!ll&&!rl&&r
030
MC/DC0%0/2
$status and $status =~ /^\d+$/
$status $status =~ /^\d+$/
Truth table33%1/3
A and B
A= $statusB= $status =~ /^\d+$/
ABresult
0X0
100
111
1037 }
1038  
1039 4 1;
1040  
1041 __END__
↑ Summary Cribbage/Player/Hand.pm »