Total Complexity | 63 |
Total Lines | 367 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like BlackjackCommand often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use BlackjackCommand, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
21 | class BlackjackCommand extends Command |
||
22 | { |
||
23 | use CommandCommon; |
||
24 | |||
25 | /** |
||
26 | * @var Input |
||
27 | */ |
||
28 | private $input; |
||
29 | |||
30 | /** |
||
31 | * @var Output |
||
32 | */ |
||
33 | private $output; |
||
34 | |||
35 | public function __construct() |
||
36 | { |
||
37 | parent::__construct('blackjack'); |
||
38 | $this->setDescription('Play blackjack'); |
||
39 | } |
||
40 | |||
41 | protected function execute(Input $input, Output $output): int |
||
42 | { |
||
43 | $this->input = $input; |
||
44 | $this->output = $output; |
||
45 | $this->telemetry(); |
||
46 | $io = new SymfonyStyle($this->input, $this->output); |
||
47 | |||
48 | if (getenv('COLORTERM') === 'truecolor') { |
||
49 | $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"); |
||
50 | $this->print("\x1b[38;2;255;128;102m│\x1b[39m \x1b[38;2;255;130;102m│\x1b[39m"); |
||
51 | $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"); |
||
52 | $this->print("\x1b[38;2;255;152;106m│\x1b[39m \x1b[38;2;255;153;106m│\x1b[39m"); |
||
53 | $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"); |
||
54 | } else { |
||
55 | $this->print("╭─────────────────────╮"); |
||
56 | $this->print("│ │"); |
||
57 | $this->print("│ Welcome! │"); |
||
58 | $this->print("│ │"); |
||
59 | $this->print("╰─────────────────────╯"); |
||
60 | } |
||
61 | |||
62 | $money = 100; |
||
63 | |||
64 | if (md5(strval(getenv('MONEY'))) === '5a7c2f336d0cc43b68951e75cdffe333') { |
||
65 | $money += 25; |
||
66 | $this->print('<fg=cyan>You got an extra $25.</>'); |
||
67 | } elseif (md5(strval(getenv('MONEY'))) === '530029252abcbda4a2a2069036ccc7fc') { |
||
68 | $money += 100; |
||
69 | $this->print('<fg=cyan>You got an extra $100.</>'); |
||
70 | } elseif (md5(strval(getenv('MONEY'))) === '1aa827a06ecbfa5d6fa7c62ad245f3a3') { |
||
71 | $money = 100000; |
||
72 | } |
||
73 | |||
74 | $hasWatch = true; |
||
75 | $orderWhiskey = false; |
||
76 | $whiskeyLevel = 0; |
||
77 | |||
78 | $deck = $this->newDeck(); |
||
79 | $graveyard = []; |
||
80 | $dealersHand = []; |
||
81 | $playersHand = []; |
||
82 | shuffle($deck); |
||
83 | $deal = function () use (&$deck, &$graveyard) { |
||
84 | if (count($deck) == 0) { |
||
85 | shuffle($graveyard); |
||
86 | $deck = $graveyard; |
||
87 | $graveyard = []; |
||
88 | } |
||
89 | return array_pop($deck); |
||
90 | }; |
||
91 | |||
92 | start: |
||
93 | $this->print("You have <info>$</info><info>$money</info>."); |
||
94 | if ($money > 0) { |
||
95 | $bet = (int) $io->ask('Your bet', '5'); |
||
96 | if ($bet <= 0) { |
||
97 | goto start; |
||
98 | } |
||
99 | if ($bet > $money) { |
||
100 | goto start; |
||
101 | } |
||
102 | } elseif ($hasWatch) { // @phpstan-ignore-line |
||
|
|||
103 | $answer = $io->askQuestion(new ChoiceQuestion('?', ['leave', '- Here, take my watch! [$25]'], 0)); |
||
104 | if ($answer == 'leave') { |
||
105 | goto leave; |
||
106 | } else { |
||
107 | $hasWatch = false; |
||
108 | $money = 25; |
||
109 | $bet = 25; |
||
110 | } |
||
111 | } else { |
||
112 | goto leave; |
||
113 | } |
||
114 | |||
115 | $graveyard = array_merge($graveyard, $dealersHand); |
||
116 | $dealersHand = []; |
||
117 | $dealersHand[] = $deal(); |
||
118 | $this->print("Dealers hand:"); |
||
119 | $this->printHand($dealersHand); |
||
120 | |||
121 | $graveyard = array_merge($graveyard, $playersHand); |
||
122 | $playersHand = []; |
||
123 | $playersHand[] = $deal(); |
||
124 | $playersHand[] = $deal(); |
||
125 | $this->print("Your hand:"); |
||
126 | $this->printHand($playersHand, 2); |
||
127 | |||
128 | while (true) { |
||
129 | $question = new ChoiceQuestion('Your turn', ['hit', 'stand'], 0); |
||
130 | $answer = $io->askQuestion($question); |
||
131 | |||
132 | if ($answer === 'hit') { |
||
133 | $playersHand[] = $deal(); |
||
134 | usleep(200000); |
||
135 | } |
||
136 | |||
137 | if ($answer === 'stand') { |
||
138 | break; |
||
139 | } |
||
140 | |||
141 | $this->printHand($playersHand); |
||
142 | $handValue = self::handValue($playersHand); |
||
143 | |||
144 | if ($handValue > 21) { |
||
145 | $this->print("You got <comment>$handValue</comment>."); |
||
146 | $this->print("<fg=cyan>Bust!</>"); |
||
147 | $this->print("-<info>$</info><info>$bet</info>"); |
||
148 | $money -= $bet; |
||
149 | goto nextRound; |
||
150 | } |
||
151 | } |
||
152 | |||
153 | $this->printHand($dealersHand); |
||
154 | $this->print("Dealer: " . self::handValue($dealersHand)); |
||
155 | sleep(1); |
||
156 | |||
157 | while (self::handValue($dealersHand) <= 17) { |
||
158 | $dealersHand[] = $deal(); |
||
159 | $this->printHand($dealersHand); |
||
160 | $this->print("Dealer: " . self::handValue($dealersHand)); |
||
161 | sleep(1); |
||
162 | } |
||
163 | |||
164 | $d = self::handValue($dealersHand); |
||
165 | $p = self::handValue($playersHand); |
||
166 | $this->print("You got <comment>$p</comment> and dealer <comment>$d</comment>."); |
||
167 | |||
168 | if ($d > 21 || $p > $d) { |
||
169 | $this->print("<fg=cyan>You won!</>"); |
||
170 | $this->print("+<info>$</info><info>$bet</info>"); |
||
171 | $money += $bet; |
||
172 | } elseif ($p < $d) { |
||
173 | $this->print("<fg=cyan>You lose!</>"); |
||
174 | $this->print("-<info>$</info><info>$bet</info>"); |
||
175 | $money -= $bet; |
||
176 | } else { |
||
177 | $this->print("<fg=cyan>Push!</>"); |
||
178 | } |
||
179 | |||
180 | nextRound: |
||
181 | $choices = ['continue', 'leave']; |
||
182 | if ($orderWhiskey) { |
||
183 | $orderWhiskey = false; |
||
184 | $whiskeyLevel = 4; |
||
185 | $this->print(); |
||
186 | $this->print('The waitress brought whiskey and says:'); |
||
187 | $this->print(' - Your whiskey, sir.'); |
||
188 | if ($money >= 5) { |
||
189 | array_push($choices, 'tip the waitress [$5]'); |
||
190 | } |
||
191 | } elseif ($money >= 5) { |
||
192 | array_push($choices, 'order whiskey [$5]'); |
||
193 | } |
||
194 | |||
195 | if ($whiskeyLevel > 0) { |
||
196 | $this->printWhiskey($whiskeyLevel); |
||
197 | $whiskeyLevel--; |
||
198 | } |
||
199 | $answer = $io->askQuestion(new ChoiceQuestion('?', $choices, 0)); |
||
200 | |||
201 | if ($answer == 'leave') { |
||
202 | goto leave; |
||
203 | } elseif ($money >= 5 && $answer == 'order whiskey [$5]') { |
||
204 | $orderWhiskey = true; |
||
205 | $this->print('You say:'); |
||
206 | $this->print(' - Whiskey, please.'); |
||
207 | $money -= 5; |
||
208 | } elseif ($money >= 5 && $answer == 'tip the waitress [$5]') { |
||
209 | $this->print('The waitress says:'); |
||
210 | $this->print(' - Thank you, sir!'); |
||
211 | $money -= 5; |
||
212 | } |
||
213 | $this->print(); |
||
214 | $this->print("=====> Next round <====="); |
||
215 | goto start; |
||
216 | |||
217 | leave: |
||
218 | if ($money >= 5) { |
||
219 | $answer = $io->ask('Leave a $5 tip to the dealer?', 'yes'); |
||
220 | if ($answer === 'yes') { |
||
221 | $this->print("You can leave a tip here:"); |
||
222 | $this->print(); |
||
223 | $this->print("- https://github.com/sponsors/antonmedv"); |
||
224 | $this->print("- https://paypal.me/antonmedv"); |
||
225 | $this->print(); |
||
226 | } |
||
227 | } |
||
228 | $this->print('Thanks for playing, Come again!'); |
||
229 | return 0; |
||
230 | } |
||
231 | |||
232 | private function newDeck(): array |
||
233 | { |
||
234 | $deck = []; |
||
235 | foreach (['♠', '♣', '♥', '♦'] as $suit) { |
||
236 | for ($i = 2; $i <= 10; $i++) { |
||
237 | $deck[] = [strval($i), $suit]; |
||
238 | } |
||
239 | $deck[] = ['J', $suit]; |
||
240 | $deck[] = ['Q', $suit]; |
||
241 | $deck[] = ['K', $suit]; |
||
242 | $deck[] = ['A', $suit]; |
||
243 | } |
||
244 | return $deck; |
||
245 | } |
||
246 | |||
247 | public static function handValue(array $hand): int |
||
303 | } |
||
304 | |||
305 | private function print(string $text = "") |
||
308 | } |
||
309 | |||
310 | private function printHand(array $hand, int $offset = 1) |
||
345 | } |
||
346 | } |
||
347 | |||
348 | private function printWhiskey(int $whiskeyLevel) |
||
388 | |||
389 | | | |
||
390 | | | |
||
391 | | | |
||
400 |