Foxxything

Untitled

Sep 26th, 2022
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.73 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.  
  69.         echo "Your hand: " . implode(', ', $this->playerHand) . " ($playerValue)\n";
  70.         echo "Dealer hand: " . implode(', ', $this->dealerHand) . " ($dealerValue)\n";
  71.  
  72.         if ($playerValue > 21) {
  73.           echo "You busted! You have $playerValue points.\n";
  74.           break;
  75.         }
  76.         if ($dealerValue > 21) {
  77.           echo "You win! The dealer busted with $dealerValue points.\n";
  78.           break;
  79.         }
  80.  
  81.         $choice = readline("Hit (H) or Stand (S)? ");
  82.         $choice = strtoupper($choice);
  83.  
  84.         if ($choice == 'H') $this->playerHand[] = $this->getCard();
  85.         else {
  86.           while ($dealerValue < 17) {
  87.             $this->dealerHand[] = $this->getCard();
  88.             $dealerValue = $this->getHandValue($this->dealerHand);
  89.           }
  90.  
  91.           if ($dealerValue > 21) {
  92.             echo "The dealer busts! You win!\n";
  93.             break;
  94.           }
  95.          
  96.           if ($playerValue > $dealerValue) {
  97.             echo "You Have $playerValue and the dealer has $dealerValue. You win!\n";
  98.             break;
  99.           } else {
  100.             echo "You Have $playerValue and the dealer has $dealerValue. You lose!\n";
  101.             break;
  102.           }
  103.         }
  104.       }
  105.     }
  106.   }
Add Comment
Please, Sign In to add comment