1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Game; |
4
|
|
|
|
5
|
|
|
use App\Card\Card; |
6
|
|
|
use App\Card\CardGraphic; |
7
|
|
|
use App\Card\CardHand; |
8
|
|
|
use App\Card\DeckOfCards; |
9
|
|
|
use App\Traits\GameTraits; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Methods for class player, that are used in the class Game to play the card game 21. |
13
|
|
|
* The object Player is used both for the player called "player" and the player called "bank". |
14
|
|
|
*/ |
15
|
|
|
class Player |
16
|
|
|
{ |
17
|
|
|
use GameTraits; |
18
|
|
|
|
19
|
|
|
private CardHand $hand; |
20
|
|
|
|
21
|
9 |
|
public function __construct() |
22
|
|
|
{ |
23
|
9 |
|
$this->hand = new CardHand(); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Draw a card from the deck of cards that is set up for the round of the game. |
28
|
|
|
* Add the drawn card to the hand. |
29
|
|
|
* @param DeckOfCards $deck |
30
|
|
|
*/ |
31
|
2 |
|
public function draw(DeckOfCards $deck): void |
32
|
|
|
{ |
33
|
2 |
|
$value = $deck->drawCard(); |
34
|
2 |
|
$card = new CardGraphic($value); |
35
|
2 |
|
$this->hand->add($card); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Add a card to the hand. |
40
|
|
|
* @param Card $card |
41
|
|
|
*/ |
42
|
2 |
|
public function setHand(Card $card): void |
43
|
|
|
{ |
44
|
2 |
|
$this->hand->add($card); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Get the values of the cards at hand. |
49
|
|
|
* @return string[][] |
50
|
|
|
*/ |
51
|
1 |
|
public function getHand(): array |
52
|
|
|
{ |
53
|
1 |
|
return $this->hand->getValues(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Get the scores of the current hand. |
58
|
|
|
*/ |
59
|
3 |
|
public function getScore(): int |
60
|
|
|
{ |
61
|
3 |
|
return $this->loopGameScore($this->hand); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Check if the total score of the current hand is over 21. |
66
|
|
|
*/ |
67
|
1 |
|
public function isOver21(): bool |
68
|
|
|
{ |
69
|
1 |
|
return $this->loopGameScore($this->hand) > 21; |
70
|
|
|
|
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Check if the total score of the current hand is less than 17. |
75
|
|
|
*/ |
76
|
1 |
|
public function isNotOver17(): bool |
77
|
|
|
{ |
78
|
1 |
|
return $this->loopGameScore($this->hand) < 17; |
79
|
|
|
|
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|