ergowolf has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to create a blackjack program in perl from scratch. It turned out to be harder than I thought. I would eventually like to make it client server with limited AI. I am stuck on creating a random number of OOP players and a dealer. I would appreciate any help.

The program is here.
use strict; use integer; use Tom::Cardplay; my $play1 = Cardplay->new(); $play1->{NAME} = "Ergowolf"; print "$play1->{NAME}\n"; # add cards &Cardplay::deal; push(@{ $play1->{CARDS} }, $_); push(@{ $play1->{VALUES} }, $value); &Cardplay::deal; push(@{ $play1->{CARDS} }, $_); push(@{ $play1->{VALUES} }, $value); print "@{ $play1->{CARDS} }\n"; print "@{ $play1->{VALUES} }\n";

Here is the library.

package Cardplay; use strict; use vars qw($VERSION @ISA @EXPORT @EXPORT_OK); use Exporter(); @ISA = qw(Exporter); @EXPORT_OK = qw( $value %deck &deal &total ); use vars qw( $value %deck &deal &total ); $VERSION = '1.00'; # import a scalar # It turns out I replaced this with # $_ so I didn't need it # my $card1 = $Cardplay::card; # print "The card is $card1.\n"; # $card = "Ace"; $value; # This is the card deck # You can test this with the code bellow # my %deck1 = %Cardplay::deck; # my $single = $deck1{'2 of spades'}; # print "The single card is $single.\n"; %deck = ( '2 of spades' => 2, '3 of spades' => 3, '4 of spades' => 4, '5 of spades' => 5, '6 of spades' => 6, '7 of spades' => 7, '8 of spades' => 8, '9 of spades' => 9, '10 of spades' => 10, 'Jack of spades' => 10, 'Queen of spades' => 10, 'King of spades' => 10, 'Ace of spades' => 11, '2 of hearts' => 2, '3 of hearts' => 3, '4 of hearts' => 4, '5 of hearts' => 5, '6 of hearts' => 6, '7 of hearts' => 7, '8 of hearts' => 8, '9 of hearts' => 9, '10 of hearts' => 10, 'Jack of hearts' => 10, 'Queen of hearts' => 10, 'King of hearts' => 10, 'Ace of hearts' => 11, '2 of clubs' => 2, '3 of clubs' => 3, '4 of clubs' => 4, '5 of clubs' => 5, '6 of clubs' => 6, '7 of clubs' => 7, '8 of clubs' => 8, '9 of clubs' => 9, '10 of clubs' => 10, 'Jack of clubs' => 10, 'Queen of clubs' => 10, 'King of clubs' => 10, 'Ace of clubs' => 11, '2 of diamonds' => 2, '3 of diamonds' => 3, '4 of diamonds' => 4, '5 of diamonds' => 5, '6 of diamonds' => 6, '7 of diamonds' => 7, '8 of diamonds' => 8, '9 of diamonds' => 9, '10 of diamonds' => 10, 'Jack of diamonds' => 10, 'Queen of diamonds' => 10, 'King of diamonds' => 10, 'Ace of diamonds' => 11, ); sub new { my $self = {}; $self->{NAME} = undef; $self->{TOTAL} = undef; $self->{CARDS} = []; $self->{VALUES} = []; bless($self); # but see below return $self; } # This is a routine to randomly deal cards # from the deck # You can test this with the code bellow # my @player = qw( red green ); # &Cardplay::deal; # push(@player, $_); # for (@player) { # print "list $_\n"; # } sub deal { $_ = (keys %deck)[rand keys %deck]; $value = $deck{"$_"}; return $_; return $value # I pushed two variables to @_ } # This subroutine compares for the larger score # I need to write the code for this based on how # OOP players turns out sub total { # if ($total > $dtotal) { # print "\n\nThe player wins with $total.\n"; # } else { # print "\n\nThe dealer wins with $dtotal.\n"; # } } END {}

Replies are listed 'Best First'.
Re: Perl Blackjack
by ZZamboni (Curate) on May 01, 2000 at 17:50 UTC
    I don't fully understand your question. But here's a code snippet that will generate the %deck hash without so much typing:
    %deck=map { $t=($_%13); sprintf("%s of %s", $t>8?(('Jack','Queen','King','Ace')[$t-9]):($t+2), (('spades','hearts','clubs','diamons')[$_/13])) => (2..9,(10)x4,11)[$t] } 0..52
RE: Perl Blackjack
by Adam (Vicar) on May 02, 2000 at 01:38 UTC
    Its always good to try things out for yourself, as well as ask questions and look at what's already been done. I just wanted to congradulate you on doing this so cleanly. I wrote a Black Jack game as an early attempt at Perl, and it was a mess. It worked, the computer was the dealer (which has strict casino rules). I never tried adding multiple players, but I did eventually extend my card manipulation code to include Baccarat and Video Poker. Then I learned the value of OO!
      I like ZZamboni's deck better than mine. It looks much more stylish and compact. However, I am concerned about sacrificing the readability and I wonder which would load faster the array or ZZamboni. My guess is yours is faster. I haven't used the Benchmark tutorial, yet. It could be a cool test. Thanks ZZamboni! That deserves some xp!

      Thanks for the complement Adam. I would like to see your code or any ideas you have on modifying mine. I try to write my perl as simply as possible. I wrote the first couple versions with all the code in the script and the deck as an array. I discovered that despite the overhead OOP and hashes were the way to go. This version is about my third or fourth start from scratch. I am mainly stuck on creating multiple players dynamically. For example, You could play 2 players the first game and 4 the next.

      I am hoping to get some more feedback or a co-author to "inspire" and work with me to finish it. Would you like to help me finish it? If your interested post back or email me off my webpage. I have never worked with someone on a programming project before. It would be interesting.

      Here are the two big benefits to completing the program I see for finishing it.

      We could make the program client server. Maybe host it on perlmonks?

      We could write a limited AI to statistaclly discover the best way to play by copying the results to a database or excel (I have done both).
Re: Perl Blackjack
by merlyn (Sage) on Apr 30, 2000 at 18:44 UTC
    search.cpan.org revealed Games::Cards in about ten seconds of searching. Why don't you start with that module instead of reinventing the wheel? -- Randal L. Schwartz, Perl hacker
      I knew about the CPAN module. I created my module to learn how to create and interact with one. You are right I should stick with the CPAN one and save learning to write modules for another project.