From 69b53a6b33c263ca47d209bc1520625277495b60 Mon Sep 17 00:00:00 2001 From: Thomas Baker Date: Thu, 12 May 2016 21:40:29 +0200 Subject: [PATCH] Determine randomness of game by whether an array of tiles is passed in or not. We were using a flag before but this shows intent to run a nonrandom game and a nonrandom game with the tiles in color then shape order isn't a very useful test of anything because it is so peculiar. --- bag.php | 21 +++++++++------------ game.php | 13 +++++++------ 2 files changed, 16 insertions(+), 18 deletions(-) diff --git a/bag.php b/bag.php index 0443765..1d6d4ae 100644 --- a/bag.php +++ b/bag.php @@ -1,17 +1,12 @@ tiles = []; - foreach (Color::colors() as $color) { - foreach (Shape::shapes() as $shape) { - $tile = new Tile($color, $shape); - $this->tiles[] = $tile; - $this->tiles[] = $tile; - $this->tiles[] = $tile; - } + public function __construct($tiles = null) { + $this->random = $tiles === null; + $this->tiles = $tiles === null ? Tile::allTiles() : $tiles; + if ($this->random) { + $this->shuffle(); } - $this->shuffle(); } public function draw($n) { @@ -28,7 +23,9 @@ public function draw($n) { public function discard($tiles) { $this->tiles = array_merge($this->tiles, $tiles); - $this->shuffle(); + if ($this->random) { + $this->shuffle(); + } } public function isEmpty() { @@ -36,7 +33,7 @@ public function isEmpty() { } private function shuffle() { - if (Game::RANDOM) { + if ($this->random) { shuffle($this->tiles); } } diff --git a/game.php b/game.php index 55b71bc..94aca09 100644 --- a/game.php +++ b/game.php @@ -3,18 +3,19 @@ class Game { const HAND_SIZE = 6; const MAX_ROUNDS_WITHOUT_SCORING = 10; - const RANDOM = true; - public function __construct(array $players) { + public function __construct(array $players, $tiles = null) { Assert::type($players, Player); $this->players = $players; + $this->random = $tiles === null; + $this->tiles = $tiles; + if ($this->random) { + shuffle($this->players); + } } public function go() { - if (Game::RANDOM) { - shuffle($this->players); - } - list($event, $bag, $board, $scores) = [new Event(), new Bag(), new Board(), new Scores($this->players)]; + list($event, $bag, $board, $scores) = [new Event(), new Bag($this->tiles), new Board(), new Scores($this->players)]; foreach ($this->players as $player) { $hand = new Hand($bag->draw(self::HAND_SIZE)); $player->setHand($hand);