-
Notifications
You must be signed in to change notification settings - Fork 0
/
line.php
63 lines (55 loc) · 1.28 KB
/
line.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
56
57
58
59
60
61
62
63
<?php
class Line {
use Tiles;
public function __construct(array $placements = []) {
Assert::type($placements, 'Placement');
$this->placements = $placements;
}
public function placements() {
return $this->placements;
}
public function tiles() {
$tiles = [];
foreach ($this->placements as $placement) {
$tiles[] = $placement->tile();
}
return $tiles;
}
public function isLegal() {
if (!$this->placements()) {
return true;
}
if ($this->tiles() !== array_unique($this->tiles())) {
return false;
}
$color = $this->tiles()[0]->color();
$shape = $this->tiles()[0]->shape();
foreach ($this->tiles() as $tile) {
if ($tile->color() !== $color) {
$color = null;
}
if ($tile->shape() !== $shape) {
$shape = null;
}
}
return $color !== null || $shape !== null;
}
public function length() {
return count($this->placements);
}
public function contains($searchTile) {
foreach ($this->tiles() as $tile) {
if ($tile === $searchTile) {
return true;
}
}
return false;
}
public function __toString() {
$a = [];
foreach ($this->placements as $placement) {
$a[] = "$placement";
}
return "Line: " . implode(', ', $a);
}
}