-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApplication.php
55 lines (44 loc) · 1.48 KB
/
Application.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?php
declare(strict_types=1);
namespace Console;
use Dominoes\Bot\BotInterface;
use Dominoes\Deck\DeckFactoryInterface;
use Dominoes\GameMediator\GameListenerInterface;
use Dominoes\GameMediator\GameMediatorInterface;
use Dominoes\Player\Player;
use Dominoes\Player\PlayerInterface;
use Psr\Log\LoggerInterface;
class Application
{
public function __construct(
private DeckFactoryInterface $deckFactory,
private GameMediatorInterface $mediator,
private BotInterface $bot,
private GameListenerInterface $gameListener,
private LoggerInterface $logger,
) {
}
public function execute(int $highestDeckPip, int $tilesPerPlayer, string ...$playersName)
{
$deck = $this->deckFactory->createDeck($highestDeckPip);
$this->mediator->start(
$this->gameListener,
$deck,
...array_map(
fn(string $playerName): PlayerInterface => new Player(
$playerName,
$deck->drawRandomTiles($tilesPerPlayer)
),
$playersName
),
);
while ($this->mediator->getStatus() == GameMediatorInterface::STATUS_IN_PROGRESS) {
$this->bot->play($this->mediator);
}
if ($winner = $this->mediator->getWinner()) {
$this->logger->debug(sprintf('%s has won!', $winner->getName()));
} else {
$this->logger->debug('It\'s a tie. Nobody wins!');
}
}
}