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 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 12 ▶use Object::Proto::Sugar -types;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 12 ▶use List::Util qw//;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 12 ▶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 12 );
15  
16  
17 44 ▶sub BUILD {
Subroutine BUILD: called CC=1 SCAR=0.0
Pod undocumented
18 44 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 47 ▶sub reset {
Subroutine reset: called CC=1 SCAR=0.0
Pod documented
26 47 $_[0]->shuffle();
27 47 $_[0];
28 }
29  
30 92 ▶sub shuffle {
Subroutine shuffle: called CC=3 SCAR=11.0
Pod documented
31 92 my $i = 0;
32 92 my @DECK;
33 92 for my $suit (qw/H S D C/) {
34 368 for ('A', 2 .. 10, 'J', 'Q', 'K') {
35 4784 $i++;
36 4784 push @DECK,
37 Game::Cribbage::Deck::Card->new(
38 suit => $suit,
39 symbol => $_,
40 id => $i
41 );
42 }
43 }
44 92 $_[0]->deck([List::Util::shuffle @DECK]);
45 92 $_[0];
46 }
47  
48 4 ▶sub from_order {
Subroutine from_order: called CC=7 SCAR=19.5
Pod documented
49 4 my ($self, $ids) = @_;
50 die 'from_order wants an arrayref of 52 card ids'
51 4 ▶ unless ref $ids eq 'ARRAY' && @{$ids} == 52;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $ids eq "ARRAY" and @{$ids;} == 52 1 3 4
Condition66%2/3
ref $ids eq "ARRAY" and @{$ids;} == 52
!ll&&!rl&&r
013
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 3 my %card;
53 3 my $i = 0;
54 3 for my $suit (qw/H S D C/) {
55 12 for ('A', 2 .. 10, 'J', 'Q', 'K') {
56 156 $i++;
57 156 $card{$i} = Game::Cribbage::Deck::Card->new(
58 suit => $suit,
59 symbol => $_,
60 id => $i
61 );
62 }
63 }
64 3 my @deck;
65 3 for (@{$ids}) {
66 156 ▶ my $c = delete $card{$_}
Branch: 1 branch
BranchTrueFalseTotal
my $c = delete $card{$_} or die "from_order: card id $_ is out of range or repeated" 155 1 156
67 or die "from_order: card id $_ is out of range or repeated";
68 155 push @deck, $c;
69 }
70 2 $self->deck(\@deck);
71 2 $self;
72 }
73  
74 33 ▶sub draw {
Subroutine draw: called CC=1 SCAR=0.0
Pod documented
75 33 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 33 ▶ if ($_->suit eq $card->{suit} && $_->symbol =~ m/^$card->{symbol}$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_->suit eq $card->{'suit'} and $_->symbol =~ /^$card->{'symbol'}$/) { } 2 31 33
Condition100%3/3
$_->suit eq $card->{"suit"} and $_->symbol =~ /^$card->{"symbol"}$/
!ll&&!rl&&r
2472
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 31 $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 2 ▶sub get {
Subroutine get: called CC=1 SCAR=0.0
Pod documented
100 2 $_[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 63 ▶ if ($_->suit eq $suit && $_->symbol =~ m/^$sym$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_->suit eq $suit and $_->symbol =~ /^$sym$/) 2 61 63
Condition100%3/3
$_->suit eq $suit and $_->symbol =~ /^$sym$/
!ll&&!rl&&r
47142
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 12 1;
125  
126 __END__
« Cribbage/Play.pm ↑ Summary Cribbage/Play/Score.pm »