-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.php
236 lines (216 loc) · 6.8 KB
/
player.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
<?php
class Player {
const MAX_SIZE_TO_TRY_ALL_PERMUTATIONS = 2;
const MAX_SIZE_TO_IGNORE_LEGALITY = 2;
public function __construct($name) {
$this->name = $name;
$this->hand = new Hand();
}
public function name() {
return $this->name;
}
public function hand() {
return $this->hand;
}
public function setHand(Hand $hand) {
$this->hand = $hand;
}
public function draw(array $tiles) {
$this->hand->draw($tiles);
}
public function discard() {
$tiles = $this->hand()->tiles();
$this->hand = new Hand();
return $tiles;
}
public function startingMove(array $tiles) {
Assert::type($tiles, 'Tile');
$lines = [];
foreach ($tiles as $tile) {
if (!isset($lines[$tile->color()->name()])) {
$lines[$tile->color()->name()] = 0;
}
$lines[$tile->color()->name()] += 1;
if (!isset($lines[$tile->shape()->name()])) {
$lines[$tile->shape()->name()] = 0;
}
$lines[$tile->shape()->name()] += 1;
}
list($best, $max) = [null, 0];
foreach ($lines as $key => $count) {
if ($count > $max) {
$best = $key;
$max = $count;
}
}
list($x, $placements) = [Move::STARTING_INDEX, []];
foreach ((new Hand($tiles))->withProperty($best) as $tile) {
$placements[] = new Placement(new Point($x, Move::STARTING_INDEX), $tile);
$x += 1;
}
return new Move($placements);
}
public function play(Move $move) {
$this->hand()->play($move);
}
public function move(Board $board, $bagIsEmpty) {
$tiles = array_unique($this->hand()->tiles());
if ($board->isEmpty()) {
return $this->startingMove($tiles);
}
return $this->chooseMove(new Hand($tiles), $board, $bagIsEmpty);
}
protected function chooseMove(Hand $hand, Board $board, $bagIsEmpty) {
$usefulSets = $this->usefulSets($hand);
list($max, $bestMove) = [0, new Move([])];
foreach ($usefulSets as $usefulSet) {
$move = $this->bestMoveWith($usefulSet, $board);
if ($move) {
$score = $this->evaluate($hand, $move, $board, $bagIsEmpty);
if ($score > $max) {
$max = $score;
$bestMove = $move;
}
}
}
return $bestMove;
}
protected function evaluate(Hand $hand, Move $move, Board $board, $bagIsEmpty) {
$score = $board->score($move);
if (count($move->placements()) === $this->hand()->size() && $bagIsEmpty) {
$score += Score::FINISHING_BONUS;
}
return $score;
}
private function usefulSets(Hand $hand) {
$usefulSets = [];
foreach (array_merge(Color::colors(), Shape::shapes()) as $property) {
$propertySet = $hand->withProperty($property->name());
if ($propertySet) {
foreach ($this->powerset($propertySet) as $set) {
if (count($set) > 0) {
$usefulSets[] = new Hand($set);
}
}
}
}
return $usefulSets;
}
private function powerset(array $items) {
$results = [[]];
foreach ($items as $item) {
foreach ($results as $combination) {
$results[] = array_merge([$item], $combination);
}
}
return $results;
}
private function bestMoveWith(Hand $hand, Board $board) {
list($max, $bestMove) = [0, new Move([])];
foreach ($board->attachmentLocations() as $location) {
if (!$this->rejectQuicklyIfPossible($location, $hand)) {
continue;
}
foreach ($this->permutations($hand, $board, $location->point()) as $move) {
$score = $board->score($move);
if ($score > $max) {
$max = $score;
$bestMove = $move;
}
}
}
return $bestMove;
}
private function rejectQuicklyIfPossible(Location $location, Hand $hand) {
foreach ($hand->sharedProperties() as $property) {
if (in_array($property, $location->sharedProperties())) {
return true;
}
}
return false;
}
private function permutations(Hand $hand, Board $board, Point $point) {
$moves = [];
foreach ([Direction::down(), Direction::right()] as $direction) {
if ($hand->size() === 1 && $direction === Direction::right()) {
continue; // No point in checking single tiles more than once in each spot.
}
for ($i = 0; $i < $hand->size(); $i++) {
list($steps, $p) = [$i, $point];
while ($steps > 0) {
$p = $p->next($direction->opposite());
if ($board->at($p) === null) {
$steps -= 1;
}
}
if ($hand->size() > self::MAX_SIZE_TO_TRY_ALL_PERMUTATIONS) {
$move = $this->tryFit($hand->tiles(), $board, $p, $direction);
if ($move !== null) {
$moves[] = $move;
}
} else {
foreach ($this->orderings($hand->tiles()) as $tilesToLay) {
$layingP = $p;
$placements = [];
while ($tilesToLay) {
if ($board->at($layingP) === null) {
$placements[] = new Placement($layingP, array_shift($tilesToLay));
}
$layingP = $layingP->next($direction);
}
$move = new Move($placements);
//BAKERT
// if (Game::$turn === 51) {
// $board2 = clone $board;
// $board2->applyWithoutChecks($move);
// echo $board2;
// }
if ($board->isLegal($move)) {
$moves[] = $move;
break; // We found a legal move using these tiles in this direction from this start position - we cannot do better score-wise.
}
}
}
}
}
return $moves;
}
private function tryFit(array $tiles, Board $board, Point $point, Direction $direction, array $rawMove = []) {
if (count($tiles) === 0) {
return new Move($rawMove);
}
foreach ($tiles as $tile) {
$placement = new Placement($point, $tile);
$newRawMove = array_merge($rawMove, [$placement]);
$move = new Move($newRawMove);
if ((count($rawMove) <= self::MAX_SIZE_TO_IGNORE_LEGALITY && count($tiles) > 1) || $board->isLegal($move)) {
$pos = array_search($tile, $tiles);
unset($tiles[$pos]);
$board = clone $board;
$board->applyWithoutChecks($move);
$move = $this->tryFit($tiles, $board, $point->next($direction), $direction, $newRawMove);
if ($move !== null) {
return $move;
}
}
}
return null;
}
private function orderings(array $items, array $processed = []) {
$result = [];
foreach ($items as $key => $value) {
$copy = $processed;
$copy[$key] = $value;
$tmp = array_diff_key($items, $copy);
if (count($tmp) === 0) {
$result[] = $copy;
} else {
$result = array_merge($result, $this->orderings($tmp, $copy));
}
}
return $result;
}
public function __toString() {
return $this->name();
}
}