Cribbage/Deck.pm

filter: Statementstmt 100% 67 / 67 Branchbran 100% 10 / 10 Conditioncond 88.8% 8 / 9 MC/DCmcdc 83.3% 5 / 6 Subroutinesub 100% 15 / 15 Podpod 90.0% 9 / 10 totaltotal 97.4% 114 / 117 CC 16 SCAR 13.0
CC
16
Cov
99%
from_order
7.0
card_exists
5.0
force_draw
4.0
CRAP 3.7
« Cribbage/Play.pm ↑ Summary Cribbage/Play/Score.pm »
1 package Game::Cribbage::Deck;
2  
3 15 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 15 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 15 ▶use Object::Proto::Sugar -types;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 15 ▶use List::Util qw//;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 15 ▶use Game::Cribbage::Deck::Card;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 has deck => (
11 is => 'rw',
12 isa => ArrayRef,
13 default => []
14 15 );
15  
16  
17 68 ▶sub BUILD {
Subroutine BUILD: called CC=1 SCAR=0.0
Pod undocumented
18 68 return $_[0]->reset();
19 }
20  
21 2 ▶sub cards {
Subroutine cards: called CC=1 SCAR=0.0
Pod documented
22 2 return $_[0]->deck;
23 }
24  
25 71 ▶sub reset {
Subroutine reset: called CC=1 SCAR=0.0
Pod documented
26 71 $_[0]->shuffle();
27 71 $_[0];
28 }
29  
30 140 ▶sub shuffle {
Subroutine shuffle: called CC=3 SCAR=11.0
Pod documented
31 140 my $i = 0;
32 140 my @DECK;
33 140 for my $suit (qw/H S D C/) {
34 560 for ('A', 2 .. 10, 'J', 'Q', 'K') {
35 7280 $i++;
36 7280 push @DECK,
37 Game::Cribbage::Deck::Card->new(
38 suit => $suit,
39 symbol => $_,
40 id => $i
41 );
42 }
43 }
44 140 $_[0]->deck([List::Util::shuffle @DECK]);
45 140 $_[0];
46 }
47  
48 5 ▶sub from_order {
Subroutine from_order: called CC=7 SCAR=19.5
Pod documented
49 5 my ($self, $ids) = @_;
50 die 'from_order wants an arrayref of 52 card ids'
51 5 ▶ unless ref $ids eq 'ARRAY' && @{$ids} == 52;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $ids eq "ARRAY" and @{$ids;} == 52 1 4 5
Condition66%2/3
ref $ids eq "ARRAY" and @{$ids;} == 52
!ll&&!rl&&r
014
MC/DC50%1/2
ref $ids eq "ARRAY" and @{$ids;} == 52
ref $ids eq "ARRAY" @{$ids;} == 52
Truth table66%2/3
A and B
A= ref $ids eq "ARRAY"B= @{$ids;} == 52
ABresult
0X0
100
111
52 4 my %card;
53 4 my $i = 0;
54 4 for my $suit (qw/H S D C/) {
55 16 for ('A', 2 .. 10, 'J', 'Q', 'K') {
56 208 $i++;
57 208 $card{$i} = Game::Cribbage::Deck::Card->new(
58 suit => $suit,
59 symbol => $_,
60 id => $i
61 );
62 }
63 }
64 4 my @deck;
65 4 for (@{$ids}) {
66 208 ▶ my $c = delete $card{$_}
Branch: 1 branch
BranchTrueFalseTotal
my $c = delete $card{$_} or die "from_order: card id $_ is out of range or repeated" 207 1 208
67 or die "from_order: card id $_ is out of range or repeated";
68 207 push @deck, $c;
69 }
70 3 $self->deck(\@deck);
71 3 $self;
72 }
73  
74 45 ▶sub draw {
Subroutine draw: called CC=1 SCAR=0.0
Pod documented
75 45 shift @{$_[0]->deck}
76 }
77  
78 2 ▶sub force_draw {
Subroutine force_draw: called CC=4 SCAR=13.9
Pod documented
79 2 my ($self, $card) = @_;
80  
81 2 my $i = 0;
82 2 for (@{$self->deck}) {
83 19 ▶ if ($_->suit eq $card->{suit} && $_->symbol =~ m/^$card->{symbol}$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_->suit eq $card->{'suit'} and $_->symbol =~ /^$card->{'symbol'}$/) { } 2 17 19
Condition100%3/3
$_->suit eq $card->{"suit"} and $_->symbol =~ /^$card->{"symbol"}$/
!ll&&!rl&&r
1162
MC/DC100%2/2
$_->suit eq $card->{"suit"} and $_->symbol =~ /^$card->{"symbol"}$/
$_->suit eq $card->{"suit"} $_->symbol =~ /^$card->{"symbol"}$/
Truth table100%3/3
A and B
A= $_->suit eq $card->{"suit"}B= $_->symbol =~ /^$card->{"symbol"}$/
ABresult
0X0
100
111
84 2 last;
85 } else {
86 17 $i++;
87 }
88 }
89  
90 2 my $force = splice @{$self->deck}, $i, 1;
91  
92 #if ($card->{used}) {
93 # $force->used = 1;
94 #}
95  
96 2 return $force;
97 }
98  
99 13 ▶sub get {
Subroutine get: called CC=1 SCAR=0.0
Pod documented
100 13 $_[0]->deck->[$_[1]];
101 }
102  
103 3 ▶sub card_exists {
Subroutine card_exists: called CC=5 SCAR=16.1
Pod documented
104 3 my ($self, $card) = @_;
105  
106 my ($suit, $sym) = ref($card) eq 'HASH'
107 ? ($card->{suit}, $card->{symbol})
108 3 ▶ : ($card->suit, $card->symbol);
Branch: 1 branch
BranchTrueFalseTotal
ref $card eq 'HASH' ? : 1 2 3
109 3 for (@{$self->deck}) {
110 108 ▶ if ($_->suit eq $suit && $_->symbol =~ m/^$sym$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_->suit eq $suit and $_->symbol =~ /^$sym$/) 2 106 108
Condition100%3/3
$_->suit eq $suit and $_->symbol =~ /^$sym$/
!ll&&!rl&&r
80262
MC/DC100%2/2
$_->suit eq $suit and $_->symbol =~ /^$sym$/
$_->suit eq $suit $_->symbol =~ /^$sym$/
Truth table100%3/3
A and B
A= $_->suit eq $suitB= $_->symbol =~ /^$sym$/
ABresult
0X0
100
111
111 2 return 1;
112 }
113 }
114  
115 1 return 0;
116 }
117  
118 1 ▶sub generate_card {
Subroutine generate_card: called CC=1 SCAR=0.0
Pod documented
119 return Game::Cribbage::Deck::Card->new(
120 1 %{ $_[1] }
121 );
122 }
123  
124 15 1;
125  
126 __END__
« Cribbage/Play.pm ↑ Summary Cribbage/Play/Score.pm »