Skip to content

Commit d4dfe20

Browse files
committed
Uploading blackjack hand calculator.
1 parent 5b09b8f commit d4dfe20

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

Exercises/PHP/blackhand.php

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
$hand = ['A-H', '5-D', 'K-C', 'A-S', '4-H'];
4+
5+
function getTotal($array) {
6+
$total = 0;
7+
$cardValue = [];
8+
$ace = 0;
9+
10+
foreach ($array as $card) {
11+
$card = explode('-', $card);
12+
switch ($card[0]) {
13+
case 'A':
14+
$ace = 11;
15+
$cardValue[] = &$ace;
16+
break;
17+
case 'K':
18+
case 'Q':
19+
case 'J':
20+
$cardValue[] = 10;
21+
break;
22+
case '10':
23+
$cardValue[] = 10;
24+
break;
25+
case '9':
26+
$cardValue[] = 9;
27+
break;
28+
case '8':
29+
$cardValue[] = 8;
30+
break;
31+
case '7':
32+
$cardValue[] = 7;
33+
break;
34+
case '6':
35+
$cardValue[] = 6;
36+
break;
37+
case '5':
38+
$cardValue[] = 5;
39+
break;
40+
case '4':
41+
$cardValue[] = 4;
42+
break;
43+
case '3':
44+
$cardValue[] = 3;
45+
break;
46+
case '2':
47+
$cardValue[] = 2;
48+
break;
49+
}
50+
51+
$total = array_sum($cardValue);
52+
53+
if ($total > 21) {
54+
$ace = 1;
55+
}
56+
}
57+
58+
return $total;
59+
}
60+
61+
$total = getTotal($hand);
62+
63+
if ($total > 21) {
64+
echo "You've gone over with $total. Too bad. Good news is that you won't lose any money. Try again?" . PHP_EOL;
65+
} elseif ($total == 21){
66+
echo "Perfect hand with a total of $total! You will win a game of Blackjack!" . PHP_EOL;
67+
} else {
68+
echo "Your total is $total. If your total is > 17, don't hit, but if your total is < 17, hit!" . PHP_EOL;
69+
}
70+
71+
?>

0 commit comments

Comments
 (0)