I'm trying to write a blackjack game, for my website, www.the-streets-of-london.co.uk. However i have no idea where to start. I've tried looking on the internet for tutorials or something, or scripts so i can get a basic idea, but there seems to be none!

So if anyone has any suggestions, or even links to where i can get a script to work from, it will be appreciated!

Thanks

Also, i need a script that keeps repeating a search on a table in my database for a new entry for it. however the only way i could think of doing this was by refreshing the page, and that justs causes problems. How could i do that?

    Wouldn't be hard to make. Grab some cards, put some [man]rand[/man]om logic in there, do the math. Pretty simple. Start adding simple functions here and there for splitting aces, double down, etc if you wanna get fancy.

      I have finally gathered some code together to create the blackjack script. The only problem is, i get lots of errors, and i don't know how i can remedy them.

      Also, i'm not sure whether in the code i can add the form, for which will have hit, stick or new deal buttons, in the getinput() function.

      Any help will be great.

      <?
      main();
      
      function init() { 
      
      print "\n*** Resuffling Deck *** \n\n"; 
      
      for ($y = 0; $y < 4; $y++) { 
      
          switch($y) { 
      
              case 0:  
                  $suit = 'C';  
                  break; 
              case 1:  
                  $suit = 'S';  
                  break; 
              case 2:  
                  $suit = 'H';  
                  break; 
              case 3:  
                  $suit = 'D';  
                  break; 
      
          }                 
      
          for ($i = 1; $i <= 13; $i++) { 
      
              if ($i <= 10) $deck[$suit][$i] = $i; 
      
              switch($i) { 
                  case 1: 
                      $deck[$suit][$i] = 'A'; 
                      break; 
                  case 11: 
                      $deck[$suit][$i] = 'J'; 
                      break; 
                  case 12: 
                      $deck[$suit][$i] = 'Q'; 
                      break; 
                  case 13: 
                      $deck[$suit][$i] = 'K'; 
                      break; 
              } 
      
          } 
      
      } 
      
      srand ((double) microtime() * 1000000); 
      
        return $deck; 
      } 
      
      function draw() { 
      
          global $deck; 
      
          static $usedcards = 0; 
      
          if ($usedcards >= 52) { 
      
              $deck = init(); 
              $usedcards = 0; 
          } 
      
          $face = array_rand($deck); 
          $id = array_rand($deck[$face]); 
      
      
          $val = ($id >= 10) ? 10 : $id; 
          $result = array('card'=>$deck[$face][$id].' '.$face,'val'=>$val); 
      
          unset($deck[$face][$id]); 
          if (count($deck[$face]) < 1) { 
              unset($deck[$face]); 
              } 
      
          $usedcards++; 
          return $result;     
      } 
      
      function hand_value($hand) { 
      
      foreach ($hand as $val) { 
      // $aces = "0";
      
          if ($val['val'] != 1) { 
              $result += $val['val']; 
          } else { 
              $aces++; 
          } 
      } 
      
      for ($i = 0; $i < $aces; $i++) { 
      
          $result += (($result + 11) > 21) ? 1 : 11; 
      
      } 
      
      return $result; 
      
      } 
      
      function show_cards($dealer, $player, $showdealer = false) { 
      
          print "Dealer has: "; 
      
          if ($showdealer) { 
      
              foreach ($dealer as $val) 
                  print "[{$val['card']}] "; 
      
              print " (".hand_value($dealer).")"; 
      
          } else { 
      
              print "[{$dealer[0]['card']}] [ # ]  (?)"; 
      
          } 
      
          print "\nPlayer has: "; 
      
          foreach ($player as $val)                  
              print "[{$val['card']}] ";         
      
      
          print " (".hand_value($player).")"; 
      
      }
      
      function new_hand($dealer, $player) 
          { 
              print "\nDealing a new hand....\n"; 
              $dealer = array(); 
              $player = array(); 
              $dealer[] = draw(); 
              $player[] = draw(); 
              $dealer[] = draw(); 
              $player[] = draw(); 
      
      if ((hand_value($player) == 21) &&  
           (hand_value($dealer) == 21)) { 
      
          show_cards($dealer, $player, true); 
          print"\n\nBoth Players have BlackJack!\n"; 
          new_hand(&$dealer, &$player); 
          return;     
      } 
      
      if (hand_value($player) == 21) { 
          show_cards($dealer, $player, true); 
          print "\n\nPlayer has BlackJack!\n"; 
          new_hand(&$dealer, &$player); 
      } else if (hand_value($dealer) == 21) { 
          show_cards($dealer, $player,true); 
          print "\n\nDealer has BlackJack!\n"; 
          new_hand(&$dealer, &$player); 
      } else { 
          show_cards($dealer, $player); 
      } 
      } 
      
      
      function main() 
      { 
          global $deck;  
      
          $deck = init(); 
          $command = 'n'; 
      
      	while (TRUE) { 
              switch ($command) { 
      
                  case 'q':             
                      print "\n\nThanks for playing!\n"; 
                      exit; 
                      break; 
      
      	case 's': 
                      while (hand_value($dealer) < 16) { 
                          $dealer[] = draw(); 
                      } 
                      show_cards($dealer, $player, true); 
      
                      if ((hand_value($dealer) > hand_value($player)) && 
                          (hand_value($dealer) <= 21)) { 
                          print "\n\nDealer Wins!\n"; 
                      } else if (hand_value($dealer) == hand_value($player)) { 
                          print "\n\nPush Hand.\n"; 
                      } else { 
                          print "\n\nPlayer Wins!\n"; 
                      } 
                      new_hand(&$dealer, &$player); 
                      break; 
      
      case 'h': 
                      $player[] = draw(); 
      
                      if (hand_value($player) > 21) { 
                          show_cards($dealer, $player, TRUE); 
                          print "\n\nPlayer Busts!\n"; 
                          new_hand(&$dealer, &$player); 
                      } else { 
                          show_cards($dealer, $player); 
                      } 
                      break; 
      
      case 'n': 
                          new_hand(&$dealer, &$player); 
                          break; 
      
                  default: 
                      show_cards($dealer, $player); 
              } 
      
              print "\n> "; 
              $command = getInput(2);         
      
          } /* end of infinite while */ 
      } /* end of main() */ 
      
      ?>

        Well, you'd get Undefined Variable notices because you're using $aces and $result without giving them values (in fact, you have a line that initialises $aces to 0 (or, rather, the string "0", but that gets converted to the number 0 later), but you've commented it out.)

        After that, with getInput() undefined, the program dies.

          Ok, i fixed the errors, simply by putting $result = 0, and $aces = 0 at the start of the hand_value function.

          Now what i want to create is a way in which the user can hit, stick or deal a new set of cards, by a form which is a function called getInput(). However, i'm not sure how i can achieve this, i have tried a few ideas, but they just give errors.

          Any ideas?

            Well you inspired me this lunchtime, to write the easiest possible card game. So here it is, PHP-Snap! Probably doesn't help you much though 🙂

            Thinking about it, 1 player Snap is really quite boring 🙂

              Write a Reply...