-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathboard.js
More file actions
461 lines (424 loc) · 13.1 KB
/
board.js
File metadata and controls
461 lines (424 loc) · 13.1 KB
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
const deepCopy = require('objtools').deepCopy;
/**
* This class represents a puzzle board and includes the dimensions (rows and columns),
* clues, and cell data. Cell data may or may not include unknowns. Also supported
* are color nonograms, where each cell can contain colors other than black.
*
* Board data is accessible at the `data` property and is a single-dimensional array
* of data. Accessors should be used to access elements.
*
* Dimensions are available as the `rows` and `cols` properties.
*
* Clues are accessible at the `rowClues` and `colClues` properties. These are arrays
* such that the array index corresponds to the number of row or column. 0,0 is at
* the upper-left of the puzzle. This array contains arrays of clue objects, where
* each clue object looks like `{ value: 1, run: 3 }`. `value` represents the color of
* the clue, and is always 1 for black-and-white puzzles. `run` is the count of the clue.
*
* Values in the data (and the clues) are represented as numbers. 0 is 'blank', and 1+
* are cell colors. The special value `null` can be used in the data (but not in the clues)
* to represent an unknown cell.
*
* @class Board
* @constructor
* @param {Number} rows - Number of rows, ie, height
* @param {Number} cols - Number of columns, ie, width
*/
class Board {
constructor(rows, cols) {
if (rows < 2) rows = 2;
if (cols < 2) cols = 2;
this.rows = rows;
this.cols = cols;
this.clearData(0);
this.rowClues = [];
this.colClues = [];
for (let i = 0; i < this.rows; i++) this.rowClues.push([]);
for (let i = 0; i < this.cols; i++) this.colClues.push([]);
}
/**
* Resizes the board
*
* @method resize
* @param {Number} newRows
* @param {Number} newCols
* @param {Number} defaultValue
*/
resize(newRows, newCols, defaultValue = 0) {
if (newRows < 2) newRows = 2;
if (newCols < 2) newCols = 2;
let haveUnknowns = false;
let newData = [];
for (let row = 0; row < newRows; row++) {
for (let col = 0; col < newCols; col++) {
let value;
if (col < this.cols && row < this.rows) {
value = this.get(row, col);
} else {
value = defaultValue;
}
newData[row * newCols + col] = value;
if (value === null) haveUnknowns = true;
}
}
this.rows = newRows;
this.cols = newCols;
this.data = newData;
if (!haveUnknowns) {
this.buildCluesFromData();
} else {
this.rowClues.length = this.rows;
this.colClues.length = this.cols;
for (let row = 0; row < this.rows; row++) {
if (!this.rowClues[row]) this.rowClues[row] = [];
}
for (let col = 0; col < this.cols; col++) {
if (!this.colClues[col]) this.colClues[col] = [];
}
}
}
/**
* Method to generate random board data.
*
* @method makeRandomBoard
* @static
* @param {Number} rows - Number of rows
* @param {Number} cols - Number of columns
* @param {Number} [values=1] - Number of values, 1 for black-and-white
* @param {Number} [density=null] - Density of filled-in cells. Default is to pick at random between 0.2 and 0.8 .
* @return {Board}
*/
static makeRandomBoard(rows, cols, values = 1, density = null) {
if (density === null) density = Math.random() * 0.6 + 0.2;
let board = new Board(rows, cols);
for (let i = 0; i < board.data.length; i++) {
if (Math.random() < density) {
board.data[i] = Math.floor(Math.random() * values) + 1;
} else {
board.data[i] = 0;
}
}
board.buildCluesFromData();
return board;
}
/**
* Fills the board data with all of the same value.
*
* @method clearData
* @param {Number} [value=null] - Value to set
*/
clearData(value = null) {
this.data = [];
for (let i = 0; i < this.rows * this.cols; i++) this.data.push(value);
}
/**
* Creates a string token that uniquely represents the full state of board data.
*
* @method makeToken
*/
makeToken() {
function valToken(val) {
if (val === null) return 'x';
if (Array.isArray(val)) {
return val.join('|');
}
return '' + val;
}
return this.data.map(valToken).join(',');
}
/**
* Take the board data and compute the board clues from it.
*
* @method buildCluesFromData
*/
buildCluesFromData() {
let { rowClues, colClues } = this._makeCluesFromData(false);
this.rowClues = rowClues;
this.colClues = colClues;
}
_makeCluesFromData(includeBlanks = false, countUnknownAsBlank = false) {
let rowClues = [];
for (let row = 0; row < this.rows; row++) {
let thisRowClues = [];
let lastValue = this.get(row, 0);
if (lastValue === null && countUnknownAsBlank) lastValue = 0;
let startOfRun = 0;
for (let col = 1; col <= this.cols; col++) {
let value = (col === this.cols) ? -1 : this.get(row, col);
if (value === null && countUnknownAsBlank) value = 0;
if (value !== lastValue || col === this.cols) {
if (typeof lastValue !== 'number') throw new Error('Cannot build clues from unknown grid');
let runLength = col - startOfRun;
if (lastValue !== 0 || includeBlanks) {
thisRowClues.push({ value: lastValue, run: runLength });
}
lastValue = value;
startOfRun = col;
}
}
rowClues.push(thisRowClues);
}
let colClues = [];
for (let col = 0; col < this.cols; col++) {
let thisColClues = [];
let lastValue = this.get(0, col);
if (lastValue === null && countUnknownAsBlank) lastValue = 0;
let startOfRun = 0;
for (let row = 1; row <= this.rows; row++) {
let value = (row === this.rows) ? -1 : this.get(row, col);
if (value === null && countUnknownAsBlank) value = 0;
if (value !== lastValue || row === this.rows) {
let runLength = row - startOfRun;
if (lastValue !== 0 || includeBlanks) {
thisColClues.push({ value: lastValue, run: runLength });
}
lastValue = value;
startOfRun = row;
}
}
colClues.push(thisColClues);
}
return { rowClues, colClues };
}
/**
* Returns true if there are no unknowns
*
* @method isComplete
* @return {Boolean}
*/
isComplete() {
for (let value of this.data) {
if (value === null) return false;
}
return true;
}
/**
* Checks for a valid solution. Returns true if valid.
*
* @method validate
* @return {Boolean}
*/
validate(countUnknownAsBlank = false) {
let { rowClues, colClues } = this._makeCluesFromData(false, countUnknownAsBlank);
for (let row = 0; row < this.rows; row++) {
if (rowClues[row].length !== this.rowClues[row].length) return false;
for (let i = 0; i < rowClues[row].length; i++) {
if (
rowClues[row][i].value !== this.rowClues[row][i].value ||
rowClues[row][i].run !== this.rowClues[row][i].run
) return false;
}
}
for (let col = 0; col < this.cols; col++) {
if (colClues[col].length !== this.colClues[col].length) return false;
for (let i = 0; i < colClues[col].length; i++) {
if (
colClues[col][i].value !== this.colClues[col][i].value ||
colClues[col][i].run !== this.colClues[col][i].run
) return false;
}
}
return true;
}
// 0 is blank, 1+ are colors, null is unknown
get(row, col) {
if (row >= this.rows || col >= this.cols) throw new Error('Out of bounds');
return deepCopy(this.data[row * this.cols + col]);
}
set(row, col, value) {
if (row >= this.rows || col >= this.cols) throw new Error('Out of bounds');
this.data[row * this.cols + col] = deepCopy(value);
}
getRow(row) {
let ar = Array(this.cols);
for (let i = 0; i < this.cols; i++) ar[i] = this.get(row, i);
return ar;
}
getCol(col) {
let ar = Array(this.rows);
for (let i = 0; i < this.rows; i++) ar[i] = this.get(i, col);
return ar;
}
setRow(row, line) {
for (let i = 0; i < this.cols; i++) this.set(row, i, line[i]);
}
setCol(col, line) {
for (let i = 0; i < this.rows; i++) this.set(i, col, line[i]);
}
/**
* Computes and returns the maximum value present across clues and data.
*
* @method getMaxValue()
* @return {Number}
*/
getMaxValue() {
let maxValue = 0;
for (let i = 0; i < this.data.length; i++) {
if (typeof this.data[i] === 'number' && this.data[i] > maxValue) maxValue = this.data[i];
else if (Array.isArray(this.data[i])) {
for (let possibleValue of this.data[i]) {
if (possibleValue > maxValue) maxValue = possibleValue;
}
}
}
for (let clues of [ this.rowClues, this.colClues ]) {
for (let rcClues of clues) {
for (let clue of rcClues) {
if (clue.value > maxValue) maxValue = clue.value;
}
}
}
return maxValue;
}
/**
* Prints to the console a textual representation of this board.
*
* @method printBoard
* @param {String} [blankStr='X'] - Character to use for blank cells
* @param {String} [unknownStr=' '] - Character to use for unknown cells
*/
printBoard(blankStr = 'X', unknownStr = ' ') {
let maxValue = this.getMaxValue();
function clueStr(clue) {
if (maxValue > 1) {
return `${clue.run}(${clue.value})`;
} else {
return '' + clue.run;
}
}
function valStr(value) {
if (typeof value !== 'number') return unknownStr;
if (value === 0) return blankStr;
return '' + value;
}
function padStr(str, len, padStyle = 'left') {
let padding = '';
let padLen = len - str.length;
for (let i = 0; i < padLen; i++) {
padding += ' ';
}
if (padStyle === 'left') {
return padding + str;
} else if (padStyle === 'right') {
return str + padding;
} else {
padding = padding.slice(0, Math.floor(padLen / 2));
return padStr(str + padding, len, 'left');
}
}
function padTo(value, len) {
return padStr(valStr(value), len, 'center');
}
let maxCellLength = 1;
for (let i = 0; i < this.data.length; i++) {
let str = valStr(this.data[i]);
if (str.length > maxCellLength) maxCellLength = str.length;
}
for (let col = 0; col < this.colClues.length; col++) {
for (let i = 0; i < this.colClues[col].length; i++) {
let str = clueStr(this.colClues[col][i]);
if (str.length > maxCellLength) maxCellLength = str.length;
}
}
let maxNumColClues = 0;
for (let i = 0; i < this.colClues.length; i++) {
if (this.colClues[i].length > maxNumColClues) maxNumColClues = this.colClues[i].length;
}
const rowSeparator = '-';
const colSeparator = ' | ';
const rowClueSpacing = ' ';
const clueColSeparator = ' | ';
const clueRowSeparator = '+';
// Generate row clues
let rowClueStrs = [];
let maxRowClueStrLen = 0;
for (let row = 0; row < this.rowClues.length; row++) {
let thisRowClues = this.rowClues[row];
let thisRowClueStr = '';
for (let i = 0; i < thisRowClues.length; i++) {
if (i > 0) thisRowClueStr += rowClueSpacing;
thisRowClueStr += clueStr(thisRowClues[i]);
}
if (thisRowClueStr.length > maxRowClueStrLen) maxRowClueStrLen = thisRowClueStr.length;
rowClueStrs.push(thisRowClueStr);
}
// Pad rowClueStrs and add separator
for (let row = 0; row < rowClueStrs.length; row++) {
rowClueStrs[row] = padStr(rowClueStrs[row], maxRowClueStrLen) + clueColSeparator;
}
const printRowSeparatorLine = (c) => {
let str = '';
let len = maxRowClueStrLen + this.cols * (maxCellLength + colSeparator.length);
for (let i = 0; i < len; i++) str += c;
console.log(str);
};
// Print column clue rows
for (let colClueRowNum = 0; colClueRowNum < maxNumColClues; colClueRowNum++) {
let colClueRowStr = padStr('', maxRowClueStrLen) + colSeparator;
for (let col = 0; col < this.colClues.length; col++) {
if (col !== 0) colClueRowStr += colSeparator;
let thisColClues = this.colClues[col];
if (colClueRowNum < maxNumColClues - thisColClues.length) {
colClueRowStr += padStr('', maxCellLength);
} else {
let clue = thisColClues[colClueRowNum - (maxNumColClues - thisColClues.length)];
colClueRowStr += padStr(clueStr(clue), maxCellLength);
}
}
console.log(colClueRowStr);
}
// Print data rows
for (let row = 0; row < this.rows; row++) {
printRowSeparatorLine(row === 0 ? clueRowSeparator : rowSeparator);
let rowStr = '';
rowStr += rowClueStrs[row];
for (let col = 0; col < this.cols; col++) {
if (col > 0) rowStr += colSeparator;
rowStr += padTo(this.get(row, col), maxCellLength);
}
console.log(rowStr);
}
}
serialize() {
function serializeClues(lineClues) {
return lineClues.map((clues) => {
return clues.map((clue) => {
return `${clue.value}x${clue.run}`;
}).join('.');
}).join(',');
}
return [
this.rows,
this.cols,
serializeClues(this.rowClues),
serializeClues(this.colClues),
this.data.map((val) => {
return val === null ? 'x' : val;
}).join(',')
].join('|');
}
static deserialize(str) {
function deserializeClues(str) {
let lines = str.split(',');
return lines.map((lineStr) => {
if (lineStr === '') return [];
let clues = lineStr.split('.');
return clues.map((clueStr) => {
let clueParts = clueStr.split('x');
return { value: parseInt(clueParts[0]), run: parseInt(clueParts[1]) };
});
});
}
let parts = str.split('|');
let rows = parseInt(parts[0]);
let cols = parseInt(parts[1]);
let board = new Board(rows, cols);
board.rowClues = deserializeClues(parts[2]);
board.colClues = deserializeClues(parts[3]);
board.data = parts[4].split(',').map((str) => {
return (str === 'x') ? null : parseInt(str);
});
return board;
}
}
module.exports = Board;