-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBlackjack.php
225 lines (200 loc) · 7.28 KB
/
Blackjack.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
<?php
class Blackjack
{
public function scoreHand(Hand $hand): string
{
$score = $this->points($hand);
$outcome = [
"BlackJack" => [
'cards' => 2,
'points' => 21
],
"Twenty-One" => [
'cards' => -1,
'points' => 21
],
"Five Card Charlie" => [
'cards' => 5,
'points' => -1,
]
];
foreach ($outcome as $key => $value) {
if (
($value['cards'] < 0 || $value['cards'] == count($hand->cards))
&& ($value['points'] < 0 || ($score <= 21 && $value['points'] == $score))
) {
return $key;
}
if (count($hand->cards) == 2 && $score == 22) {
return 'Two-Aces';
}
}
return $score <= 21 ? (string)$score : 'is Busted';
}
public function points(Hand $hand): string
{
$score = 0;
$haveAce = false;
foreach ($hand->cards as $card) {
$score += $card->score();
if ($card->getValue() == 'A') {
$haveAce = true;
}
}
if ($haveAce == true && $score < 21) {
return "$score/" . $score - 10;
}
if ($haveAce == true && $score > 21) {
$haveAce = false;
return $score - 10;
}
return (string)$score;
}
public function splitCheck(Hand $hand)
{
if ($hand->cards[0]->getValue() === $hand->cards[1]->getValue()) {
return true;
}
return false;
}
public function resultsValidation($players, $dealer): void
{
$info = [
'WINNERS' => [],
'TIED' => [],
'LOSERS' => []
];
$dealerPoints = $this->points($dealer->hands[0]);
if ($dealerPoints > 21) {
$this->bustedDealer($players, $info);
}
if ($dealerPoints == 21 || $this->scoreHand($dealer->hands[0]) == 'Five Card Charlie!') {
$this->dealerWins($players, $dealer, $info);
}
if ($dealerPoints < 21) {
$this->dealerBelow21($players, $dealer, $info);
}
$this->results($dealer, $players, $info);
}
private function bustedDealer($players, &$info)
{
foreach ($players as $player) {
foreach ($player->hands as $hand) {
$points = $this->points($hand);
if ($points > 21) {
$info['LOSERS'][] = $hand;
} else {
$info['WINNERS'][] = $hand;
}
}
}
}
private function dealerWins($players, $dealer, &$info): void
{
foreach ($players as $player) {
foreach ($player->hands as $hand) {
$outputPlayer = $this->scoreHand($hand);
switch ($this->scoreHand($dealer->hands[0])) {
case 'BlackJack':
if ($outputPlayer == 'BlackJack!') {
$info['TIED'][] = $hand;
} else {
$info['LOSERS'][] = $hand;
}
// BlackJack!
case 'Five Card Charlie':
if ($outputPlayer == 'BlackJack!') {
$info['WINNERS'][] = $hand;
} elseif ($outputPlayer == 'Five Card Charlie!') {
$info['TIED'][] = $hand;
} else {
$info['LOSERS'][] = $hand;
}
// Five-Card-Charlie
case 'Twenty-One':
if ($outputPlayer == 'BlackJack!' || $outputPlayer == 'Five Card Charlie!') {
$info['WINNERS'][] = $hand;
} elseif ($outputPlayer == 'Twenty-One!') {
$info['TIED'][] = $hand;
} else {
$info['LOSERS'][] = $hand;
}
// No default possible
}
}
}
}
private function dealerBelow21($players, $dealer, &$info): void
{
foreach ($players as $player) {
foreach ($player->hands as $hand) {
$dealerPoints = $this->points($dealer->hands[0]);
$playerPoints = $this->points($hand);
if ($playerPoints > 21) {
$info['LOSERS'][] = $hand;
} elseif ($playerPoints > $dealerPoints || $this->scoreHand($hand) == 'Five Card Charlie!') {
$info['WINNERS'][] = $hand;
} elseif ($playerPoints < $dealerPoints) {
$info['LOSERS'][] = $hand;
} else {
$info['TIED'][] = $hand;
}
}
}
}
private function results($dealer, $players, $info): void
{
echo PHP_EOL . 'DEALER:' . PHP_EOL;
echo 'Dealer has ' . $dealer->showHand($dealer->hands[0]) . '=> ' . $this->scoreHand($dealer->hands[0]) . PHP_EOL;
echo PHP_EOL . 'SHOW HAND:' . PHP_EOL;
foreach ($this->descResults($players) as $player) {
echo $player['hand'] . '=> ' . $player['points'] . PHP_EOL;
}
foreach ($info as $key => $value) {
if (empty($value)) {
continue;
} else {
echo PHP_EOL . $key . ':' . PHP_EOL;
foreach ($value as $result) {
if ($key == 'TIED') {
echo $result->handName . ' tied with Dealer!' . PHP_EOL;
} else {
echo $result->handName . PHP_EOL;
}
}
}
}
if (!empty($info['WINNERS']) || !empty($info['TIED'])) {
echo PHP_EOL . 'BET:' . PHP_EOL;
foreach ($info as $key => $value) {
foreach ($value as $hand) {
$message = $this->scoreHand($hand);
if ($key == 'TIED') {
echo $hand->handName . ' has ' . $message . ' => ' . $hand->bet . ' X 1 => ' . $hand->bet . PHP_EOL;
} elseif ($key == 'WINNERS' && in_array($message, ['BlackJack', 'Five Card Charlie'])) {
echo $hand->handName . ' has ' . $message . ' => ' . $hand->bet . ' X 2.5 => ' . $hand->bet * 2.5 . PHP_EOL;
} elseif ($key == 'WINNERS') {
echo $hand->handName . ' has ' . $message . ' => ' . $hand->bet . ' X 2 => ' . $hand->bet * 2 . PHP_EOL;
}
}
}
}
die;
}
public function descResults($players): array
{
$mostPoints = [];
foreach ($players as $player) {
foreach ($player->hands as $hand) {
$mostPoints[] = [
'hand' => $hand->handName . ' has ' . $player->showHand($hand),
'points' => $this->scoreHand($hand)
];
}
}
usort($mostPoints, function ($a, $b) {
return $a['points'] <=> $b['points'];
});
return $mostPoints;
}
}