Game21::drawForPlayer()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 6
c 1
b 0
f 0
nc 4
nop 0
dl 0
loc 10
ccs 7
cts 7
cp 1
crap 3
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 9
    public function __construct(Deck $deck, CardHand $playerHand, CardHand $bankHand)
31
    {
32 9
        $this->deck = $deck;
33 9
        $this->playerHand = $playerHand;
34 9
        $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
    /**
70
     * Compare the final results of the game.
71
     */
72 2
    public function compareResults(): string
73
    {
74 2
        $player = $this->playerHand->getTotal();
75 2
        $bank = $this->bankHand->getTotal();
76
77 2
        if ($player > 21) {
78 1
            return "Du förlorade!";
79
        }
80 2
        if ($bank > 21) {
81 1
            return "Du vann!";
82
        }
83 2
        if ($bank >= $player) {
84 2
            return "Du förlorade!";
85
        }
86 1
        return "Du vann!";
87
    }
88
89
    /**
90
     * Get the player's hand.
91
     *
92
     * @return CardHand The player's hand.
93
     */
94 5
    public function getPlayerHand(): CardHand
95
    {
96 5
        return $this->playerHand;
97
    }
98
99
    /**
100
     * Get the banks's hand.
101
     *
102
     * @return CardHand The bank's hand.
103
     */
104 3
    public function getBankHand(): CardHand
105
    {
106 3
        return $this->bankHand;
107
    }
108
109
    /**
110
     * Check if the game is over.
111
     *
112
     * @return bool True if the game has ended, otherwise false.
113
     */
114 1
    public function isGameOver(): bool
115
    {
116 1
        return $this->gameOver;
117
    }
118
119
    /**
120
     * Get the final or current result message.
121
     *
122
     * @return string
123
     */
124 1
    public function getResult(): string
125
    {
126 1
        return $this->result;
127
    }
128
}
129