-
Notifications
You must be signed in to change notification settings - Fork 17
/
infinite-grid.js
231 lines (203 loc) · 5.49 KB
/
infinite-grid.js
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
/**
* @typedef {String} GridId - Two numbers separated by a comma.
* @example "10,5"
*/
/**
* @typedef {Object} InfiniteGridConstructorOptions
* @property {?Function<x, y>} defaultFactory - Defaults to returning 0 for new coords
* @property {?Object} string_map - Map grid values to strings.
* @property {String|any[][]} load - Initial grid to load. Can be a "2D" string (string with new lines), or a "2D" array.
*/
class InfiniteGrid {
/**
* @param {InfiniteGridConstructorOptions} options
* @param {Function<x, y>} [options.defaultFactory] - Defaults to returning 0 for new coords
* @param {Object} [options.string_map] - Map grid values to strings.
* @param {Object} [options.string_map] - Map grid values to strings.
*/
constructor({ defaultFactory = (x, y) => 0, string_map = {}, load } = {}) {
this.defaultFactory = defaultFactory.bind(this);
this.string_map = string_map;
this.grid = new Map();
this.max_x = -Infinity;
this.min_x = Infinity;
this.max_y = -Infinity;
this.min_y = Infinity;
if (load) {
this.load(load);
}
}
/**
* @param {Number} x
* @param {Number} y
* @returns {GridId}
*/
static toId(x, y) {
return `${x},${y}`;
}
/**
* @param {GridId} id
* @param {Boolean} [return_as_object=false]
* @returns {{x: Number, y: Number} | [Number, Number]}
*/
static toCoords(id, return_as_object = false) {
let [x, y] = id.split(',');
x = parseInt(x, 10);
y = parseInt(y, 10);
return return_as_object ? { x, y } : [x, y];
}
/**
* @param {String} two_dimensional_string
* @returns {any[][]}
*/
static split(two_dimensional_string) {
return two_dimensional_string.split('\n').map((row) => row.split(''));
}
reset() {
this.grid = new Map();
this.max_x = -Infinity;
this.min_x = Infinity;
this.max_y = -Infinity;
this.min_y = Infinity;
return this;
}
/**
* @param {String|any[][]} input
*/
load(input) {
this.reset();
let grid = input;
if (typeof input === 'string') {
grid = InfiniteGrid.split(input);
}
for (let y = 0; y < grid.length; y++) {
for (let x = 0; x < grid[y].length; x++) {
this.set(x, y, grid[y][x]);
}
}
}
/**
* @param {Number} x
* @param {Number} y
* @returns {Map} Return a map with optional keys N, W, E, S (if those neights are within the bounds of the map)
*/
neighbors(x, y) {
const neighboring_cells = new Map();
if (!this.inBounds(x, y)) {
return neighboring_cells;
}
const neighbors_lookup = [
['N', [x, y - 1]],
['W', [x - 1, y]],
['E', [x + 1, y]],
['S', [x, y + 1]],
];
for (let [key, coord] of neighbors_lookup) {
let [cx, cy] = coord;
if (this.inBounds(cx, cy)) {
neighboring_cells.set(key, { coord, value: this.get(cx, cy) });
}
}
return neighboring_cells;
}
/**
* @param {Number} x
* @param {Number} y
* @param {any} value
*/
set(x, y, value) {
if (typeof x !== 'number' || typeof y !== 'number') {
throw new Error(`x and y must be numbers, got (${typeof x})${x} and (${typeof y})${y}`);
}
if (x < this.min_x) this.min_x = x;
if (x > this.max_x) this.max_x = x;
if (y < this.min_y) this.min_y = y;
if (y > this.max_y) this.max_y = y;
const id = InfiniteGrid.toId(x, y);
this.grid.set(id, value);
}
/**
* @param {Number} x
* @param {Number} y
* @returns {any}
*/
get(x, y) {
const id = InfiniteGrid.toId(x, y);
if (!this.grid.has(id)) {
this.set(x, y, this.defaultFactory(x, y));
}
return this.grid.get(id);
}
/**
* @param {RegExp|any} value
* @param {Boolean} [as_coords] - When true, returns a Map of `[x, y]` number values, otherwise returns a Map of string IDs.
* @returns {Array<[any,GridId|Coord]>} - Returns an Array, the first value matching the cell found, and the 2nd the coords or ID.
*/
findAll(value, as_coords = true) {
const found = [];
for (let [id, cell] of this.grid) {
const check = value instanceof RegExp ? value.test(cell) : value === cell;
if (check) {
found.push([cell, as_coords ? InfiniteGrid.toCoords(id) : id]);
}
}
return found;
}
inBounds(x, y) {
return x >= this.min_x && x <= this.max_x && y >= this.min_y && y <= this.max_y;
}
clone() {
const infinite_grid_clone = new InfiniteGrid();
const new_map = new Map();
for (let [key, val] of this.grid) {
new_map.set(key, typeof val === 'object' ? JSON.parse(JSON.stringify(val)) : val);
}
infinite_grid_clone.defaultFactory = this.defaultFactory.bind(this);
infinite_grid_clone.string_map = JSON.parse(JSON.stringify(this.string_map));
infinite_grid_clone.grid = new_map;
infinite_grid_clone.max_x = this.max_x;
infinite_grid_clone.min_x = this.min_x;
infinite_grid_clone.max_y = this.max_y;
infinite_grid_clone.min_y = this.min_y;
return infinite_grid_clone;
}
toGrid() {
let grid = [];
for (let y = this.min_y; y <= this.max_y; y++) {
let row = [];
for (let x = this.min_x; x <= this.max_x; x++) {
let cell = this.get(x, y);
row.push(cell);
}
grid.push(row);
}
return grid;
}
sum() {
let sum = 0;
for (let value of this.grid.values()) {
sum += value;
}
return sum;
}
toString() {
let grid = this.toGrid();
let rows = '';
for (let y = 0; y < grid.length; y++) {
let row = '';
for (let x = 0; x < grid[y].length; x++) {
let cell = grid[y][x];
let cell_string = cell in this.string_map ? this.string_map[cell] : String(cell);
row += cell_string;
}
rows += rows.length ? '\n' + row : row;
}
return rows;
}
*[Symbol.iterator]() {
yield* this.grid.entries();
}
}
module.exports = {
InfiniteGrid,
};