Blackjack Php Game

Started on the old forum in August 2008 · view the original in the archive · old links redirect here

5 replies · 3,860 views · started 18 years ago · 1 min read

4 participants
WhiteWarrior Principal · 1,419 posts #1

Cannot Be Bothered To zip It Up So...

create a index.php

<?
 include "Deck.class.php";
 
 
 session_start();
 
 $hideDealer = "hide";
 
 if($bank < 0)
 {
     session_register("bank");
     $bank=10;
 }
 
 if($playerHand == "")
 {
     session_register("deck");
     if($wins == "")
     {
         session_register("wins");
         $wins=0;
     }
     session_register("playerHand");
     session_register("dealerHand");
     session_register("currentBet");
     $currentBet="10";
     $deck = new Deck();
     $playerHand="";
     $dealerHand="";
 
 }
 
 
 if($action == 'changeBet')
 {
     $currentBet = $bet;
 }
 
 
 if($action == 'draw')
 {
     $playerHand=  $playerHand . $deck->drawCard() . ",";
     if($deck->points($playerHand) > 21)
     {
         $message = 'Player Busts';
         $bank-=$currentBet;
     }
 
     if($dealerHand=="")
     {
         $dealerHand = $dealerHand . $deck->drawCard() . ",";
         $dealerHand = $dealerHand . $deck->drawCard() . ",";
     }
 }
 if($action == 'new')
 {
     $playerHand ="";
     $dealerHand ="";
     $hideDealer = "hide";
 
     $playerHand=  $playerHand . $deck->drawCard() . ",";
     $playerHand=  $playerHand . $deck->drawCard() . ",";
 
     if($deck->points($playerHand) == 21)
     {
         $message = 'Player has BlackJack';
         $bank+=($currentBet + $currentBet/3);
     }
 
     $dealerHand = $dealerHand . $deck->drawCard() . ",";
     $dealerHand = $dealerHand . $deck->drawCard() . ",";
 
     if($deck->points($dealerHand) == 21)
     {
         $message = 'Dealer has BlackJack';
         $bank-=$currentBet;
     }
 
 
 }
 
 if($action == 'stay' && $deck->points($playerHand) > 0)
 {
     while($deck->points($dealerHand) < 16)
     {
         $dealerHand=  $dealerHand . $deck->drawCard() . ",";
     }
 
     if($deck->points($dealerHand) > 21)
     {
         $message = 'Dealer Busts';
         $bank += $currentBet;
 
     }
     else
     {
         $action = 'determineWinner';
     }
 
 }
 
 if($action == 'determineWinner')
 {
     $dealer = $deck->points($dealerHand);
     $player = $deck->points($playerHand);
 
     if($dealer == 21)
     {
         $message = 'Dealer has BlackJack';
         $bank-=$currentBet;
 
     }
     elseif($player == 21)
     {
         $message = 'Player has BlackJack';
         $bank+=($currentBet + ($currentBet/3));
     }
     elseif($player > $dealer)
     {
         $message = 'Player wins';
         $bank +=$currentBet;
 
     }
     else
     {
         $message = 'Push';
     }
     $action ='stay';
 
 }
 
 
 
 if($action == 'clear')
 {
     $playerHand= "";
 
     $dealerHand="";
 
     $message = "Your hand " . $playerHand;
 }
 
 if($action == 'stay' || $deck->points($playerHand) > 21 || $deck->points($dealerHand) > 21 || $deck->points($dealerHand) == 21 || $playerHand == "")
 {
     $hideDealer="";
 }
 
 ?>
 <html>
 <head>
 <title>BlaqJack (PHP Style)</title>
 <? include "stylesheet_tan.css"; ?>
 </head>
 <body>
 
 <table border=0 cellpadding=0 cellspacing=0><tr><td>
 
 <font class=label>
 <table border=0 cellpadding=0 cellspacing=0>
 <tr><td>
 <font class=norm>Dealer:

 <? $deck->displayHand($dealerHand, "$hideDealer"); ?>
 </font>
 </td></tr>
 <tr><td>
 <font class=norm>Player: <? echo $deck->points($playerHand); ?>

 <? $deck->displayHand($playerHand, ''); ?>
 </font>
 </td></tr>
 </table>
 
 <?
 if ($message)
     print "<center><font class=norm>[b]"  .$message . "

[/b]</font></center>";
 ?>
 [url="<?"]?action=draw>Hit[/url] |
 [url="<?"]?action=stay>Stay[/url]
 <?
     if($action == 'stay' || $deck->points($playerHand) > 21 || $deck->points($dealerHand) > 21 || $playerHand == "")
     {
         $dealerHand="";
         $playerHand="";
         ?>
         [url="<?"]?action=new>New Hand[/url]
         <?
     }
 ?>
 

<form>
 <input type=hidden name=action value=changeBet>
 <font class=norm>
 Bank: <? echo $bank; ?> Bet <input type=text size=4 name=bet value=<? echo $currentBet; ?>><input type=submit value='New Bet'><font class=norm>
 </font>
 </tr></td></table>
 
 
 </body>
 </html>

create Deck.class.php

<?
 class Deck
 {
     var $worth;
     var $name;
     var $suite;
     var $drawn;
 
     function Deck()
     {
         $this->shuffle();
     }
 
 
     function drawCard()
     {
         $card = rand(0, sizeOf($this->name)-1);
 
         //print "drawing Card:" . $this->drawn[$card] . "

";
 
         while($this->drawn[$card] == 'drawn')
         {
             $count++;
             $card = rand(0, sizeOf($this->name)-1);
             if($count > 25)
             {
                 print "took 25th card

";
                 $this->drawn[$card] = 'drawn';        
                 return ($this->name[$card] . " " . $this->suite[$card]);
 
                 $this->shuffle();
                 $count=0;
             }
         }
 
         $this->drawn[$card] = 'drawn';
         
         return ($this->name[$card] . " " . $this->suite[$card]);
         
 
     }
 
 
     function displayHand($hand, $hide)
     {
 
         //print "$hand
";
         $card = explode(",", $hand);
 
         print "
         <table border=1 height=150 cellpadding=0 cellspacgin=10>
         <tr>";
 
         $i=0;
 
         while($card[$i] )
         {
             $info = explode(" ", $card[$i]);
             
             switch($info[0])
             {
                 case 'A': $face = 'Ace';break;
                 case 'K': $face = 'King';break;
                 case 'Q': $face = 'Queen';break;
                 case 'J': $face = 'Jack';break;
                 default: $face = $info[0];
             }
 
 
             switch($info[1])
             {
                 case 'H': $su = 'Hearts';break;
                 case 'S': $su = 'Spades';break;
                 case 'C': $su = 'Clubs';break;
 
                 case 'D': $su = 'Diamonds';break;
                 default: $su = $info[0];
             }
 
             if($hide && $i==0)
             {
 
             print"<td width=100 valign=center align=center><font class=norm>
             Face Down
             </font></td>
             ";
             }
             else
             {
 
             print"<td width=100 valign=center align=center><font class=norm>
             " . $face . " of " . $su . "
             </font></td>
             ";
             }
 
             $i++;
         }
 
         print "</tr></table>";
 
     }
 
     function points($hand)
     {
 
         $total=0;
 
         $card = explode(",", $hand);
 
         $i=0;
 
         while($card[$i] )
         {
             $info = explode(" ", $card[$i]);
             
             switch($info[0])
             {
                 case 'A': $total += 11;break;
                 case 'K': $total += 10;break;
                 case 'Q': $total += 10;break;
                 case 'J': $total += 10;break;
                 default: $total += $info[0];
             }
 
             $i++;
         }
 
         return $total;
 
     }
 
     function getValue($card)
     {
         $info = explode(" " ,$card);
         if($info[0] == 'J')
             return 10;
 
         if($info[0] == 'K')
             return 10;
 
         if($info[0] == 'A')
             return 10;
 
         if($info[0] == 'Q')
             return 10;
 
         return $info[0];
     }
 
     function shuffle()
     {
         //print "Deck Shuffled";
 
         $count=0;
 
         for($i=2;$i<11;$i++)
         {
             $worth[$count] = $i;
             $name[$count]=$i;
             $drawn[$count]=false;
             $suite[$count]='C';
             $count++;
         }
 
         $worth[$count] = 10;
         $name[$count] = 'J';
         $drawn[$count] = false;
         $suite[$count] = 'C';
         $count++;
 
         $worth[$count] = 10;
         $name[$count] = 'Q';
         $drawn[$count] = false;
         $suite[$count] = 'C';
         $count++;
 
         $worth[$count] = 10;
         $name[$count] = 'K';
         $drawn[$count] = false;
         $suite[$count] = 'C';
         $count++;
 
         $worth[$count] = 11;
         $name[$count] = 'A';
         $drawn[$count] = false;
         $suite[$count] = 'C';
         $count++;
 
 
 
         for($i=2;$i<11;$i++)
         {
             $worth[$count] = $i;
             $name[$count]=$i;
             $drawn[$count]=false;
             $suite[$count]='H';
             $count++;
         }
 
         $worth[$count] = 10;
         $name[$count] = 'J';
         $drawn[$count] = false;
         $suite[$count] = 'H';
         $count++;
 
         $worth[$count] = 10;
         $name[$count] = 'Q';
         $drawn[$count] = false;
         $suite[$count] = 'H';
         $count++;
 
         $worth[$count] = 10;
         $name[$count] = 'K';
         $drawn[$count] = false;
         $suite[$count] = 'H';
         $count++;
 
         $worth[$count] = 11;
         $name[$count] = 'A';
         $drawn[$count] = false;
         $suite[$count] = 'H';
         $count++;
 
         for($i=2;$i<11;$i++)
         {
             $worth[$count] = $i;
             $name[$count]=$i;
             $drawn[$count]=false;
             $suite[$count]='S';
             $count++;
         }
 
         $worth[$count] = 10;
         $name[$count] = 'J';
         $drawn[$count] = false;
         $suite[$count] = 'S';
         $count++;
 
         $worth[$count] = 10;
         $name[$count] = 'Q';
         $drawn[$count] = false;
         $suite[$count] = 'H';
         $count++;
 
         $worth[$count] = 10;
         $name[$count] = 'K';
         $drawn[$count] = false;
         $suite[$count] = 'S';
         $count++;
 
         $worth[$count] = 11;
         $name[$count] = 'A';
         $drawn[$count] = false;
         $suite[$count] = 'S';
         $count++;
 
         for($i=2;$i<11;$i++)
         {
             $worth[$count] = $i;
             $name[$count]=$i;
             $drawn[$count]=false;
             $suite[$count]='D';
             $count++;
         }
 
         $worth[$count] = 10;
         $name[$count] = 'J';
         $drawn[$count] = false;
         $suite[$count] = 'D';
         $count++;
 
         $worth[$count] = 10;
         $name[$count] = 'Q';
         $drawn[$count] = false;
         $suite[$count] = 'D';
         $count++;
 
         $worth[$count] = 10;
         $name[$count] = 'K';
         $drawn[$count] = false;
         $suite[$count] = 'D';
         $count++;
 
         $worth[$count] = 11;
         $name[$count] = 'A';
         $drawn[$count] = false;
         $suite[$count] = 'D';
         $count++;
         
 
         $this->worth = $worth;
         $this->name = $name;
         $this->drawn = $drawn;
         $this->suite = $suite;
 
     
 
     }
 }

create stylesheet_tan.css

    <style>
     <!--
     a:link {font-family: Verdana;
             font-size: 8Pt;
             color: #656953;
             text-decoration: bold;
         font-weight: bold;
         }
 
     a:visited {font-family: Verdana;
                font-size: 8Pt;
             color: #656953;
                text-decoration: bold;
         font-weight: bold;
         }
 
 
     a:hover {font-family: Verdana;
               font-size: 8Pt;
              color: #A7B274;
              text-decoration: underline;
         font-weight: bold;
         }
 
    
     font.norm {font-family: Verdana;
                font-size: 8Pt;
                color: #656953;
                font-weight: none;}
 
 
     font.speech {font-family: Verdana;
                font-size: 8Pt;
                color: #656953;
                font-weight: none;}
 
     font.label {font-family: Verdana;
                font-size: 8Pt;
                color: #FFFFFF;
                font-weight: bold;}
 
     font.header {font-family: Verdana;
                font-size: 8Pt;
                color: #000000;
                font-weight: bold;}
 
     font.warnings {font-family: Verdana;
                font-size: 8Pt;
                color: #cc0000;
                font-weight: bold;}
 
     td.header {font-family: Verdana;
                  font-size: 8Pt;
                  color: #A50400;
          align=center;
                  background-color: #A7B274;}
 
      td.desc {font-family: Verdana;
                  font-size: 8Pt;
                  color: #000099;
         align: right;
                  background-color: #656953;}
 
       td.data {font-family: Verdana;
                  font-size: 8Pt;
                  color: 000000;
                  align: right;
                  background-color: #000000;}
 
     table.borderedTable {{font-family: Verdana;
                  font-size: 8Pt;
                  color: 000000;
                  align: right;
                  border: 1px solid;
                  border-color: #656953;}
 
     .button {font-family: Verdana;
              font-size: 8Pt;
              font-weight: none;
              color: #E7E7E7;
              background-color: #0099cc;
              border: 1px solid #000099;}
 
      .textinput {font-family: Verdana;
                  font-size: 8Pt;
                  color: #656953;
                  background-color: #A7B274;
                  border: 1px solid #000000;}
     -->
     </style>
Prab Junior · 10 posts #2

where is tease.gif or any other if there?

Blythe Newbie · 4 posts #3

🤷 I dont know anything but can learn fast, can any one please tell me step by step how and where to include this code? pppplsssss

Guest #4

Doesnt work

Parse error: syntax error, unexpected T_STRING in /public_html/blackjack/Deck.class.php on line 29

Mysterio Lead · 630 posts #5

Blythe wrote: 🤷 I dont know anything but can learn fast, can any one please tell me step by step how and where to include this code? pppplsssss

try by urself if you want to learn. guy said clear where to put the code just think more.

mysterio.al - programming is a functional art

WhiteWarrior Principal · 1,419 posts #6

lol

Wow this is still going? lol anyhows i can;t remember a thing about it so topic closed as the post is about 2 years old lol

Sign in to reply.

Similar topics

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close