MediumBanker   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 21
c 3
b 0
f 0
dl 0
loc 63
rs 10
ccs 21
cts 21
cp 1
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getCardsLeft() 0 21 4
A keepHitting() 0 21 2
1
<?php
2
3
namespace App\Game;
4
5
use App\Game\ReceiveTrait;
6
use App\Game\PassInfoTrait;
7
use App\Game\BankerInterface;
8
use App\Card\Card;
9
10
class MediumBanker implements BankerInterface
11
{
12
    use ReceiveTrait;
13
14
    use PassInfoTrait;
15
16
    /**
17
     * From a simple representation of a full deck,
18
     * remove cards matching the ones that the banker has draw this turn,
19
     * as well as any cards drawn in previous turns (suit does not matter).
20
     *
21
     * @return int[] $deck Deck of values that the banker assumes are left.
22
     */
23 7
    private function getCardsLeft(): array
24
    {
25
        /** @var int[] $deck Simple representation of a full deck using plain numbers */
26 7
        $deck = [];
27 7
        foreach (range(1, 13) as $value) {
28 7
            $temp = array_fill(0, 4, $value);
29 7
            $deck = array_merge($deck, $temp);
30
        }
31
32
        /** @var Card[] $cardsToRemove All cards drawn by the banker this turn or removed in previous turns. */
33 7
        $cardsToRemove = array_merge($this->cards, $this->removedCards);
34
35
        // Remove from deck
36 7
        foreach ($cardsToRemove as $card) {
37 7
            $key = array_search($card->getRank(), $deck);
38 7
            if ($key !== false) {
39 7
                unset($deck[$key]);
40
            }
41
        }
42
43 7
        return $deck;
44
    }
45
46
    /**
47
     * Returns whether or not the banker should keep hitting another card.
48
     * This banker does not assume anything about the cards drawn by the player this turn.
49
     *
50
     * @return bool As true if the risk of bursting is less than O.5, otherwise false.
51
     */
52 8
    public function keepHitting(): bool
53
    {
54
        /** @var int $margin Number of points the banker can score before bursting. */
55 8
        $margin = 21 - $this->points;
56
57 8
        if ($margin > 13) {
58 1
            return true;    // No card will put the banker over 21, return early
59
        }
60
61
        /** @var int[] $cardsLeft Values of all cards that the bankers assumes are left. */
62 7
        $cardsLeft = $this->getCardsLeft();
63
64
        /** @var int[] $burstCards Values of all cards that will make the banker burst. */
65 7
        $burstCards = array_filter($cardsLeft, function ($value) use ($margin) {
66 7
            return $value > $margin;
67 7
        });
68
69
        /** @var float $burstRisk Risk of drawing over 21. */
70 7
        $burstRisk = fdiv(count($burstCards), count($cardsLeft));
71
72 7
        return $burstRisk <= 0.5;
73
    }
74
}
75