Passed
Push — main ( a57e19...f52e71 )
by Vedrana
28:10
created

BlackJackRules   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 19
dl 0
loc 56
ccs 22
cts 22
cp 1
rs 10
c 1
b 0
f 0
wmc 11

3 Methods

Rating   Name   Duplication   Size   Complexity  
A hasBlackJackDirectly() 0 3 2
A drawForBank() 0 6 3
A determineResult() 0 27 6
1
<?php
2
3
namespace App\Card;
4
5
/**
6
 * Class BlackJackRules.
7
 */
8
class BlackJackRules
9
{
10
    /**
11
     * Determine the result of the game based on player and bank hands.
12
     *
13
     * @param CardHand $hand The player's hand (single hand).
14
     * @param CardHand $bank The bank's hand.
15
     *
16
     * @return array<string, string>
17
     */
18 6
    public function determineResult(CardHand $hand, CardHand $bank, int $index): array
19
    {
20
21 6
        if ($this->hasBlackJackDirectly($hand)) {
22 1
            return [
23 1
                'result' => 'blackjack',
24 1
                'message' => "Hand " . ($index + 1) . ": Du vann med Blackjack!"
25 1
            ];
26
        }
27
28 5
        $playerTotal = $hand->getTotalBlackJack();
29 5
        $bankTotal = $bank->getTotalBlackJack();
30
31 5
        if ($playerTotal > 21) {
32 1
            return ['result' => 'loss', 'message' => "Hand " . ($index + 1) . ": Du förlorade!"];
33
34
        }
35
36 4
        if ($bankTotal > 21 || $playerTotal > $bankTotal) {
37 2
            return ['result' => 'win', 'message' => "Hand " . ($index + 1) . ": Du vann!"];
38
        }
39
40 2
        if ($bankTotal > $playerTotal) {
41 1
            return ['result' => 'loss', 'message' => "Hand " . ($index + 1) . ": Du förlorade!"];
42
        }
43
44 1
        return ['result' => 'draw', 'message' => "Hand " . ($index + 1) . ": Oavgjort!"];
45
    }
46
47
    /**
48
     * Checks if a hand has exactly 21 with 2 cards (Blackjack).
49
     */
50 6
    public function hasBlackJackDirectly(CardHand $hand): bool
51
    {
52 6
        return $hand->getTotalBlackJack() === 21 && $hand->getNrOfCards() === 2;
53
    }
54
55
    /**
56
     * Draw a card for bank from deck and add it to the bank's hand.
57
     */
58 3
    public function drawForBank(CardHand $bankHand, Deck $deck): void
59
    {
60 3
        while ($bankHand->getTotalBlackJack() < 17) {
61 2
            $card = $deck->drawCard();
62 2
            if ($card !== null) {
63 2
                $bankHand->addCard($card);
64
            }
65
        }
66
    }
67
}
68