Passed
Push — main ( 8bc290...60b955 )
by Peter
04:33
created

BlackjackGameProj::checkForBlackjack()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
namespace App\GameProj;
4
5
use App\Card\DeckProj;
6
use App\Card\Card;
7
use InvalidArgumentException;
8
use App\GameProj\BlackjackHandHelperProj;
9
10
/**
11
 * Manages an advanced Blackjack game with multiple hands and computer player support.
12
 */
13
class BlackjackGameProj
14
{
15
    /** @var DeckProj The deck of cards */
16
    private $deck;
17
18
    /** @var BlackjackHandManagerProj Manages the hands in the game */
19
    private $handManager;
20
21
    /** @var string The name of the player */
22
    private $playerName;
23
24
    /** @var float The player bank balance */
25
    private $playerBank;
26
27
    /** @var int|null The index of the computer hand */
28
    private $computerHandIndex;
29
30
    /** @var string The strategy used by the computer player */
31
    private $computerStrategy;
32
33
    /**
34
     * BlackjackGameProj constructor.
35
     *
36
     * @param string $playerName The name of the player
37
     * @param float $playerBank The initial amount in the player bank
38
     * @param int $numberOfHands The number of hands to be played
39
     * @param float $totalBet The total bet amount for all hands
40
     * @param int|null $computerHandIndex The index of the computer hand, or null if not used
41
     * @param string $computerStrategy The strategy used by the computer player
42
     */
43 16
    public function __construct(string $playerName, float $playerBank, int $numberOfHands, float $totalBet, int $computerHandIndex = null, string $computerStrategy = 'dumb')
44
    {
45 16
        $this->deck = new DeckProj(4);
46 16
        $this->deck->shuffle();
47
48 16
        $this->handManager = new BlackjackHandManagerProj($numberOfHands, $totalBet);
49 15
        $this->playerName = $playerName;
50 15
        $this->playerBank = $playerBank;
51 15
        $this->playerBank -= $totalBet;
52
53 15
        $this->computerHandIndex = $computerHandIndex;
54 15
        $this->computerStrategy = $computerStrategy;
55
    }
56
57
    /**
58
     * Deal initial cards to all hands.
59
     */
60 7
    public function dealInitialCards(): void
61
    {
62 7
        $this->handManager->dealInitialCards($this->deck);
63
    }
64
65
    /**
66
     * Draw a card for a specific hand.
67
     *
68
     * @param int $handIndex The index of the hand to hit
69
     */
70 4
    public function hitHand(int $handIndex): void
71
    {
72 4
        $this->handManager->hitHand($handIndex, $this->deck);
73
    }
74
75
    /**
76
     * Stand a specific hand.
77
     *
78
     * @param int $handIndex The index of the hand to stand
79
     */
80 4
    public function standHand(int $handIndex): void
81
    {
82 4
        $this->handManager->standHand($handIndex);
83
    }
84
85
    /**
86
     * Draw a card for the dealer.
87
     */
88 2
    public function hitDealer(): void
89
    {
90 2
        $this->handManager->hitDealer($this->deck);
91
    }
92
93
    /**
94
     * Get all player hands.
95
     *
96
     * @return array[] An array of player hands
97
     */
98 7
    public function getHands(): array
99
    {
100 7
        return $this->handManager->getPlayerHands();
101
    }
102
103
    /**
104
     * Get dealer hand.
105
     *
106
     * @return Card[] The dealer hand
107
     */
108 3
    public function getDealerHand(): array
109
    {
110 3
        return $this->handManager->getDealerHand();
111
    }
112
113
    /**
114
     * Calculate the total value of a hand.
115
     *
116
     * @param Card[] $hand The hand to calculate
117
     * @return int The value of the hand
118
     */
119 1
    public function getHandValue(array $hand): int
120
    {
121 1
        return $this->handManager->getHandValue($hand);
122
    }
123
124
    /**
125
     * Check if the dealer must draw more cards.
126
     *
127
     * @return bool True if dealer hand value is below 17
128
     */
129 1
    public function dealerMustDraw(): bool
130
    {
131 1
        return $this->handManager->dealerMustDraw();
132
    }
133
134
    /**
135
     * Check if a specific hand is busted.
136
     *
137
     * @param int $handIndex The index of the hand to check
138
     * @return bool True if the hand is busted
139
     */
140 3
    public function isHandBusted(int $handIndex): bool
141
    {
142 3
        return $this->handManager->isHandBusted($handIndex);
143
    }
144
145
    /**
146
     * Check if the game is over.
147
     *
148
     * @return bool True if the game is over
149
     */
150 1
    public function isGameOver(): bool
151
    {
152 1
        return $this->handManager->isGameOver($this->computerHandIndex);
153
    }
154
155
    /**
156
     * Finish the game by playing the computer hand and settling bets.
157
     */
158 1
    public function finishGame(): void
159
    {
160 1
        $this->playComputerHand();
161 1
        while ($this->dealerMustDraw()) {
162 1
            $this->hitDealer();
163
        }
164 1
        $this->settleBets();
165
    }
166
167
    /**
168
     * Get the probability of busting for a specific hand.
169
     *
170
     * @param int $handIndex The index of the hand to check
171
     * @return float The probability of busting
172
     */
173 2
    public function getBustProbability(int $handIndex): float
174
    {
175 2
        return $this->handManager->getBustProbability($handIndex, $this->deck);
176
    }
177
178
    /**
179
     * Get the index of the current hand being played.
180
     *
181
     * @return int|null The index of the current hand, or null if no hands are playing
182
     */
183 2
    public function getCurrentHand(): ?int
184
    {
185 2
        return $this->handManager->getCurrentHand();
186
    }
187
188
    /**
189
     * Move to the next hand.
190
     */
191 2
    public function nextHand(): void
192
    {
193 2
        $this->handManager->nextHand();
194
    }
195
196
    /**
197
     * Play the computer hand according to its strategy.
198
     */
199 2
    public function playComputerHand(): void
200
    {
201 2
        $this->handManager->playComputerHand($this->computerHandIndex, $this->computerStrategy, $this->deck);
202
    }
203
204
    /**
205
     * Check for blackjack in all hands.
206
     */
207 1
    public function checkForBlackjack(): void
208
    {
209 1
        $this->handManager->checkForBlackjack();
210
    }
211
212
    /**
213
     * Check if a hand has blackjack.
214
     *
215
     * @param Card[] $hand The hand to check
216
     * @return bool True if the hand has blackjack
217
     */
218
    public function hasBlackjack(array $hand): bool
219
    {
220
        return $this->handManager->hasBlackjack($hand);
221
    }
222
223
    /**
224
     * Settle all bets and update the player bank.
225
     */
226 1
    public function settleBets(): void
227
    {
228 1
        $bankAdjustment = $this->handManager->settleBets();
229 1
        $this->playerBank += $bankAdjustment;
230
    }
231
232
    /**
233
     * Get the current balance in the player bank.
234
     *
235
     * @return float The player bank balance
236
     */
237 2
    public function getPlayerBank(): float
238
    {
239 2
        return $this->playerBank;
240
    }
241
242
    /**
243
     * Get the player name.
244
     *
245
     * @return string The player name
246
     */
247 1
    public function getPlayerName(): string
248
    {
249 1
        return $this->playerName;
250
    }
251
252
    /**
253
     * Check if a specific hand is controlled by the computer.
254
     *
255
     * @param int $handIndex The index of the hand to check
256
     * @return bool True if the hand is controlled by the computer
257
     */
258 1
    public function isComputerHand(int $handIndex): bool
259
    {
260 1
        return $handIndex === $this->computerHandIndex;
261
    }
262
263
    /**
264
     * For testing, set a specific player hand.
265
     *
266
     * @param array $hand The hand to set
267
     * @param int $index The index of the hand to set
268
     */
269 4
    public function setPlayerHand(array $hand, int $index = 0)
270
    {
271 4
        $this->handManager->setPlayerHand($hand, $index);
272
    }
273
274
    /**
275
     * For testing, set a specific dealer hand.
276
     *
277
     * @param array $hand The hand to set
278
     */
279
    public function setDealerHand(array $hand)
280
    {
281
        $this->handManager->setDealerHand($hand);
282
    }
283
}
284