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
    print "Dealer: ", $dealer->as_string(), "\n";
    $dealer->draw(); # 2nd card not shown

    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) {

       # 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

The English version appeared in the British "Linux-Magazine" 01/2004 on the newsstands and will be available online later at

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.

The module uses Quantum::Superpositions under the hood for educational purposes.

Classes and Methods


Abstracts the "shoe", the container which the dealer extracts the cards from. A shoe typically holds a number of decks of cards.

$shoe = Games::Blackjack::Shoe->new(nof_decks => $n)

Create a new Games::Blackjack::Shoe object, containing the specified number of decks.


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.

$card = $shoe->draw_card()

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.


Abstracts a player's or the dealer's "hand", a number of cards held by either party.

$hand = Games::Blackjack::Hand->new(shoe => $shoe)

Create a new Games::Blackjack::Hand object, connected to a "shoe", which will feed this "hand" via the draw() method.


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.


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.


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).

Debugging with Log::Log4perl

Games::Blackjack is Log::Log4perl-enabled. To figure out what goes on behind the scenes, simple put something like

    use Log::Log4perl qw(:easy);

in front of your program. For more detailed Log::Log4perl option, check out


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 <>