|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Card; |
|
4
|
|
|
|
|
5
|
|
|
class Deck |
|
6
|
|
|
{ |
|
7
|
|
|
protected $cards = []; |
|
8
|
|
|
|
|
9
|
8 |
|
public function __construct() |
|
10
|
|
|
{ |
|
11
|
8 |
|
$suits = ['Hearts', 'Diamonds', 'Clubs', 'Spades']; |
|
12
|
8 |
|
$values = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'Jack', 'Queen', 'King', 'Ace']; |
|
13
|
|
|
|
|
14
|
8 |
|
foreach ($suits as $suit) { |
|
15
|
8 |
|
foreach ($values as $value) { |
|
16
|
8 |
|
$this->cards[$suit][] = new CardGraphic($suit, $value); |
|
17
|
|
|
} |
|
18
|
|
|
} |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
4 |
|
public function shuffle(): void |
|
22
|
|
|
{ |
|
23
|
4 |
|
foreach ($this->cards as &$cards) { |
|
24
|
4 |
|
shuffle($cards); |
|
25
|
|
|
} |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
4 |
|
public function drawCard(): ?Card |
|
29
|
|
|
{ |
|
30
|
|
|
// Return null if no cards left |
|
31
|
4 |
|
if (empty($this->cards)) { |
|
32
|
1 |
|
return null; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
// Get a random suit from available suits |
|
36
|
4 |
|
$suits = array_keys($this->cards); |
|
37
|
4 |
|
$randomSuit = $suits[array_rand($suits)]; |
|
38
|
|
|
|
|
39
|
4 |
|
shuffle($this->cards[$randomSuit]); |
|
40
|
|
|
|
|
41
|
|
|
// Draw a card from the suit |
|
42
|
4 |
|
$card = array_shift($this->cards[$randomSuit]); |
|
43
|
|
|
|
|
44
|
|
|
// If suit is empty, remove it from deck |
|
45
|
4 |
|
if (empty($this->cards[$randomSuit])) { |
|
46
|
1 |
|
unset($this->cards[$randomSuit]); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
4 |
|
return $card; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
|
|
53
|
|
|
|
|
54
|
3 |
|
public function remainingCardsCount(): int |
|
55
|
|
|
{ |
|
56
|
3 |
|
$totalCards = 0; |
|
57
|
3 |
|
foreach ($this->cards as $cardsInSuit) { |
|
58
|
2 |
|
$totalCards += count($cardsInSuit); |
|
59
|
|
|
} |
|
60
|
3 |
|
return $totalCards; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
1 |
|
public function getCards(): array |
|
64
|
|
|
{ |
|
65
|
1 |
|
return $this->cards; |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|