-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnew_board.js
359 lines (327 loc) · 10.9 KB
/
new_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
// WARNING - print functions are very ugly!
// Import below is required to run each animation.
// const { execSync } = require("child_process");
class Board {
constructor(width, height) {
this.width = width || 10;
this.height = height || 10;
this.board = Array(this.height) // Create the array of rows
.fill('.') // Fill anything to avoid undefined value
.map(row => Array(this.width).fill('.')); // Each row is an array of characters.
this.rowIndex = this.height - 1;
this.puzzleIndex = 0;
}
fillLastRow() {
const gaps = this.findGaps();
gaps.forEach(gap => this.fillGap(gap));
this.rowIndex -= 1;
// Return if is possible to fill another rows
return this.rowIndex > 0;
}
findGaps() {
const row = this.board[this.rowIndex];
const gaps = [];
let pointer = 0;
let print = (label, start, end) => {
return; // break to free the animation!
console.clear();
this.toColorConsole();
const sp = start * 2;
const ep = end * 2;
const wp = (ep - sp);
console.log("".padStart(wp, "↑").padStart(ep, " "));
console.log(`${label}=${end - 1}`.padStart(ep, " "));
gaps.forEach(([start, end]) => {
console.log(`[${start}:${end}]`.padStart(ep, " "));
});
execSync("sleep 2");
}
while(pointer < this.width) {
// Find next empty cell
const start = row.findIndex((c, i) => i >= pointer && c == ".");
if (start == -1) { // not found
break;
}
print("start", start, start + 1);
// Find next non-empty cell
const end = row.findIndex((c, i) => i >= start && c != ".");
if (end == -1) { // not found
gaps.push([start, this.width - 1]);
print("end", start, this.width);
break;
}
gaps.push([start, end - 1]);
print("end", start, end);
pointer = end;
}
return gaps;
}
fillGap([start, end]) {
let pointer = start;
let print = () => {
return; // break to free the animation!
console.clear();
this.toColorConsole();
const p = (pointer + 1) * 2;
console.log("↑↑".padStart(p, " "));
console.log(`pointer=${pointer}`.padStart(p, " "));
execSync("sleep 0.25");
}
print();
while(pointer <= end) {
const coords = [pointer, this.rowIndex];
const puzzle = this.pickRandomPuzzle(coords);
if (puzzle === null) {
// Let's try with another place.
pointer += 1;
continue;
}
this.putPuzzle(puzzle, coords);
pointer += puzzle.lastRowWidth;
print();
}
}
pickRandomPuzzle(coords) {
const validPuzzles = PUZZLES
.filter(puzzle => this.isPuzzleMatching(puzzle, coords));
if (validPuzzles.length == 0) {
// Any puzzle is not valid.
return null;
}
const index = Math.floor(Math.random() * validPuzzles.length);
return validPuzzles[index];
}
isPuzzleMatching(puzzle, [startX, startY]) {
let print = (puzzleX, puzzleY, state, wait) => {
return; // break to free the animation!
console.clear();
let s = '';
for(let y = -1 - startY + 3; y < this.height - 6; y++) {
const boardY = startY + y - 3;
const boardRow = this.board[boardY] || [];
for(let x = -1 - startX; x < this.width + 1 - startX; x++) {
const boardX = startX + x - puzzle.lastRowStart;
const boardCell = boardRow[boardX] || "!";
const isInside = y >= 0 && x >= 0 && y < 4 && x < 4;
const collision = isInside ? puzzle.array[y][x] == "#" : false;
if (boardX == puzzleX && boardY == puzzleY) {
if (state == "NO MATCH") {
s += '\x1b[31m!!\x1b[0m';
} else {
s += '\x1b[34m??\x1b[0m';
}
} else {
const d = boardCell.charCodeAt() - 48;
let fg = 31;
if (boardCell == "!") fg = 31;
else if (boardCell == ".") fg = isInside ? 97 : 90;
else if (collision) fg = 97;
else fg = parseInt(d) + (d < 7 ? 31 : 91 - 7);
const bg = d >= 0 && d <= 9 ? parseInt(d) + (d < 7 ? 41 : 101 - 7) : 49;
const cell = collision ? "▒▒" : "..";
s += `\x1b[${fg}m\x1b[${bg}m${cell}\x1b[0m`;
}
}
s += '\n';
}
console.log(s, state);
execSync("sleep 0.05");
if (wait) {
execSync("sleep 2");
}
}
// The main idea is to check board cell with puzzle cell.
// So we have to check 4x4 area.
for(let y = 0; y < 4; y++) {
for(let x = 0; x < 4; x++) {
const puzzleCell = puzzle.array[y][x];
// Assume start of puzzle coordination
// is first left bottom non-empty cell.
const boardX = startX + x - puzzle.lastRowStart;
const boardY = startY + y - 3;
// boardX/Y could be a negative / above size of board.
// so we need to check if row and cell exists.
// If coords are above size of board then
// we can treat cell as "solid".
const boardRow = this.board[boardY] || [];
const boardCell = boardRow[boardX] || "#";
if (boardCell != "." && puzzleCell != ".") {
// Collision between board and puzzle,
// so puzzle can't fit in the board.
print(boardX, boardY, "NO MATCH", true);
return false;
} else {
print(boardX, boardY, "MATCHING", false);
}
}
}
// All cells are matching with board.
print("?", "?", "MATCH", true);
return true;
}
putPuzzle(puzzle, [startX, startY]) {
// Assume a puzzle can be added to the board.
// Otherwise it could "damage" the board.
for(let y = 0; y < 4; y++) {
for(let x = 0; x < 4; x++) {
const puzzleCell = puzzle.array[y][x];
if (puzzleCell == ".") {
continue;
}
// Assume start of puzzle coordination
// is first left bottom non-empty cell.
const boardX = startX + x - puzzle.lastRowStart;
const boardY = startY + y - 3;
if ( // check if cell on the board exists.
boardX >= 0
&& boardX < this.width
&& boardY >= 0
&& boardY < this.height) {
const ascii = 48 + this.puzzleIndex % 10;
const char = String.fromCharCode(ascii);
this.board[boardY][boardX] = char;
}
}
}
this.puzzleIndex += 1;
}
toConsole() {
console.log( // Convert the array to string
this.board.map(row => row.join('')).join('\n')
);
}
toColorConsole() {
console.log(
this.board.map(
row => row
.join('')
.replace(/\d/g, w => {
const d = w.charCodeAt() - 48;
const fg = parseInt(d) + (d < 7 ? 31 : 91 - 7);
const bg = parseInt(d) + (d < 7 ? 41 : 101 - 7);
return `\x1b[${fg}m\x1b[${bg}m${w + w}\x1b[0m`;
})
.replace(/\./g, '..')
).join('\n')
);
}
}
class Puzzle {
constructor(array) {
this.array = array;
const lastRow = array[3];
this.lastRowStart = lastRow.indexOf("#");
this.lastRowStop = lastRow.lastIndexOf("#");
this.lastRowWidth = this.lastRowStop - this.lastRowStart + 1;
}
}
function makePuzzleWithRotating(puzzle) {
const rotations = [puzzle];
let rot = puzzle;
for(let i=0; i < 4; i++) {
const newRot = shiftToBottomLeft(rotate90(rot));
const isAnyEqual = rotations.some(r => isArrayEquals(r, newRot));
if (!isAnyEqual) {
rotations.push(newRot);
}
rot = newRot;
}
return rotations.map(p => new Puzzle(p));
}
function rotate90(array) {
// Let's create an array of arrays.
const newArray = Array(4).fill('.').map(() => ['.', '.', '.', '.']);
for(let y=0; y < 4; y++) {
for(let x=0; x < 4; x++) {
const cell = array[y][x];
const row = newArray[4 - x - 1];
row[y] = cell;
}
}
// Return to array of strings.
return newArray.map(c => c.join(''));
}
function shiftToBottomLeft(array) {
array = shiftToBottom(array);
array = shiftToLeft(array);
return array;
}
function shiftToBottom(array) {
const newArray = [...array];
// If the last row is empty
// then drop him
// and put on the top.
while(newArray[3] == '....') {
const row = newArray.pop();
newArray.unshift(row);
}
return newArray;
}
function shiftToLeft(array) {
let newArray = [...array];
// If the first column is empty
// then for each row drop the first cell
// and put empty cell on the end of row.
while(newArray.every(col => col[0] == '.')) {
newArray = newArray.map(col => col.substr(1) + '.');
}
return newArray;
}
function isArrayEquals(a, b) {
// Checks each cell.
for(let y=0; y < 4; y++) {
for(let x=0; x < 4; x++) {
if (a[y][x] != b[y][x]) {
return false;
}
}
}
return true;
}
const PUZZLES = [
[
'....',
'....',
'##..',
'##..',
],
[
'....',
'....',
'##..',
'.##.',
],
[
'#...',
'#...',
'#...',
'#...',
],
[
'....',
'....',
'.##.',
'##..',
],
[
'....',
'#...',
'#...',
'##..',
],
[
'....',
'.#..',
'.#..',
'##..',
],
[
'....',
'....',
'.#..',
'###.',
],
].map(makePuzzleWithRotating).flat();
const b = new Board(60, 40);
while(b.fillLastRow());
b.toColorConsole();