Game21::getPlayerHand()   A
last analyzed

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 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
namespace App\Card;
4
5
/**
6
 * Class Game21 is game logic handler.
7
 */
8
class Game21
9
{
10
    /** @var Deck */
11
    private Deck $deck;
12
13
    /** @var CardHand */
14
    private CardHand $playerHand;
15
16
    /** @var CardHand */
17
    private CardHand $bankHand;
18
19
    /** @var bool Flag for whether the game is over. */
20
    private bool $gameOver = false;
21
22
    /** @var string The result message. */
23
    private string $result = '';
24
25
    /**
26
     * Game21 constructor.
27
     *
28
     * Init the game by creating a new deck and empty hands for the player and the bank.
29
     */
30 6
    public function __construct(Deck $deck, CardHand $playerHand, CardHand $bankHand)
31
    {
32 6
        $this->deck = $deck;
33 6
        $this->playerHand = $playerHand;
34 6
        $this->bankHand = $bankHand;
35
    }
36
37
    /**
38
     * Draw a card for player from deck and add it to the player's hand.
39
     */
40 1
    public function drawForPlayer(): void
41
    {
42 1
        $card = $this->deck->drawCard();
43 1
        if ($card !== null) {
44 1
            $this->playerHand->addCard($card);
45
        }
46
47 1
        if ($this->playerHand->getTotal() > 21) {
48 1
            $this->result = "Du har över 21! Du förlorade!";
49 1
            $this->gameOver = true;
50
        }
51
    }
52
53
    /**
54
     * Draw a card for bank from deck and add it to the bank's hand.
55
     */
56 2
    public function drawForBank(): void
57
    {
58 2
        while ($this->bankHand->getTotal() < 17) {
59 2
            $card = $this->deck->drawCard();
60 2
            if ($card === null) {
61 1
                return;
62
            }
63 1
            $this->bankHand->addCard($card);
64
        }
65 1
        $this->result = $this->compareResults();
66 1
        $this->gameOver = true;
67
    }
68
69 2
    public function compareResults(): string
70
    {
71 2
        $player = $this->playerHand->getTotal();
72 2
        $bank = $this->bankHand->getTotal();
73
74 2
        if ($player > 21) {
75 1
            return "Du förlorade!";
76
        }
77 2
        if ($bank > 21) {
78 1
            return "Du vann!";
79
        }
80 2
        if ($bank >= $player) {
81 2
            return "Du förlorade!";
82
        }
83 1
        return "Du vann!";
84
    }
85
86
    /**
87
     * Get the player's hand.
88
     *
89
     * @return CardHand The player's hand.
90
     */
91 2
    public function getPlayerHand(): CardHand
92
    {
93 2
        return $this->playerHand;
94
    }
95
96
    /**
97
     * Get the banks's hand.
98
     *
99
     * @return CardHand The bank's hand.
100
     */
101 2
    public function getBankHand(): CardHand
102
    {
103 2
        return $this->bankHand;
104
    }
105
106
    /**
107
     * Check if the game is over.
108
     *
109
     * @return bool True if the game has ended, otherwise false.
110
     */
111 1
    public function isGameOver(): bool
112
    {
113 1
        return $this->gameOver;
114
    }
115
116
    /**
117
     * Get the final or current result message.
118
     *
119
     * @return string
120
     */
121 1
    public function getResult(): string
122
    {
123 1
        return $this->result;
124
    }
125
}
126