Listing 2: blackjack

01 #!/usr/bin/perl
02 ###########################################
03 # play - Blackjack against Las Vegas Dealer
04 # Mike Schilli, 2003 (m@perlmeister.com)
05 ###########################################
06 use warnings; use strict;
07 
08 use Blackjack;
09 use Term::ANSIColor qw(:constants);
10 use Term::ReadKey;
11 
12 $| = 1; my $total = 0;
13 
14 my $shoe = Blackjack::Shoe->new(
15                            nof_decks => 4);
16 {
17   if($shoe->remaining() < 52) {
18     print "Shuffling ...\n";
19     $shoe->reshuffle();
20   }
21 
22   my $player = Blackjack::Hand->new(
23              shoe => $shoe);
24   my $dealer = Blackjack::Hand->new(
25              shoe => $shoe);
26 
27   $dealer->draw();
28   P(RED, "D", $dealer);
29   $dealer->draw();
30 
31   $player->draw();
32   $player->draw();
33 
34   while(!$player->busted()) {
35     P(BLUE, "P", $player);
36     print "([H]it/[S]tand/[Q]uit) ";
37     ReadMode 4;
38     my $move = ReadKey(0);
39     ReadMode 0;
40     print "\r";
41     last if $move =~ /^s/i;
42     exit 0 if $move =~ /^q/i;
43     $player->draw();
44   }
45 
46   P(BLUE, "P", $player);
47 
48   while(!$dealer->busted() and 
49         $dealer->count("soft") < 17) {
50     P(RED, "D", $dealer);
51     $dealer->draw();
52   }
53 
54   P(RED,  "D", $dealer);
55 
56   $total += $player->score($dealer);
57 
58   print "Score: ", 
59         $player->score($dealer), 
60         ", Total: ", $total, "\n\n";
61 
62   redo;
63 }
64 
65 sub P { # Print status in color
66     print(BOLD, $_[0], "$_[1]", "[",
67     $_[2]->count_as_string(), "]",
68     RESET, ": ", $_[2]->as_string(), "\n")
69 }