BlackjackController::doc()   A
last analyzed

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 2
Bugs 0 Features 1
Metric Value
cc 1
eloc 2
c 2
b 0
f 1
nc 1
nop 0
dl 0
loc 4
rs 10
ccs 0
cts 2
cp 0
crap 2
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\Session\SessionInterface;
8
use Symfony\Component\Routing\Annotation\Route;
9
use App\Game\BlackjackGame;
10
11
class BlackjackController extends AbstractController
12
{
13
    #[Route("/game/doc", name: "doc")]
14
    public function doc(): Response
15
    {
16
        return $this->render('game/doc.html.twig');
17
    }
18
19
    #[Route("/game", name: "home")]
20
    public function home(): Response
21
    {
22
        return $this->render('game/home.html.twig');
23
    }
24
25
    #[Route("/game/play", name: "play")]
26
    public function play(SessionInterface $session): Response
27
    {
28
        if (!$session->has('game')) {
29
            $game = new BlackjackGame();
30
            $game->dealCards();
31
            $session->set('game', $game);
32
        }
33
34
        $game = $session->get('game');
35
36
        return $this->render('game/play.html.twig', [
37
            'playerHand' => $game->getPlayerHand(),
38
            'dealerHand' => $game->getDealerHand(),
39
        ]);
40
    }
41
42
    #[Route("/game/play/hit", name: "game_hit")]
43
    public function hit(SessionInterface $session): Response
44
    {
45
        $game = $session->get('game');
46
        $game->hitPlayer();
47
48
        if ($game->isPlayerBusted()) {
49
            $session->set('game', $game);
50
            return $this->redirectToRoute('game_result');
51
        }
52
53
        $session->set('game', $game);
54
55
        return $this->redirectToRoute('play');
56
    }
57
58
    #[Route("/game/play/stand", name: "game_stand")]
59
    public function stand(SessionInterface $session): Response
60
    {
61
        $game = $session->get('game');
62
63
        while ($game->dealerMustDraw()) {
64
            $game->hitDealer();
65
        }
66
67
        $session->set('game', $game);
68
69
        return $this->render('game/result.html.twig', [
70
            'playerHand' => $game->getPlayerHand(),
71
            'dealerHand' => $game->getDealerHand(),
72
            'playerValue' => $game->getHandValue($game->getPlayerHand()),
73
            'dealerValue' => $game->getHandValue($game->getDealerHand()),
74
        ]);
75
    }
76
77
    #[Route("/game/play/result", name: "game_result")]
78
    public function result(SessionInterface $session): Response
79
    {
80
        $game = $session->get('game');
81
82
        return $this->render('game/result.html.twig', [
83
            'playerHand' => $game->getPlayerHand(),
84
            'dealerHand' => $game->getDealerHand(),
85
            'playerValue' => $game->getHandValue($game->getPlayerHand()),
86
            'dealerValue' => $game->getHandValue($game->getDealerHand()),
87
        ]);
88
    }
89
90
    #[Route("/game/play/play_again", name: "game_play_again")]
91
    public function playAgain(): Response
92
    {
93
        return $this->redirectToRoute('session_destroy');
94
    }
95
}
96