File Coverage

blib/lib/Games/Tournament/BlackJack/Utilities.pm
Criterion Covered Total %
statement 6 46 13.0
branch 0 8 0.0
condition 0 6 0.0
subroutine 2 6 33.3
pod 0 4 0.0
total 8 70 11.4


line stmt bran cond sub pod time code
1             # Games::Tournament::BlackJack::Utilities.pm - utilities for blackjack
2             # Author: Paul Jacobs, paul@pauljacobs.net
3             package Games::Tournament::BlackJack::Utilities;
4              
5 1     1   6 use Exporter;
  1         1  
  1         38  
6 1     1   6 use Games::Tournament::BlackJack::Shoe;
  1         2  
  1         772  
7              
8             our @ISA = qw(Exporter);
9              
10             our @EXPORT = qw(
11             handValue
12             cardValue
13             handStr
14             firstLetter
15             );
16              
17              
18             # general utilities
19 0     0 0   sub firstLetter { return substr($_[0],0,1); }
20              
21             # Stats utilities
22             sub handValue {
23 0     0 0   my $hand = shift;
24 0           my $total = 0;
25 0           my $counts = {};
26            
27 0           foreach my $card (@$hand) {
28 0           $total += cardValue($card);
29 0           $counts->{ firstLetter($card) }++;
30             }
31            
32 0 0         if ($total > 21) {
33 0           $total++;
34 0           $total--;
35             }
36            
37 0 0 0       if ($total > 21 and $counts->{'A'} > 0) {
38             # try using aces as 1 instead of 11 if total is too high
39 0           foreach my $card (@$hand) {
40 0 0 0       if ($card =~ /^A/ and $total > 21) {
41 0           $card =~ s/^A/a/;
42 0           $total = handValue($hand); # recurse as much as possible to reduce the As below 21
43             }
44             }
45             }
46            
47 0           return $total;
48             }
49              
50             sub cardValue {
51 0     0 0   my $card = shift;
52            
53            
54 0           my %values = (
55             'a' => 1, # 'a' is an ace in a hand that can't fit an 11 - it is forced to 1.
56             '2' => 2,
57             '3' => 3,
58             '4' => 4,
59             '5' => 5,
60             '6' => 6,
61             '7' => 7,
62             '8' => 8,
63             '9' => 9,
64             '10' => 10,
65             '1' => 10, # abbrev for 10, so they can all be 1 character and still work.
66             'J' => 10,
67             'Q' => 10,
68             'K' => 10,
69             'A' => 11,
70             # 'A' is an "open" ace, and is assumed to be 11 but may change to 1 later.
71             # Aces are dealt as 'A's.
72             );
73            
74 0           return $values{ firstLetter($card) };
75             }
76              
77             sub handStr {
78 0     0 0   my $hand = shift;
79 0 0         die "bad hand $hand [@$hand]" unless ref $hand eq 'ARRAY';
80 0           my @h = @$hand;
81 0           my $out = "";
82              
83 0           foreach my $c (@h) {
84 0           my $card = $c;
85 0           $card =~ s/_/ /g;
86 0           $card =~ s/ of /\-/g;
87            
88 0           $card =~ s/Clubs/C/g;
89 0           $card =~ s/Diamonds/D/g;
90 0           $card =~ s/Hearts/H/g;
91 0           $card =~ s/Spades/S/g;
92              
93 0           $card =~ s/King/K/g;
94 0           $card =~ s/Jack/J/g;
95 0           $card =~ s/Queen/Q/g;
96 0           $card =~ s/Ace/A/g;
97 0           $card =~ s/ace/a/g;
98              
99              
100 0           $out .= "${card}, "
101             }
102            
103             # remove trailing comma + space
104 0           chop($out);
105 0           chop($out);
106            
107 0           return $out;
108             }
109              
110             1;