Repository navigation
Expand file tree
/
Copy pathmain.js
More file actions
256 lines (224 loc) · 6.44 KB
/
Copy pathmain.js
File metadata and controls
256 lines (224 loc) · 6.44 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
import {
compareDown,
compareLeft,
compareRight,
compareUp,
getPossibleNeighborCells,
updateCell,
} from "./cell.js";
import { drawTile } from "./draw.js";
import {
CANVAS_SIZE,
createEmptyBoard,
createTileSelector,
currentDim,
DIM,
getRandomCell,
getRandomTile,
setCurrentDim,
setSize,
setTileList,
} from "./utils.js";
// The dimension is the width and height of the board
//faces = [up, right, down, left]
export const EMPTY = { src: "img/blank.png", faces: [0, 0, 0, 0] };
const DOWN = { src: "img/down.png", faces: [0, 1, 1, 1] };
const LEFT = { src: "img/left.png", faces: [1, 0, 1, 1] };
const RIGHT = { src: "img/right.png", faces: [1, 1, 1, 0] };
const UP = { src: "img/up.png", faces: [1, 1, 0, 1] };
const DOWN_END = { src: "img/down_end.png", faces: [1, 0, 0, 0] };
const LEFT_END = { src: "img/left_end.png", faces: [0, 0, 0, 1] };
const RIGHT_END = { src: "img/right_end.png", faces: [0, 1, 0, 0] };
const UP_END = { src: "img/up_end.png", faces: [0, 0, 1, 0] };
const CORNER_DOWN_RIGHT = {
src: "img/corner_down_right.png",
faces: [0, 1, 1, 0],
};
const CORNER_DOWN_LEFT = {
src: "img/corner_down_left.png",
faces: [0, 0, 1, 1],
};
const CORNER_UP_RIGHT = { src: "img/corner_up_right.png", faces: [1, 1, 0, 0] };
const CORNER_UP_LEFT = { src: "img/corner_up_left.png", faces: [1, 0, 0, 1] };
const HORIZONTAL = { src: "img/horizontal.png", faces: [0, 1, 0, 1] };
const VERTICAL = { src: "img/vertical.png", faces: [1, 0, 1, 0] };
const POSSIBLE_TILES = [
DOWN,
LEFT,
RIGHT,
UP,
HORIZONTAL,
VERTICAL,
DOWN_END,
LEFT_END,
UP_END,
RIGHT_END,
CORNER_DOWN_RIGHT,
CORNER_DOWN_LEFT,
CORNER_UP_LEFT,
CORNER_UP_RIGHT,
];
// The tiles that are used in the image
const TILE_LIST = [
DOWN,
LEFT,
RIGHT,
UP,
HORIZONTAL,
VERTICAL,
DOWN_END,
LEFT_END,
UP_END,
RIGHT_END,
CORNER_DOWN_RIGHT,
CORNER_DOWN_LEFT,
CORNER_UP_LEFT,
CORNER_UP_RIGHT,
];
let board;
let cells = [];
let newCells = [];
let avalaibleCells = [];
let pause = false;
let speed = 300; // speed is in ms
let interval;
window.onload = () => {
const tileSelector = createTileSelector(POSSIBLE_TILES);
document.getElementById("tileSelectorDiv").append(tileSelector);
for (let tile of POSSIBLE_TILES) {
document
.getElementById(tile.src.replace("img/", "").replace(".png", ""))
.addEventListener("click", setTileList);
}
document.getElementById("speedRange").setAttribute("value", speed);
document.getElementById("speedInput").setAttribute("value", speed);
document.getElementById("pauseButton").addEventListener("click", () => {
pause = !pause;
});
document.getElementById("generateBtn").addEventListener("click", () => {
clearInterval(interval);
try {
document.getElementById("board").remove();
} catch (error) {
console.log(error);
}
setup();
interval = setInterval(loop, speed);
});
document.getElementById("sizeSelector").addEventListener("change", setSize);
document.getElementById("speedRange").addEventListener("change", setSpeed);
document.getElementById("speedInput").addEventListener("input", setSpeed);
};
function setup() {
setCurrentDim(DIM);
const emptyCell = {
coordinates: { x: undefined, y: undefined },
possibleTiles: TILE_LIST.slice(),
// If a tile has been set on this cell or not
collapsed: false,
// If a tile can be set on the cell
inRange: true,
// When collapsed true this contains the current tile on the cell
currentTile: null,
};
cells = Array.from({ length: currentDim }, (e, y) =>
Array.from({ length: currentDim }, (e, x) => {
const newEmptyCell = Object.assign({}, emptyCell);
newEmptyCell.coordinates = { x: x, y: y };
return Object.assign({}, newEmptyCell);
})
);
avalaibleCells = cells.flat();
newCells = [];
board = createEmptyBoard(CANVAS_SIZE, CANVAS_SIZE);
document.getElementById("boardDiv").append(board);
const startCell = getRandomCell(avalaibleCells);
const startTile = getRandomTile(startCell.possibleTiles);
updateCell(startTile, startCell, avalaibleCells);
updateNeighborCells(startCell);
drawTile(startTile, startCell.coordinates.x, startCell.coordinates.y);
newCells.push(startCell);
}
function loop() {
let newCell, newTile;
if (!pause) {
// When all the cells are collapsed we stop the interval
if (avalaibleCells.length === 0) {
console.log("Finished");
clearInterval(interval);
}
let neighborCells;
let possibleNeighborCells;
try {
neighborCells = getNeighbors(newCells[0], cells);
possibleNeighborCells = getPossibleNeighborCells(neighborCells);
newCell = getRandomCell(possibleNeighborCells);
if (newCell) {
if (newCell.possibleTiles) {
newTile = getRandomTile(newCell.possibleTiles);
} else {
newTile = EMPTY;
}
if (newTile === undefined) {
newTile = EMPTY;
}
drawTile(newTile, newCell.coordinates.x, newCell.coordinates.y);
updateCell(newTile, newCell, avalaibleCells);
updateNeighborCells(newCell);
newCells.push(newCell);
} else {
newCells.shift();
}
} catch (error) {
console.log("err loop", error);
newCells.shift();
}
}
}
function getNeighbors(fromCell, cells) {
const cellX = fromCell.coordinates.x;
const cellY = fromCell.coordinates.y;
let neighborCells = [];
for (let x = cellX - 1; x <= cellX + 1; x++) {
for (let y = cellY - 1; y <= cellY + 1; y++) {
try {
if (
y >= 0 &&
x >= 0 &&
y <= currentDim - 1 &&
x <= currentDim - 1
) {
neighborCells.push(cells[y][x]);
} else {
neighborCells.push(null);
}
} catch (error) {}
}
}
// We only need the top, right, bottom, left cell. So we remove the neighbors corner cells
// we don't remove the null values because we need to alwasy have 4 cells in the array to know the postion of each cell (neighbor array[left, up, right, down])
neighborCells = neighborCells.filter((cell, index) => index % 2 === 1);
return neighborCells;
}
function setSpeed() {
speed = this.value;
clearInterval(interval);
interval = setInterval(loop, speed);
}
function updateNeighborCells(cell) {
const neighbors = getNeighbors(cell, cells);
// we only want to update the non collapsed neighbor cells because the collapsed cells don't need to be updated
const neighborToUpdate = getPossibleNeighborCells(neighbors);
const left = 0,
up = 1,
down = 2,
right = 3;
if (cell) {
compareLeft(cell, neighborToUpdate[left]);
compareRight(cell, neighborToUpdate[right]);
compareUp(cell, neighborToUpdate[up]);
compareDown(cell, neighborToUpdate[down]);
}
}
setup();
interval = setInterval(loop, speed);