BlackjackControllerProj::play()   B
last analyzed

Complexity

Conditions 6
Paths 7

Size

Total Lines 40
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 28
c 1
b 0
f 0
nc 7
nop 1
dl 0
loc 40
ccs 0
cts 28
cp 0
crap 42
rs 8.8497
1
<?php
2
3
namespace App\Controller;
4
5
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
6
use Symfony\Component\HttpFoundation\Response;
7
use Symfony\Component\HttpFoundation\Request;
8
use Symfony\Component\HttpFoundation\Session\SessionInterface;
9
use Symfony\Component\Routing\Annotation\Route;
10
use App\GameProj\BlackjackGameProj;
11
12
class BlackjackControllerProj extends AbstractController
13
{
14
    #[Route("/proj/doc", name: "about_proj")]
15
    public function doc(): Response
16
    {
17
        return $this->render('gameproj/doc.html.twig');
18
    }
19
20
    #[Route("/proj", name: "proj")]
21
    public function home(SessionInterface $session): Response
22
    {
23
        $playerBank = $session->get('player_bank', 1000.00);
24
        $playerName = $session->get('player_name');
25
26
        if (!$playerName) {
27
            return $this->render('gameproj/set_name.html.twig');
28
        }
29
30
        return $this->render('gameproj/home.html.twig', [
31
            'playerBank' => $playerBank,
32
            'playerName' => $playerName
33
        ]);
34
    }
35
36
    #[Route("/proj/set-name", name: "set_name", methods: ["POST"])]
37
    public function setName(Request $request, SessionInterface $session): Response
38
    {
39
        $playerName = $request->request->get('player_name');
40
        if ($playerName) {
41
            $session->set('player_name', $playerName);
42
        }
43
        return $this->redirectToRoute('proj');
44
    }
45
46
    #[Route("/proj/start", name: "start_game_proj")]
47
    public function startGame(Request $request, SessionInterface $session): Response
48
    {
49
        $playerName = $session->get('player_name', 'Player');
50
        $playerBank = $session->get('player_bank', 1000.00);
51
        $numberOfHands = (int)$request->request->get('hands', 1);
52
        $totalBet = (float)$request->request->get('bet', 10.00);
53
        $includeComputer = $request->request->has('include_computer');
54
        $computerStrategy = $request->request->get('computer_strategy', 'dumb');
55
56
        $game = new BlackjackGameProj($playerName, $playerBank, $numberOfHands, $totalBet, $includeComputer ? $numberOfHands - 1 : null, $computerStrategy);
57
        $game->dealInitialCards();
58
        $game->checkForBlackjack();
59
        $session->set('game', $game);
60
61
        return $this->redirectToRoute('play_proj');
62
    }
63
64
    #[Route("/proj/play", name: "play_proj")]
65
    public function play(SessionInterface $session): Response
66
    {
67
        if (!$session->has('game')) {
68
            return $this->redirectToRoute('proj');
69
        }
70
71
        $game = $session->get('game');
72
73
        $currentHand = $game->getCurrentHand();
74
        if ($currentHand !== null && $game->isComputerHand($currentHand)) {
75
            $game->playComputerHand();
76
            $game->nextHand();
77
        }
78
79
        if ($game->isGameOver()) {
80
            $game->finishGame();
81
            $session->set('game', $game);
82
            return $this->redirectToRoute('game_result_proj');
83
        }
84
85
        $hands = $game->getHands();
86
        $bustProbabilities = [];
87
        $handValues = [];
88
89
        foreach ($hands as $index => $hand) {
90
            $bustProbabilities[$index] = $game->getBustProbability($index);
91
            $handValues[$index] = $game->getHandValue($hand['hand']);
92
        }
93
94
        return $this->render('gameproj/play.html.twig', [
95
            'playerName' => $game->getPlayerName(),
96
            'hands' => $hands,
97
            'dealerHand' => $game->getDealerHand(),
98
            'bustProbabilities' => $bustProbabilities,
99
            'handValues' => $handValues,
100
            'dealerValue' => $game->getHandValue($game->getDealerHand()),
101
            'currentHand' => $game->getCurrentHand(),
102
            'playerBank' => $game->getPlayerBank(),
103
            'isComputerHand' => array_map([$game, 'isComputerHand'], array_keys($hands)),
104
        ]);
105
    }
106
107
    #[Route("/proj/play/hit", name: "game_hit_proj")]
108
    public function hit(SessionInterface $session): Response
109
    {
110
        $game = $session->get('game');
111
        $currentHand = $game->getCurrentHand();
112
        $game->hitHand($currentHand);
113
114
        if ($game->isHandBusted($currentHand)) {
115
            $game->nextHand();
116
        }
117
118
        if ($game->isGameOver()) {
119
            $game->finishGame();
120
            $session->set('game', $game);
121
            return $this->redirectToRoute('game_result_proj');
122
        }
123
124
        $session->set('game', $game);
125
        return $this->redirectToRoute('play_proj');
126
    }
127
128
    #[Route("/proj/play/stand", name: "game_stand_proj")]
129
    public function stand(SessionInterface $session): Response
130
    {
131
        $game = $session->get('game');
132
        $game->standHand($game->getCurrentHand());
133
        $game->nextHand();
134
135
        if ($game->isGameOver()) {
136
            $game->finishGame();
137
            $session->set('game', $game);
138
            return $this->redirectToRoute('game_result_proj');
139
        }
140
141
        $session->set('game', $game);
142
        return $this->redirectToRoute('play_proj');
143
    }
144
145
    #[Route("/proj/play/result", name: "game_result_proj")]
146
    public function result(SessionInterface $session): Response
147
    {
148
        $game = $session->get('game');
149
150
        $hands = $game->getHands();
151
        $handValues = [];
152
153
        foreach ($hands as $index => $hand) {
154
            $handValues[$index] = $game->getHandValue($hand['hand']);
155
        }
156
157
        return $this->render('gameproj/result.html.twig', [
158
            'playerName' => $game->getPlayerName(),
159
            'hands' => $hands,
160
            'dealerHand' => $game->getDealerHand(),
161
            'handValues' => $handValues,
162
            'dealerValue' => $game->getHandValue($game->getDealerHand()),
163
            'playerBank' => $game->getPlayerBank(),
164
            'isComputerHand' => array_map([$game, 'isComputerHand'], array_keys($hands)),
165
        ]);
166
    }
167
168
    #[Route("/proj/play/play_again", name: "game_play_again_proj")]
169
    public function playAgain(SessionInterface $session): Response
170
    {
171
        $game = $session->get('game');
172
        $session->set('player_bank', $game->getPlayerBank());
173
        $session->remove('game');
174
        return $this->redirectToRoute('proj');
175
    }
176
}
177