PHP Card Deck - PHP Online

Form of PHP Sandbox

Enter Your PHP code here for testing/debugging in the Online PHP Sandbox. As in the usual PHP files, you can also add HTML, but do not forget to add the tag <?php in the places where the PHP script should be executed.



Your result can be seen below.

Result of php executing





Full code of PHP Card Deck.php

  1. <?php class DeckOfCards { 
  2.        
  3.         private $suits;
  4.         private $ranks;
  5.         public $deck;
  6.  
  7.         function __construct(){
  8.                 $this->suits = ["S","D","H","C"];
  9.                 $this->ranks = ["A",2,3,4,5,6,7,8,9,10,"J","Q","K"];
  10.                 foreach($this->suits as $s){
  11.                         foreach($this->ranks as $r){
  12.                                 $this->deck[]="$r$s";
  13.                         }
  14.                 }
  15.         }
  16.  
  17.         function show_deck() {
  18.                 // returns an array of all cards currently in the deck.
  19.                 return $this->deck;
  20.         }
  21.  
  22.         function deal_one_card(){
  23.                 // deal one random card from the deck, then remove it.
  24.                 if(empty($this->deck)) return false; // no cards left
  25.                 $arrLen = count($this->deck);
  26.                 $key = mt_rand(0,$arrLen-1);
  27.                 $card = $this->deck[$key];
  28.                 array_splice($this->deck, $key,1);
  29.                 return $card;
  30.         }
  31.  
  32.         function deal_top_card(){
  33.                 // deal the top card from the deck, then remove it.
  34.                 if(empty($this->deck)) return false; // no cards left
  35.                 array_splice($this->deck, 0, 1);
  36.                 return $card;
  37.         }
  38.  
  39.         function shuffle(){
  40.                 // Shuffles all the cards currently in the deck.
  41.                 if(empty($this->deck)) return; // no cards in the deck to shuffle.
  42.                 $arrLen = count($this->deck);
  43.                 for($i=0; $i < $arrLen; $i++) {
  44.                         $newDeck[] = $this->deal_one_card();
  45.                 }
  46.                 $this->deck = $newDeck;
  47.         }
  48. }
  49.  
  50. $myCards = new DeckOfCards;
  51.  
  52. //// TEST: Deal cards until deck is empty
  53.  
  54. $myCards->shuffle();
  55.  
  56. // 1.a. ensure the deck was shuffled properly:
  57. print_r($myCards->show_deck());
  58.  
  59. // 1.b. deal cards until they are gone.
  60. // ( Iterate 53 times to ensure deal_one_card() properly returns false when the deck is empty. )
  61. for($i=1;$i<=53;$i++){
  62.         $card = $myCards->deal_one_card();
  63.         if($card) {
  64.                echo "You were dealt " . $card ."<br>";
  65.         }
  66.         else {
  67.                 echo "There are no cards left";
  68.                 break;
  69.         }
  70. }
  71. ?>
File Description
  • PHP Card Deck
  • PHP Code
  • 22 Feb-2021
  • 1.69 Kb
You can Share it: