-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhand.php
50 lines (42 loc) · 1.07 KB
/
hand.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
<?php
class Hand {
use Tiles;
public function __construct(array $tiles = []) {
Assert::type($tiles, 'Tile');
$this->tiles = $tiles;
}
public function tiles() {
return $this->tiles;
}
public function size() {
return count($this->tiles);
}
public function play(Move $move) {
foreach ($move->placements() as $placement) {
$pos = array_search($placement->tile(), $this->tiles);
if ($pos === false) {
throw new IllegalMoveException("Could not find tile {$placement->tile()} in {$this}");
}
unset($this->tiles[$pos]);
}
}
public function draw(array $tiles) {
$this->tiles = array_merge($this->tiles, $tiles);
}
public function withProperty($property) {
$matches = [];
foreach ($this->tiles as $tile) {
if ($tile->color()->name() === $property || $tile->shape()->name === $property) {
$matches[] = $tile;
}
}
return $matches;
}
public function __toString() {
$a = [];
foreach ($this->tiles() as $tile) {
$a[] = "$tile";
}
return implode(' ', $a);
}
}