Skip to content

Commit

Permalink
Change the way we determine score and winner
Browse files Browse the repository at this point in the history
Introduce new service Croupier
  • Loading branch information
tyx committed Sep 5, 2014
1 parent 9b96d19 commit ebd06e5
Show file tree
Hide file tree
Showing 13 changed files with 312 additions and 68 deletions.
3 changes: 3 additions & 0 deletions composer.json
Expand Up @@ -55,5 +55,8 @@
"branch-alias": {
"dev-master": "1.0.x-dev"
}
},
"require-dev": {
"phpspec/phpspec": "~2.0"
}
}
217 changes: 215 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 12 additions & 13 deletions src/Afsy/App/GameEngineService.php
Expand Up @@ -8,40 +8,39 @@

class GameEngineService
{
protected $dealer;
private $dealer;

protected $repository;
private $gameRepository;

public function __construct(Dealer $dealer, GameRepository $repository)
public function __construct(Dealer $dealer, GameRepository $gameRepository)
{
$this->dealer = $dealer;
$this->repository = $repository;
$this->gameRepository = $gameRepository;
}

public function startGame(Command\StartGameCommand $command)
{
$rules = new Model\GameRules;
$discardPile = $rules->deal($this->dealer);
$discardPile = $this->dealer->deal(new Model\GameRules);

$game = new Model\Game($command->gameId);
$game->start($command->nbPlayers, $discardPile);

$this->repository->save($game);
$this->gameRepository->save($game);
}

public function dealCard(Command\DealCardCommand $command)
{
$game = $this->repository->find($command->gameId);
$game->deal();
$game = $this->gameRepository->find($command->gameId);
$game->dealCard();

$this->repository->save($game);
$this->gameRepository->save($game);
}

public function stopDealCard(Command\StopDealCardCommand $command)
{
$game = $this->repository->find($command->gameId);
$game->stop();
$game = $this->gameRepository->find($command->gameId);
$game->stopDealCard();

$this->repository->save($game);
$this->gameRepository->save($game);
}
}
3 changes: 2 additions & 1 deletion src/Afsy/Blackjack/Domain/Model/Card.php
Expand Up @@ -57,6 +57,7 @@ public function getName()

public function getPoints()
{
return $this->points;
// Even Card can have multiple values, for now we consider there is only one
return current($this->points);
}
}
7 changes: 1 addition & 6 deletions src/Afsy/Blackjack/Domain/Model/DiscardPile.php
Expand Up @@ -6,7 +6,7 @@ class DiscardPile
{
protected $cards;

public function __construct($cards)
public function __construct(array $cards)
{
$this->cards = $cards;
}
Expand All @@ -21,9 +21,4 @@ public function deal($upturned = false)

return $card;
}

public function countCards()
{
return count($this->cards);
}
}
25 changes: 9 additions & 16 deletions src/Afsy/Blackjack/Domain/Model/Game.php
Expand Up @@ -11,6 +11,7 @@
use Afsy\Blackjack\Domain\Specification\PlayerBustedSpec;
use Afsy\Blackjack\Domain\Specification\DealerLimitSpec;
use Afsy\Blackjack\Domain\Specification\GetVisibleCardSpec;
use Afsy\Blackjack\Domain\Service\Croupier;

class Game extends AggregateRoot
{
Expand Down Expand Up @@ -41,17 +42,17 @@ public function start($nbPlayers, $discardPile)
$card = $discardPile->deal(
$visibleCard->isSatisfiedBy($player, $i)
);
$player->receiveCards([$card]);
$player->receiveCard($card);
}
}

$this->apply(new GameCreated($this->getId(), $players, $discardPile));
}

public function deal()
public function dealCard()
{
$player = $this->getCurrentPlayer();
$player->receiveCards(array($this->discardPile->deal(true)));
$player->receiveCard($this->discardPile->deal(true));

$this->apply(new CardDealt($this->getId(), $player, $this->discardPile));

Expand All @@ -68,10 +69,9 @@ public function deal()
}
}

public function stop()
public function stopDealCard()
{
$player = $this->getCurrentPlayer();
$player->stop();
$this->getCurrentPlayer()->stop();
$this->nextPlayer();

while ($this->getCurrentPlayer()->isInGame()) {
Expand Down Expand Up @@ -114,16 +114,9 @@ public function createPlayers($nbPlayers)

public function verifyWinner()
{
$max = 0;

foreach ($this->players as $index => $player) {
if ($player->getPoints() > $max) {
$max = $player->getPoints();
$this->winner = $player;
}
}

return $this->winner;
// should be injected
$croupier = new Croupier;
$this->winner = $croupier->findWinner($this->players);
}

protected function applyGameCreated($event)
Expand Down

0 comments on commit ebd06e5

Please sign in to comment.