#!/usr/bin/perl -- use List::Util 'shuffle'; %params = (); $queryString = $ENV{"QUERY_STRING"}; @keyValuePairs = split(/\&/, $queryString); foreach my $keyValuePair (@keyValuePairs) { ($key, $value) = split(/\=/, $keyValuePair); $key =~ s/%([a-f0-9]{2})/chr(hex($1))/eig; # URL decode the key $value =~ s/%([a-f0-9]{2})/chr(hex($1))/eig; # URL decode the value $params{$key} = $value; } @deck = (); @player = (); @dealer = (); @discard = (); if(!$params{"SESSIONID"} || !-e "/tmp/r_leguen.cgi_blackjack.".$params{"SESSIONID"}) { # if no session ID is provided, # we initialize a session @cards = ( "c_2", # the 2 of clubs "c_3", # the 3 of clubs "c_4", # the 4 of clubs "c_5", # the 5 of clubs "c_6", # the 6 of clubs "c_7", # the 7 of clubs "c_8", # the 8 of clubs "c_9", # the 9 of clubs "c_10", # the 10 of clubs "c_j", #the Jack of clubs "c_q", #the Queen of clubs "c_k", #the King of clubs "d_2", # the 2 of diamonds "d_3", # the 3 of diamonds "d_4", # the 4 of diamonds "d_5", # the 5 of diamonds "d_6", # the 6 of diamonds "d_7", # the 7 of diamonds "d_8", # the 8 of diamonds "d_9", # the 9 of diamonds "d_10", # the 10 of diamonds "d_j", #the Jack of diamonds "d_q", #the Queen of diamonds "d_k", #the King of diamonds "h_2", # the 2 of hearts "h_3", # the 3 of hearts "h_4", # the 4 of hearts "h_5", # the 5 of hearts "h_6", # the 6 of hearts "h_7", # the 7 of hearts "h_8", # the 8 of hearts "h_9", # the 9 of hearts "h_10", # the 10 of hearts "h_j", #the Jack of hearts "h_q", #the Queen of hearts "h_k", #the King of hearts "s_2", # the 2 of spades "s_3", # the 3 of spades "s_4", # the 4 of spades "s_5", # the 5 of spades "s_6", # the 6 of spades "s_7", # the 7 of spades "s_8", # the 8 of spades "s_9", # the 9 of spades "s_10", # the 10 of spades "s_j", #the Jack of spades "s_q", #the Queen of spades "s_k", #the King of spades ); @deck = shuffle(@cards); $sessionID = int(rand(999999999999999)); } else { # if a session ID is provided, we initialize session data. $sessionID = $params{"SESSIONID"}; open("SESSION", "; chomp($cards); @deck = split(/,/, $cards); # we read the second line, # which is the list of cards in the hand # seperated by commas $cards = ; chomp($cards); @player = split(/,/, $cards); # we read the third line, # which is the list of cards in the computer's hand # seperated by commas $cards = ; chomp($cards); @dealer = split(/,/, $cards); # we read the fourth line, # which is the list of cards in the deck # seperated by commas $cards = ; chomp($cards); @discard = split(/,/, $cards); close(SESSION); } if(!@player && !@dealer) { for($i=0; $i<2; $i++) { push(@player, pop(@deck)); push(@dealer, pop(@deck)); } } if($params{"Action"} eq "Hit") { push(@player, pop(@deck)); if(!@deck) { # if the player draws # all the cards from # the deck, we shuffle the # discard pile back in. @deck = shuffle(@discard); @discard = (); } } elsif($params{"Action"} eq "Stand") { $dealerScore = 0; for($i=0; $i<@dealer; $i++) { $card = $dealer[$i]; if($card =~ /(\d+)/) { $dealerScore += $1; } else { $dealerScore += 10; } # the dealer has to keep drawing cards # until his score is 17 or higher if($dealerScore < 17 && $i+1 >= @dealer) { push(@dealer, pop(@deck)); if(!@deck) { # if the dealer draws # all the cards from # the deck, we shuffle the # discard pile back in. @deck = shuffle(@discard); @discard = (); } } } } print "Content-Type: text/html\n\n"; print "\n"; print "\n"; print ""; print "CGI BlackJack"; print "\n"; print "\n"; print "

Welcome To CGI BlackJack

\n"; print "

Dealer's Hand:

\n"; print $addition; print "

\n"; if(@dealer) { print "

"; print "\"".$dealer[0]."\" "; for($i=1; $i<@dealer; $i++) { if($params{"Action"} eq "Stand") { print "\"".$dealer[$i]."\" "; } else { print "\"?\"/ "; } } print "
"; } print "

"; print "

Your Hand:

\n"; print "

\n"; if(@player) { print "

"; for($i=0; $i<@player; $i++) { print "\"".$player[$i]."\" "; } print "
"; } print "

\n"; if($params{"Action"} eq "Stand") { while(@player) { push(@discard, pop(@player)); } while(@dealer) { push(@discard, pop(@dealer)); } print "Play Again?"; } else { print "Hit or Stand"; } print "\n"; print ""; # save session information in a file open("SESSION", ">/tmp/r_leguen.cgi_blackjack.".$sessionID); print SESSION join(",",@deck)."\n"; print SESSION join(",",@player)."\n"; print SESSION join(",",@dealer)."\n"; print SESSION join(",",@discard)."\n"; close(SESSION);