Games::Blackjack - Blackjack Utility Classes
use Games::Blackjack; # Create new shoe of cards my $shoe = Games::Blackjack::Shoe->new(nof_decks => 4); # Create two hands, player/dealer my $player = Games::Blackjack::Hand->new(shoe => $shoe); my $dealer = Games::Blackjack::Hand->new(shoe => $shoe); # Two dealer cards $dealer->draw(); print "Dealer: ", $dealer->as_string(), "\n"; $dealer->draw(); # 2nd card not shown $player->draw(); $player->draw(); print "Player: ", $player->as_string, "(", $player->count_as_string, ")\n"; # Let's assume player decides to stand. Dealer's turn. # Dealer plays Las Vegas rules while(!$dealer->busted() and $dealer->count("soft") < 17) { $dealer->draw(); } # Show winner (-1: Dealer, 1: Player, 1.5: Player Blackjack) print "Player score: ", $player->score($dealer), "\n";
Games::Blackjack provides the plumbing for implementing Blackjack games. It was originally published in the German "Linux-Magazin", the article is available online at
http://www.linux-magazin.de/Artikel/ausgabe/2003/12/perl/perl.html
The English version appeared in the British "Linux-Magazine" 01/2004 on the newsstands and will be available online later at
http://www.linux-magazine.com/issue/38
A sample program, available in the distribution as eg/blackjack, shows a simple command line tool allowing a simplified game against a Las-Vegas-Style dealer.
eg/blackjack
The module uses Quantum::Superpositions under the hood for educational purposes.
Abstracts the "shoe", the container which the dealer extracts the cards from. A shoe typically holds a number of decks of cards.
Create a new Games::Blackjack::Shoe object, containing the specified number of decks.
Games::Blackjack::Shoe
Number of cards still available in the shoe.
Refill the shoe with a number of decks, as specified in the constructor call earlier and shuffle them with Fisher-Yates.
Extract a card from the shoe. $card is a reference to an array containing the suit of the card ("Heart", "Diamond", "Spade", "Club") as the first element and the value ("A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K") as the second. undef is returned if no more cards are available. This Method is being called by a Games::Blackjack::Hand object if its draw() method gets called.
undef
Abstracts a player's or the dealer's "hand", a number of cards held by either party.
Create a new Games::Blackjack::Hand object, connected to a "shoe", which will feed this "hand" via the draw() method.
Games::Blackjack::Hand
draw()
Draw a card from the shoe and put it into the hand. This will change the count of the hand. If the shoe runs out of cards, it automatically refills itself.
Show the cards of a hand as string, e.g. Heart A, Spade 10.
Heart A, Spade 10
Show the different counts of a hand as a string.
Count a hand. If $how is set to "soft", the soft count of the hand is calculated. If $how is set to "hard", the hard count is returned. If the hand is busted, undef is returned.
$how
Returns true if the hand is busted (hard count exceeds 21), and false otherwise.
Returns true if the hand is a Blackjack and false otherwise.
Returns the score of the player against the dealer hand object, passed in as $dealer. According to the Blackjack rules, this can be -1, 0, 1 and 1.5 (if the Player has a Blackjack).
Games::Blackjack is Log::Log4perl-enabled. To figure out what goes on behind the scenes, simple put something like
Games::Blackjack
Log::Log4perl
use Log::Log4perl qw(:easy); Log::Log4perl->easy_init($DEBUG);
in front of your program. For more detailed Log::Log4perl option, check out
http://log4perl.sourceforge.net
Copyright 2003 by Mike Schilli, all rights reserved. This program is free software, you can redistribute it and/or modify it under the same terms as Perl itself.
2003, Mike Schilli <cpan@perlmeister.com>
To install Games::Blackjack, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Games::Blackjack
CPAN shell
perl -MCPAN -e shell install Games::Blackjack
For more information on module installation, please visit the detailed CPAN module installation guide.