### Perl learns Blackjack

by YuckFoo (Abbot)
 on Jul 01, 2002 at 20:39 UTC ( #178676=CUFP: print w/replies, xml ) Need Help??

Ok, maybe not really 'learning', more of a statistical model, perhaps even a flawed one. Many hands of Blackjack are played and results of play are reported: the score of your hand, the house's up-card, whether you should stay or hit, percent won by staying, percent won by hitting.

An 'infinite' deck is used, a better model would use a 4-deck or 8-deck shoe. It would also be useful to analyse when to split pairs and when to double down.

There are some surprising results. I thought conventional wisdom was to hit a 16 if dealer has a 7 or better, but this model suggests to only hit 16 if dealer has an 7 or Ace. Maybe I need to play more hands...

JackFoo

```#!/usr/bin/perl

use strict;

my @DECK = qw(02 03 04 05 06 07 08 09 10 10 10 10 11);

my (%grid);

for (1..64000) { learn(\%grid); }

for my \$score (12..20) {
print "  You House S/H S%  H%\n";
for my \$house (2..11) {

\$house = sprintf("%2.2d", \$house);

my (\$std, \$hit, \$do);
my \$key = "\$house-\$score";

if (\$grid{\$key}{'stot'} > 0) {
\$std = \$grid{\$key}{'swin'} / \$grid{\$key}{'stot'};
}

if (\$grid{\$key}{'htot'} > 0) {
\$hit = \$grid{\$key}{'hwin'} / \$grid{\$key}{'htot'};
}

if (\$std > \$hit) { \$do = 'S'; }
else             { \$do = 'H'; }

\$std = sprintf("%3d", (\$std*100));
\$hit = sprintf("%3d", (\$hit*100));

print "  \$score   \$house    \$do \$std \$hit\n";
}
print "\n";
}

#-----------------------------------------------------------
sub learn {

my (\$grid) = @_;
my (@house, \$hscore);
my (@mine,  \$mscore);

push (@house, draw());
push (@house, draw());
\$hscore = score(\@house);

while (\$hscore > 0 && \$hscore < 17) {
push (@house, draw());
\$hscore = score(\@house);
}

push (@mine, draw());
push (@mine, draw());
\$mscore = score(\@mine);

while (\$mscore > 0 && \$mscore < 21) {

my \$key = "\$house[0]-\$mscore";
if (!defined(\$grid->{\$key})) { \$grid->{\$key} = {}; }

if (\$mscore > \$hscore) { \$grid->{\$key}{'swin'}++; }
\$grid->{\$key}{'stot'}++;

push (@mine, draw());
\$mscore = score(\@mine);

if (\$mscore > \$hscore) { \$grid->{\$key}{'hwin'}++; }
\$grid->{\$key}{'htot'}++;
}
}

#-----------------------------------------------------------
sub score {

my (\$cards) = @_;
my (\$aces, \$score);

for my \$card (@\$cards) {
\$score += \$card;
if (\$card == 11) { \$aces++; };
}

while(\$score > 21 && \$aces) {
\$score -= 10;
\$aces--;
}

if (\$score > 21) { \$score = 0; }

return sprintf("%2.2d", \$score);
}

#-----------------------------------------------------------
sub draw {

return \$DECK[rand(@DECK)];

}

Replies are listed 'Best First'.
Re: Perl learns Blackjack
by hossman (Prior) on Jul 01, 2002 at 22:40 UTC
There are some surprising results. I thought conventional wisdom was to hit a 16 if dealer has a 7 or better, but this model suggests to only hit 16 if dealer has an 7 or Ace. Maybe I need to play more hands...

• More hands may help, but I think your infinite deck is a bigger concern.
• I only skimmed the code, but one major problem I think you have is that you aren't ruling out the possibility that the dealer has a Blackjack -- if the dealer has a face up Ace, and the dealers down card has a value of 10, all play stops, dealer wins, there is no betting choice/action. That means that when calculating wether or not to hit or stand, you should completely rule out the possibility that the dealer has 21 -- becuase if they do, the question is moot, and shouldn't affect the probibilities the player should use to determine their action.

(edited: cleaned up formatting, and miss-choosen term)

Re: Perl learns Blackjack
by seanbo (Chaplain) on Jul 02, 2002 at 13:54 UTC
my @DECK = qw(02 03 04 05 06 07 08 09 10 10 10 10 11);

What about if an ace is to be used as a 1? It can be either 1 or 11.

Update:DOH! I guess I should have read a little further. Thanks for the correction hossman.

perl -e 'print reverse qw/o b n a e s/;'
that's covered in his score method.

Create A New User
Node Status?
node history
Node Type: CUFP [id://178676]
Approved by blakem
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (9)
As of 2019-10-15 08:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
In 2019 the site I miss most is:

Results (37 votes). Check out past polls.

Notices?