Skip to content

Commit c893ca5

Browse files
committed
Fix blackjack
1 parent d80bf98 commit c893ca5

File tree

2 files changed

+63
-16
lines changed

2 files changed

+63
-16
lines changed

src/Command/BlackjackCommand.php

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Symfony\Component\Console\Output\OutputInterface as Output;
1313
use Symfony\Component\Console\Question\ChoiceQuestion;
1414
use Symfony\Component\Console\Style\SymfonyStyle;
15+
use function Deployer\Support\array_flatten;
1516

1617
class BlackjackCommand extends Command
1718
{
@@ -40,13 +41,32 @@ protected function execute(Input $input, Output $output): int
4041
$this->telemetry();
4142
$io = new SymfonyStyle($input, $output);
4243

43-
$this->print("\x1b[38;2;255;95;109m╭\x1b[39m\x1b[38;2;255;95;107m─\x1b[39m\x1b[38;2;255;96;106m─\x1b[39m\x1b[38;2;255;96;104m─\x1b[39m\x1b[38;2;255;96;103m─\x1b[39m\x1b[38;2;255;97;101m─\x1b[39m\x1b[38;2;255;97;100m─\x1b[39m\x1b[38;2;255;97;99m─\x1b[39m\x1b[38;2;255;98;97m─\x1b[39m\x1b[38;2;255;100;98m─\x1b[39m\x1b[38;2;255;102;98m─\x1b[39m\x1b[38;2;255;104;98m─\x1b[39m\x1b[38;2;255;106;99m─\x1b[39m\x1b[38;2;255;108;99m─\x1b[39m\x1b[38;2;255;110;99m─\x1b[39m\x1b[38;2;255;112;100m─\x1b[39m\x1b[38;2;255;114;100m─\x1b[39m\x1b[38;2;255;116;100m─\x1b[39m\x1b[38;2;255;118;100m─\x1b[39m\x1b[38;2;255;120;101m─\x1b[39m\x1b[38;2;255;122;101m─\x1b[39m\x1b[38;2;255;124;101m─\x1b[39m\x1b[38;2;255;126;102m╮\x1b[39m");
44-
$this->print("\x1b[38;2;255;128;102m│\x1b[39m \x1b[38;2;255;130;102m│\x1b[39m");
45-
$this->print("\x1b[38;2;255;132;103m│\x1b[39m \x1b[38;2;255;134;103mW\x1b[39m\x1b[38;2;255;136;103me\x1b[39m\x1b[38;2;255;138;104ml\x1b[39m\x1b[38;2;255;140;104mc\x1b[39m\x1b[38;2;255;142;104mo\x1b[39m\x1b[38;2;255;144;104mm\x1b[39m\x1b[38;2;255;146;105me\x1b[39m\x1b[38;2;255;148;105m!\x1b[39m \x1b[38;2;255;150;105m│\x1b[39m");
46-
$this->print("\x1b[38;2;255;152;106m│\x1b[39m \x1b[38;2;255;153;106m│\x1b[39m");
47-
$this->print("\x1b[38;2;255;155;106m╰\x1b[39m\x1b[38;2;255;157;107m─\x1b[39m\x1b[38;2;255;159;107m─\x1b[39m\x1b[38;2;255;161;107m─\x1b[39m\x1b[38;2;255;163;108m─\x1b[39m\x1b[38;2;255;165;108m─\x1b[39m\x1b[38;2;255;166;108m─\x1b[39m\x1b[38;2;255;168;108m─\x1b[39m\x1b[38;2;255;170;109m─\x1b[39m\x1b[38;2;255;172;109m─\x1b[39m\x1b[38;2;255;174;109m─\x1b[39m\x1b[38;2;255;176;110m─\x1b[39m\x1b[38;2;255;177;110m─\x1b[39m\x1b[38;2;255;179;110m─\x1b[39m\x1b[38;2;255;181;111m─\x1b[39m\x1b[38;2;255;183;111m─\x1b[39m\x1b[38;2;255;185;111m─\x1b[39m\x1b[38;2;255;186;111m─\x1b[39m\x1b[38;2;255;188;112m─\x1b[39m\x1b[38;2;255;190;112m─\x1b[39m\x1b[38;2;255;192;112m─\x1b[39m\x1b[38;2;255;193;113m─\x1b[39m\x1b[38;2;255;195;113m╯\x1b[0m");
44+
if (getenv('COLORTERM') === 'truecolor') {
45+
$this->print("\x1b[38;2;255;95;109m╭\x1b[39m\x1b[38;2;255;95;107m─\x1b[39m\x1b[38;2;255;96;106m─\x1b[39m\x1b[38;2;255;96;104m─\x1b[39m\x1b[38;2;255;96;103m─\x1b[39m\x1b[38;2;255;97;101m─\x1b[39m\x1b[38;2;255;97;100m─\x1b[39m\x1b[38;2;255;97;99m─\x1b[39m\x1b[38;2;255;98;97m─\x1b[39m\x1b[38;2;255;100;98m─\x1b[39m\x1b[38;2;255;102;98m─\x1b[39m\x1b[38;2;255;104;98m─\x1b[39m\x1b[38;2;255;106;99m─\x1b[39m\x1b[38;2;255;108;99m─\x1b[39m\x1b[38;2;255;110;99m─\x1b[39m\x1b[38;2;255;112;100m─\x1b[39m\x1b[38;2;255;114;100m─\x1b[39m\x1b[38;2;255;116;100m─\x1b[39m\x1b[38;2;255;118;100m─\x1b[39m\x1b[38;2;255;120;101m─\x1b[39m\x1b[38;2;255;122;101m─\x1b[39m\x1b[38;2;255;124;101m─\x1b[39m\x1b[38;2;255;126;102m╮\x1b[39m");
46+
$this->print("\x1b[38;2;255;128;102m│\x1b[39m \x1b[38;2;255;130;102m│\x1b[39m");
47+
$this->print("\x1b[38;2;255;132;103m│\x1b[39m \x1b[38;2;255;134;103mW\x1b[39m\x1b[38;2;255;136;103me\x1b[39m\x1b[38;2;255;138;104ml\x1b[39m\x1b[38;2;255;140;104mc\x1b[39m\x1b[38;2;255;142;104mo\x1b[39m\x1b[38;2;255;144;104mm\x1b[39m\x1b[38;2;255;146;105me\x1b[39m\x1b[38;2;255;148;105m!\x1b[39m \x1b[38;2;255;150;105m│\x1b[39m");
48+
$this->print("\x1b[38;2;255;152;106m│\x1b[39m \x1b[38;2;255;153;106m│\x1b[39m");
49+
$this->print("\x1b[38;2;255;155;106m╰\x1b[39m\x1b[38;2;255;157;107m─\x1b[39m\x1b[38;2;255;159;107m─\x1b[39m\x1b[38;2;255;161;107m─\x1b[39m\x1b[38;2;255;163;108m─\x1b[39m\x1b[38;2;255;165;108m─\x1b[39m\x1b[38;2;255;166;108m─\x1b[39m\x1b[38;2;255;168;108m─\x1b[39m\x1b[38;2;255;170;109m─\x1b[39m\x1b[38;2;255;172;109m─\x1b[39m\x1b[38;2;255;174;109m─\x1b[39m\x1b[38;2;255;176;110m─\x1b[39m\x1b[38;2;255;177;110m─\x1b[39m\x1b[38;2;255;179;110m─\x1b[39m\x1b[38;2;255;181;111m─\x1b[39m\x1b[38;2;255;183;111m─\x1b[39m\x1b[38;2;255;185;111m─\x1b[39m\x1b[38;2;255;186;111m─\x1b[39m\x1b[38;2;255;188;112m─\x1b[39m\x1b[38;2;255;190;112m─\x1b[39m\x1b[38;2;255;192;112m─\x1b[39m\x1b[38;2;255;193;113m─\x1b[39m\x1b[38;2;255;195;113m╯\x1b[0m");
50+
} else {
51+
$this->print("╭─────────────────────╮");
52+
$this->print("│ │");
53+
$this->print("│ Welcome! │");
54+
$this->print("│ │");
55+
$this->print("╰─────────────────────╯");
56+
}
4857

4958
$money = 100;
59+
60+
if (md5(strval(getenv('MONEY'))) === '5a7c2f336d0cc43b68951e75cdffe333') {
61+
$money += 25;
62+
$this->print('<fg=cyan>You got an extra $25.</>');
63+
} else if (md5(strval(getenv('MONEY'))) === '530029252abcbda4a2a2069036ccc7fc') {
64+
$money += 100;
65+
$this->print('<fg=cyan>You got an extra $100.</>');
66+
} else if (md5(strval(getenv('MONEY'))) === '1aa827a06ecbfa5d6fa7c62ad245f3a3') {
67+
$money = 100000;
68+
}
69+
5070
$hasWatch = true;
5171
$orderWhiskey = false;
5272
$whiskeyLevel = 0;
@@ -111,7 +131,7 @@ protected function execute(Input $input, Output $output): int
111131
}
112132

113133
$this->printHand($playersHand);
114-
$handValue = $this->handValue($playersHand);
134+
$handValue = self::handValue($playersHand);
115135

116136
if ($handValue > 21) {
117137
$this->print("You got <comment>$handValue</comment>.");
@@ -123,18 +143,18 @@ protected function execute(Input $input, Output $output): int
123143
}
124144

125145
$this->printHand($dealersHand);
126-
$this->print("Dealer: " . $this->handValue($dealersHand));
146+
$this->print("Dealer: " . self::handValue($dealersHand));
127147
sleep(1);
128148

129-
while ($this->handValue($dealersHand) <= 17) {
149+
while (self::handValue($dealersHand) <= 17) {
130150
$dealersHand[] = $deal();
131151
$this->printHand($dealersHand);
132-
$this->print("Dealer: " . $this->handValue($dealersHand));
152+
$this->print("Dealer: " . self::handValue($dealersHand));
133153
sleep(1);
134154
}
135155

136-
$d = $this->handValue($dealersHand);
137-
$p = $this->handValue($playersHand);
156+
$d = self::handValue($dealersHand);
157+
$p = self::handValue($playersHand);
138158
$this->print("You got <comment>$p</comment> and dealer <comment>$d</comment>.");
139159

140160
if ($d > 21 || $p > $d) {
@@ -216,7 +236,7 @@ private function newDeck(): array
216236
return $deck;
217237
}
218238

219-
private function handValue(array $hand): int
239+
public static function handValue(array $hand): int
220240
{
221241
$aces = 0;
222242
$value = 0;
@@ -257,14 +277,21 @@ private function handValue(array $hand): int
257277
break;
258278
}
259279
}
280+
$variants = [$value];
260281
while ($aces-- > 0) {
261-
if ($value + 11 <= 21) {
262-
$value += 11;
282+
$variants = array_flatten(array_map(function ($v) {
283+
return [$v + 1, $v + 11];
284+
}, $variants));
285+
}
286+
$sum = $variants[0];
287+
for ($i = 1; $i < count($variants); $i++) {
288+
if ($variants[$i] <= 21) {
289+
$sum = $variants[$i];
263290
} else {
264-
$value += 1;
291+
break;
265292
}
266293
}
267-
return $value;
294+
return $sum;
268295
}
269296

270297
private function print(string $text = "")
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
use Deployer\Command\BlackjackCommand;
4+
use PHPUnit\Framework\TestCase;
5+
6+
class BlackjackCommandTest extends TestCase
7+
{
8+
9+
public function testHandValue()
10+
{
11+
self::assertEquals(3, BlackjackCommand::handValue([['3']]));
12+
self::assertEquals(10, BlackjackCommand::handValue([['3'], ['7']]));
13+
self::assertEquals(12, BlackjackCommand::handValue([['A'], ['A'], ['3'], ['7']]));
14+
self::assertEquals(12, BlackjackCommand::handValue([['A'], ['A']]));
15+
self::assertEquals(18, BlackjackCommand::handValue([['A'], ['7']]));
16+
self::assertEquals(21, BlackjackCommand::handValue([['A'], ['3'], ['7']]));
17+
self::assertEquals(21, BlackjackCommand::handValue([['A'], ['Q']]));
18+
self::assertEquals(22, BlackjackCommand::handValue([['A'], ['Q'], ['A'], ['Q']]));
19+
}
20+
}

0 commit comments

Comments
 (0)