Passed
Push — main ( 8bc290...60b955 )
by Peter
04:33
created

BlackjackControllerProj::doc()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 4
ccs 0
cts 2
cp 0
crap 2
rs 10
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
        while ($game->getCurrentHand() !== null && $game->isComputerHand($game->getCurrentHand())) {
74
            $game->playComputerHand();
75
            $game->nextHand();
76
77
            if ($game->isGameOver()) {
78
                $game->finishGame();
79
                $session->set('game', $game);
80
                return $this->redirectToRoute('game_result_proj');
81
            }
82
        }
83
84
        $hands = $game->getHands();
85
        $bustProbabilities = [];
86
        $handValues = [];
87
88
        foreach ($hands as $index => $hand) {
89
            $bustProbabilities[$index] = $game->getBustProbability($index);
90
            $handValues[$index] = $game->getHandValue($hand['hand']);
91
        }
92
93
        return $this->render('gameproj/play.html.twig', [
94
            'playerName' => $game->getPlayerName(),
95
            'hands' => $hands,
96
            'dealerHand' => $game->getDealerHand(),
97
            'bustProbabilities' => $bustProbabilities,
98
            'handValues' => $handValues,
99
            'dealerValue' => $game->getHandValue($game->getDealerHand()),
100
            'currentHand' => $game->getCurrentHand(),
101
            'playerBank' => $game->getPlayerBank(),
102
            'isComputerHand' => array_map([$game, 'isComputerHand'], array_keys($hands)),
103
        ]);
104
    }
105
106
    #[Route("/proj/play/hit", name: "game_hit_proj")]
107
    public function hit(SessionInterface $session): Response
108
    {
109
        $game = $session->get('game');
110
        $currentHand = $game->getCurrentHand();
111
        $game->hitHand($currentHand);
112
113
        if ($game->isHandBusted($currentHand)) {
114
            $game->nextHand();
115
        }
116
117
        while ($game->getCurrentHand() !== null && $game->isComputerHand($game->getCurrentHand())) {
118
            $game->playComputerHand();
119
            $game->nextHand();
120
        }
121
122
        if ($game->isGameOver()) {
123
            $game->finishGame();
124
            $session->set('game', $game);
125
            return $this->redirectToRoute('game_result_proj');
126
        }
127
128
        $session->set('game', $game);
129
        return $this->redirectToRoute('play_proj');
130
    }
131
132
    #[Route("/proj/play/stand", name: "game_stand_proj")]
133
    public function stand(SessionInterface $session): Response
134
    {
135
        $game = $session->get('game');
136
        $game->standHand($game->getCurrentHand());
137
        $game->nextHand();
138
139
        if ($game->isGameOver()) {
140
            $game->finishGame();
141
            $session->set('game', $game);
142
            return $this->redirectToRoute('game_result_proj');
143
        }
144
145
        $session->set('game', $game);
146
        return $this->redirectToRoute('play_proj');
147
    }
148
149
    #[Route("/proj/play/result", name: "game_result_proj")]
150
    public function result(SessionInterface $session): Response
151
    {
152
        $game = $session->get('game');
153
154
        $hands = $game->getHands();
155
        $handValues = [];
156
157
        foreach ($hands as $index => $hand) {
158
            $handValues[$index] = $game->getHandValue($hand['hand']);
159
        }
160
161
        return $this->render('gameproj/result.html.twig', [
162
            'playerName' => $game->getPlayerName(),
163
            'hands' => $hands,
164
            'dealerHand' => $game->getDealerHand(),
165
            'handValues' => $handValues,
166
            'dealerValue' => $game->getHandValue($game->getDealerHand()),
167
            'playerBank' => $game->getPlayerBank(),
168
            'isComputerHand' => array_map([$game, 'isComputerHand'], array_keys($hands)),
169
        ]);
170
    }
171
172
    #[Route("/proj/play/play_again", name: "game_play_again_proj")]
173
    public function playAgain(SessionInterface $session): Response
174
    {
175
        $game = $session->get('game');
176
        $session->set('player_bank', $game->getPlayerBank());
177
        $session->remove('game');
178
        return $this->redirectToRoute('proj');
179
    }
180
}
181