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
|
|
|
$bet = BlackJack::MINIMUM_BET; // default bet if blank |
155
|
|
|
} |
156
|
|
|
$bets[] = $bet; |
157
|
|
|
} |
158
|
|
|
|
159
|
1 |
|
$blackJack->newGame($bets); |
160
|
|
|
|
161
|
|
|
// Save to session |
162
|
1 |
|
$session->set('proj_black_jack', $blackJack); |
163
|
1 |
|
$session->set('proj_newGame', false); |
164
|
|
|
|
165
|
1 |
|
return $this->redirectToRoute('proj_BlackJack_game'); |
166
|
|
|
} |
167
|
|
|
|
168
|
2 |
|
#[Route('/proj/blackjack/stay', name: 'proj_BlackJack_Stay', methods: ['POST'])] |
169
|
|
|
public function blackJackStay( |
170
|
|
|
SessionInterface $session, |
171
|
|
|
): Response { |
172
|
|
|
/** @var BlackJack|null $blackJack */ |
173
|
2 |
|
$blackJack = $session->get('proj_black_jack'); |
174
|
|
|
/** @var int|null $playerTurn */ |
175
|
2 |
|
$playerTurn = $session->get('proj_playerTurn'); |
176
|
|
|
|
177
|
|
|
// redirect if blackJack or playerTurn is not in session |
178
|
2 |
|
if (null === $blackJack || null === $playerTurn) { |
179
|
1 |
|
$this->addFlash( |
180
|
1 |
|
'warning', |
181
|
1 |
|
'A Black Jack game is not setup, choose a number of players to start!' |
182
|
1 |
|
); |
183
|
|
|
|
184
|
1 |
|
return $this->redirectToRoute('proj_start'); |
185
|
|
|
} |
186
|
|
|
|
187
|
1 |
|
$blackJack->stayPlayer($playerTurn); |
188
|
1 |
|
++$playerTurn; |
189
|
|
|
|
190
|
|
|
// Save to session |
191
|
1 |
|
$session->set('black_jack', $blackJack); |
192
|
1 |
|
$session->set('proj_playerTurn', $playerTurn); |
193
|
|
|
|
194
|
1 |
|
return $this->redirectToRoute('proj_BlackJack_game'); |
195
|
|
|
} |
196
|
|
|
|
197
|
2 |
|
#[Route('/proj/blackjack/hit', name: 'proj_BlackJack_Hit', methods: ['POST'])] |
198
|
|
|
public function blackJackHit( |
199
|
|
|
SessionInterface $session, |
200
|
|
|
): Response { |
201
|
|
|
/** @var BlackJack|null $blackJack */ |
202
|
2 |
|
$blackJack = $session->get('proj_black_jack'); |
203
|
|
|
/** @var int|null $playerTurn */ |
204
|
2 |
|
$playerTurn = $session->get('proj_playerTurn'); |
205
|
|
|
|
206
|
|
|
// redirect if blackJack or playerTurn is not in session |
207
|
2 |
|
if (null === $blackJack || null === $playerTurn) { |
208
|
1 |
|
$this->addFlash( |
209
|
1 |
|
'warning', |
210
|
1 |
|
'A Black Jack game is not setup, choose a number of players to start!' |
211
|
1 |
|
); |
212
|
|
|
|
213
|
1 |
|
return $this->redirectToRoute('proj_BlackJack_game'); |
214
|
|
|
} |
215
|
|
|
|
216
|
1 |
|
$blackJack->hitPlayer($playerTurn); |
217
|
|
|
|
218
|
|
|
// If bust then next players turn |
219
|
1 |
|
if (true === $blackJack->isPlayerBust($playerTurn)) { |
220
|
|
|
++$playerTurn; |
221
|
|
|
$session->set('proj_playerTurn', $playerTurn); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
// Save to session |
225
|
1 |
|
$session->set('black_jack', $blackJack); |
226
|
|
|
|
227
|
1 |
|
return $this->redirectToRoute('proj_BlackJack_game'); |
228
|
|
|
} |
229
|
|
|
|
230
|
2 |
|
#[Route('/proj/blackjack/double_down', name: 'proj_BlackJack_DoubleDown', methods: ['POST'])] |
231
|
|
|
public function blackJackDoubleDown( |
232
|
|
|
SessionInterface $session, |
233
|
|
|
): Response { |
234
|
|
|
/** @var BlackJack|null $blackJack */ |
235
|
2 |
|
$blackJack = $session->get('proj_black_jack'); |
236
|
|
|
/** @var int|null $playerTurn */ |
237
|
2 |
|
$playerTurn = $session->get('proj_playerTurn'); |
238
|
|
|
|
239
|
|
|
// redirect if blackJack or playerTurn is not in session |
240
|
2 |
|
if (null === $blackJack || null === $playerTurn) { |
241
|
1 |
|
$this->addFlash( |
242
|
1 |
|
'warning', |
243
|
1 |
|
'A Black Jack game is not setup, choose a number of players to start!' |
244
|
1 |
|
); |
245
|
|
|
|
246
|
1 |
|
return $this->redirectToRoute('proj_start'); |
247
|
|
|
} |
248
|
|
|
|
249
|
1 |
|
$blackJack->doubleDownPlayer($playerTurn); |
250
|
1 |
|
++$playerTurn; |
251
|
|
|
|
252
|
|
|
// Save to session |
253
|
1 |
|
$session->set('black_jack', $blackJack); |
254
|
1 |
|
$session->set('proj_playerTurn', $playerTurn); |
255
|
|
|
|
256
|
1 |
|
return $this->redirectToRoute('proj_BlackJack_game'); |
257
|
|
|
} |
258
|
|
|
|
259
|
1 |
|
#[Route('/proj/blackjack/newGame', name: 'proj_BlackJack_NewGame', methods: ['POST'])] |
260
|
|
|
public function blackJackNewGame( |
261
|
|
|
SessionInterface $session, |
262
|
|
|
): Response { |
263
|
|
|
// Save to session |
264
|
1 |
|
$session->set('proj_newGame', true); |
265
|
1 |
|
$session->set('proj_playerTurn', 0); |
266
|
|
|
|
267
|
1 |
|
return $this->redirectToRoute('proj_BlackJack_game'); |
268
|
|
|
} |
269
|
|
|
} |
270
|
|
|
|