1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Card; |
4
|
|
|
|
5
|
|
|
use App\Card\CardGraphic; |
6
|
|
|
use Exception; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* DeckOfCards - represents a deck of Cards |
10
|
|
|
*/ |
11
|
|
|
class DeckOfCards |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* deck |
15
|
|
|
* |
16
|
|
|
* @var array<CardGraphic> $deck |
17
|
|
|
*/ |
18
|
|
|
private array $deck = []; |
19
|
|
|
|
20
|
8 |
|
public function __construct() |
21
|
|
|
{ |
22
|
8 |
|
$this->deck = []; |
23
|
8 |
|
$this->buildDeck(); |
24
|
|
|
} |
25
|
|
|
/** |
26
|
|
|
* buildDeck - fills the deck with 52 cards |
27
|
|
|
* |
28
|
|
|
* @return void |
29
|
|
|
*/ |
30
|
8 |
|
private function buildDeck(): void |
31
|
|
|
{ |
32
|
8 |
|
for ($i = 0; $i < 52; $i++) { |
33
|
8 |
|
$this->deck[] = new CardGraphic(); |
34
|
8 |
|
$this->deck[$i]->set($i); |
35
|
|
|
} |
36
|
|
|
} |
37
|
|
|
/** |
38
|
|
|
* shuffle - shuffles the deck of cards |
39
|
|
|
* |
40
|
|
|
* @return void |
41
|
|
|
*/ |
42
|
7 |
|
public function shuffle(): void |
43
|
|
|
{ |
44
|
7 |
|
shuffle($this->deck); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* drawCard - |
49
|
|
|
* |
50
|
|
|
* @return CardGraphic |
51
|
|
|
*/ |
52
|
5 |
|
public function drawCard(): CardGraphic |
53
|
|
|
{ |
54
|
5 |
|
if (count($this->deck) === 0) { |
55
|
|
|
throw new Exception("Error: Deck of cards is empty"); |
56
|
|
|
} |
57
|
5 |
|
$card = array_pop($this->deck); |
58
|
5 |
|
return $card; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* getNumberOfCards - returns the number of remaining cards in the deck |
64
|
|
|
* |
65
|
|
|
* @return int |
66
|
|
|
*/ |
67
|
1 |
|
public function getNumberOfCards(): int |
68
|
|
|
{ |
69
|
1 |
|
return count($this->deck); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* getDeckAsValues - returns the values of the cards in the deck |
74
|
|
|
* @return array<int|null> $values |
75
|
|
|
*/ |
76
|
1 |
|
public function getDeckAsValues(): array |
77
|
|
|
{ |
78
|
1 |
|
$values = []; |
79
|
1 |
|
foreach ($this->deck as $card) { |
80
|
1 |
|
$values[] = $card->getValue(); |
81
|
|
|
} |
82
|
1 |
|
return $values; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* getDeck - returns the string representation of the cards in the deck |
87
|
|
|
* @return array<string|null> $values |
88
|
|
|
*/ |
89
|
1 |
|
public function getDeck(): array |
90
|
|
|
{ |
91
|
1 |
|
$values = []; |
92
|
1 |
|
foreach ($this->deck as $card) { |
93
|
1 |
|
$values[] = $card->getAsString(); |
94
|
|
|
} |
95
|
1 |
|
return $values; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|