blib/lib/Game/Durak/Card.pm

filter: Statementstmt 100% 66 / 66 Branchbran 95.0% 19 / 20 Conditioncond 85.7% 18 / 21 MC/DCmcdc 66.6% 8 / 12 Subroutinesub 100% 18 / 18 Podpod 100% 12 / 12 totaltotal 94.6% 141 / 149 CC 18 SCAR 13.7
CC
18
Cov
96%
beats
8.0
_card
6.0
_suit
4.1
CRAP 3.9
« blib/lib/Game/Durak/Deck.pm ↑ Summary blib/lib/Game/Durak/Rules.pm »
1 package Game::Durak::Card;
2  
3 24 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 24 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 24 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 24 our $VERSION = '0.03';
9 24 our @EXPORT_OK = qw(rank_of suit_of power_of name_of long_name_of face_of
10 id_of ids_of_suit six_of beats ranks suits CARDS);
11  
12 24 ▶use constant CARDS => 36;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 24 my @RANKS = qw(A K Q J T 9 8 7 6);
15 24 my @SUITS = qw(S H D C);
16  
17 24 my %RANK_NAME = (A => 'ace', K => 'king', Q => 'queen', J => 'jack',
18 T => 'ten', 9 => 'nine', 8 => 'eight', 7 => 'seven',
19 6 => 'six');
20 24 my %SUIT_NAME = (S => 'spades', H => 'hearts', D => 'diamonds', C => 'clubs');
21  
22 24 my %RANK_AT;
23 24 @RANK_AT{@RANKS} = 0 .. $#RANKS;
24  
25 24 my %SUIT_AT;
26 24 @SUIT_AT{@SUITS} = 0 .. $#SUITS;
27  
28 1 ▶sub ranks { return @RANKS }
Subroutine ranks: called CC=1 SCAR=0.0
Pod documented
29 880 ▶sub suits { return @SUITS }
Subroutine suits: called CC=1 SCAR=0.0
Pod documented
30  
31 1564385 ▶sub _card {
Subroutine _card: called CC=6 SCAR=17.9
32 1564385 my ($id) = @_;
33 1564385 ▶ die 'Game::Durak::Card: no card has id ' . (defined $id ? "'$id'" : 'undef') . "\n"
Branch: 2 branches
BranchTrueFalseTotal
unless defined $id and $id =~ /\A[1-9][0-9]*\z/ and $id >= 1 and $id <= 36 6 1564379 1564385
defined $id ? : 5 1 6
Condition88%8/9
defined $id and $id =~ /\A[1-9][0-9]*\z/ and $id >= 1 and $id <= 36
!ll&&!rl&&r
511564379
defined $id and $id =~ /\A[1-9][0-9]*\z/ and $id >= 1
!ll&&!rl&&r
501564380
defined $id and $id =~ /\A[1-9][0-9]*\z/
!ll&&!rl&&r
141564380
MC/DC75%3/4
defined $id and $id =~ /\A[1-9][0-9]*\z/ and $id >= 1 and $id <= 36
defined $id $id =~ /\A[1-9][0-9]*\z/ $id >= 1 $id <= 36
Truth table80%4/5
A and B and C and D
A= defined $idB= $id =~ /\A[1-9][0-9]*\z/C= $id >= 1D= $id <= 36
ABCDresult
0XXX0
10XX0
110X0
11100
11111
34 unless defined $id && $id =~ /\A[1-9][0-9]*\z/ && $id >= 1 && $id <= CARDS;
35 1564379 return $id;
36 }
37  
38 329393 ▶sub _suit {
Subroutine _suit: called CC=4 SCAR=14.2
39 329393 my ($suit) = @_;
40 die 'Game::Durak::Card: no suit is ' . (defined $suit ? "'$suit'" : 'undef') . "\n"
41 329393 ▶ unless defined $suit && exists $SUIT_AT{$suit};
Branch: 2 branches
BranchTrueFalseTotal
unless defined $suit and exists $SUIT_AT{$suit} 2 329391 329393
defined $suit ? : 2 0 2
Condition66%2/3
defined $suit and exists $SUIT_AT{$suit}
!ll&&!rl&&r
02329391
MC/DC50%1/2
defined $suit and exists $SUIT_AT{$suit}
defined $suit exists $SUIT_AT{$suit}
Truth table66%2/3
A and B
A= defined $suitB= exists $SUIT_AT{$suit}
ABresult
0X0
100
111
42 329391 return $suit;
43 }
44  
45 467442 ▶sub rank_of { my $id = _card($_[0]); return $RANKS[ ($id - 1) % @RANKS ] }
Subroutine rank_of: called CC=1 SCAR=0.0
Pod documented
46 565761 ▶sub suit_of { my $id = _card($_[0]); return $SUITS[ int(($id - 1) / @RANKS) ] }
Subroutine suit_of: called CC=1 SCAR=0.0
Pod documented
47  
48 160112 ▶sub power_of { return $#RANKS - $RANK_AT{ rank_of($_[0]) } }
Subroutine power_of: called CC=1 SCAR=0.0
Pod documented
49  
50 900 ▶sub name_of { my ($id) = @_; return rank_of($id) . suit_of($id) }
Subroutine name_of: called CC=1 SCAR=0.0
Pod documented
51  
52 24 my %FACE = (T => '10');
53  
54 3528 ▶sub face_of { my ($id) = @_; my $rank = rank_of($id); return $FACE{$rank} // $rank }
Subroutine face_of: called CC=2 SCAR=7.0
Pod documented
Condition66%2/3
$FACE{$rank} // $rank
l!l&&r!l&&!r
33231960
MC/DC0%0/2
$FACE{$rank} // $rank
$FACE{$rank} $rank
Truth table66%2/3
A // B
A= $FACE{$rank}B= $rank
ABresult
1X1
011
000
55  
56 56 ▶sub long_name_of {
Subroutine long_name_of: called CC=1 SCAR=0.0
Pod documented
57 56 my ($id) = @_;
58 56 return $RANK_NAME{ rank_of($id) } . ' of ' . $SUIT_NAME{ suit_of($id) };
59 }
60  
61 24 my %ID;
62 24 for my $id (1 .. CARDS) { $ID{ name_of($id) } = $id }
63  
64 801 ▶sub id_of {
Subroutine id_of: called CC=2 SCAR=6.9
Pod documented
65 801 my ($name) = @_;
66 801 ▶ return undef unless defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 1 800 801
67 800 return $ID{ uc $name };
68 }
69  
70 2 ▶sub ids_of_suit {
Subroutine ids_of_suit: called CC=1 SCAR=0.0
Pod documented
71 2 my ($suit) = @_;
72 2 my $at = $SUIT_AT{ _suit($suit) };
73 2 return [ map { $at * @RANKS + $_ } 1 .. scalar @RANKS ];
74 }
75  
76 63800 ▶sub six_of { my ($suit) = @_; return $ID{ '6' . _suit($suit) } }
Subroutine six_of: called CC=1 SCAR=0.0
Pod documented
77  
78 265591 ▶sub beats {
Subroutine beats: called CC=8 SCAR=20.8
Pod documented
79 265591 my ($def, $att, $trump) = @_;
80 265591 _card($def);
81 265591 _card($att);
82 265591 _suit($trump);
83  
84 265590 ▶ return 0 if $def == $att;
Branch: 1 branch
BranchTrueFalseTotal
if $def == $att 1 265589 265590
85  
86 265589 my $ds = suit_of($def);
87 265589 my $as = suit_of($att);
88  
89 265589 ▶ return ($ds eq $trump && power_of($def) > power_of($att)) ? 1 : 0
Branch: 2 branches
BranchTrueFalseTotal
if $as eq $trump 32006 233583 265589
$ds eq $trump && power_of($def) > power_of($att) ? : 3792 28214 32006
Condition100%3/3
$ds eq $trump and power_of($def) > power_of($att)
!ll&&!rl&&r
2663315813792
MC/DC100%2/2
$ds eq $trump and power_of($def) > power_of($att)
$ds eq $trump power_of($def) > power_of($att)
Truth table100%3/3
A and B
A= $ds eq $trumpB= power_of($def) > power_of($att)
ABresult
0X0
100
111
90 if $as eq $trump;
91  
92 233583 ▶ return 1 if $ds eq $trump;
Branch: 1 branch
BranchTrueFalseTotal
if $ds eq $trump 39018 194565 233583
93  
94 194565 ▶ return ($ds eq $as && power_of($def) > power_of($att)) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$ds eq $as && power_of($def) > power_of($att) ? : 38087 156478 194565
Condition100%3/3
$ds eq $as and power_of($def) > power_of($att)
!ll&&!rl&&r
1279642851438087
MC/DC100%2/2
$ds eq $as and power_of($def) > power_of($att)
$ds eq $as power_of($def) > power_of($att)
Truth table100%3/3
A and B
A= $ds eq $asB= power_of($def) > power_of($att)
ABresult
0X0
100
111
95 }
96  
97 24 1;
98  
99 __END__
« blib/lib/Game/Durak/Deck.pm ↑ Summary blib/lib/Game/Durak/Rules.pm »