1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Cards; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Card. |
7
|
|
|
*/ |
8
|
|
|
class Card |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Constant strings for the no rank anc no suit message. |
12
|
|
|
*/ |
13
|
|
|
public const NO_RANK = 'no rank'; |
14
|
|
|
public const NO_SUIT = 'no suit'; |
15
|
|
|
|
16
|
|
|
protected string $rank; |
17
|
|
|
protected string $suit; |
18
|
|
|
protected string $color; |
19
|
|
|
protected int $value; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* __construct. |
23
|
|
|
* |
24
|
|
|
* Constructor of the class |
25
|
|
|
* |
26
|
|
|
* @return void |
27
|
|
|
*/ |
28
|
51 |
|
public function __construct(string $rank, string $suit) |
29
|
|
|
{ |
30
|
51 |
|
$this->findRank($rank); |
31
|
|
|
|
32
|
51 |
|
$this->findSuit($suit); |
33
|
|
|
|
34
|
51 |
|
$this->findValue(); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* findRank. |
39
|
|
|
* |
40
|
|
|
* Finds rank that matches the string |
41
|
|
|
*/ |
42
|
51 |
|
protected function findRank(string $rank): void |
43
|
|
|
{ |
44
|
51 |
|
match ($rank) { |
45
|
51 |
|
'a', 'A', 'ace', 'Ace' => $this->rank = 'A', |
46
|
51 |
|
'2', 'two', 'Two' => $this->rank = '2', |
47
|
51 |
|
'3', 'three', 'Three' => $this->rank = '3', |
48
|
51 |
|
'4', 'four', 'Four' => $this->rank = '4', |
49
|
51 |
|
'5', 'five', 'Five' => $this->rank = '5', |
50
|
51 |
|
'6', 'six', 'Six' => $this->rank = '6', |
51
|
51 |
|
'7', 'seven', 'Seven' => $this->rank = '7', |
52
|
51 |
|
'8', 'eight', 'Eight' => $this->rank = '8', |
53
|
51 |
|
'9', 'nine', 'Nine' => $this->rank = '9', |
54
|
51 |
|
'10', 'ten', 'Ten' => $this->rank = '10', |
55
|
51 |
|
'11', 'j', 'J', 'jack', 'Jack' => $this->rank = 'J', |
56
|
51 |
|
'c', 'C', 'knave', 'Knave', 'knight', 'Knight' => $this->rank = 'C', |
57
|
51 |
|
'12', 'q', 'Q', 'lady', 'Lady', 'queen', 'Queen' => $this->rank = 'Q', |
58
|
51 |
|
'13', 'k', 'K', 'king', 'King' => $this->rank = 'K', |
59
|
51 |
|
'joker', 'Joker' => $this->rank = 'Joker', |
60
|
51 |
|
default => $this->rank = self::NO_RANK, |
61
|
51 |
|
}; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* findSuit. |
66
|
|
|
* |
67
|
|
|
* Finds suit that matches the string |
68
|
|
|
*/ |
69
|
51 |
|
protected function findSuit(string $suit): void |
70
|
|
|
{ |
71
|
51 |
|
match ($suit) { |
72
|
51 |
|
'♥', '♡', 'hearts', 'Hearts', 'heart', 'Heart' => $this->suit = '♥', |
73
|
51 |
|
'♦', '♢', 'tiles', 'Tiles','tile', 'Tile', 'diamonds', 'Diamonds', 'diamond', 'Diamond' => $this->suit = '♦', |
74
|
51 |
|
'♣', '♧', 'clovers', 'Clovers', 'clover', 'Clover', 'clubs', 'Clubs', 'club', 'Club' => $this->suit = '♣', |
75
|
51 |
|
'♠', '♤', 'pikes', 'Pikes', 'pike', 'Pike', 'spades', 'Spades' ,'spade', 'Spade' => $this->suit = '♠', |
76
|
51 |
|
'red', 'Red' => $this->suit = 'Red', |
77
|
51 |
|
'black', 'Black' => $this->suit = 'Black', |
78
|
51 |
|
'white', 'White' => $this->suit = 'White', |
79
|
51 |
|
default => $this->suit = self::NO_SUIT, |
80
|
51 |
|
}; |
81
|
|
|
|
82
|
51 |
|
$this->checkIfSuitValid(); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* findValue. |
87
|
|
|
* |
88
|
|
|
* Finds value of the rank |
89
|
|
|
*/ |
90
|
51 |
|
protected function findValue(): void |
91
|
|
|
{ |
92
|
51 |
|
if (self::NO_SUIT != $this->suit) { |
93
|
49 |
|
match ($this->rank) { |
94
|
49 |
|
'a', 'A', 'ace', 'Ace' => $this->value = 1, |
95
|
49 |
|
'2', 'two', 'Two' => $this->value = 2, |
96
|
49 |
|
'3', 'three', 'Three' => $this->value = 3, |
97
|
49 |
|
'4', 'four', 'Four' => $this->value = 4, |
98
|
49 |
|
'5', 'five', 'Five' => $this->value = 5, |
99
|
49 |
|
'6', 'six', 'Six' => $this->value = 6, |
100
|
49 |
|
'7', 'seven', 'Seven' => $this->value = 7, |
101
|
49 |
|
'8', 'eight', 'Eight' => $this->value = 8, |
102
|
49 |
|
'9', 'nine', 'Nine' => $this->value = 9, |
103
|
49 |
|
'10', 'ten', 'Ten' => $this->value = 10, |
104
|
49 |
|
'11', 'j', 'J', 'jack', 'Jack' => $this->value = 11, |
105
|
49 |
|
'c', 'C', 'knave', 'Knave', 'knight', 'Knight' => $this->value = 11, |
106
|
49 |
|
'12', 'q', 'Q', 'lady', 'Lady', 'queen', 'Queen' => $this->value = 12, |
107
|
49 |
|
'13', 'k', 'K', 'king', 'King' => $this->value = 13, |
108
|
49 |
|
default => $this->value = 0, |
109
|
49 |
|
}; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* checkIfSuitValid. |
115
|
|
|
* |
116
|
|
|
* Checks if the Joker suits 'Red', 'Black' and 'White' is only assigned to a card with rank 'Joker' |
117
|
|
|
*/ |
118
|
51 |
|
protected function checkIfSuitValid(): void |
119
|
|
|
{ |
120
|
51 |
|
match ($this->rank) { |
121
|
51 |
|
'Joker' => match ($this->suit) { |
122
|
51 |
|
'red', 'Red', 'black', 'Black', 'white', 'White' => $this->suit, |
123
|
51 |
|
default => $this->suit = self::NO_SUIT, |
124
|
51 |
|
}, |
125
|
51 |
|
default => match ($this->suit) { |
126
|
51 |
|
'red', 'Red', 'black', 'Black', 'white', 'White' => $this->suit = self::NO_SUIT, |
127
|
51 |
|
default => $this->suit, |
128
|
51 |
|
}, |
129
|
51 |
|
}; |
130
|
|
|
|
131
|
51 |
|
if (self::NO_SUIT == $this->suit) { |
132
|
5 |
|
$this->value = 0; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* getRank. |
138
|
|
|
* |
139
|
|
|
* Returns the rank of the card |
140
|
|
|
*/ |
141
|
4 |
|
public function getRank(): string |
142
|
|
|
{ |
143
|
4 |
|
return $this->rank; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* setRank. |
148
|
|
|
* |
149
|
|
|
* Sets the rank of the card |
150
|
|
|
*/ |
151
|
2 |
|
public function setRank(string $rank): void |
152
|
|
|
{ |
153
|
2 |
|
$this->findRank($rank); |
154
|
|
|
|
155
|
2 |
|
$this->checkIfSuitValid(); |
156
|
|
|
|
157
|
2 |
|
$this->findValue(); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* getSuit. |
162
|
|
|
* |
163
|
|
|
* Returns the suit of the cards |
164
|
|
|
*/ |
165
|
4 |
|
public function getSuit(): string |
166
|
|
|
{ |
167
|
4 |
|
return $this->suit; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* setSuit. |
172
|
|
|
* |
173
|
|
|
* Sets the suit of the cards |
174
|
|
|
*/ |
175
|
2 |
|
public function setSuit(string $suit): void |
176
|
|
|
{ |
177
|
2 |
|
$this->findSuit($suit); |
178
|
|
|
|
179
|
2 |
|
$this->findValue(); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* getValue. |
184
|
|
|
* |
185
|
|
|
* Returns the value of the card |
186
|
|
|
*/ |
187
|
26 |
|
public function getValue(): int |
188
|
|
|
{ |
189
|
26 |
|
return $this->value; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* getString. |
194
|
|
|
* |
195
|
|
|
* Returns the rank and suit of the card as a string |
196
|
|
|
*/ |
197
|
8 |
|
public function getString(): string |
198
|
|
|
{ |
199
|
8 |
|
return '['.$this->suit.' '.$this->rank.']'; |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
|