| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace App\Game; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use App\Card\CardGraphic; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use App\Card\CardHand; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use App\Card\DeckOfCards; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use App\Game\Player; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use App\Traits\GameTraits; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * Methods for class Game; for playing the card game 21. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | class Game | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     use GameTraits; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     /** @var DeckOfCards */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     private DeckOfCards $deck; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     /** @var Player */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     private Player $player; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     /** @var Player */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     private Player $bank; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 | 8 |  |     public function __construct(?DeckOfCards $deck = null, ?Player $player = null, ?Player $bank = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 | 8 |  |         $this->deck = $deck ?? new DeckOfCards(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 | 8 |  |         $this->player = $player ?? new Player(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 | 8 |  |         $this->bank = $bank ?? new Player(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * Round of the game when it is the player's turn. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * @return array<string, mixed> { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      *     cards: string[][], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      *     score: int, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      *     lost: bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 1 |  |     public function playerTurn(): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 1 |  |         $value = $this->deck->drawCard(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 1 |  |         $this->player->setHand(new CardGraphic($value)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 1 |  |         $gameLost = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 1 |  |         if ($this->player->isOver21()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 1 |  |             $gameLost = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 1 |  |         return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 1 |  |             'cards' => $this->player->getHand(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 1 |  |             'score' => $this->player->getScore(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 | 1 |  |             'lost' => $gameLost | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 1 |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      * Round of the game when it is the bank's turn. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      * @return array<string, mixed> { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      *     cards: string[][], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      *     score: int, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      *     lost: bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      * } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 2 |  |     public function bankTurn(): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         do { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 2 |  |             $value = $this->deck->drawCard(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 2 |  |             $this->bank->setHand(new CardGraphic($value)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 2 |  |             $gameLost = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 2 |  |             if ($this->bank->isOver21()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 1 |  |                 $gameLost = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 1 |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 1 |  |         } while ($this->bank->isNotOver17()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 2 |  |         return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 2 |  |             'cards' => $this->bank->getHand(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 2 |  |             'score' => $this->bank->getScore(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 | 2 |  |             'lost' => $gameLost | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 2 |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |      * Returns the player's total score. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |      * @return int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 | 5 |  |     public function getPlayerScore(): int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 | 5 |  |         return $this->player->getScore(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |      * Returns the bank's total score. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |      * @return int | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 97 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 98 | 5 |  |     public function getBankScore(): int | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 100 | 5 |  |         return $this->bank->getScore(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |      * Returns the winner of the game, based on the player and bank scores. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 | 4 |  |     public function isWinner(): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 | 4 |  |         $maxScore = 21; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 | 4 |  |         $playerScore = $this->getPlayerScore(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 | 4 |  |         $bankScore = $this->getBankScore(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 | 4 |  |         if ($playerScore > $maxScore) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 | 1 |  |             return "Banken"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 | 3 |  |         if ($bankScore > $maxScore) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 | 1 |  |             return "Spelaren"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 | 2 |  |         if ($bankScore >= $playerScore) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 | 1 |  |             return "Banken"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 | 1 |  |         return "Spelaren"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 127 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 128 |  |  |  |