From 19d5a52a36345d70bc41aee9bb06fc3f6b299a71 Mon Sep 17 00:00:00 2001 From: florentdevk Date: Sun, 21 Jun 2026 17:59:22 +0200 Subject: [PATCH 1/7] feat: add Hand with empty value --- src/Domain/Hand.php | 15 +++++++++++++++ tests/Domain/HandTest.php | 17 +++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 src/Domain/Hand.php create mode 100644 tests/Domain/HandTest.php diff --git a/src/Domain/Hand.php b/src/Domain/Hand.php new file mode 100644 index 0000000..a6c5921 --- /dev/null +++ b/src/Domain/Hand.php @@ -0,0 +1,15 @@ +assertSame(0, $hand->value()); + } +} \ No newline at end of file From 613180e262570fe23096d23e0a035c5eecfe03b0 Mon Sep 17 00:00:00 2001 From: florentdevk Date: Sun, 21 Jun 2026 18:01:24 +0200 Subject: [PATCH 2/7] feat: Hand can add cards and calculate value --- src/Domain/Hand.php | 11 ++++++++++- tests/Domain/HandTest.php | 8 ++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/Domain/Hand.php b/src/Domain/Hand.php index a6c5921..ff033bc 100644 --- a/src/Domain/Hand.php +++ b/src/Domain/Hand.php @@ -8,8 +8,17 @@ public function __construct( private array $cards = [] ) {} + public function addCard(Card $card): void + { + $this->cards[] = $card; + } + public function value(): int { - return 0; + $total = 0; + foreach ($this->cards as $card) { + $total += $card->numericValue(); + } + return $total; } } diff --git a/tests/Domain/HandTest.php b/tests/Domain/HandTest.php index 64d2b27..199cd62 100644 --- a/tests/Domain/HandTest.php +++ b/tests/Domain/HandTest.php @@ -14,4 +14,12 @@ public function testEmptyHandHasValueOfZero(): void $this->assertSame(0, $hand->value()); } + + public function testHandWithOneCardReturnsCardValue(): void + { + $hand = new Hand(); + $hand->addCard(new Card('7', 'Hearts')); + + $this->assertSame(7, $hand->value()); + } } \ No newline at end of file From 98f08ea16298b1d67abfc3c802759723da047045 Mon Sep 17 00:00:00 2001 From: florentdevk Date: Sun, 21 Jun 2026 18:10:54 +0200 Subject: [PATCH 3/7] feat: Hand handles flexible Ace value --- src/Domain/Hand.php | 11 +++++++++++ tests/Domain/HandTest.php | 10 ++++++++++ 2 files changed, 21 insertions(+) diff --git a/src/Domain/Hand.php b/src/Domain/Hand.php index ff033bc..e51db18 100644 --- a/src/Domain/Hand.php +++ b/src/Domain/Hand.php @@ -16,9 +16,20 @@ public function addCard(Card $card): void public function value(): int { $total = 0; + $aces = 0; + foreach ($this->cards as $card) { $total += $card->numericValue(); + if ($card->rank() === 'Ace') { + $aces++; + } + } + + while ($total > 21 && $aces > 0) { + $total -= 10; + $aces--; } + return $total; } } diff --git a/tests/Domain/HandTest.php b/tests/Domain/HandTest.php index 199cd62..4dccd22 100644 --- a/tests/Domain/HandTest.php +++ b/tests/Domain/HandTest.php @@ -22,4 +22,14 @@ public function testHandWithOneCardReturnsCardValue(): void $this->assertSame(7, $hand->value()); } + + public function testAceCountsAsOneWhenHandWouldBust(): void + { + $hand = new Hand(); + $hand->addCard(new Card('Ace', 'Hearts')); + $hand->addCard(new Card('King', 'Spades')); + $hand->addCard(new Card('5', 'Diamonds')); + + $this->assertSame(16, $hand->value()); + } } \ No newline at end of file From 86870fd969f31d9ccd4a9d17fe1f85637365f70a Mon Sep 17 00:00:00 2001 From: florentdevk Date: Mon, 22 Jun 2026 20:00:17 +0200 Subject: [PATCH 4/7] feat: Hand detects natural Blackjack --- src/Domain/Hand.php | 5 +++++ tests/Domain/HandTest.php | 9 +++++++++ 2 files changed, 14 insertions(+) diff --git a/src/Domain/Hand.php b/src/Domain/Hand.php index e51db18..1e16459 100644 --- a/src/Domain/Hand.php +++ b/src/Domain/Hand.php @@ -32,4 +32,9 @@ public function value(): int return $total; } + + public function isBlackjack(): bool + { + return count($this->cards) === 2 && $this->value() === 21; + } } diff --git a/tests/Domain/HandTest.php b/tests/Domain/HandTest.php index 4dccd22..ab69bc3 100644 --- a/tests/Domain/HandTest.php +++ b/tests/Domain/HandTest.php @@ -32,4 +32,13 @@ public function testAceCountsAsOneWhenHandWouldBust(): void $this->assertSame(16, $hand->value()); } + + public function testNaturalBlackjackWithAceAndKing(): void + { + $hand = new Hand(); + $hand->addCard(new Card('Ace', 'Hearts')); + $hand->addCard(new Card('King', 'Spades')); + + $this->assertTrue($hand->isBlackjack()); + } } \ No newline at end of file From 7b4d7ecf30cbb704d22548c74d3cfdb5f045986d Mon Sep 17 00:00:00 2001 From: florentdevk Date: Mon, 22 Jun 2026 20:02:11 +0200 Subject: [PATCH 5/7] test: three card 21 is not a Blackjack --- tests/Domain/HandTest.php | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/tests/Domain/HandTest.php b/tests/Domain/HandTest.php index ab69bc3..d75cdfe 100644 --- a/tests/Domain/HandTest.php +++ b/tests/Domain/HandTest.php @@ -41,4 +41,14 @@ public function testNaturalBlackjackWithAceAndKing(): void $this->assertTrue($hand->isBlackjack()); } -} \ No newline at end of file + + public function testThreeCardTwentyOneIsNotBlackjack(): void + { + $hand = new Hand(); + $hand->addCard(new Card('7', 'Hearts')); + $hand->addCard(new Card('7', 'Spades')); + $hand->addCard(new Card('7', 'Diamonds')); + + $this->assertFalse($hand->isBlackjack()); + } +} From f19c2928b76cd43676f3289cfdf3e807ef388f84 Mon Sep 17 00:00:00 2001 From: florentdevk Date: Mon, 22 Jun 2026 20:06:40 +0200 Subject: [PATCH 6/7] feat: Hand detects bust --- src/Domain/Hand.php | 5 +++++ tests/Domain/HandTest.php | 10 ++++++++++ 2 files changed, 15 insertions(+) diff --git a/src/Domain/Hand.php b/src/Domain/Hand.php index 1e16459..efb9f5e 100644 --- a/src/Domain/Hand.php +++ b/src/Domain/Hand.php @@ -37,4 +37,9 @@ public function isBlackjack(): bool { return count($this->cards) === 2 && $this->value() === 21; } + + public function isBust(): bool + { + return $this->value() > 21; + } } diff --git a/tests/Domain/HandTest.php b/tests/Domain/HandTest.php index d75cdfe..b34f1fb 100644 --- a/tests/Domain/HandTest.php +++ b/tests/Domain/HandTest.php @@ -51,4 +51,14 @@ public function testThreeCardTwentyOneIsNotBlackjack(): void $this->assertFalse($hand->isBlackjack()); } + + public function testHandIsBustWhenValueExceeds21(): void + { + $hand = new Hand(); + $hand->addCard(new Card('King', 'Hearts')); + $hand->addCard(new Card('Queen', 'Spades')); + $hand->addCard(new Card('5', 'Diamonds')); + + $this->assertTrue($hand->isBust()); + } } From 6feb793b0b8426df7b96a75a9ddc0a2c064b6b79 Mon Sep 17 00:00:00 2001 From: florentdevk Date: Mon, 22 Jun 2026 20:11:28 +0200 Subject: [PATCH 7/7] feat: Hand detects soft hand --- src/Domain/Hand.php | 15 +++++++++++++++ tests/Domain/HandTest.php | 9 +++++++++ 2 files changed, 24 insertions(+) diff --git a/src/Domain/Hand.php b/src/Domain/Hand.php index efb9f5e..2177526 100644 --- a/src/Domain/Hand.php +++ b/src/Domain/Hand.php @@ -42,4 +42,19 @@ public function isBust(): bool { return $this->value() > 21; } + + public function isSoft(): bool + { + $total = 0; + $aces = 0; + + foreach ($this->cards as $card) { + $total += $card->numericValue(); + if ($card->rank() === 'Ace') { + $aces++; + } + } + + return $aces > 0 && $total <= 21; + } } diff --git a/tests/Domain/HandTest.php b/tests/Domain/HandTest.php index b34f1fb..146f465 100644 --- a/tests/Domain/HandTest.php +++ b/tests/Domain/HandTest.php @@ -61,4 +61,13 @@ public function testHandIsBustWhenValueExceeds21(): void $this->assertTrue($hand->isBust()); } + + public function testHandWithAceCountedAs11IsSoft(): void + { + $hand = new Hand(); + $hand->addCard(new Card('Ace', 'Hearts')); + $hand->addCard(new Card('6', 'Spades')); + + $this->assertTrue($hand->isSoft()); + } }