-
Notifications
You must be signed in to change notification settings - Fork 0
/
bag.php
40 lines (35 loc) · 783 Bytes
/
bag.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
<?php
class Bag {
public function __construct($tiles = null) {
$this->random = $tiles === null;
$this->tiles = $tiles === null ? Tile::allTiles() : $tiles;
if ($this->random) {
$this->shuffle();
}
}
public function draw($n) {
$tiles = [];
for ($i = 0; $i < $n; $i++) {
$tile = array_shift($this->tiles);
if ($tile === null) {
return $tiles;
}
$tiles[$i] = $tile;
}
return $tiles;
}
public function discard($tiles) {
$this->tiles = array_merge($this->tiles, $tiles);
if ($this->random) {
$this->shuffle();
}
}
public function isEmpty() {
return count($this->tiles) === 0;
}
private function shuffle() {
if ($this->random) {
shuffle($this->tiles);
}
}
}