Passed
Push — main ( 101f2d...06b8a6 )
by Emil
25:10
created

Player::setGameState()   B

Complexity

Conditions 7
Paths 7

Size

Total Lines 21
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 7

Importance

Changes 0
Metric Value
cc 7
eloc 19
c 0
b 0
f 0
nc 7
nop 1
dl 0
loc 21
ccs 13
cts 13
cp 1
crap 7
rs 8.8333
1
<?php
2
3
namespace App\Game\BlackJack;
4
5
use App\Cards\Card;
6
use App\Cards\CardHand;
7
8
/**
9
 * Player.
10
 */
11
class Player extends CardHand
12
{
13
    public const DEFAULT_PLAYER_NAME = 'Player';
14
    public const DEFAULT_STARTING_CREDITS = 1000;
15
    // Game states
16
    public const DOUBLE_DOWN = -3;
17
    public const STAYED = -2;
18
    public const UNDECIDED = -1;
19
    // Outcomes
20
    public const TIE = 0;
21
    public const PLAYER_WIN = 1;
22
    public const DEALER_WIN = 2;
23
24
    protected string $name;
25
    protected int $credits;
26
    protected int $bet;
27
    protected int $handValue;
28
    protected int $gameState;
29
    protected bool $isBust;
30
31
    /**
32
     * __construct.
33
     *
34
     * @return void
35
     */
36 29
    public function __construct(string $name = self::DEFAULT_PLAYER_NAME, int $credits = self::DEFAULT_STARTING_CREDITS)
37
    {
38 29
        $this->setName($name);
39
40 29
        $this->setCredits($credits);
41
42 29
        $this->bet = 0;
43
44 29
        $this->handValue = 0;
45
46 29
        $this->gameState = self::UNDECIDED;
47
    }
48
49
    /**
50
     * dropHand.
51
     */
52 14
    public function dropHand(): void
53
    {
54 14
        $this->hand = [];
55
    }
56
57
    /**
58
     * checkIsBust.
59
     */
60 23
    protected function checkIsBust(): void
61
    {
62 23
        ($this->handValue > 21) ? $this->isBust = true : $this->isBust = false;
63
    }
64
65
    /**
66
     * calculateHandValue.
67
     */
68 23
    protected function calculateHandValue(): void
69
    {
70 23
        $lowValue = $this->getBlackJackValue();
71 23
        $highValue = $this->getBlackJackValueAceHigh();
72
73 23
        $this->handValue = ($highValue > 21) ? $lowValue : $highValue;
74
75 23
        $this->checkIsBust();
76
    }
77
78
    /**
79
     * getName.
80
     */
81 12
    public function getName(): string
82
    {
83 12
        return $this->name;
84
    }
85
86
    /**
87
     * setName.
88
     */
89 29
    public function setName(string $name): void
90
    {
91 29
        $this->name = htmlspecialchars($name);
92
    }
93
94
    /**
95
     * getCredits.
96
     */
97 18
    public function getCredits(): int
98
    {
99 18
        return $this->credits;
100
    }
101
102
    /**
103
     * setCredits.
104
     */
105 29
    public function setCredits(int $credits): void
106
    {
107 29
        if ($credits < 0) {
108 1
            $this->credits = 0;
109
110 1
            return;
111
        }
112
113 28
        $this->credits = $credits;
114
    }
115
116
    /**
117
     * getBet.
118
     */
119 18
    public function getBet(): int
120
    {
121 18
        return $this->bet;
122
    }
123
124
    /**
125
     * increaseBet.
126
     */
127 14
    public function increaseBet(int $betIncrease): void
128
    {
129 14
        if ($betIncrease > 0) {
130
            // If you are betting more then the credits left
131 14
            if ($betIncrease > $this->credits) {
132 1
                $betIncrease = $this->credits;
133
            }
134
135 14
            $this->bet += $betIncrease;
136 14
            $this->credits -= $betIncrease;
137
        }
138
    }
139
140
    /**
141
     * resetBet.
142
     */
143 19
    public function resetBet(): void
144
    {
145 19
        $this->bet = 0;
146
    }
147
148
    /**
149
     * changeCredits.
150
     */
151 1
    public function changeCredits(int $change): void
152
    {
153
        // Need to cast to int or else the balance can become a float
154 1
        $balance = (int) ($this->credits + $change);
155
156 1
        if ($balance < 0) {
157
            // If integer overflow happen
158 1
            if ($change > 0) {
159 1
                $this->credits = PHP_INT_MAX;
160
161 1
                return;
162
            }
163
164 1
            $this->credits = 0;
165
166 1
            return;
167
        }
168
169 1
        $this->credits = $balance;
170
    }
171
172
    /**
173
     * isBust.
174
     */
175 14
    public function isBust(): bool
176
    {
177 14
        return $this->isBust;
178
    }
179
180 19
    public function isBroke(): bool
181
    {
182 19
        return (0 === $this->credits and 0 === $this->bet) ? true : false;
183
    }
184
185
    /**
186
     * getHandValue.
187
     */
188 15
    public function getHandValue(): int
189
    {
190 15
        return $this->handValue;
191
    }
192
193
    /**
194
     * getGameState.
195
     */
196 16
    public function getGameState(): int
197
    {
198 16
        return $this->gameState;
199
    }
200
201
    /**
202
     * setGameState.
203
     */
204 11
    public function setGameState(int $gameState): void
205
    {
206
        switch ($gameState) {
207
            case self::DOUBLE_DOWN:
208 3
                $this->gameState = self::DOUBLE_DOWN;
209 3
                break;
210
            case self::STAYED:
211 11
                $this->gameState = self::STAYED;
212 11
                break;
213
            case self::UNDECIDED:
214 1
                $this->gameState = self::UNDECIDED;
215 1
                break;
216
            case self::TIE:
217 2
                $this->gameState = self::TIE;
218 2
                break;
219
            case self::PLAYER_WIN:
220 7
                $this->gameState = self::PLAYER_WIN;
221 7
                break;
222
            case self::DEALER_WIN:
223 7
                $this->gameState = self::DEALER_WIN;
224 7
                break;
225
        }
226
    }
227
228
    /**
229
     * addCard.
230
     */
231 23
    public function addCard(Card $card): void
232
    {
233 23
        parent::addCard($card);
234
235 23
        $this->calculateHandValue();
236
    }
237
}
238