| 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 | ▶ | use Exporter; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use Games::Tournament::BlackJack::Shoe; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 1 | our @ISA = qw(Exporter); | |||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 1 | our @EXPORT = qw( | |||||||||||||||||||||||||||
| 11 | handValue | ||||||||||||||||||||||||||||
| 12 | cardValue | ||||||||||||||||||||||||||||
| 13 | handStr | ||||||||||||||||||||||||||||
| 14 | firstLetter | ||||||||||||||||||||||||||||
| 15 | ); | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | # general utilities | ||||||||||||||||||||||||||||
| 19 | 0 | ▶ | sub firstLetter { return substr($_[0],0,1); } | ||||||||||||||||||||||||||
|
Subroutine
firstLetter: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | # Stats utilities | ||||||||||||||||||||||||||||
| 22 | 0 | ▶ | sub handValue { | ||||||||||||||||||||||||||
|
Subroutine
handValue: not called CC=8 SCAR=42.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 23 | 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 | ▶ | if ($total > 21) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 33 | 0 | $total++; | |||||||||||||||||||||||||||
| 34 | 0 | $total--; | |||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 0 | ▶ | if ($total > 21 and $counts->{'A'} > 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$total > 21 and $counts->{"A"} > 0
MC/DC0%0/2
$total > 21 and $counts->{"A"} > 0
$total > 21
$counts->{"A"} > 0
Truth table0%0/3
A and B
A= $total > 21B= $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 | ▶ | if ($card =~ /^A/ and $total > 21) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$card =~ /^A/ and $total > 21
MC/DC0%0/2
$card =~ /^A/ and $total > 21
$card =~ /^A/
$total > 21
Truth table0%0/3
A and B
A= $card =~ /^A/B= $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 | 0 | ▶ | sub cardValue { | ||||||||||||||||||||||||||
|
Subroutine
cardValue: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 51 | 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 | 0 | ▶ | sub handStr { | ||||||||||||||||||||||||||
|
Subroutine
handStr: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 78 | 0 | my $hand = shift; | |||||||||||||||||||||||||||
| 79 | 0 | ▶ | die "bad hand $hand [@$hand]" unless ref $hand eq 'ARRAY'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 | 1; | |||||||||||||||||||||||||||