-
Notifications
You must be signed in to change notification settings - Fork 0
/
board.php
165 lines (148 loc) · 4.28 KB
/
board.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php
class Board {
const SIZE = 200;
const MIN_VIEWPORT_SIZE = 0;
private $board;
private $locations = [];
public function __construct(Board $board = null) {
if (!$board) {
$this->board = array_fill(0, self::SIZE, []);
for ($y = 0; $y <= self::SIZE; $y++) {
$this->board[$y] = array_fill(0, self::SIZE, null);
}
} else {
$this->board = $board;
}
}
public function isLegal(Move $move) {
foreach ($move->placements() as $placement) {
if ($this->at($placement->point()) !== null) {
return false;
}
}
$board = clone $this;
$board->applyWithoutChecks($move);
foreach ($move->placements() as $placement) {
if (!$placement->isLegal($board)) {
return false;
}
}
return true;
}
public function apply(Move $move) {
if (!$this->isLegal($move)) {
throw new IllegalMoveException("Cannot place move `{$move}` on board `{$this}`");
}
$this->applyWithoutChecks($move, true);
return $this->score($move);
}
public function applyWithoutChecks(Move $move, $updateLocations = false) {
foreach ($move->placements as $placement) {
$this->removeLocation($placement->point());
list($x, $y) = [$placement->point()->x(), $placement->point()->y()];
$this->board[$y][$x] = $placement->tile();
}
if ($updateLocations) {
foreach ($move->placements as $placement) {
foreach ($placement->point()->neighbors() as $point) {
if ($this->at($point) === null) {
$this->addLocation($point);
}
}
}
}
return $this->board;
}
private function addLocation(Point $pointToAdd) {
foreach ($pointToAdd->neighbors() as $point) {
$tile = $this->at($point);
if ($tile !== null) {
$neighbors[] = $tile;
}
}
$sharedProperties = (new Hand($neighbors))->sharedProperties();
if ($sharedProperties !== null) {
$this->locations[(string)$pointToAdd] = new Location($pointToAdd, $sharedProperties);
}
}
private function removeLocation(Point $point) {
unset($this->locations[(string)$point]);
}
public function at(Point $point) {
if ($point->x() < 0 || $point->y() < 0 || $point->x() >= self::SIZE || $point->y() >= self::SIZE) {
return null;
}
return $this->board[$point->y()][$point->x()];
}
public function attachmentLocations() {
$locations = array_values($this->locations);
return $locations;
}
public function isEmpty() {
foreach ($this->board as $column) {
foreach ($column as $tile) {
if ($tile) {
return false;
}
}
}
return true;
}
public function score(Move $move) {
$board = clone $this;
$board->applyWithoutChecks($move);
$lines = $move->lines($board);
$score = 0;
foreach ($lines as $line) {
if ($line->length() === count(Color::colors())) {
$score += Score::QWIRKLE_BONUS;
}
if ($line->length() > 1) {
$score += $line->length();
}
}
return $score;
}
public function tiles() {
$tiles = [];
foreach ($this->board as $column) {
foreach ($column as $tile) {
$tiles[] = $tile;
}
}
return $tiles;
}
public function __toString() {
$highest = self::SIZE / 2 - self::MIN_VIEWPORT_SIZE / 2 - 1;
$lowest = self::SIZE / 2 + self::MIN_VIEWPORT_SIZE / 2 - 1;
$leftmost = self::SIZE / 2 - self::MIN_VIEWPORT_SIZE / 2 - 1;
$rightmost = self::SIZE / 2 + self::MIN_VIEWPORT_SIZE / 2 - 1;
for ($y = 0; $y < count($this->board); $y++) {
for ($x = 0; $x < count($this->board[$y]); $x++) {
if ($this->board[$y][$x] !== null) {
$highest = min($y, $highest);
$lowest = max($y, $lowest);
$leftmost = min($x, $leftmost);
$rightmost = max($x, $rightmost);
}
}
}
$highest -= 1;
$lowest += 2;
$leftmost -= 1;
$rightmost += 2;
$s = '';
for ($y = $highest; $y < $lowest; $y++) {
$a = [];
for ($x = $leftmost; $x < $rightmost; $x++) {
$tile = $this->board[$y][$x];
if ($tile === null) {
$tile = Tile::USE_ANSI ? Color::ANSI_BACKGROUND . " \e[0m" : ' ';
}
$a[] = "{$tile}";
}
$s .= '|' . implode("|", $a) . "|\n";
}
return $s;
}
}