Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions src/Domain/BasicStrategy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace App\Domain;

final class BasicStrategy
{
public function decide(Hand $hand, Card $dealerCard): Decision
{
$value = $hand->value();
$dealerValue = $dealerCard->numericValue();

if ($hand->isSoft() && $value === 18 && $dealerValue >= 3 && $dealerValue <= 6) {
return Decision::Double;
}

if ($hand->isSoft() && $value === 17 && $dealerValue >= 3 && $dealerValue <= 6) {
return Decision::Double;
}

if ($value >= 17) {
return Decision::Stand;
}

if ($value >= 13 && $value <= 16 && $dealerValue >= 2 && $dealerValue <= 6) {
return Decision::Stand;
}

if ($value === 12 && $dealerValue >= 4 && $dealerValue <= 6) {
return Decision::Stand;
}

if ($value === 11 && $dealerValue >= 2 && $dealerValue <= 10) {
return Decision::Double;
}

if ($value === 10 && $dealerValue >= 2 && $dealerValue <= 9) {
return Decision::Double;
}

if ($value === 9 && $dealerValue >= 3 && $dealerValue <= 6) {
return Decision::Double;
}

return Decision::Hit;
}
}
11 changes: 11 additions & 0 deletions src/Domain/Decision.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace App\Domain;

enum Decision
{
case Hit;
case Stand;
case Double;
case Split;
}
154 changes: 154 additions & 0 deletions tests/Domain/BasicStrategyTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
<?php

namespace App\Tests\Domain;

use App\Domain\BasicStrategy;
use App\Domain\Card;
use App\Domain\Decision;
use App\Domain\Hand;
use PHPUnit\Framework\TestCase;

final class BasicStrategyTest extends TestCase
{
public function testHard16AgainstDealerTenShouldHit(): void
{
$strategy = new BasicStrategy();

$hand = new Hand();
$hand->addCard(new Card('9', 'Hearts'));
$hand->addCard(new Card('7', 'Spades'));

$dealerCard = new Card('10', 'Diamonds');

$this->assertSame(Decision::Hit, $strategy->decide($hand, $dealerCard));
}

public function testHard17ShouldStand(): void
{
$strategy = new BasicStrategy();

$hand = new Hand();
$hand->addCard(new Card('King', 'Hearts'));
$hand->addCard(new Card('7', 'Spades'));

$dealerCard = new Card('6', 'Diamonds');

$this->assertSame(Decision::Stand, $strategy->decide($hand, $dealerCard));
}
public function testHard12AgainstDealer4ShouldStand(): void
{
$strategy = new BasicStrategy();

$hand = new Hand();
$hand->addCard(new Card('7', 'Hearts'));
$hand->addCard(new Card('5', 'Spades'));

$dealerCard = new Card('4', 'Diamonds');

$this->assertSame(Decision::Stand, $strategy->decide($hand, $dealerCard));
}

public function testHard13AgainstDealer2ShouldStand(): void
{
$strategy = new BasicStrategy();

$hand = new Hand();
$hand->addCard(new Card('7', 'Hearts'));
$hand->addCard(new Card('6', 'Spades'));

$dealerCard = new Card('2', 'Diamonds');

$this->assertSame(Decision::Stand, $strategy->decide($hand, $dealerCard));
}

public function testHard11AgainstDealer6ShouldDouble(): void
{
$strategy = new BasicStrategy();

$hand = new Hand();
$hand->addCard(new Card('7', 'Hearts'));
$hand->addCard(new Card('4', 'Spades'));

$dealerCard = new Card('6', 'Diamonds');

$this->assertSame(Decision::Double, $strategy->decide($hand, $dealerCard));
}

public function testHard10AgainstDealer5ShouldDouble(): void
{
$strategy = new BasicStrategy();

$hand = new Hand();
$hand->addCard(new Card('6', 'Hearts'));
$hand->addCard(new Card('4', 'Spades'));

$dealerCard = new Card('5', 'Diamonds');

$this->assertSame(Decision::Double, $strategy->decide($hand, $dealerCard));
}

public function testHard9AgainstDealer3ShouldDouble(): void
{
$strategy = new BasicStrategy();

$hand = new Hand();
$hand->addCard(new Card('5', 'Hearts'));
$hand->addCard(new Card('4', 'Spades'));

$dealerCard = new Card('3', 'Diamonds');

$this->assertSame(Decision::Double, $strategy->decide($hand, $dealerCard));
}

public function testSoft18AgainstDealer5ShouldDouble(): void
{
$strategy = new BasicStrategy();

$hand = new Hand();
$hand->addCard(new Card('Ace', 'Hearts'));
$hand->addCard(new Card('7', 'Spades'));

$dealerCard = new Card('5', 'Diamonds');

$this->assertSame(Decision::Double, $strategy->decide($hand, $dealerCard));
}

public function testSoft18AgainstDealer2ShouldStand(): void
{
$strategy = new BasicStrategy();

$hand = new Hand();
$hand->addCard(new Card('Ace', 'Hearts'));
$hand->addCard(new Card('7', 'Spades'));

$dealerCard = new Card('2', 'Diamonds');

$this->assertSame(Decision::Stand, $strategy->decide($hand, $dealerCard));
}

public function testSoft17AgainstDealer4ShouldDouble(): void
{
$strategy = new BasicStrategy();

$hand = new Hand();
$hand->addCard(new Card('Ace', 'Hearts'));
$hand->addCard(new Card('6', 'Spades'));

$dealerCard = new Card('4', 'Diamonds');

$this->assertSame(Decision::Double, $strategy->decide($hand, $dealerCard));
}

public function testSoft19ShouldAlwaysStand(): void
{
$strategy = new BasicStrategy();

$hand = new Hand();
$hand->addCard(new Card('Ace', 'Hearts'));
$hand->addCard(new Card('8', 'Spades'));

$dealerCard = new Card('6', 'Diamonds');

$this->assertSame(Decision::Stand, $strategy->decide($hand, $dealerCard));
}
}
Loading