Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

Passed
Push — master ( 27e319...99d485 )
by Dan
04:07
created

Hand::getNumCards()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
rs 10
c 1
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace Blackjack;
4
5
/**
6
 * Hand of Blackjack cards.
7
 */
8
class Hand {
9
10
	private array $cards = [];
11
	private int $value = 0;
12
13
	/**
14
	 * Add a hand to this card by drawing it from $deck.
15
	 */
16
	public function drawCard(Deck $deck) : void {
17
		$this->cards[] = $deck->drawCard();
18
		$this->updateValue();
19
	}
20
21
	/**
22
	 * Return the hand's total blackjack value.
23
	 */
24
	public function getValue() : int {
25
		return $this->value;
26
	}
27
28
	/**
29
	 * Return the number of cards in this hand.
30
	 */
31
	public function getNumCards() : int {
32
		return count($this->cards);
33
	}
34
35
	public function getCards() : array {
36
		return $this->cards;
37
	}
38
39
	/**
40
	 * Does this hand have Blackjack?
41
	 */
42
	public function hasBlackjack() : bool {
43
		return $this->getNumCards() == 2 && $this->getValue() == 21;
44
	}
45
46
	/**
47
	 * Update the stored value of this hand.
48
	 */
49
	private function updateValue() : void {
50
		$numAces11 = 0; // Aces have a value of 11 by default
51
		$value = 0;
52
		foreach ($this->cards as $card) {
53
			if ($card->isAce()) {
54
				$numAces11 += 1;
55
			}
56
			$value += $card->getValue();
57
		}
58
		// Modify value of aces if we're over 21
59
		while ($value > 21 && $numAces11 > 0) {
60
			$value -= 10;
61
			$numAces11 -= 1;
62
		}
63
		$this->value = $value;
64
	}
65
66
}
67