generated from lloydevans/tiny-webpack-typescript
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathms-state.ts
475 lines (394 loc) · 9.1 KB
/
ms-state.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
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
import type { MSCellState } from "./ms-cell-state";
import type { MSGameConfig } from "./ms-config";
import * as PIXI from "pixi.js";
export const MIN_GRID_WIDTH = 4;
export const MIN_GRID_HEIGHT = 4;
export const MAX_GRID_WIDTH = PIXI.utils.isMobile.any ? 24 : 32;
export const MAX_GRID_HEIGHT = PIXI.utils.isMobile.any ? 24 : 32;
export const MIN_EMPTY = 2;
/**
*
*/
export interface LossData {
incorrect: MSCellState[];
correct: MSCellState[];
}
/**
* Class handles Minesweeper game state and logic.
*/
export class MSState {
public width: number = 0;
public height: number = 0;
public config: MSGameConfig = {
startMines: 1,
gridWidth: 4,
gridHeight: 4,
};
public get totalMines() {
return this.cells.filter((el) => el.mine).length;
}
public get totalFlags() {
return this.cells.filter((el) => el.flag).length;
}
public get totalCells() {
return this.cells.length;
}
public get flagCount() {
return this.totalMines - this.totalFlags;
}
private cells: MSCellState[] = [];
public init(config: MSGameConfig) {
if (config.gridWidth < MIN_GRID_WIDTH) {
throw new Error("Grid width below " + MIN_GRID_WIDTH);
}
if (config.gridHeight < MIN_GRID_HEIGHT) {
throw new Error("Grid height below " + MIN_GRID_HEIGHT);
}
if (config.gridWidth > MAX_GRID_WIDTH) {
throw new Error("Grid width bigger than " + MAX_GRID_WIDTH);
}
if (config.gridHeight > MAX_GRID_HEIGHT) {
throw new Error("Grid height bigger than " + MAX_GRID_HEIGHT);
}
if (config.startMines < 1) {
throw new Error("Must have at least 1 mine");
}
if (config.startMines > config.gridWidth * config.gridHeight - 2) {
throw new Error(
`Too many mines (${config.startMines}) for grid size: ${config.gridWidth} x ${config.gridHeight}`
);
}
this.config = { ...config };
this.width = config.gridWidth;
this.height = config.gridHeight;
this.initCells();
this.shuffleMines(config.startMines);
}
public reset() {
for (let x = 0; x < this.width; x++) {
for (let y = 0; y < this.height; y++) {
const cell = this.cells[this.indexOf(x, y)];
cell.x = x;
cell.y = y;
cell.adjacent = 0;
cell.mine = false;
cell.flag = false;
cell.covered = true;
}
}
this.shuffleMines(this.config.startMines);
}
private initCells() {
this.cells = [];
for (let x = 0; x < this.width; x++) {
for (let y = 0; y < this.height; y++) {
this.cells[this.indexOf(x, y)] = {
x,
y,
adjacent: 0,
covered: true,
mine: false,
flag: false,
};
}
}
}
public forEach(cb: (cell: MSCellState, i: number) => void) {
this.cells.forEach(cb);
}
public cellAt(x: number, y: number): MSCellState | undefined {
return this.cells[x + y * this.width];
}
/**
* Returns the value of the first cell where predicate is true, and undefined otherwise.
*/
public find(predicate: (value: MSCellState, index: number) => boolean): MSCellState | undefined {
return this.cells.find(predicate);
}
/**
* Place a flag at given coords.
*/
public placeFlag(x: number, y: number) {
const cell = this.cellAt(x, y);
if (!cell) {
throw new Error(`Can't find cell at ${x},${y}`);
}
cell.flag = true;
}
/**
* Clear flag at given coords.
*/
public clearFlag(x: number, y: number) {
const cell = this.cellAt(x, y);
if (!cell) {
throw new Error(`Can't find cell at ${x},${y}`);
}
cell.flag = false;
}
/**
* Place a mine at given coords.
*/
public placeMine(x: number, y: number) {
const cell = this.cellAt(x, y);
if (!cell) {
throw new Error(`Can't find cell at ${x},${y}`);
}
cell.mine = true;
}
/**
* Clear a mine at given coords.
*/
public clearMine(x: number, y: number) {
const cell = this.cellAt(x, y);
if (!cell) {
throw new Error(`Can't find cell at ${x},${y}`);
}
if (cell.mine) {
cell.mine = false;
}
}
/**
* Clear mines.
*/
public clearAllMines() {
for (let x = 0; x < this.width; x++) {
for (let y = 0; y < this.height; y++) {
this.clearMine(x, y);
}
}
}
/**
* Check and get losing cell states.
*/
public getLossData() {
const lossData: LossData = {
correct: [],
incorrect: [],
};
for (let i = 0; i < this.cells.length; i++) {
const cell = this.cells[i];
if (cell.mine && cell.flag) {
lossData.correct.push(cell);
}
if (cell.mine && !cell.flag) {
lossData.incorrect.push(cell);
}
if (!cell.mine && cell.flag) {
lossData.incorrect.push(cell);
}
}
return lossData;
}
/**
* Return array of unplaced flags.
*/
public getUnplacedFlags() {
return this.cells.filter((el) => el.mine && !el.flag);
}
/**
* Return array of unplaced flags.
*/
public getCorrectFlags() {
return this.cells.filter((el) => el.covered && el.mine && el.flag);
}
/**
* Check if the current game is in win state.
*
*/
public isWin() {
// Can we find a covered cell without a mine?
return !this.cells.find((el) => el.covered && !el.mine);
}
/**
* Get array index of some coords.
*
* @param x
* @param y
*/
public indexOf(x: number, y: number): number {
return (x % this.width) + y * this.width;
}
/**
* Check array index in bounds.
*
* @param x
* @param y
*/
public indexInBounds(index: number) {
return index > -1 && index < this.cells.length;
}
/**
* Get coords of an array index.
*
* @param index
*/
public coordsOf(index: number): [number, number] {
return [index % this.width, Math.floor(index / this.width)];
}
/**
* Check if coords are in bounds.
*
* @param x
* @param y
*/
public coordsInBounds(x: number, y: number) {
return x > -1 && x < this.width && y > -1 && y < this.height;
}
/**
* User action on a cell. Uncovered cells are returned in an array.
*
* @param x
* @param y
*/
public select(x: number, y: number): MSCellState[] {
const cell = this.cellAt(x, y);
if (!cell) {
throw new Error(`Can't find cell at ${x},${y}`);
}
const result: MSCellState[] = [];
if (cell.adjacent === 0 && !cell.mine) {
this.fill(x, y, result);
} //
else if (cell.adjacent > 0) {
result.push(cell);
this.uncover(x, y);
} //
else if (cell.mine) {
result.push(cell);
this.uncover(x, y);
}
return result;
}
/**
* Special logic for ensuring no mine is hit on first click.
*
* @param x
* @param y
*/
public selectFirst(x: number, y: number): MSCellState[] {
const cell = this.cellAt(x, y);
if (!cell) {
throw new Error(`Can't find cell at ${x},${y}`);
}
let foundMove = !cell.mine && cell.adjacent === 0;
let i = 0;
while (!foundMove) {
this.shuffleMines(this.config.startMines);
if (i < 1000) {
foundMove = !cell.mine && cell.adjacent === 0;
} else {
foundMove = !cell.mine;
}
i++;
}
return this.select(x, y);
}
/**
* Uncover a given cell.
*
* @param cell
*/
public uncover(x: number, y: number) {
const cell = this.cellAt(x, y);
if (!cell) {
throw new Error(`Can't find cell at ${x},${y}`);
}
cell.covered = false;
if (cell.flag) {
cell.flag = false;
}
}
/**
* Simple fill.
*
* @param x
* @param y
*/
private fill(x: number, y: number, result: MSCellState[]) {
const cell = this.cellAt(x, y);
if (!cell) {
throw new Error(`Can't find cell at ${x},${y}`);
}
if (cell.covered) {
result.push(cell);
this.uncover(x, y);
if (cell.adjacent > 0) {
return;
}
if (x > 0) {
this.fill(x - 1, y, result);
if (y > 0) {
this.fill(x - 1, y - 1, result);
}
if (y < this.height - 1) {
this.fill(x - 1, y + 1, result);
}
}
if (x < this.width - 1) {
this.fill(x + 1, y, result);
if (y > 0) {
this.fill(x + 1, y - 1, result);
}
if (y < this.height - 1) {
this.fill(x + 1, y + 1, result);
}
}
if (y > 0) {
this.fill(x, y - 1, result);
}
if (y < this.height - 1) {
this.fill(x, y + 1, result);
}
}
}
/**
* Calculate number of adjacent mines for all cells.
*/
public calculateAdjacent() {
const width = this.width;
const height = this.height;
for (let i = 0; i < this.cells.length; i++) {
const el = this.cells[i];
const x = i % width;
const y = Math.floor(i / width);
let count = 0;
if (x > 0) {
count += this.cells[i - 1].mine ? 1 : 0;
if (y < height - 1) {
count += this.cells[i - 1 + width].mine ? 1 : 0;
}
if (y > 0) {
count += this.cells[i - 1 - width].mine ? 1 : 0;
}
}
if (x < width - 1) {
count += this.cells[i + 1].mine ? 1 : 0;
if (y < height - 1) {
count += this.cells[i + 1 + width].mine ? 1 : 0;
}
if (y > 0) {
count += this.cells[i + 1 - width].mine ? 1 : 0;
}
}
if (y > 0) {
count += this.cells[i - width].mine ? 1 : 0;
}
if (y < height - 1) {
count += this.cells[i + width].mine ? 1 : 0;
}
el.adjacent = count;
}
}
public shuffleMines(total: number) {
this.clearAllMines();
const sequence: number[] = [];
while (sequence.length < total) {
const idx = Math.floor(Math.random() * this.cells.length);
if (sequence.indexOf(idx) === -1) {
sequence.push(idx);
}
}
sequence.forEach((idx) => (this.cells[idx].mine = true));
this.calculateAdjacent();
}
}