|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Card; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Methods for the DeckOfCards class. |
|
7
|
|
|
*/ |
|
8
|
|
|
class DeckOfCards |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* @var string[][] |
|
12
|
|
|
*/ |
|
13
|
|
|
private array $deck; |
|
14
|
|
|
|
|
15
|
12 |
|
public function __construct() |
|
16
|
|
|
{ |
|
17
|
12 |
|
$this->deck = $this->createDeck(); |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Create a deck of french standard with 52 cards. |
|
22
|
|
|
* @return string[][] |
|
23
|
|
|
*/ |
|
24
|
12 |
|
public function createDeck(): array |
|
25
|
|
|
{ |
|
26
|
12 |
|
$suits = ['C', 'S', 'H', 'D']; |
|
27
|
12 |
|
$ranks = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K']; |
|
28
|
12 |
|
$deckArr = []; |
|
29
|
|
|
|
|
30
|
12 |
|
foreach ($suits as $suit) { |
|
31
|
12 |
|
foreach ($ranks as $rank) { |
|
32
|
12 |
|
$deckArr[] = [$suit, $rank]; |
|
33
|
|
|
} |
|
34
|
|
|
}; |
|
35
|
|
|
|
|
36
|
12 |
|
return $deckArr; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Draw a random card from the deck, and remove the card from the deck. |
|
41
|
|
|
* @return string[] |
|
42
|
|
|
*/ |
|
43
|
3 |
|
public function drawCard(): array |
|
44
|
|
|
{ |
|
45
|
3 |
|
$key = array_rand($this->deck, 1); |
|
46
|
3 |
|
$value = $this->deck[$key]; |
|
47
|
3 |
|
unset($this->deck[$key]); |
|
48
|
3 |
|
return $value; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Sort the cards in the deck. |
|
53
|
|
|
* The first card of a suite has the value "A". |
|
54
|
|
|
* The last value of a suite has the value "K". |
|
55
|
|
|
*/ |
|
56
|
2 |
|
public function sortCards(): void |
|
57
|
|
|
{ |
|
58
|
|
|
//Sort per suit, and add ace infront |
|
59
|
2 |
|
$suits = ['C', 'S', 'H', 'D']; |
|
60
|
2 |
|
$sortArray = []; |
|
61
|
|
|
|
|
62
|
2 |
|
foreach ($suits as $suit) { |
|
63
|
2 |
|
$loopValues = []; |
|
64
|
2 |
|
$startVal = []; |
|
65
|
2 |
|
$endVal = []; |
|
66
|
|
|
|
|
67
|
2 |
|
foreach ($this->deck as $values) { |
|
68
|
|
|
|
|
69
|
2 |
|
if ($values[0] !== $suit) { |
|
70
|
2 |
|
continue; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
2 |
|
if ($values[1] === 'A') { |
|
74
|
2 |
|
$startVal = $values; |
|
75
|
2 |
|
continue; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
2 |
|
if ($values[1] === 'K') { |
|
79
|
2 |
|
$endVal = $values; |
|
80
|
2 |
|
continue; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
2 |
|
$loopValues[] = $values; |
|
84
|
|
|
} |
|
85
|
2 |
|
asort($loopValues); |
|
86
|
2 |
|
array_unshift($loopValues, $startVal); |
|
87
|
2 |
|
array_push($loopValues, $endVal); |
|
88
|
2 |
|
$sortArray = array_merge($sortArray, $loopValues); |
|
89
|
|
|
} |
|
90
|
2 |
|
$this->deck = $sortArray; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Shuffle the order of the cards in the deck. |
|
95
|
|
|
*/ |
|
96
|
2 |
|
public function shuffleCards(): void |
|
97
|
|
|
{ |
|
98
|
2 |
|
shuffle($this->deck); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Get the current number of cards in the deck. |
|
103
|
|
|
*/ |
|
104
|
3 |
|
public function getNumberCards(): int |
|
105
|
|
|
{ |
|
106
|
3 |
|
return count($this->deck); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Return all the values of the cards in the deck. |
|
111
|
|
|
* @return string[][] |
|
112
|
|
|
*/ |
|
113
|
4 |
|
public function getValues(): array |
|
114
|
|
|
{ |
|
115
|
4 |
|
return $this->deck; |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|