1 | <?php |
||
2 | |||
3 | namespace App\Card; |
||
4 | |||
5 | use App\Card\DeckOfCards; |
||
6 | use App\Card\CardHand; |
||
7 | use Exception; |
||
8 | |||
9 | /** |
||
10 | * Game21 represents the 21 card game with a dealer that always draws to 17. |
||
11 | */ |
||
12 | class Game21 |
||
13 | { |
||
14 | private CardHand $bankHand; |
||
15 | private DeckOfCards $deck; |
||
16 | private CardHand $playerHand; |
||
17 | private ?string $winner; |
||
18 | private string $turn; |
||
19 | |||
20 | /** |
||
21 | * __construct sets the value of the card to null |
||
22 | * |
||
23 | * @return void |
||
24 | */ |
||
25 | 7 | public function __construct() |
|
26 | { |
||
27 | 7 | $this->deck = new DeckOfCards(); |
|
28 | 7 | $this->deck->shuffle(); |
|
29 | 7 | $this->bankHand = new CardHand(); |
|
30 | 7 | $this->playerHand = new CardHand(); |
|
31 | 7 | $this->winner = null; |
|
32 | 7 | $this->turn = "player"; |
|
33 | } |
||
34 | |||
35 | /** |
||
36 | * getHandValue - returns the best value of the hand |
||
37 | * |
||
38 | * @param CardHand $hand |
||
39 | * @return int |
||
40 | */ |
||
41 | 5 | public function getHandValue( |
|
42 | CardHand $hand |
||
43 | ): int { |
||
44 | 5 | $handValue = 0; |
|
45 | 5 | $aceCount = 0; |
|
46 | |||
47 | |||
48 | 5 | foreach ($hand->getHand() as $card) { |
|
49 | 5 | $value = $card->getRank(); |
|
50 | 5 | if ($value == 1) { |
|
51 | 1 | $aceCount++; |
|
52 | } |
||
53 | 5 | $handValue += $value; |
|
54 | } |
||
55 | // Can aces by 14 without busting |
||
56 | |||
57 | 5 | for ($i = 0; $i < $aceCount; $i++) { |
|
58 | 1 | if ($handValue < 9) { |
|
59 | $handValue += 13; |
||
60 | } |
||
61 | } |
||
62 | 5 | return $handValue; |
|
63 | } |
||
64 | 5 | public function playerDraw(): int |
|
65 | { |
||
66 | 5 | $handValue = 0; |
|
0 ignored issues
–
show
Unused Code
introduced
by
![]() |
|||
67 | |||
68 | 5 | if ($this->turn !== "player") { |
|
69 | 1 | throw new Exception("Player has stopped or game not started"); |
|
70 | } |
||
71 | 5 | $this->playerHand->add($this->deck->drawCard()); |
|
72 | 5 | $handValue = $this->getHandValue($this->playerHand); |
|
73 | 5 | if ($handValue > 21) { |
|
74 | 1 | $this->winner = "bank"; |
|
75 | } |
||
76 | 5 | return $handValue; |
|
77 | |||
78 | } |
||
79 | 4 | public function bankDraw(): int |
|
80 | { |
||
81 | 4 | if ($this->turn !== "bank") { |
|
82 | 1 | throw new Exception("Player has not stopped"); |
|
83 | } |
||
84 | 3 | $this->bankHand->add($this->deck->drawCard()); |
|
85 | 3 | $handValue = $this->getHandValue($this->bankHand); |
|
86 | 3 | if ($handValue > 21) { |
|
87 | 1 | $this->winner = "player"; |
|
88 | 3 | } elseif ($handValue >= 17) { |
|
89 | if ($handValue >= $this->getHandValue($this->playerHand)) { |
||
90 | $this->winner = "bank"; |
||
91 | } else { |
||
92 | $this->winner = "player"; |
||
93 | } |
||
94 | } |
||
95 | 3 | return $handValue; |
|
96 | } |
||
97 | /** |
||
98 | * stop - Player stops drawing cards, banks turn |
||
99 | * |
||
100 | * @return void |
||
101 | */ |
||
102 | 4 | public function playerStop() |
|
103 | { |
||
104 | 4 | $this->turn = "bank"; |
|
105 | } |
||
106 | /** |
||
107 | * getPlayerHand - return the player's hand |
||
108 | * |
||
109 | * @return CardHand |
||
110 | * |
||
111 | */ |
||
112 | 1 | public function getPlayerHand(): CardHand |
|
113 | { |
||
114 | 1 | return $this->playerHand; |
|
115 | } |
||
116 | /** |
||
117 | * getBankHand - returs banks hand |
||
118 | * |
||
119 | * @return CardHand |
||
120 | */ |
||
121 | 1 | public function getBankHand(): CardHand |
|
122 | { |
||
123 | 1 | return $this->bankHand; |
|
124 | } |
||
125 | /** |
||
126 | * getTurn - return who's next turn: player or bank |
||
127 | * |
||
128 | * @return string |
||
129 | */ |
||
130 | |||
131 | 2 | public function getTurn(): string |
|
132 | { |
||
133 | 2 | return $this->turn; |
|
134 | } |
||
135 | /** |
||
136 | * getWinner - returns the winner. If null, the game is active |
||
137 | * |
||
138 | * @return string|null |
||
139 | */ |
||
140 | 4 | public function getWinner(): ?string |
|
141 | { |
||
142 | 4 | return $this->winner; |
|
143 | } |
||
144 | } |
||
145 |