CardHand::getValueAceHigh()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 5
c 0
b 0
f 0
dl 0
loc 9
ccs 6
cts 6
cp 1
rs 10
cc 3
nc 3
nop 0
crap 3
1
<?php
2
3
namespace App\Cards;
4
5
/**
6
 * CardHand.
7
 */
8
class CardHand
9
{
10
    /**
11
     * @var array<Card> Is an array that contains the Card objects
12
     */
13
    protected array $hand = [];
14
15
    /**
16
     * addCard.
17
     *
18
     * Add a card to the hand array
19
     */
20 35
    public function addCard(Card $card): void
21
    {
22 35
        $this->hand[] = $card;
23
    }
24
25
    /**
26
     * cardCount.
27
     *
28
     * Returns the number of cards in hand
29
     */
30 1
    public function cardCount(): int
31
    {
32 1
        return count($this->hand);
33
    }
34
35
    /**
36
     * getValueOfHand'.
37
     *
38
     * Return the value of the hand
39
     */
40 1
    public function getValue(): int
41
    {
42 1
        $value = 0;
43 1
        foreach ($this->hand as $card) {
44 1
            $value += $card->getValue();
45
        }
46
47 1
        return $value;
48
    }
49
50
    /**
51
     * getValueOfHandAceHigh.
52
     *
53
     * Return the value of the hand if the Ace cards have a high value (14)
54
     */
55 1
    public function getValueAceHigh(): int
56
    {
57 1
        $valueHand = 0;
58 1
        foreach ($this->hand as $card) {
59 1
            $valueCard = $card->getValue();
60 1
            $valueHand += (1 == $valueCard) ? 14 : $valueCard;
61
        }
62
63 1
        return $valueHand;
64
    }
65
66
    /**
67
     * getBlackJackValueOfHand.
68
     *
69
     * Return the Black Jack value of the hand (J,Q,K = 10)
70
     */
71 24
    public function getBlackJackValue(): int
72
    {
73 24
        $valueHand = 0;
74 24
        foreach ($this->hand as $card) {
75 24
            $valueCard = $card->getValue();
76 24
            $valueHand += ($valueCard > 10) ? 10 : $valueCard;
77
        }
78
79 24
        return $valueHand;
80
    }
81
82
    /**
83
     * getBlackJackValueOfHandAceHigh.
84
     *
85
     * Return the Black Jack value of the hand (J,Q,K = 10) if the Ace cards have a high value (11)
86
     */
87 24
    public function getBlackJackValueAceHigh(): int
88
    {
89 24
        $valueHand = 0;
90 24
        foreach ($this->hand as $card) {
91 24
            $valueCard = $card->getValue();
92 24
            $valueHand += (1 == $valueCard) ? 11 :
93 24
             (($valueCard > 10) ? 10 : $valueCard);
94
        }
95
96 24
        return $valueHand;
97
    }
98
99
    /**
100
     * getString.
101
     *
102
     * Return the hand as an array of strings
103
     *
104
     * @return array<string>
105
     */
106 30
    public function getString(): array
107
    {
108 30
        $cards = [];
109 30
        foreach ($this->hand as $card) {
110 28
            $cards[] = $card->getString();
111
        }
112
113 30
        return $cards;
114
    }
115
}
116