BlackjackGame::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 2
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 4
rs 10
ccs 3
cts 3
cp 1
crap 1
1
<?php
2
3
namespace App\Game;
4
5
use App\Card\Deck;
6
use App\Card\Card;
7
8
/**
9
 * Manages a Blackjack game.
10
 */
11
class BlackjackGame
12
{
13
    /** @var Deck The deck of cards */
14
    private $deck;
15
16
    /** @var Card[] The player hand */
17
    private $playerHand = [];
18
19
    /** @var Card[] The dealer hand */
20
    private $dealerHand = [];
21
22
    /**
23
     * BlackjackGame constructor.
24
     */
25 4
    public function __construct()
26
    {
27 4
        $this->deck = new Deck();
28 4
        $this->deck->shuffle();
29
    }
30
31
    /**
32
     * Deal first cards to player and dealer.
33
     */
34 2
    public function dealCards(): void
35
    {
36 2
        $this->playerHand[] = $this->deck->drawCard();
37 2
        $this->playerHand[] = $this->deck->drawCard();
38 2
        $this->dealerHand[] = $this->deck->drawCard();
39
    }
40
41
    /**
42
     * Draw a card for the player.
43
     */
44 1
    public function hitPlayer(): void
45
    {
46 1
        $this->playerHand[] = $this->deck->drawCard();
47
    }
48
49
    /**
50
     * Draw a card for the dealer.
51
     */
52 1
    public function hitDealer(): void
53
    {
54 1
        $this->dealerHand[] = $this->deck->drawCard();
55
    }
56
57
    /**
58
     * Get player hand.
59
     *
60
     * @return Card[] The player hand
61
     */
62 3
    public function getPlayerHand(): array
63
    {
64 3
        return $this->playerHand;
65
    }
66
67
    /**
68
     * Get dealer hand.
69
     *
70
     * @return Card[] The dealer hand
71
     */
72 3
    public function getDealerHand(): array
73
    {
74 3
        return $this->dealerHand;
75
    }
76
77
    /**
78
     * Calculate the total value of a hand.
79
     *
80
     * @param Card[] $hand The hand to calculate
81
     * @return int The value of the hand
82
     */
83 1
    public function getHandValue(array $hand): int
84
    {
85 1
        $value = 0;
86 1
        $aceCount = 0;
87
88 1
        foreach ($hand as $card) {
89 1
            $cardValue = $card->getBlackjackValue();
90 1
            $value += $cardValue;
91 1
            if ($card->getValue() == 'Ace') {
92 1
                $aceCount++;
93
            }
94
        }
95
96 1
        while ($value > 21 && $aceCount > 0) {
97
            $value -= 10;
98
            $aceCount--;
99
        }
100
101 1
        return $value;
102
    }
103
104
    /**
105
     * Check if the dealer must draw more cards.
106
     *
107
     * @return bool True if dealer hand value is below 17
108
     */
109
    public function dealerMustDraw(): bool
110
    {
111
        return $this->getHandValue($this->dealerHand) < 17;
112
    }
113
114
    /**
115
     * Check if the player has busted.
116
     *
117
     * @return bool True if player hand value exceeds 21
118
     */
119
    public function isPlayerBusted(): bool
120
    {
121
        return $this->getHandValue($this->playerHand) > 21;
122
    }
123
}
124