Cribbage/Terminal.pm

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