#!/usr/bin/perl ############################################# ############################################# ## Blackjack ## ## Written by ZiaTioN ## ## (email ziation@yahoo.com) ## ## ## ## version 1.0 ## ## latest version is available from ## ## http://thenewage.dyndns.org ## ## ## ## Thanx to DrNikon of HDC for help ## ## with the cgi portion ## ############################################# ############################################# # COPYRIGHT NOTICE: # # Copyright 2003 ZiaTioN All Rights Reserved. # # This program may be used and modified free of charge by anyone, so # long as this copyright notice and the header above remain intact. By # using this program you agree to indemnify the author from any # liability. # # Selling the code for this program without prior written consent is # expressly forbidden. Obtain permission before redistributing this # program over the Internet or in any other medium. In all cases # copyright and header must remain intact. use CGI qw(:all); print header, start_html("Blackjack Game!"), h1("Blackjack Game!"); #This defines the value of each possible face card. %face = ( A => 11, K => 10, Q => 10, J => 10, ); $player=0; $dealer=0; #&intro; if (param('answer')) { again(); } else { intro(); } #This subroutine starts the game and gives player #200 dollars to start with. sub intro { print start_form(), hr, p('Would you like to play?', popup_menu('answer', ['Yes', 'No'])), #param 'answer' get's defines as Yes or No. p(submit('Answer')), $answer = param('answer'); if ($answer =~ /Yes/i) { $total=200; print p("You have $total dollars to start with!"); sleep 2; game(); } else { print p("Please come back again!"); exit; end_form, hr, end_html; } } sub game { $player=0; $dealer=0; p('Place your bet.', popup_menu('bet', [""])), $bet = param('bet'); play(); end_form, hr, end_html; } #This subroutine deals dealers first card and players first 2 cards. sub play { $aced=0; $acep=0; $dealer=(2..10, A,K,Q,J)[rand 13]; print p("Dealers first card is $dealer"); #Checks if card is an ace to later determine if it #should have a value of 1 or 11. if ($dealer =~ m/A/i) { $aced=1; } #else{} #Checks hash to see if the card dealt matches any #any in it's list and resolves value if true. foreach $key (keys %face) { if ($dealer =~ m/$key/i) { $dealer=$face{$key}; } #else{} } #Players first card. $player=(2..10, A,K,Q,J)[rand 13]; print p("Your first card is $player"); if ($player =~ m/A/i) { $acep=1; }else{} foreach $key (keys %face) { if ($player =~ m/$key/i) { $player=$face{$key}; }else{} } #Players second card. $player3=(2..10, A,K,Q,J)[rand 13]; print p("Your second card is $player3"); if ($player3 =~ m/A/i) { $acep=1; }else{} foreach $key (keys %face) { if ($player3 =~ m/$key/i) { $player3=$face{$key}; }else{} } #Calculates value of players first and second cards. $player=$player+$player3; #If player has 21 on the deal game declares "Blackjack" has been #dealt and awards 1 and a half the total bet and stops current hand. if ($player eq 21) { print p("YOU HAVE BLACKJACK!!"); $total=$total+$bet+($bet/2); print p("Your now have $total dollars."); again(); }elsif ($player ne 21) { print p("You have $player"); sleep 2; hit(); }else{} end_form, hr, end_html; } #This subroutine asks player if he/she would like a hit, if yes it deals #another random card and if no it jumps to the "results" subroutine. sub hit { p('Would you like a hit?', popup_menu('answer2', ['Yes', 'No'])); $answer2 = param('answer2'); if ($answer2 =~ /Yes/i) { $player2=(2..10, A,K,Q,J)[rand 13]; print p("Your next card is $player2"); if ($player2 =~ m/A/i) { $acep2=1; }else{} foreach $key (keys %face) { if ($player2 =~ m/$key/i) { $player2=$face{$key}; }else{} } #Calculates new value of players first 2 cards and #the new card that was dealt. $player=$player+$player2; #This function determines if the total value is more than 21, then #checks to see if an Ace was dealt in the hand somewhere. If so it #reassigns the value of the Ace to 1. If no ace is present it alerts #the player that they have busted. if (($acep2 eq 1) && ($player > 21)) { $player=$player-10; $acep2=0; }elsif (($acep eq 1) && ($player >21)) { $player=$player-10; $acep=0; }else{} }else{ $no=1; } end_form, hr, end_html; results(); } #This subroutine gives dealer their second card. sub dhit { $aced2=0; if ($dealer < 17 ) { $dealer2=(2..10, A,K,Q,J)[rand 13]; print p("Dealers next card is $dealer2"); if ($dealer2 =~ m/A/i) { $aced2=1; }else{} foreach $key (keys %face) { if ($dealer2 =~ m/$key/i) { $dealer2=$face{$key}; }else{} } #Calculates dealers total for first 2 cards. $dealer=$dealer+$dealer2; #Same Ace function as player. if (($aced2 eq 1) && ($dealer > 21)) { $dealer=$dealer-10; $aced2=0; }elsif (($aced eq 1) && ($dealer > 21)) { $dealer=$dealer-10; $aced=0; }else{} } end_form, hr, end_html; results(); } #This subroutine decides based on summ of all cards in each #hand who has won and lost. sub results { print p("You have $player"); if (($player < 22) && ($no != 1)) { hit(); }elsif ($player > 21) { print p("You have busted so you lose"); $total=$total-$bet; print p("Your new total is $total dollars!"); sleep 2; again(); }else{} print p("Dealer has $dealer"); #This declares that if dealer has less than 17 they must take another hit. if ($dealer < 17) { dhit(); }else{} if (($player > $dealer) && ($player < 22) && ($dealer < 22)) { print p("You are the winner!"); $total=$total+$bet; sleep 1; print p("Your new total is $total dollars!"); }elsif (($player < $dealer) && ($player < 22) && ($dealer < 22)) { print p("You have lost!"); $total=$total-$bet; sleep 1; print p("Your new total is $total dollars!"); }elsif (($player < 22) && ($dealer > 21)) { print p("The dealer has busted and you have won!"); $total=$total+$bet; sleep 1; print p("Your new total is $total dollars!"); }else{ print p("It is a push"); sleep 1; print p("Your total remains the same at $total dollars!"); } end_form, hr, end_html; again(); } #This subroutine checks to see if the players money total is zero #or less than zero and ends game if true. Also asks if player would #like to play another hand if player still has money to play with. sub again { if ($total <= 0) { print p("You have lost your ass scum bag!"); exit; }else{} p('Would you like to play another hand?', popup_menu('answer3', ['Yes', 'No'])); $answer3 = param('answer3'); if ($answer3 =~ /Yes/i) { reset '$no'; game(); }else{ print p("Thanx for playing!"); end_html(); exit; } }