Foxxything

Untitled

Sep 26th, 2022
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.84 KB | None | 0 0
  1. <?php
  2.   namespace Foxx\ComputerScience\U1;
  3.  
  4.   /**
  5.    * A simple BlackJack(21) game.
  6.    * @author Foxx Azalea Pinkerton
  7.    * @since 2022-09-26
  8.    * @teacher Mr.Wachs
  9.    */
  10.   class BlackJack {
  11.     const CARDS = [
  12.       'A' => 11,
  13.       '2' => 2,
  14.       '3' => 3,
  15.       '4' => 4,
  16.       '5' => 5,
  17.       '6' => 6,
  18.       '7' => 7,
  19.       '8' => 8,
  20.       '9' => 9,
  21.       '10' => 10,
  22.       'J' => 10,
  23.       'Q' => 10,
  24.       'K' => 10
  25.     ];
  26.  
  27.     private $playerHand = [];
  28.     private $dealerHand = [];
  29.  
  30.     public function __construct() {
  31.       $this->playerHand[] = $this->getCard();
  32.       $this->playerHand[] = $this->getCard();
  33.       $this->dealerHand[] = $this->getCard();
  34.       $this->dealerHand[] = $this->getCard();
  35.       $this->play();
  36.     }
  37.  
  38.     /**
  39.      * Gets a random card.
  40.      * @return string
  41.      */
  42.     private function getCard(): string {
  43.       $randomNumber = rand(0, count(self::CARDS) - 1);
  44.       $card = array_keys(self::CARDS)[$randomNumber];
  45.       return $card;
  46.     }
  47.  
  48.     /**
  49.      * Gets the value of a hand.
  50.      * @param array $hand
  51.      * @return int
  52.      */
  53.     private function getHandValue(array $hand): int {
  54.       $value = 0;
  55.       foreach ($hand as $card) {
  56.         $value += self::CARDS[$card];
  57.       }
  58.       return $value;
  59.     }
  60.  
  61.     /**
  62.      * Plays the game.
  63.      */
  64.     private function play() {
  65.       while (true) {
  66.         $playerValue = $this->getHandValue($this->playerHand);
  67.         $dealerValue = $this->getHandValue($this->dealerHand);
  68.         echo "Your hand: ";
  69.         foreach ($this->playerHand as $card) {
  70.           echo "$card ";
  71.         }
  72.         echo "($playerValue)\n";
  73.         echo "Dealer's hand: ";
  74.         foreach ($this->dealerHand as $card) {
  75.           echo "$card ";
  76.         }
  77.         echo "($dealerValue)\n";
  78.         if ($playerValue > 21) {
  79.           echo "You busted! You have $playerValue points.\n";
  80.           break;
  81.         }
  82.         if ($dealerValue > 21) {
  83.           echo "You win! The dealer busted with $dealerValue points.\n";
  84.           break;
  85.         }
  86.         $choice = readline("Hit (H) or Stand (S)? ");
  87.         $choice = strtoupper($choice);
  88.         if ($choice == 'H') {
  89.           $this->playerHand[] = $this->getCard();
  90.         } else {
  91.           while ($dealerValue < 17) {
  92.             $this->dealerHand[] = $this->getCard();
  93.             $dealerValue = $this->getHandValue($this->dealerHand);
  94.           }
  95.           if ($dealerValue > 21) {
  96.             echo "The dealer busts! You win!\n";
  97.             break;
  98.           }
  99.           if ($playerValue > $dealerValue) {
  100.             echo "You Have $playerValue and the dealer has $dealerValue. You win!\n";
  101.             break;
  102.           } else {
  103.             echo "You Have $playerValue and the dealer has $dealerValue. You lose!\n";
  104.             break;
  105.           }
  106.         }
  107.       }
  108.     }
  109.   }
Add Comment
Please, Sign In to add comment