Score.pm

filter: Statementstmt 96.7% 59 / 61 Branchbran 64.2% 9 / 14 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 90.4% 19 / 21 Podpod 28.5% 2 / 7 totaltotal 86.4% 89 / 103 CC 11 SCAR 6.2
CC
11
Cov
91%
hand_name
3.0
stringify_cards
3.0
__ANON__
2.1
CRAP 1.9
« Score/Badugi27.pm ↑ Summary Score/Badugi.pm »
1 package Poker::Score;
2 13 our $VERSION = '0.12';
3  
4  
5 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 13 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 13 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 =head1 NAME
10  
11 Poker::Score - Identify and score specific poker hands. Base class for specific scoring systems.
12  
13 =head1 VERSION
14  
15 Version 0.12
16  
17 =cut
18  
19  
20 =head1 SYNOPSIS
21
22 # This is just a base class. Poker::Score::High shows a real example.
23  
24 use Poker::Score::High; #standard highball
25 use Poker::Dealer;
26 use feature qw(say);
27  
28 # Create highball score object
29 my $scorer = Poker::Score::High->new;
30  
31 # Create dealer, shuffle deck and deal out five cards
32 my $dealer = Poker::Dealer->new;
33 $dealer->shuffle_deck;
34 my $cards = $dealer->deal_up(5);
35  
36 # Numerical score of five card poker hand
37 my $score = $scorer->score($cards);
38 say $score;
39  
40 # English name of hand (e.g. 'Two Pair')
41 say $scorer->hand_name($score);
42  
43 =cut
44  
45  
46 has 'hands' => (
47 is => 'rw',
48 90 ▶ isa => sub { die "Not an array_ref!" unless ref( $_[0] ) eq 'ARRAY' },
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] eq "ARRAY" 0 90 90
49 44 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
50 13 );
51  
52 has '_hand_lookup' => (
53 is => 'rw',
54 44 ▶ isa => sub { die "Not an hash_ref!" unless ref( $_[0] ) eq 'HASH' },
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] eq "HASH" 0 44 44
55 13 init_arg => undef,
56 );
57  
58 has '_hand_map' => (
59 is => 'rw',
60 82 ▶ isa => sub { die "Not an hash_ref!" unless ref( $_[0] ) eq 'HASH' },
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] eq "HASH" 0 82 82
61 44 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
62 13 );
63  
64 has '_rank_map' => (
65 is => 'rw',
66 44 ▶ isa => sub { die "Not an hash_ref!" unless ref( $_[0] ) eq 'HASH' },
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] eq "HASH" 0 44 44
67 13 );
68  
69 33 ▶sub _build_rank_map {
Subroutine _build_rank_map: called CC=1 SCAR=0.0
70 33 my $self = shift;
71 33 $self->_rank_map(
72 {
73 '2' => '02',
74 '3' => '03',
75 '4' => '04',
76 '5' => '05',
77 '6' => '06',
78 '7' => '07',
79 '8' => '08',
80 '9' => '09',
81 'T' => '10',
82 'J' => '11',
83 'Q' => '12',
84 'K' => '13',
85 'A' => '14',
86 }
87 );
88 }
89  
90 has '_suit_map' => (
91 is => 'rw',
92 44 ▶ isa => sub { die "Not an hash_ref!" unless ref( $_[0] ) eq 'HASH' },
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] eq "HASH" 0 44 44
93 13 );
94  
95 44 ▶sub _build_suit_map {
Subroutine _build_suit_map: called CC=1 SCAR=0.0
96 44 my $self = shift;
97 44 $self->_suit_map(
98 {
99 'c' => '01',
100 'd' => '02',
101 'h' => '03',
102 's' => '04',
103 }
104 );
105 }
106  
107 =head1 METHODS
108  
109 =head2 hand_name
110  
111 English name of given hand (e.g., 'Two Pair')
112  
113 =cut
114  
115 29585 ▶sub hand_name {
Subroutine hand_name: called CC=3 SCAR=11.0
Pod documented
116 29585 my ( $self, $score ) = @_;
117 29585 for my $key ( sort { $b <=> $a } keys %{ $self->_hand_map } ) {
118 232503 ▶ if ( $score > $key ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($score > $key) 29581 202922 232503
119 29581 return $self->_hand_map->{$key};
120 }
121 }
122 }
123  
124 3107924 ▶sub rank_val {
Subroutine rank_val: called CC=1 SCAR=0.0
Pod undocumented
125 3107924 my ( $self, $rank ) = @_;
126 3107924 return $self->_rank_map->{$rank};
127 }
128  
129 0 ▶sub suit_val {
Subroutine suit_val: not called CC=1 SCAR=6.9
Pod undocumented
130 0 my ( $self, $suit ) = @_;
131 0 return $self->_suit_map->{$suit};
132 }
133  
134 621874 ▶sub hand_score {
Subroutine hand_score: called CC=1 SCAR=0.0
Pod undocumented
135 621874 my ( $self, $hand ) = @_;
136 621874 return $self->_hand_lookup->{$hand};
137 }
138  
139 =head2 score
140  
141 Numercal score of given hand (higher is better)
142  
143 =cut
144  
145 621575 ▶sub score {
Subroutine score: called CC=1 SCAR=0.0
Pod documented
146 621575 my ( $self, $cards ) = @_;
147 621575 return $self->hand_score( $self->stringify_cards($cards) );
148 }
149  
150 621563 ▶sub stringify_cards {
Subroutine stringify_cards: called CC=3 SCAR=11.0
Pod undocumented
151 621563 my ( $self, $cards ) = @_;
152 621563 my %suit;
153 621563 for my $card (@$cards) {
154 3107815 $suit{ $card->suit }++;
155 }
156 my $flat = join( '',
157 4788721 sort { $b <=> $a }
158 621563 map { sprintf( "%02d", $self->rank_val( $_->rank ) ) } @$cards );
159 621563 ▶ $flat .= 's' if scalar keys %suit == 1;
Branch: 1 branch
BranchTrueFalseTotal
if scalar keys %suit == 1 695 620868 621563
160 621563 return $flat;
161 }
162  
163 44 ▶sub _build_hand_lookup {
Subroutine _build_hand_lookup: called CC=2 SCAR=6.9
164 44 my $self = shift;
165 44 my %look;
166 44 for my $i ( 0 .. $#{ $self->hands } ) {
167 269831 $look{ $self->hands->[$i] } = $i;
168 }
169 44 $self->_hand_lookup( \%look );
170 }
171  
172 0 ▶sub _build_hands { }
Subroutine _build_hands: not called CC=1 SCAR=0.0
173  
174 44 ▶sub BUILD {
Subroutine BUILD: called CC=1 SCAR=0.0
Pod undocumented
175 44 my $self = shift;
176 44 $self->_build_hands;
177 44 $self->_build_hand_lookup;
178 44 $self->_build_rank_map;
179 44 $self->_build_suit_map;
180 }
181  
182 =head1 SEE ALSO
183  
184 Poker::Score::High, Poker::Score::Low8, Poker::Score::Low27, Poker::Score::LowA5, Poker::Score::Badugi, Poker::Score::Badugi27, Poker::Score::HighSuit, Poker::Score::Bring::High, Poker::Score::Bring::Low, Poker::Score::Bring::Wild
185  
186 =head1 AUTHOR
187  
188 Nathaniel Graham, C<< <ngraham at cpan.org> >>
189  
190 =head1 LICENSE AND COPYRIGHT
191  
192 Copyright 2016 Nathaniel Graham.
193  
194 This program is free software; you can redistribute it and/or modify it
195 under the terms of the the Artistic License (2.0). You may obtain a
196 copy of the full license at:
197  
198 L<http://www.perlfoundation.org/artistic_license_2_0>
199  
200 =cut
201  
202 13 1;
« Score/Badugi27.pm ↑ Summary Score/Badugi.pm »