Passed
Push — main ( 06b8a6...5a482c )
by Emil
05:26
created

Player::setGameState()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 8
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 13
rs 10
ccs 10
cts 10
cp 1
crap 1
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 14
        $this->handValue = 0;
56
    }
57
58
    /**
59
     * checkIsBust.
60
     */
61 23
    protected function checkIsBust(): void
62
    {
63 23
        ($this->handValue > 21) ? $this->isBust = true : $this->isBust = false;
64
    }
65
66
    /**
67
     * calculateHandValue.
68
     */
69 23
    protected function calculateHandValue(): void
70
    {
71 23
        $lowValue = $this->getBlackJackValue();
72 23
        $highValue = $this->getBlackJackValueAceHigh();
73
74 23
        $this->handValue = ($highValue > 21) ? $lowValue : $highValue;
75
76 23
        $this->checkIsBust();
77
    }
78
79
    /**
80
     * getName.
81
     */
82 12
    public function getName(): string
83
    {
84 12
        return $this->name;
85
    }
86
87
    /**
88
     * setName.
89
     */
90 29
    public function setName(string $name): void
91
    {
92 29
        $this->name = htmlspecialchars($name);
93
    }
94
95
    /**
96
     * getCredits.
97
     */
98 18
    public function getCredits(): int
99
    {
100 18
        return $this->credits;
101
    }
102
103
    /**
104
     * setCredits.
105
     */
106 29
    public function setCredits(int $credits): void
107
    {
108 29
        if ($credits < 0) {
109 1
            $this->credits = 0;
110
111 1
            return;
112
        }
113
114 28
        $this->credits = $credits;
115
    }
116
117
    /**
118
     * getBet.
119
     */
120 18
    public function getBet(): int
121
    {
122 18
        return $this->bet;
123
    }
124
125
    /**
126
     * increaseBet.
127
     */
128 14
    public function increaseBet(int $betIncrease): void
129
    {
130 14
        if ($betIncrease > 0) {
131
            // If you are betting more then the credits left
132 14
            if ($betIncrease > $this->credits) {
133 1
                $betIncrease = $this->credits;
134
            }
135
136 14
            $this->bet += $betIncrease;
137 14
            $this->credits -= $betIncrease;
138
        }
139
    }
140
141
    /**
142
     * resetBet.
143
     */
144 19
    public function resetBet(): void
145
    {
146 19
        $this->bet = 0;
147
    }
148
149
    /**
150
     * changeCredits.
151
     */
152 1
    public function changeCredits(int $change): void
153
    {
154
        // Need to cast to int or else the balance can become a float
155 1
        $balance = (int) ($this->credits + $change);
156
157 1
        if ($balance < 0) {
158
            // If integer overflow happen
159 1
            if ($change > 0) {
160 1
                $this->credits = PHP_INT_MAX;
161
162 1
                return;
163
            }
164
165 1
            $this->credits = 0;
166
167 1
            return;
168
        }
169
170 1
        $this->credits = $balance;
171
    }
172
173
    /**
174
     * isBust.
175
     */
176 14
    public function isBust(): bool
177
    {
178 14
        return $this->isBust;
179
    }
180
181 19
    public function isBroke(): bool
182
    {
183 19
        return (0 === $this->credits and 0 === $this->bet) ? true : false;
184
    }
185
186
    /**
187
     * getHandValue.
188
     */
189 15
    public function getHandValue(): int
190
    {
191 15
        return $this->handValue;
192
    }
193
194
    /**
195
     * getGameState.
196
     */
197 16
    public function getGameState(): int
198
    {
199 16
        return $this->gameState;
200
    }
201
202
    /**
203
     * setGameState.
204
     */
205 20
    public function setGameState(int $gameState): void
206
    {
207
        // Map Player game states
208 20
        $gameStateMap = [
209 20
            Player::DOUBLE_DOWN => Player::DOUBLE_DOWN,
210 20
            Player::STAYED => Player::STAYED,
211 20
            Player::UNDECIDED => Player::UNDECIDED,
212 20
            Player::TIE => Player::TIE,
213 20
            Player::PLAYER_WIN => Player::PLAYER_WIN,
214 20
            Player::DEALER_WIN => Player::DEALER_WIN,
215 20
        ];
216
217 20
        $this->gameState = $gameStateMap[$gameState] ?? 404;
218
    }
219
220
    /**
221
     * addCard.
222
     */
223 23
    public function addCard(Card $card): void
224
    {
225 23
        parent::addCard($card);
226
227 23
        $this->calculateHandValue();
228
    }
229
}
230