BlackJack/Game.pm

filter: Statementstmt 10.2% 12 / 117 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 27 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 20.0% 3 / 15 Podpod 0.0% 0 / 12 totaltotal 7.0% 15 / 212 CC 32 SCAR 55.1
CC
32
Cov
7%
playRound
506.0
setupPlayerScenario
30.0
new
6.0
CRAP 247.1
↑ Summary BlackJack/Utilities.pm »
1 # Games::Tournament::BlackJack::Game.pm - perl module for playing blackjack with a deck.pm of cards.
2 # Author: Paul Jacobs, paul@pauljacobs.net
3 package Games::Tournament::BlackJack::Game;
4  
5 1 ▶use Games::Tournament::BlackJack::Shoe;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Games::Tournament::BlackJack::Utilities;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Games::Tournament::BlackJack::Player;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 our $running = 0; # not running at first
10  
11 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod undocumented
12 0 my $invocant = shift;
13 0 ▶ my $class = ref($invocant) || $invocant;
Condition0%0/3
ref $invocant || $invocant
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $invocant || $invocant
ref $invocant $invocant
Truth table0%0/3
A || B
A= ref $invocantB= $invocant
ABresult
1X1
011
000
14 0 my $self = {
15 'deck' => Games::Tournament::BlackJack::Shoe::openNewShoe(), # shuffles before every round, and before returning for good measure.
16 'players' => [], # add them later or override
17 'playersScores' => [], # 1 per win
18 'playersHands' => [], # need seperate official record -- can't trust player code to not change hand
19 # is parallel array to players[]. is 2D array (one array of cards per player)
20 'dealer' => undef, # add later or specify in override attribute
21 'dealersHand' => [], # card 1 is the upcard
22 'numDecks' => 2, # defaults to 2 decks
23 'srand' => time(), # rand seed for game - can be overridden by attr.
24 'options' => ['hit','stand'], # list of acceptable responses to a decide_complex
25 # call, may (later) include split, døuble..
26 @_, # Override previous (default) attributes
27 };
28 0 srand($self->{'srand'}); # randomness is per game.
29 0 $self->{'deck'}->shuffle(); # have to do after the srand().
30 0 return bless $self, $class;
31 }
32  
33 0 ▶sub clearHands {
Subroutine clearHands: not called CC=2 SCAR=17.9
Pod undocumented
34 0 my $self = shift;
35 0 foreach my $p (@{$self->{'players'}}) {
36 0 $self->{'playersHands'}[$p->{'storage'}{'num'}] = [];
37 0 $p->{'hand'} = [];
38 }
39 0 $self->{'dealersHand'} = [];
40 }
41  
42 1 my $total_players = 0;
43 0 ▶sub addPlayers {
Subroutine addPlayers: not called CC=2 SCAR=17.9
Pod undocumented
44 0 my $self = shift;
45 0 my @players = @_;
46 0 foreach my $player (@players) {
47 0 $player->{'storage'}{'num'} = $total_players++;
48 0 push @{$self->{'players'}}, $player;
49 0 push @{$self->{'playersHands'}}, [];
50 }
51 }
52  
53 0 ▶sub addPlayer { return addPlayers(@_); }
Subroutine addPlayer: not called CC=1 SCAR=6.9
Pod undocumented
54  
55 0 ▶sub setDealer {
Subroutine setDealer: not called CC=1 SCAR=6.9
Pod undocumented
56 0 $_[0]->{'dealer'} = $_[1]; # set the dealer pointer to point to the passed obj.
57 }
58  
59 0 ▶sub dealersUpcard { $_[0]->{'dealersHand'}->[1] || ''; }
Subroutine dealersUpcard: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{"dealersHand"}[1] or "" 0 0 0
60  
61 0 ▶sub playersScores { $_[0]->{'playersScores'} || []; }
Subroutine playersScores: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{"playersScores"} or [] 0 0 0
62  
63 0 ▶sub setupPlayerScenario {
Subroutine setupPlayerScenario: not called CC=5 SCAR=34.0
Pod undocumented
64 0 my $self = shift;
65 0 my $player = shift;
66
67 0 ▶ if ($player eq $self->{'dealer'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($player eq $self->{'dealer'}) { } 0 0 0
68 # player is the dealer
69 0 ▶ $player->{'hand'} = $self->{'dealersHand'} || [];
Condition0%0/2
$self->{'dealersHand'} || []
l!l
00
MC/DC0%0/1
$self->{'dealersHand'} || []
$self->{'dealersHand'}
Truth table0%0/2
A
A= $self->{'dealersHand'}
Aresult
11
00
70 } else {
71 # player is not the dealer
72 0 ▶ $player->{'hand'} = $self->{'playersHands'}->[$player->{'storage'}{'num'}] || [];
Condition0%0/2
$self->{'playersHands'}[$player->{'storage'}{'num'}] || []
l!l
00
MC/DC0%0/1
$self->{'playersHands'}[$player->{'storage'}{'num'}] || []
$self->{'playersHands'}[$player->{'storage'}{'num'}]
Truth table0%0/2
A
A= $self->{'playersHands'}[$player->{'storage'}{'num'}]
Aresult
11
00
73 0 ▶ $player->{'dealerUpcard'} = $self->dealersUpcard() || '';
Condition0%0/2
$self->dealersUpcard || ''
l!l
00
MC/DC0%0/1
$self->dealersUpcard || ''
$self->dealersUpcard
Truth table0%0/2
A
A= $self->dealersUpcard
Aresult
11
00
74 }
75 0 return $player;
76 }
77  
78 # playRound is the SIMPLE version. playComplexRound will come later.
79 0 ▶sub playRound {
Subroutine playRound: not called CC=22 SCAR=62.3
Pod undocumented
80 0 my $self = shift;
81 0 my @players = @{$self->{'players'}};
82
83 # open new deck
84 0 $self->{"deck"} = openNewDeck();
85
86 # shuffle deck
87 0 $self->{"deck"}->shuffle;
88  
89 # clear hands
90 0 $self->clearHands();
91  
92 # deal two cards in proper order:
93 0 foreach (0..1) {
94 0 print "dealing card to each player..\n";
95 0 foreach (0..$#players) {
96 #deal one from the deck to each player's hand
97 0 deal( 1, $self->{"deck"}, $self->{"playersHands"}->[$_]);
98 }
99  
100 0 print "dealing card to dealer..\n";
101 #deal one from the deck to the dealer's hand
102 0 deal( 1, $self->{"deck"}, $self->{"dealersHand"} );
103 }
104  
105 # each player in turn decides to hit or stay until they are done.
106 0 my $player_hit = 1; # to get into loop
107 0 foreach (0..$#players) {
108 0 $player_hit = 1; # to get into loop
109 0 $player = $players[$_] = $self->setupPlayerScenario($players[$_]); # load up private data fields in player obj.
110 0 my $handVal = handValue($self->{"playersHands"}->[$_]);
111 0 ▶ while ( ($handVal = handValue($self->{"playersHands"}->[$_])) < 21 and $player_hit) {
Branch: 1 branch
BranchTrueFalseTotal
if (($handVal = handValue($self->{"playersHands"}[$_])) < 21 and $player_hit) 0 0 0
Condition0%0/3
($handVal = handValue($self->{"playersHands"}[$_])) < 21 and $player_hit
!ll&&!rl&&r
000
MC/DC0%0/2
($handVal = handValue($self->{"playersHands"}[$_])) < 21 and $player_hit
($handVal = handValue($self->{"playersHands"}[$_])) < 21 $player_hit
Truth table0%0/3
A and B
A= ($handVal = handValue($self->{"playersHands"}[$_])) < 21B= $player_hit
ABresult
0X0
100
111
112 0 my $decision = $player->decide_simple();
113 0 ▶ if ($decision eq 'hit' or ($decision and $decision ne 'stand')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($decision eq 'hit' or $decision and $decision ne 'stand') { } 0 0 0
Condition0%0/6
$decision eq "hit" or $decision and $decision ne "stand"
l!l&&r!l&&!r
000
$decision and $decision ne "stand"
!ll&&!rl&&r
000
MC/DC0%0/3
$decision eq "hit" or $decision and $decision ne "stand"
$decision eq "hit" $decision $decision ne "stand"
Truth table0%0/4
A or B and C
A= $decision eq "hit"B= $decisionC= $decision ne "stand"
ABCresult
1XX1
0111
00X0
0100
114 #deal one from the deck to the player's hand
115 0 deal( 1, $self->{"deck"}, $self->{"playersHands"}->[$_]);
116 0 print "player $_ hits on $handVal..\n";
117 } else {
118 0 print "player $_ stays on $handVal..\n";
119 0 $player_hit = 0;
120 }
121 }
122
123 }
124  
125 # now let dealer play out
126 0 my $dealer = $self->{'dealer'} = $self->setupPlayerScenario($self->{'dealer'});
127 0 $player_hit = 1; # get into loop
128 0 ▶ while ( ($handVal = $dealer->myHandValue()) < 21 and $player_hit) {
Branch: 1 branch
BranchTrueFalseTotal
if (($handVal = $dealer->myHandValue) < 21 and $player_hit) 0 0 0
Condition0%0/3
($handVal = $dealer->myHandValue) < 21 and $player_hit
!ll&&!rl&&r
000
MC/DC0%0/2
($handVal = $dealer->myHandValue) < 21 and $player_hit
($handVal = $dealer->myHandValue) < 21 $player_hit
Truth table0%0/3
A and B
A= ($handVal = $dealer->myHandValue) < 21B= $player_hit
ABresult
0X0
100
111
129 0 my $decision = $dealer->decide_simple();
130 0 my $handStr = $dealer->myHandStr();
131 0 ▶ if ($decision eq 'hit' or ($decision and $decision ne 'stand')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($decision eq 'hit' or $decision and $decision ne 'stand') { } 0 0 0
Condition0%0/6
$decision eq "hit" or $decision and $decision ne "stand"
l!l&&r!l&&!r
000
$decision and $decision ne "stand"
!ll&&!rl&&r
000
MC/DC0%0/3
$decision eq "hit" or $decision and $decision ne "stand"
$decision eq "hit" $decision $decision ne "stand"
Truth table0%0/4
A or B and C
A= $decision eq "hit"B= $decisionC= $decision ne "stand"
ABCresult
1XX1
0111
00X0
0100
132 #deal one from the deck to the player's hand
133 0 deal( 1, $self->{"deck"}, $self->{"dealersHand"});
134 0 print "dealer hits on [$handStr] ($handVal)..\n";
135 0 ▶ if ($handVal == 0) {my $handStr = $dealer->myHandStr(); print "dealer hand [$handStr]";}
Branch: 1 branch
BranchTrueFalseTotal
if ($handVal == 0) 0 0 0
136 } else {
137 0 print "dealer stays on [$handStr] ($handVal)..\n";
138 0 $player_hit = 0;
139 }
140 }
141  
142  
143
144 # compare values
145 0 my $dval = handValue($self->{'dealersHand'});
146 0 print "dealer hand value $dval\n";
147  
148 0 my @pvals = map {handValue($_)} @{$self->{'playersHands'}};
149 0 print "player hand values [@pvals]\n";
150  
151 0 my @phstrs = map {handStr($_)} @{$self->{'playersHands'}};
152  
153 0 ▶ if ($dval > 21) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dval > 21) 0 0 0
154 # dealer busts
155 0 print "dealer busts with $dval; all players win.\n";
156 0 foreach (0..$#players) {$self->{'playersScores'}[$_]++} # add 1 for all players
157 0 return; # and end the round.
158 }
159 0 my $pnum = 0;
160 0 foreach my $p (@pvals) {
161
162 0 ▶ if ($p > 21) {
Branch: 4 branches
BranchTrueFalseTotal
if ($p > 21) { } 0 0 0
elsif ($p == 21) { } 0 0 0
elsif ($p == $dval) { } 0 0 0
elsif ($p > $dval) { } 0 0 0
163 0 print "player $pnum busts with $p ($phstrs[$pnum]).\n";
164 0 $pnum++;
165 0 next; }
166 elsif ($p == 21) {
167 0 print "player $pnum gets a 21 (Assuming blackjack for now) $p ($phstrs[$pnum]) (score 1.5).\n";
168 0 $self->{'playersScores'}[$pnum] += 1.5;
169 0 next; }
170 elsif ($p == $dval) {
171 0 print "player $pnum has $p ($phstrs[$pnum]) vs. dealer's $dval, push (score 0.5).\n";
172 0 $self->{'playersScores'}[$pnum] += 0.5;
173 }
174 elsif ($p > $dval) {
175 0 print "player $pnum has $p ($phstrs[$pnum]) vs. dealer's $dval, player $pnum wins (score 1).\n";
176 0 $self->{'playersScores'}[$pnum]++;
177 } else {
178 0 print "player $pnum has $p ($phstrs[$pnum]) vs. dealer's $dval, player $pnum loses (score 0)\n";
179 }
180 0 $pnum++;
181 }
182  
183 }
184  
185 0 ▶sub running {
Subroutine running: not called CC=1 SCAR=6.9
Pod undocumented
186 0 return $running;
187 }
188  
189 0 ▶sub quit {
Subroutine quit: not called CC=1 SCAR=6.9
Pod undocumented
190 0 $running = 0;
191 }
192  
193 0 ▶sub start {
Subroutine start: not called CC=2 SCAR=17.9
Pod undocumented
194 0 my $self = shift;
195 0 my @players = @{$self->{'players'}};
196 # game config is done and I can prepare to run game
197 0 $running = 1;
198 # blank out player scores
199 0 foreach (0..$#players) {
200 0 $self->{'playersScores'}[$_] = 0;
201 }
202 }
203  
204 1 1;
↑ Summary BlackJack/Utilities.pm »