1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Controller; |
4
|
|
|
|
5
|
|
|
use App\Game\BlackJack; |
6
|
|
|
use App\Game\BlackJack\Player; |
7
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
8
|
|
|
use Symfony\Component\HttpFoundation\Request; |
9
|
|
|
use Symfony\Component\HttpFoundation\Response; |
10
|
|
|
use Symfony\Component\HttpFoundation\Session\SessionInterface; |
11
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
12
|
|
|
|
13
|
|
|
class ProjectController extends AbstractController |
14
|
|
|
{ |
15
|
1 |
|
#[Route('/proj', name: 'proj_start')] |
16
|
|
|
public function home(): Response |
17
|
|
|
{ |
18
|
1 |
|
return $this->render('proj/home.html.twig'); |
19
|
|
|
} |
20
|
|
|
|
21
|
1 |
|
#[Route('/proj/about', name: 'proj_doc')] |
22
|
|
|
public function about(): Response |
23
|
|
|
{ |
24
|
1 |
|
return $this->render('proj/doc.html.twig'); |
25
|
|
|
} |
26
|
|
|
|
27
|
3 |
|
#[Route('/proj/player/init/{num<\d+>}', name: 'proj_player_init')] |
28
|
|
|
public function playerInit( |
29
|
|
|
int $num, |
30
|
|
|
): Response { |
31
|
3 |
|
if ($num > BlackJack::MAX_PLAYERS) { |
32
|
1 |
|
throw new \RuntimeException("Can't have more then ".BlackJack::MAX_PLAYERS.'players!'); |
33
|
|
|
} |
34
|
2 |
|
if ($num < 1) { |
35
|
1 |
|
throw new \RuntimeException("Can't have less than 1 player!"); |
36
|
|
|
} |
37
|
|
|
|
38
|
1 |
|
$data = [ |
39
|
1 |
|
'numOfPlayers' => $num, |
40
|
1 |
|
'players' => [], |
41
|
1 |
|
]; |
42
|
|
|
|
43
|
|
|
// Create default name |
44
|
1 |
|
for ($i = 1; $i <= $num; ++$i) { |
45
|
1 |
|
$data['players'][] = "Player $i"; |
46
|
|
|
} |
47
|
|
|
|
48
|
1 |
|
return $this->render('proj/player_creation.html.twig', $data); |
49
|
|
|
} |
50
|
|
|
|
51
|
1 |
|
#[Route('/proj/blackjack/init', name: 'proj_BlackJack_init', methods: ['POST'])] |
52
|
|
|
public function blackJackInit( |
53
|
|
|
SessionInterface $session, |
54
|
|
|
Request $request, |
55
|
|
|
): Response { |
56
|
|
|
/** @var BlackJack $blackJack */ |
57
|
1 |
|
$blackJack = new BlackJack(); |
58
|
|
|
|
59
|
|
|
/** @var int $numOfPlayers */ |
60
|
1 |
|
$numOfPlayers = $request->request->get('numOfPlayers'); |
61
|
|
|
|
62
|
|
|
// Get the names of the players |
63
|
1 |
|
$players = []; |
64
|
|
|
|
65
|
1 |
|
for ($i = 1; $i <= $numOfPlayers; ++$i) { |
66
|
|
|
/** @var string|null $playerName */ |
67
|
1 |
|
$playerName = $request->request->get("Player_$i"); |
68
|
1 |
|
if (null === $playerName || '' === trim($playerName)) { |
69
|
1 |
|
$playerName = "Player $i"; // default name if blank |
70
|
|
|
} |
71
|
1 |
|
$players[] = new Player($playerName); |
72
|
|
|
} |
73
|
|
|
|
74
|
1 |
|
$blackJack->setPlayers($players); |
75
|
|
|
|
76
|
|
|
// Save to session |
77
|
1 |
|
$session->set('proj_black_jack', $blackJack); |
78
|
1 |
|
$session->set('proj_newGame', true); |
79
|
1 |
|
$session->set('proj_playerTurn', 0); |
80
|
|
|
|
81
|
1 |
|
return $this->redirectToRoute('proj_BlackJack_game'); |
82
|
|
|
} |
83
|
|
|
|
84
|
2 |
|
#[Route('/proj/blackjack/game', name: 'proj_BlackJack_game', methods: ['GET'])] |
85
|
|
|
public function blackJack( |
86
|
|
|
SessionInterface $session, |
87
|
|
|
): Response { |
88
|
|
|
/** @var BlackJack|null $blackJack */ |
89
|
2 |
|
$blackJack = $session->get('proj_black_jack'); |
90
|
|
|
/** @var bool|null $newGame */ |
91
|
2 |
|
$newGame = $session->get('proj_newGame'); |
92
|
|
|
/** @var int|null $playerTurn */ |
93
|
2 |
|
$playerTurn = $session->get('proj_playerTurn'); |
94
|
|
|
|
95
|
|
|
// redirect if blackJack or newGame or playerTurn is not in session |
96
|
2 |
|
if (null === $blackJack || null === $newGame || null === $playerTurn) { |
97
|
1 |
|
$this->addFlash( |
98
|
1 |
|
'warning', |
99
|
1 |
|
'A Black Jack game is not setup, choose a number of players to start!' |
100
|
1 |
|
); |
101
|
|
|
|
102
|
1 |
|
return $this->redirectToRoute('proj_start'); |
103
|
|
|
} |
104
|
|
|
|
105
|
1 |
|
$data = $blackJack->stateOfGame(); |
106
|
|
|
|
107
|
|
|
// If it is any of the players turn |
108
|
1 |
|
if ($playerTurn < $data['numOfPlayers']) { |
109
|
|
|
// If the player is broke skip them |
110
|
1 |
|
if (true === $blackJack->isPlayerBroke($playerTurn)) { |
111
|
|
|
++$playerTurn; |
112
|
|
|
$session->set('proj_playerTurn', $playerTurn); |
113
|
|
|
|
114
|
|
|
return $this->redirectToRoute('proj_BlackJack_game'); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
// Add data |
119
|
1 |
|
$data['newGame'] = $newGame; |
120
|
1 |
|
$data['playerTurn'] = $playerTurn; |
121
|
|
|
|
122
|
1 |
|
return $this->render('proj/black_jack.html.twig', $data); |
123
|
|
|
} |
124
|
|
|
|
125
|
2 |
|
#[Route('/proj/blackjack/bet', name: 'proj_BlackJack_Bet', methods: ['POST'])] |
126
|
|
|
public function blackJacBet( |
127
|
|
|
SessionInterface $session, |
128
|
|
|
Request $request, |
129
|
|
|
): Response { |
130
|
|
|
/** @var BlackJack|null $blackJack */ |
131
|
2 |
|
$blackJack = $session->get('proj_black_jack'); |
132
|
|
|
/** @var bool|null $newGame */ |
133
|
2 |
|
$newGame = $session->get('proj_newGame'); |
134
|
|
|
|
135
|
|
|
// redirect if blackJack or newGame is not in session |
136
|
2 |
|
if (null === $blackJack || null === $newGame) { |
137
|
1 |
|
$this->addFlash( |
138
|
1 |
|
'warning', |
139
|
1 |
|
'A Black Jack game is not setup, choose a number of players to start!' |
140
|
1 |
|
); |
141
|
|
|
|
142
|
1 |
|
return $this->redirectToRoute('proj_start'); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** @var int $numOfPlayers */ |
146
|
1 |
|
$numOfPlayers = $request->request->get('numOfPlayers'); |
147
|
|
|
|
148
|
1 |
|
$bets = []; |
149
|
|
|
|
150
|
1 |
|
for ($i = 0; $i < $numOfPlayers; ++$i) { |
151
|
|
|
/** @var int|null $bet */ |
152
|
|
|
$bet = $request->request->get("$i"); |
153
|
|
|
if (null !== $bet) { |
154
|
|
|
$bets[$i] = $bet; |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
158
|
1 |
|
$blackJack->newGame($bets); |
159
|
|
|
|
160
|
|
|
// Save to session |
161
|
1 |
|
$session->set('proj_black_jack', $blackJack); |
162
|
1 |
|
$session->set('proj_newGame', false); |
163
|
|
|
|
164
|
1 |
|
return $this->redirectToRoute('proj_BlackJack_game'); |
165
|
|
|
} |
166
|
|
|
|
167
|
2 |
|
#[Route('/proj/blackjack/stay', name: 'proj_BlackJack_Stay', methods: ['POST'])] |
168
|
|
|
public function blackJackStay( |
169
|
|
|
SessionInterface $session, |
170
|
|
|
): Response { |
171
|
|
|
/** @var BlackJack|null $blackJack */ |
172
|
2 |
|
$blackJack = $session->get('proj_black_jack'); |
173
|
|
|
/** @var int|null $playerTurn */ |
174
|
2 |
|
$playerTurn = $session->get('proj_playerTurn'); |
175
|
|
|
|
176
|
|
|
// redirect if blackJack or playerTurn is not in session |
177
|
2 |
|
if (null === $blackJack || null === $playerTurn) { |
178
|
1 |
|
$this->addFlash( |
179
|
1 |
|
'warning', |
180
|
1 |
|
'A Black Jack game is not setup, choose a number of players to start!' |
181
|
1 |
|
); |
182
|
|
|
|
183
|
1 |
|
return $this->redirectToRoute('proj_start'); |
184
|
|
|
} |
185
|
|
|
|
186
|
1 |
|
$blackJack->stayPlayer($playerTurn); |
187
|
1 |
|
++$playerTurn; |
188
|
|
|
|
189
|
|
|
// Save to session |
190
|
1 |
|
$session->set('black_jack', $blackJack); |
191
|
1 |
|
$session->set('proj_playerTurn', $playerTurn); |
192
|
|
|
|
193
|
1 |
|
return $this->redirectToRoute('proj_BlackJack_game'); |
194
|
|
|
} |
195
|
|
|
|
196
|
2 |
|
#[Route('/proj/blackjack/hit', name: 'proj_BlackJack_Hit', methods: ['POST'])] |
197
|
|
|
public function blackJackHit( |
198
|
|
|
SessionInterface $session, |
199
|
|
|
): Response { |
200
|
|
|
/** @var BlackJack|null $blackJack */ |
201
|
2 |
|
$blackJack = $session->get('proj_black_jack'); |
202
|
|
|
/** @var int|null $playerTurn */ |
203
|
2 |
|
$playerTurn = $session->get('proj_playerTurn'); |
204
|
|
|
|
205
|
|
|
// redirect if blackJack or playerTurn is not in session |
206
|
2 |
|
if (null === $blackJack || null === $playerTurn) { |
207
|
1 |
|
$this->addFlash( |
208
|
1 |
|
'warning', |
209
|
1 |
|
'A Black Jack game is not setup, choose a number of players to start!' |
210
|
1 |
|
); |
211
|
|
|
|
212
|
1 |
|
return $this->redirectToRoute('proj_BlackJack_game'); |
213
|
|
|
} |
214
|
|
|
|
215
|
1 |
|
$blackJack->hitPlayer($playerTurn); |
216
|
|
|
|
217
|
|
|
// If bust then next players turn |
218
|
1 |
|
if (true === $blackJack->isPlayerBust($playerTurn)) { |
219
|
|
|
++$playerTurn; |
220
|
|
|
$session->set('proj_playerTurn', $playerTurn); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
// Save to session |
224
|
1 |
|
$session->set('black_jack', $blackJack); |
225
|
|
|
|
226
|
1 |
|
return $this->redirectToRoute('proj_BlackJack_game'); |
227
|
|
|
} |
228
|
|
|
|
229
|
2 |
|
#[Route('/proj/blackjack/double_down', name: 'proj_BlackJack_DoubleDown', methods: ['POST'])] |
230
|
|
|
public function blackJackDoubleDown( |
231
|
|
|
SessionInterface $session, |
232
|
|
|
): Response { |
233
|
|
|
/** @var BlackJack|null $blackJack */ |
234
|
2 |
|
$blackJack = $session->get('proj_black_jack'); |
235
|
|
|
/** @var int|null $playerTurn */ |
236
|
2 |
|
$playerTurn = $session->get('proj_playerTurn'); |
237
|
|
|
|
238
|
|
|
// redirect if blackJack or playerTurn is not in session |
239
|
2 |
|
if (null === $blackJack || null === $playerTurn) { |
240
|
1 |
|
$this->addFlash( |
241
|
1 |
|
'warning', |
242
|
1 |
|
'A Black Jack game is not setup, choose a number of players to start!' |
243
|
1 |
|
); |
244
|
|
|
|
245
|
1 |
|
return $this->redirectToRoute('proj_start'); |
246
|
|
|
} |
247
|
|
|
|
248
|
1 |
|
$blackJack->doubleDownPlayer($playerTurn); |
249
|
1 |
|
++$playerTurn; |
250
|
|
|
|
251
|
|
|
// Save to session |
252
|
1 |
|
$session->set('black_jack', $blackJack); |
253
|
1 |
|
$session->set('proj_playerTurn', $playerTurn); |
254
|
|
|
|
255
|
1 |
|
return $this->redirectToRoute('proj_BlackJack_game'); |
256
|
|
|
} |
257
|
|
|
|
258
|
1 |
|
#[Route('/proj/blackjack/newGame', name: 'proj_BlackJack_NewGame', methods: ['POST'])] |
259
|
|
|
public function blackJackNewGame( |
260
|
|
|
SessionInterface $session, |
261
|
|
|
): Response { |
262
|
|
|
// Save to session |
263
|
1 |
|
$session->set('proj_newGame', true); |
264
|
1 |
|
$session->set('proj_playerTurn', 0); |
265
|
|
|
|
266
|
1 |
|
return $this->redirectToRoute('proj_BlackJack_game'); |
267
|
|
|
} |
268
|
|
|
} |
269
|
|
|
|