-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathboard.ts
187 lines (153 loc) · 4.46 KB
/
board.ts
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
import Tile, { TileData } from "./tile.js";
import { clamp, all as allDirections, Vector } from "./direction.js";
import { NONE } from "./edge.js";
import { Score, get as getScore } from "./score.js";
import CellRepo, { Cell } from "./cell-repo.js";
import { LAKE } from "./edge.js";
interface CellData {
x: number;
y: number;
round: number;
tile: TileData;
}
export type SerializedBoard = CellData[];
export default class Board {
node: HTMLElement | null;
blob: Blob | null = null;
_cells = new CellRepo();
constructor(readonly _tileCtor = Tile) {
this.node = this._build();
this._placeInitialTiles();
}
_build(): HTMLElement | null { return null; };
signal(_cells: Cell[]) {};
showScore(_score: Score) {}
onClick(_cell: Cell) {}
getScore() { return getScore(this._cells); }
fromJSON(cells: SerializedBoard) {
const Tile = this._tileCtor;
this._cells.forEach(cell => {
if (!cell.border) { cell.tile = null; }
});
cells.forEach(cell => {
let tile = Tile.fromJSON(cell.tile);
this.place(tile, cell.x, cell.y, cell.round);
});
this.commit(0);
return this;
}
toJSON() {
let result: SerializedBoard = [];
this._cells.forEach(cell => {
const tile = cell.tile;
if (cell.border || !tile) { return; }
result.push({
x: cell.x,
y: cell.y,
round: cell.round,
tile: tile.toJSON()
});
})
return result;
}
commit(round: number) {
round && this._surroundLakes(round);
}
cycleTransform(x: number, y: number) {
let tile = this._cells.at(x, y).tile;
if (!tile) { return; }
let avail = this._getTransforms(tile, x, y);
let index = avail.indexOf(tile.transform);
if (index == -1 || avail.length <= 1) { return; }
index = (index+1) % avail.length;
tile.transform = avail[index];
}
placeBest(tile: Tile, x: number, y: number, round: number) {
let avail = this._getTransforms(tile, x, y);
if (!avail.length) { return false; }
tile.transform = avail[0];
this.place(tile, x, y, round);
return true;
}
place(tile: Tile | null, x: number, y: number, round: number) {
let cell = this._cells.at(x, y);
cell.tile = tile;
cell.round = round;
}
getNeighborEdges(x: number, y: number) {
return allDirections.map(dir => {
let vector = Vector[dir];
let neighbor = this._cells.at(x + vector[0], y + vector[1]).tile;
if (!neighbor) { return NONE; }
return neighbor.getEdge(clamp(dir + 2)).type;
});
}
getAvailableCells(tile: Tile) {
return this._cells.filter(cell => {
if (cell.border || cell.tile) { return false; }
let transforms = this._getTransforms(tile, cell.x, cell.y);
return (transforms.length > 0);
});
}
_getTransforms(tile: Tile, x: number, y: number) {
let neighborEdges = this.getNeighborEdges(x, y);
let clone = tile.clone();
function compare(t1: string, t2: string) {
clone.transform = t1;
let c1 = clone.fitsNeighbors(neighborEdges);
clone.transform = t2;
let c2 = clone.fitsNeighbors(neighborEdges);
return c2-c1;
}
return tile.getTransforms().filter(t => {
clone.transform = t;
return clone.fitsNeighbors(neighborEdges);
}).sort(compare);
}
_placeInitialTiles() {
const Tile = this._tileCtor;
this._cells.forEach(cell => {
const x = cell.x;
const y = cell.y;
let tile: Tile | null = null;
switch (true) {
case (x==2 && y==0):
case (x==6 && y==0):
tile = new Tile("road-half", "2");
break;
case (x==2 && y==8):
case (x==6 && y==8):
tile = new Tile("road-half", "0");
break;
case (x==0 && y==2):
case (x==0 && y==6):
tile = new Tile("rail-half", "1");
break;
case (x==8 && y==2):
case (x==8 && y==6):
tile = new Tile("rail-half", "-1");
break;
case (x==4 && y==0): tile = new Tile("rail-half", "2"); break;
case (x==4 && y==8): tile = new Tile("rail-half", "0"); break;
case (x==0 && y==4): tile = new Tile("road-half", "1"); break;
case (x==8 && y==4): tile = new Tile("road-half", "-1"); break;
}
this.place(tile, x, y, 0);
});
this.commit(0);
}
_surroundLakes(round: number) {
const Tile = this._tileCtor;
const isSurrounded = (cell: Cell) => {
if (cell.tile || cell.border) { return false; }
let neighborEdges = this.getNeighborEdges(cell.x, cell.y);
return neighborEdges.filter(e => e == LAKE).length >= 3;
}
let surrounded = this._cells.filter(isSurrounded);
surrounded.forEach(cell => {
let tile = new Tile("lake-4", "0");
this.place(tile, cell.x, cell.y, round);
});
surrounded.length && this.commit(round);
}
}