Cribbage.pm

filter: Statementstmt 31.4% 114 / 363 Branchbran 27.2% 30 / 110 Conditioncond 25.0% 9 / 36 MC/DCmcdc 21.7% 5 / 23 Subroutinesub 53.3% 16 / 30 Podpod 100% 28 / 28 totaltotal 34.2% 202 / 590 CC 79 SCAR 44.8
CC
79
Cov
30%
play_hand
342.0
init_draw
132.0
show_face_down_split
110.0
CRAP 88.0
↑ Summary Cribbage/Player/Hand.pm »
1 package Game::Cribbage;
2  
3 2 our $VERSION = "0.16";
4  
5 2 ▶use Object::Proto::Sugar -types;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Game::Cribbage::Board;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 has board => (
9 is => 'rw',
10 isa => Object
11 2 );
12  
13 has [qw/dealer crib_set/] => (
14 is => 'rw',
15 isa => Bool,
16 default => 0
17 2 );
18  
19 has starter_card => (
20 is => 'rw'
21 2 );
22  
23 0 ▶sub start {
Subroutine start: not called CC=1 SCAR=6.9
Pod documented
24 0 my ($self, %params) = @_;
25  
26 0 $self->clear_screen;
27  
28 0 $self->print_header("Welcome to terminal Cribbage");
29  
30 0 $self->print_footer("Insert name: ");
31  
32 0 my $name = <STDIN>;
33  
34 0 chomp($name);
35  
36 0 my $board = Game::Cribbage::Board->new();
37  
38 0 $board->add_player(name => 'Bot');
39 0 $board->add_player(name => $name);
40  
41 0 $board->start_game();
42  
43 0 $self->board($board);
44  
45 0 $self->init_game();
46 }
47  
48 0 ▶sub init_game {
Subroutine init_game: not called CC=1 SCAR=6.9
Pod documented
49 0 my ($self) = @_;
50  
51 0 $self->clear_screen;
52  
53 0 $self->reset_cursor();
54  
55 0 $self->split_cards();
56  
57 0 $self->init_draw();
58 }
59  
60 0 ▶sub init_draw {
Subroutine init_draw: not called CC=11 SCAR=48.8
Pod documented
61 0 my ($self, $switch) = @_;
62  
63 0 $self->crib_set(0);
64 0 $self->starter_card(undef);
65 0 ▶ $self->dealer(!$self->dealer) if $switch;
Branch: 1 branch
BranchTrueFalseTotal
if $switch 0 0 0
66  
67 0 my $winner;
68 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
69 0 $winner = 'player1';
70 } elsif ($self->board->score->player2->{current} > 120) {
71 0 $winner = 'player2';
72 }
73 0 ▶ return $self->winner($winner) if $winner;
Branch: 1 branch
BranchTrueFalseTotal
if $winner 0 0 0
74  
75 0 $self->draw_cards();
76  
77 0 $self->discard_cards();
78  
79 0 $self->starter();
80  
81 0 ▶ while (!$winner) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($winner) 0 0 0
82 0 $self->clear_screen();
83  
84 0 $self->print_header(q|It is your turn to play a card, type go if you're unable to play.|);
85  
86 0 my $ok = eval { $self->play_hand(); };
87  
88 0 ▶ unless ($@) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($@) 0 0 0
89 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
90 0 $winner = 'player1';
91 0 next;
92 } elsif ($self->board->score->player2->{current} > 120) {
93 0 $winner = 'player2';
94 0 next;
95 }
96  
97  
98 0 my $hands = $self->board->get_hands;
99 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
100 0 eval {
101 0 $self->end_hands();
102 };
103 0 return $self->init_draw(1);
104 }
105  
106 0 ▶ if ($self->board->no_player_can_play) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->board->no_player_can_play) 0 0 0
107 0 eval { $self->board->next_play(); };
108 }
109 }
110 }
111 0 $self->winner($winner);
112 }
113  
114 0 ▶sub end_hands {
Subroutine end_hands: not called CC=3 SCAR=24.8
Pod documented
115 0 my ($self) = @_;
116  
117 0 $self->board->end_play();
118
119 0 my $hands = $self->board->get_hands;
120  
121 0 $self->board->end_hands();
122  
123 0 $self->crib_set(0);
124  
125 0 $self->clear_screen();
126  
127 0 my $last = $self->board->last_round_hands();
128 0 my $player1_score = $last->player1->score->total_score;
129 0 my $player2_score = $last->player2->score->total_score;
130 0 ▶ my $crib_player = $self->dealer ? $self->board->players->[-1]->name : 'Bot';
Branch: 1 branch
BranchTrueFalseTotal
$self->dealer ? : 0 0 0
131 0 ▶ my $player = $self->dealer ? 'player2' : 'player1';
Branch: 1 branch
BranchTrueFalseTotal
$self->dealer ? : 0 0 0
132 0 my $crib_score = $last->$player->crib_score->total_score;
133  
134 0 $self->print_header(sprintf "Bot scored: %s - %s scored: %s - %s crib scored: %s",
135 $player1_score,
136 $self->board->players->[-1]->name,
137 $player2_score,
138 $crib_player,
139 $crib_score
140 );
141  
142 0 $self->draw_scores();
143  
144 0 $self->render_player_cards($hands->player1->cards, 3, 20, 1);
145  
146 0 $crib_player = $hands->crib_player;
147 0 $self->render_player_cards($hands->$crib_player->crib, 15, 20, 1);
148  
149 0 $self->render_player_cards($hands->player2->cards, 26, 20, 1);
150  
151 0 $self->print_footer(q|Press enter to continue|);
152  
153 <STDIN>
154 0 }
155  
156 0 ▶sub play_hand {
Subroutine play_hand: not called CC=18 SCAR=58.3
Pod documented
157 0 my ($self) = @_;
158  
159 0 my $hands = $self->board->get_hands;
160 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
161 0 my $card = $self->board->best_run_play('player1');
162 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
163 0 $self->board->cannot_play('player1');
164 0 $self->draw_go(6, 2);
165 } else {
166 0 $self->board->play_card('player1', $card);
167 0 ▶ if ($self->board->current_play_score == 31) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->board->current_play_score == 31) 0 0 0
168 0 $self->board->next_play();
169 0 return;
170 }
171  
172 0 ▶ if ($self->board->score->player1->{current} > 120) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->board->score->player1->{"current"} > 120) 0 0 0
173 0 return;
174 }
175 }
176 }
177  
178 0 ▶ if ($self->board->player_cannot_play('player2')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->board->player_cannot_play("player2")) 0 0 0
179 0 ▶ if (!$self->board->player_cannot_play('player1')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->board->player_cannot_play("player1")) 0 0 0
180 0 $self->board->set_next_to_play('player1');
181 }
182 0 return;
183 }
184  
185 0 $_[0]->draw_scores();
186  
187 0 $self->render_opponent_cards(scalar grep { !$_->used } @{$hands->player1->cards});
188  
189 0 $self->render_run_play();
190  
191 0 $self->render_player_cards($hands->player2->cards, 26, 20);
192  
193 0 $self->print_footer(q|Pick a card: |);
194  
195 0 my $number = <STDIN>;
196  
197 0 chomp($number);
198  
199 0 ▶ if ($number =~ m/go/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($number =~ /go/i) 0 0 0
200 0 my @can = $self->board->cannot_play('player2');
201 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
202 0 return;
203 }
204 0 $self->draw_go(30);
205 0 return;
206 }
207  
208 0 ▶ if ($number eq 'b') {
Branch: 1 branch
BranchTrueFalseTotal
if ($number eq "b") 0 0 0
209 0 my $card = $self->board->best_run_play('player2');
210 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
211 0 $self->board->cannot_play('player2');
212 0 $self->draw_go(6, 2);
213 } else {
214 0 $self->board->play_card('player2', $card);
215 0 ▶ if ($self->board->current_play_score == 31) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->board->current_play_score == 31) 0 0 0
216 0 $self->board->next_play();
217 }
218 }
219 0 return;
220 }
221
222 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
223 0 return;
224 }
225  
226 0 my $card = $self->board->get_card('player2', $number - 1);
227 0 $self->board->play_card('player2', $card);
228  
229 0 ▶ if ($self->board->current_play_score == 31) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->board->current_play_score == 31) 0 0 0
230 0 $self->board->next_play();
231 }
232  
233 0 return 1;
234 }
235  
236 0 ▶sub split_cards {
Subroutine split_cards: not called CC=7 SCAR=40.3
Pod documented
237 0 my ($self) = @_;
238  
239 0 $self->print_header(q|Split cards lowest goes first|);
240  
241 0 $self->show_face_down_split(52);
242
243 0 $self->print_footer(q|Pick a number: |);
244  
245 0 my $number = <STDIN>;
246
247 0 ▶ if ($number !~ m/^\d+$/ || $number > 52) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $number =~ /^\d+$/ or $number > 52) 0 0 0
Condition0%0/3
not $number =~ /^\d+$/ or $number > 52
l!l&&r!l&&!r
000
MC/DC0%0/2
not $number =~ /^\d+$/ or $number > 52
not $number =~ /^\d+$/ $number > 52
Truth table0%0/3
not(A) or B
A= not $number =~ /^\d+$/B= $number > 52
ABresult
1X1
011
000
248 0 $self->clear_screen();
249 0 $self->split_cards();
250 0 return;
251 }
252  
253 0 my $low = $self->board->deck->get($number - 1);
254  
255 0 my $rand = int(rand(52));
256
257 0 ▶ while ($number == $rand) {
Branch: 1 branch
BranchTrueFalseTotal
if ($number == $rand) 0 0 0
258 0 $rand = int(rand(52));
259 }
260  
261 0 my $bot = $self->board->deck->get($rand - 1);
262  
263 0 $self->clear_screen();
264  
265 0 ▶ my $dealer = $low->value < $bot->value ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$low->value < $bot->value ? : 0 0 0
266  
267 0 ▶ $self->print_header(q|Low cards picked, | . ($dealer ? "you are the dealer" : "they are the dealer"));
Branch: 1 branch
BranchTrueFalseTotal
$dealer ? : 0 0 0
268  
269 0 $self->render_card($bot, 2, 45);
270  
271 0 $self->render_card($low, 27, 45);
272  
273 0 ▶ $self->board->set_crib_player($dealer ? 'player2' : 'player1');
Branch: 1 branch
BranchTrueFalseTotal
$dealer ? : 0 0 0
274  
275 0 $self->print_footer(q|Press enter to continue|);
276  
277 0 $self->dealer($dealer);
278  
279 <STDIN>
280 0 }
281  
282 1 ▶sub draw_cards {
Subroutine draw_cards: called CC=1 SCAR=0.0
Pod documented
283 1 my ($self) = @_;
284  
285 1 $self->board->draw_hands();
286 }
287  
288 0 ▶sub discard_cards {
Subroutine discard_cards: not called CC=4 SCAR=30.0
Pod documented
289 0 my ($self) = @_;
290
291 0 $self->clear_screen();
292  
293 0 $self->print_header(q|Discard two cards to the crib, seperate the indexes with a space.|);
294  
295 0 my $hands = $self->board->get_hands;
296  
297 0 $self->render_opponent_cards(6);
298  
299 0 $self->render_player_cards($hands->player2->cards, 26, 5);
300  
301 0 $self->print_footer(q|Discard cards: |);
302  
303 0 my $cards_index = <STDIN>;
304  
305 0 chomp($cards_index);
306
307 0 my @cards;
308 0 ▶ if ($cards_index =~ m/b/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cards_index =~ /b/i) { } 0 0 0
309 0 my @worst = $self->board->identify_worst_cards('player2');
310 0 @cards = @{$worst[0]};
311 } else {
312 @cards = map {
313 0 $self->board->get_card('player2', $_ - 1);
314 0 ▶ } grep { $_ =~ m/^\d+$/ && $_ <= 6 } split " ", $cards_index;
Condition0%0/3
$_ =~ /^\d+$/ && $_ <= 6
!ll&&!rl&&r
000
MC/DC0%0/2
$_ =~ /^\d+$/ && $_ <= 6
$_ =~ /^\d+$/ $_ <= 6
Truth table0%0/3
A && B
A= $_ =~ /^\d+$/B= $_ <= 6
ABresult
0X0
100
111
315 }
316  
317 0 ▶ unless (scalar @cards == 2) {
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @cards == 2) 0 0 0
318 0 $self->discard_cards();
319 0 return;
320 }
321  
322 0 $self->board->crib_player_cards('player2', \@cards);
323  
324 0 my @bot = $self->board->identify_worst_cards('player1');
325  
326 0 $self->board->crib_player_cards('player1', $bot[0]);
327  
328 0 $self->crib_set(1);
329 }
330  
331 0 ▶sub starter {
Subroutine starter: not called CC=4 SCAR=30.0
Pod documented
332 0 my ($self) = @_;
333  
334 0 $self->clear_screen();
335  
336 0 my $count = scalar @{$self->board->deck->deck};
337 0 ▶ if ($self->dealer) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->dealer) 0 0 0
338 0 my $card = $self->board->deck->get(int(rand(39)));
339 0 $self->starter_card($card);
340 0 $self->board->add_starter_card('player1', $card);
341 0 return;
342 }
343  
344 0 $self->print_header(q|Split deck for starter card.|);
345
346 0 $self->show_face_down_split(40, 13);
347  
348 0 $self->print_footer(q|Split deck: |);
349  
350 0 my $number = <STDIN>;
351
352 0 chomp($number);
353
354 0 ▶ if ($number !~ m/^\d+$/ || $number > 52) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $number =~ /^\d+$/ or $number > 52) 0 0 0
Condition0%0/3
not $number =~ /^\d+$/ or $number > 52
l!l&&r!l&&!r
000
MC/DC0%0/2
not $number =~ /^\d+$/ or $number > 52
not $number =~ /^\d+$/ $number > 52
Truth table0%0/3
not(A) or B
A= not $number =~ /^\d+$/B= $number > 52
ABresult
1X1
011
000
355 0 $self->clear_screen();
356 0 $self->starter();
357 0 return;
358 }
359  
360 0 my $card = $self->board->deck->get($number);
361 0 $self->starter_card($card);
362 0 my $scored = $self->board->add_starter_card('player2', $card);
363 0 return;
364 }
365  
366 23 ▶sub render_card {
Subroutine render_card: called CC=5 SCAR=16.1
Pod documented
367 23 my ($self, $card, $row, $col) = @_;
368  
369 23 $self->set_cursor_vertical($row);
370 23 $self->set_cursor_horizontal($col);
371 23 ▶ my $color = $card->suit =~ m/H|D/ ? 91 : 90;
Branch: 1 branch
BranchTrueFalseTotal
$card->suit =~ /H|D/ ? : 13 10 23
372
373 23 my %suits = (
374 H => '♥',
375 D => '♦',
376 C => '♣',
377 S => '♠'
378 );
379  
380 my @card = (
381 "┌─────────┐",
382 sprintf("│%s. . . .│", $card->symbol =~ m/10/ ? $card->symbol : $card->symbol . " "),
383 "│. . . . .│",
384 "│. . . . .│",
385 23 ▶ sprintf("│. . %s . .│", $suits{$card->suit}),
Branch: 2 branches
BranchTrueFalseTotal
$card->symbol =~ /10/ ? : 4 19 23
$card->symbol =~ /10/ ? : 4 19 23
386 "│. . . . .│",
387 "│. . . . .│",
388 sprintf("│. . . .%s│", $card->symbol =~ m/10/ ? $card->symbol : " " . $card->symbol),
389 "└─────────┘"
390 );
391  
392 23 for (@card) {
393 207 $self->say($_, 1, 0, $color);
394 207 $self->set_cursor_horizontal($col);
395 }
396 }
397  
398  
399 2 ▶sub render_opponent_cards {
Subroutine render_opponent_cards: called CC=3 SCAR=11.0
Pod documented
400 2 my ($self, $num) = @_;
401  
402 2 ▶ return unless $num;
Branch: 1 branch
BranchTrueFalseTotal
unless $num 1 1 2
403  
404 1 $self->set_cursor_vertical(2);
405 1 $self->set_cursor_horizontal((100 - ($num * 4)) / 2);
406  
407 1 my $string = "┌──" x ($num - 1);
408 1 $self->say($string);
409 1 $self->say("┌─────────┐", 1, 1);
410 1 $self->set_cursor_horizontal((100 - ($num * 4)) / 2);
411 1 for (0..6) {
412 7 $string = "│. " x ($num - 1);
413 7 $self->say($string);
414 7 $self->say("│. . . . .│", 1, 1);
415 7 $self->set_cursor_horizontal((100 - ($num * 4)) / 2);
416 }
417 1 $string = "└──" x ($num - 1);
418 1 $self->say($string);
419 1 $self->say("└─────────┘", 1, 1);
420 }
421  
422 2 ▶sub render_player_cards {
Subroutine render_player_cards: called CC=4 SCAR=13.9
Pod documented
423 2 my ($self, $cards, $top, $left, $all) = @_;
424 2 my $i = 1;
425 2 for (@{$cards}) {
426 6 ▶ if (!$all && $_->used) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $all and $_->used) 1 5 6
Condition100%3/3
not $all and $_->used
!ll&&!rl&&r
321
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
427 1 $i++;
428 1 next;
429 }
430 5 $left += 11;
431 5 $self->set_cursor_vertical($top);
432 5 $self->set_cursor_horizontal($left + 5);
433 5 $self->say($i++);
434 5 $self->render_card($_, $top + 1, $left);
435 }
436 }
437  
438 2 ▶sub render_run_play {
Subroutine render_run_play: called CC=3 SCAR=11.0
Pod documented
439 2 my ($self) = @_;
440  
441 2 my $cards = $self->board->current_play->cards;
442  
443 2 ▶ return unless (scalar @{$cards});
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @{$cards;} 1 1 2
444  
445 1 my $left = 25;
446 1 for (@{$cards}) {
447 1 $self->render_card($_->card, 15, $left);
448 1 $left += 10;
449 }
450 }
451  
452  
453 0 ▶sub show_face_down_split {
Subroutine show_face_down_split: not called CC=10 SCAR=47.0
Pod documented
454 0 my ($self, $num, $vertical) = @_;
455  
456 0 ▶ $self->set_cursor_vertical($vertical || 10);
Condition0%0/2
$vertical || 10
l!l
00
MC/DC0%0/1
$vertical || 10
$vertical
Truth table0%0/2
A
A= $vertical
Aresult
11
00
457  
458 0 my $width = ($num / 2) - 1;
459  
460 0 for my $it (0 .. 1) {
461 0 my $string = "┌──" x $width;
462 0 $self->say($string);
463 0 $self->say("┌─────────┐", 1, 1);
464 0 $string = "";
465 0 for (1 .. $width) {
466 0 ▶ my $v = $it ? 26 + $_ : $_;
Branch: 1 branch
BranchTrueFalseTotal
$it ? : 0 0 0
467 0 ▶ $string .= sprintf("│%s", (!$it && $_ < 10 ? "$v " : $v));
Branch: 1 branch
BranchTrueFalseTotal
!$it && $_ < 10 ? : 0 0 0
Condition0%0/3
not $it and $_ < 10
!ll&&!rl&&r
000
MC/DC0%0/2
not $it and $_ < 10
not $it $_ < 10
Truth table0%0/3
not(A) and B
A= not $itB= $_ < 10
ABresult
0X0
100
111
468 }
469 0 $self->say($string);
470 0 ▶ $self->say(sprintf("│%s. . . .│", $it ? ($width + 1) * 2 : $width + 1), 1, 1);
Branch: 1 branch
BranchTrueFalseTotal
$it ? : 0 0 0
471 0 for (0..4) {
472 0 $string = "│. " x $width;
473 0 $self->say($string);
474 0 $self->say("│. . . . .│", 1, 1);
475 }
476 0 $string = "│. " x $width;
477 0 $self->say($string);
478 0 ▶ $self->say(sprintf("│. . . .%s│", $it ? ($width + 1) * 2 : $width + 1), 1, 1);
Branch: 1 branch
BranchTrueFalseTotal
$it ? : 0 0 0
479 0 my $string = "└──" x $width;
480 0 $self->say($string);
481 0 $self->say("└─────────┘", 1, 1);
482 }
483 }
484  
485 0 ▶sub draw_background {
Subroutine draw_background: not called CC=2 SCAR=17.9
Pod documented
486 0 for (0 .. 35) {
487 0 print "\e[102;1m";
488 0 print pack("A100", " ");
489 0 print "\n";
490 0 print "\e[0";
491 }
492 0 $_[0]->draw_dealer();
493 #$_[0]->draw_scores();
494 0 $_[0]->draw_crib();
495 0 $_[0]->draw_starter();
496 0 $_[0]->reset_cursor();
497 }
498  
499 2 ▶sub draw_starter {
Subroutine draw_starter: called CC=2 SCAR=6.9
Pod documented
500 2 my ($self) = @_;
501  
502 2 ▶ return unless $self->starter_card;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->starter_card 1 1 2
503  
504 1 $self->render_card($self->starter_card, 15, 2);
505 }
506  
507  
508 3 ▶sub draw_crib {
Subroutine draw_crib: called CC=5 SCAR=16.1
Pod documented
509 3 my ($self) = shift;
510 3 my $dealer = $self->dealer;
511 3 ▶ return unless defined $dealer;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dealer 0 3 3
512 3 ▶ return unless $self->crib_set;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->crib_set 1 2 3
513 2 my @card = (
514 "┌─────────┐",
515 "│. . . . .│",
516 "│. . . . .│",
517 "│. . . . .│",
518 "│. . . . .│",
519 "│. . . . .│",
520 "│. . . . .│",
521 "│. . . . .│",
522 "└─────────┘"
523 );
524  
525 2 ▶ if ($dealer) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dealer) { } 1 1 2
526 1 $self->set_cursor_vertical(27);
527 } else {
528 1 $self->set_cursor_vertical(3);
529 }
530  
531 2 for (@card) {
532 18 $self->set_cursor_horizontal(10);
533 18 $self->say($_, 1, 0);
534 }
535 }
536  
537 1 ▶sub draw_go {
Subroutine draw_go: called CC=1 SCAR=0.0
Pod documented
538 1 my ($self, $top, $left) = @_;
539 1 my $message = ' GO ';
540 1 $self->set_cursor_vertical($top);
541 1 $self->say($message, 0, 0, 31, 40);
542 }
543  
544  
545 3 ▶sub draw_dealer {
Subroutine draw_dealer: called CC=3 SCAR=11.0
Pod documented
546 3 my ($self) = @_;
547
548 3 my $dealer = $self->dealer;
549  
550 3 ▶ return unless defined $dealer;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dealer 0 3 3
551 3 my $message = ' Dealer ';
552 3 ▶ if ($dealer) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dealer) { } 1 2 3
553 1 $self->set_cursor_vertical(34);
554 1 $self->say($message, 0, 0, 31, 40);
555 } else {
556 2 $self->set_cursor_vertical(3);
557 2 $self->say($message, 0, 0, 31, 40);
558 }
559 }
560  
561 3 ▶sub draw_scores {
Subroutine draw_scores: called CC=4 SCAR=13.9
Pod documented
562 3 my ($self) = @_;
563 3 my $message;
564 3 ▶ return unless $self->board;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->board 1 2 3
565 2 ▶ if ($self->board && $self->board->current_play_score) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->board and $self->board->current_play_score) 1 1 2
Condition66%2/3
$self->board and $self->board->current_play_score
!ll&&!rl&&r
011
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
566 1 $message = pack("A2", $self->board->current_play_score);
567 1 $self->set_cursor_vertical(14);
568 1 $self->set_cursor_horizontal(49);
569 1 $self->say($message, 0, 0, 31, 40);
570 }
571 2 $message = ' Score: ' . $self->board->score->player2->{current} . ' ';
572 2 $self->set_cursor_vertical(34);
573 2 $self->set_cursor_horizontal(100 - (length($message) + 1));
574 2 $self->say($message, 0, 0, 31, 40);
575 2 $message = ' Score: ' . $self->board->score->player1->{current} . ' ';
576 2 $self->set_cursor_vertical(3);
577 2 $self->set_cursor_horizontal(100 - (length($message) + 1));
578 2 $self->say($message, 0, 0, 31, 40);
579 }
580  
581 0 ▶sub winner {
Subroutine winner: not called CC=3 SCAR=24.8
Pod documented
582 0 print "\e[0m\e[2J\e[0;1H";
583 0 for (0 .. 35) {
584 0 print "\e[102;1m";
585 0 print pack("A100", " ");
586 0 print "\n";
587 0 print "\e[0";
588 }
589 0 $_[0]->set_cursor_vertical(17);
590 0 my $player = $_[0]->board->players->[1];
591 0 ▶ $_[0]->say($_[1] eq 'player2'
Branch: 1 branch
BranchTrueFalseTotal
$_[1] eq 'player2' ? : 0 0 0
592 ? sprintf(q|Congratualations %s, you won the game!|, $player->name)
593 : sprintf(q|Unlucky %s, you lost this time!|, $player->name)
594 );
595 <STDIN>
596 0 }
597  
598  
599 0 ▶sub clear_screen {
Subroutine clear_screen: not called CC=1 SCAR=6.9
Pod documented
600 0 print "\e[0m\e[2J\e[0;1H";
601 0 $_[0]->draw_background();
602 }
603  
604 1 ▶sub reset_cursor {
Subroutine reset_cursor: called CC=1 SCAR=0.0
Pod documented
605 1 print "\e[;2H";
606 }
607  
608 41 ▶sub set_cursor_vertical {
Subroutine set_cursor_vertical: called CC=1 SCAR=0.0
Pod documented
609 41 print "\e[$_[1];2H";
610 }
611  
612 278 ▶sub set_cursor_horizontal {
Subroutine set_cursor_horizontal: called CC=1 SCAR=0.0
Pod documented
613 278 print "\e[$_[1]G";
614 }
615  
616 259 ▶sub say {
Subroutine say: called CC=5 SCAR=16.1
Pod documented
617 259 my ($self, $msg, $nl, $indent, $color, $back) = @_;
618 259 ▶ $color ||= 90;
Condition100%2/2
$color ||= 90
l!l
21742
MC/DC100%1/1
$color ||= 90
$color
Truth table100%2/2
A
A= $color
Aresult
11
00
619 259 ▶ $back ||= 102;
Condition100%2/2
$back ||= 102
l!l
10249
MC/DC100%1/1
$back ||= 102
$back
Truth table100%2/2
A
A= $back
Aresult
11
00
620 259 print "\e[$color;1;1m\e[$back;1m";
621 259 print $msg;
622 259 ▶ print "\n" if $nl;
Branch: 1 branch
BranchTrueFalseTotal
if $nl 235 24 259
623 259 ▶ $self->set_cursor_horizontal(2) if $indent;
Branch: 1 branch
BranchTrueFalseTotal
if $indent 10 249 259
624 259 print "\e[0";
625 259 1;
626 }
627  
628 0 ▶sub print_header {
Subroutine print_header: not called CC=1 SCAR=6.9
Pod documented
629 0 my ($self, $message) = @_;
630 0 $self->set_cursor_vertical(0);
631 0 $self->set_cursor_horizontal(0);
632 0 print "\e[40;1m";
633 0 print pack("A100", " ");
634 0 $self->set_cursor_horizontal(2);
635 0 $self->say($message, 0, 0, 31, 40);
636 }
637  
638 0 ▶sub print_footer {
Subroutine print_footer: not called CC=1 SCAR=6.9
Pod documented
639 0 my ($self, $message) = @_;
640 0 $self->set_cursor_vertical(36);
641 0 $self->set_cursor_horizontal(0);
642 0 print "\e[40;1m";
643 0 print pack("A100", " ");
644 0 $self->set_cursor_horizontal(2);
645 0 $self->say($message, 0, 0, 31, 40);
646 0 $self->set_cursor_horizontal(length($message) + 2);
647 }
648  
649  
650 2 1;
651  
652 __END__
↑ Summary Cribbage/Player/Hand.pm »