Skip to content

Commit

Permalink
Determine randomness of game by whether an array of tiles is passed i…
Browse files Browse the repository at this point in the history
…n 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.
  • Loading branch information
Thomas Baker committed May 12, 2016
1 parent 1d40455 commit 69b53a6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 18 deletions.
21 changes: 9 additions & 12 deletions bag.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
<?php

class Bag {
public function __construct() {
$this->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) {
Expand All @@ -28,15 +23,17 @@ 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() {
return count($this->tiles) === 0;
}

private function shuffle() {
if (Game::RANDOM) {
if ($this->random) {
shuffle($this->tiles);
}
}
Expand Down
13 changes: 7 additions & 6 deletions game.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 69b53a6

Please sign in to comment.