-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathboard.js
407 lines (349 loc) Β· 9.95 KB
/
board.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
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
'use strict';
const chalk = require('chalk');
const shuffleSeed = require('shuffle-seed');
const shortid = require('shortid');
const clone = require('clone');
const Base64 = require('js-base64').Base64;
const log4js = require('log4js');
/**
* Creates a mines board with specified params.
*
* @constructor
* @param {number} width Width of the board
* @param {number} height Height of the board
* @param {number} mines Number of mines on the board
*/
const Board = function(width, height, mines, id) {
this.width = width;
this.height = height;
this.mines = mines;
this.seed = (id) ? id : shortid.generate();
this.generated = false;
this.lost = false;
this.won = false;
this.squares = [];
this.reveals = [];
this.numberOfHiddenSafeSquares = 0;
this.startTime;
this.endTime;
this.flagCount = 0;
this.logger = log4js.getLogger(`Board ${this.seed}`);
this.logger.debug('Created!');
for (let x = 0; x < width; x++) {
this.squares[x] = [];
for (let y = 0; y < height; y++)
this.squares[x][y] = {
x: x,
y: y,
revealed: false,
flagged: false
};
}
};
Board.prototype.getDimensions = function() {
return {
width: this.width,
height: this.height,
mines: this.mines
};
};
/**
* Fills the board with mines.
* @param {number} x x-coordinate of the first click
* @param {number} y y-coordinate of the first click
*/
Board.prototype.generate = function(x, y) {
this.initialX = x;
this.initialY = y;
// mark reserved squares
this.squares[x][y].reserved = true;
var numberOfReservedSquares = 0;
var adjacentSquares = this.getAdjacentSquares(x, y);
for (let i in adjacentSquares) {
adjacentSquares[i].reserved = true;
numberOfReservedSquares++;
}
// distribute mines
var minesToPlace = this.mines;
var randomMines = [];
this.numberOfHiddenSafeSquares += numberOfReservedSquares + 1;
for (let i = 0; i < this.width * this.height - numberOfReservedSquares - 1; i++) {
if (minesToPlace > 0) {
randomMines.push(true);
minesToPlace--;
} else {
this.numberOfHiddenSafeSquares++;
randomMines.push(false);
}
}
randomMines = shuffleSeed.shuffle(randomMines, this.seed);
var currentIndexInRandomMines = 0;
for (let x in this.squares) {
for (let y in this.squares[x]) {
if (!this.squares[x][y].reserved) {
this.squares[x][y].mine = randomMines[currentIndexInRandomMines];
currentIndexInRandomMines++;
} else {
this.squares[x][y].mine = false;
}
}
}
// add counts to squares
for (let x in this.squares) {
for (let y in this.squares[x]) {
this.squares[x][y].count = this.mineCount(parseInt(x), parseInt(y));
}
}
this.generated = true;
this.startTime = Date.now();
this.logger.debug(`Start serialization: ${this.serializeStart()}`);
};
/**
* Returns adjacent squares to the one indicated.
* @param {number} x x-coordinate of the square
* @param {number} y y-coordinate of the square
* @return {Array<Object>} array of adjacent squares
*/
Board.prototype.getAdjacentSquares = function(x, y) {
var adjacentSquares = [];
// Squares to the left
if (this.squares[x - 1]) {
if (this.squares[x - 1][y - 1])
adjacentSquares.push(this.squares[x - 1][y - 1]);
if (this.squares[x - 1][y])
adjacentSquares.push(this.squares[x - 1][y]);
if (this.squares[x - 1][y + 1])
adjacentSquares.push(this.squares[x - 1][y + 1]);
}
// Square above and below
if (this.squares[x][y - 1])
adjacentSquares.push(this.squares[x][y - 1]);
if (this.squares[x][y + 1])
adjacentSquares.push(this.squares[x][y + 1]);
// Squares to the right
if (this.squares[x + 1]) {
if (this.squares[x + 1][y - 1])
adjacentSquares.push(this.squares[x + 1][y - 1]);
if (this.squares[x + 1][y])
adjacentSquares.push(this.squares[x + 1][y]);
if (this.squares[x + 1][y + 1])
adjacentSquares.push(this.squares[x + 1][y + 1]);
}
return adjacentSquares;
};
/**
* Get the squares that were revealed by the specified player.
* @param {string} username the username of the player
* @return {Array<Object>} array of squares
*/
Board.prototype.getSquaresByPlayer = function(username) {
var squares = [];
for (let x in this.squares)
for (let y in this.squares[x])
if (this.squares[x][y].revealedBy == username)
squares.push(this.squares[x][y]);
return squares;
};
/**
* Counts the number of mines adjacent to the specified square.
* @param {number} x x-coordinate of the square
* @param {number} y y-coordinate of the square
* @return {number} number of mines adjacent to the square
*/
Board.prototype.mineCount = function(x, y) {
var count = 0;
var adjacentSquares = this.getAdjacentSquares(x, y);
for (let i in adjacentSquares) {
if (adjacentSquares[i].mine)
count++;
}
return count;
};
/**
* Generates the board if not already generated based on the
* first click. Then reveals the clicked square and all
* adjacent squares if the clicked square was a 0. All
* 0 squares are further revealed.
* @param {number} x x-coordinate of the square to reveal
* @param {number} y y-coordinate of the square to reveal
* @param {string} [username] username of the person that revealed the square
* @return {Array<Object>} Array of squares that have been changed
*/
Board.prototype.reveal = function(x, y, username) {
if (!this.squares[x] || !this.squares[x][y])
return [];
if (!this.generated) {
this.generate(x, y);
username = 'default';
}
if (this.squares[x][y].revealed)
return [];
this.squares[x][y].revealed = true;
this.squares[x][y].revealedBy = username;
this.reveals.push({
x: x,
y: y
});
// check if was a flag
if (this.squares[x][y].flagged)
this.flagCount--;
// lose detection
if (this.squares[x][y].mine) {
this.lost = true;
} else {
this.squares[x][y].flagged = false;
this.numberOfHiddenSafeSquares--;
}
// win detection
if (this.numberOfHiddenSafeSquares == 0) {
this.won = true;
this.endTime = Date.now();
}
var changedSquares = [this.squares[x][y]];
if (this.squares[x][y].count === 0 && !this.squares[x][y].mine) {
var adjacentSquares = this.getAdjacentSquares(x, y);
for (let i in adjacentSquares)
changedSquares = changedSquares.concat(this.reveal(adjacentSquares[i].x, adjacentSquares[i].y, username));
}
return changedSquares;
};
Board.prototype.chordReveal = function(x, y, username) {
if (!this.squares[x] || !this.squares[x][y])
return [];
if (!this.generated)
return [];
if (!this.squares[x][y].revealed)
return [];
var adjacentSquares = this.getAdjacentSquares(x, y);
var numFlaggedSquares = adjacentSquares.filter(function(square) {
return square.flagged;
}).length;
if (numFlaggedSquares != this.squares[x][y].count)
return [];
var squaresToReveal = adjacentSquares.filter(function(square) {
return !square.revealed && !square.flagged;
});
var changedSquares = [];
for (let i in squaresToReveal)
changedSquares = changedSquares.concat(this.reveal(squaresToReveal[i].x, squaresToReveal[i].y, username));
return changedSquares;
};
Board.prototype.toggleFlag = function(x, y) {
if (this.squares[x][y].revealed)
return;
if (this.squares[x][y].flagged)
this.unflag(x, y);
else
this.flag(x, y);
return this.squares[x][y];
};
Board.prototype.unflag = function(x, y) {
this.squares[x][y].flagged = false;
this.flagCount--;
};
Board.prototype.flag = function(x, y) {
this.squares[x][y].flagged = true;
this.flagCount++;
};
/**
* Calculates the time it took for the player(s) to
* finish the game.
* @return {number} time elapsed in seconds
*/
Board.prototype.getTime = function() {
if (!(this.startTime && this.endTime))
return null;
return (this.endTime - this.startTime) / 1000;
};
Board.prototype.makeSquareSafeForPlayer = function(square) {
var squareForPlayer = clone(square);
if (squareForPlayer && !squareForPlayer.revealed) {
delete squareForPlayer.mine;
delete squareForPlayer.count;
}
return squareForPlayer;
};
/**
* Get all the squares on the board with the data
* that the user should not see removed from them.
* @return {Array<Object>} array of squares
*/
Board.prototype.getSquaresForPlayer = function() {
var squaresForPlayer = [];
for (let x in this.squares) {
squaresForPlayer[x] = [];
for (let y in this.squares[x]) {
squaresForPlayer[x][y] = this.makeSquareSafeForPlayer(this.squares[x][y]);
}
}
return squaresForPlayer;
};
/**
* Returns an array of all the squares that have not been revealed.
* This is usually used when the player hits and mine in order to
* show them where the remaining mines were.
* @return {Array<Object>} array of squares
*/
Board.prototype.getRemainingSquares = function() {
var remainingSquares = [];
for (let x in this.squares) {
for (let y in this.squares[x]) {
if (!this.squares[x][y].revealed) {
this.squares[x][y].lose = true;
remainingSquares.push(this.squares[x][y]);
}
}
}
return remainingSquares;
};
Board.prototype.toString = function() {
if (!this.squares)
return '';
var out = 'β';
for (let x in this.squares) {
out += 'ββ';
}
out += 'ββ\n';
for (let y in this.squares[0]) {
out += 'β ';
for (let x in this.squares) {
var square = '';
if (this.squares[x][y].mine)
square = chalk.bold.red('x');
else if (this.squares[x][y].count === 0)
square = chalk.gray(this.squares[x][y].count);
else if (this.squares[x][y].count > 0)
square = this.squares[x][y].count;
else
square = chalk.gray('?');
if (this.squares[x][y].revealed)
out += square;
else
out += chalk.dim(square);
out += ' ';
}
out += 'β\n';
}
out += 'β';
for (let x in this.squares) {
out += 'ββ';
}
out += 'ββ';
return out;
};
Board.prototype.serializeStart = function() {
return `${this.height}/${this.width}/${this.mines}/${this.seed}/${this.initialX}/${this.initialY}`;
};
Board.prototype.serialize = function() {
var stateData = {
height: this.height,
width: this.width,
mines: this.mines,
seed: this.seed,
initialX: this.initialX,
initialY: this.initialY,
reveals: this.reveals
};
return `${Base64.encode(JSON.stringify(stateData))}`;
};
module.exports = Board;