Advertisement
zachdyer

PHP Deck of Cards Object

Sep 21st, 2013
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.21 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <?php
  3.     class Card{
  4.         public $rank;
  5.         public $suit;
  6.         function __construct($rank, $suit){
  7.             $this->rank = $rank;
  8.             $this->suit = $suit;
  9.         }
  10.     }
  11.    
  12.     class Deck{
  13.         public $card = array();
  14.         function __construct(){
  15.             $ranks = array("Ace",2,3,4,5,6,7,8,9,10,"Jack","Queen","King");
  16.             $suits = array("Clubs","Spades","Hearts","Diamonds");
  17.             foreach($suits as $suit){
  18.                 foreach($ranks as $rank){
  19.                     $card = new Card($rank, $suit);
  20.                     array_push($this->card,$card);
  21.                 }
  22.             }
  23.         }
  24.     }
  25.    
  26.     $deck = new Deck();
  27.    
  28.     function deal($deck){
  29.         $cardNum = rand(0,52);
  30.         $card = $deck->card[$cardNum];
  31.         echo $card->rank . " of " . $card->suit;
  32.     }
  33.    
  34.     //Session to save card count
  35.     session_start();
  36.     $cardCount = 0;
  37.     if($_GET['draw']){
  38.         $_SESSION['cardCount'] += intval($_GET['draw']);
  39.         $cardCount = $_SESSION['cardCount'];
  40.     }
  41.    
  42.    
  43. ?>
  44. <html>
  45.     <head>
  46.         <title>Cards</title>
  47.         <meta charset="UTF-8">
  48.         <link rel="stylesheet" href="style.css" type="text/css">
  49.     </head>
  50.     <body>
  51.         <a id="drawCard" href="index.php?draw=1">Draw Card</a>
  52.         <ul id="output">
  53.             <?php while($cardCount): ?>
  54.             <li><?php deal($deck);?></li>
  55.             <?php $cardCount--; endwhile; ?>
  56.         </ul>
  57.     </body>
  58. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement