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)]; }

In reply to Perl learns Blackjack by YuckFoo

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.