-
Notifications
You must be signed in to change notification settings - Fork 1
/
aswh.js
executable file
·435 lines (374 loc) · 15.8 KB
/
aswh.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
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
var aswh = aswh || {};
aswh.PRB_ELBOW = 0.5; // Probability of generating an elbow pipe when a straight pipe is valid
aswh.PRB_ALIGNED = 0.35; // Probability that a pipe in the generated solution is guaranteed to be aligned
document.addEventListener("DOMContentLoaded", function() {
"use strict";
var sets, // Number of sets
rows, // Number of rows per set
columns; // Number of columns per row
var cellPipe, // 3D array where [i][j][k] is the type of pipe at row j, column k of set i
connected, // 3D array whose values are true iff the corresponding pipe is connected to the start
pipeElems,
chain, // 3D array where ([i][j][0], [i][j][1]) are the coordinates of the (j-1)th pipe in the chain of set i
inProgress = []; // inProgress[i] === false iff set i is finished
var startTime, // The date at which the puzzles were created
countCompleted, // Counter for the number of sets completed so far
timeStats = {}; // A hash table that maps from "sets,rows,columns" to the best/average times of that board
var pipeMap = [ // Types of pipes
"LR", // left-right
"TB", // top-bottom
"TR", // top-right
"BR", // bottom-right
"BL", // bottom-left
"TL" // top-left
];
// rot[i] is what a pipe of type i becomes after rotation
var rot = [1, 0, 3, 4, 5, 2];
// left[i] === true iff a pipe of type i connects to the left
var left = [true, false, false, false, true, true],
right = [true, false, true, true, false, false],
up = [false, true, true, false, false, true],
down = [false, true, false, true, true, false];
var hasAudio = !!document.createElement("audio").canPlayType,
MAX_AUDIO_CHANNELS,
audioChannels;
if (hasAudio) {
MAX_AUDIO_CHANNELS = 8;
audioChannels = new Array(MAX_AUDIO_CHANNELS);
for (var i = 0; i < MAX_AUDIO_CHANNELS; i++) {
audioChannels[i] = [];
audioChannels[i].channel = new Audio();
audioChannels[i].endTime = -1;
}
}
function playSound(s) {
if (hasAudio && !document.getElementById("muteSound").checked) {
for (var i = 0; i < audioChannels.length; i++) {
var theTime = new Date().getTime();
if (audioChannels[i].endTime < theTime) {
audioChannels[i].endTime = theTime + document.getElementById(s).duration * 1000;
audioChannels[i].channel.src = document.getElementById(s).src;
audioChannels[i].channel.load();
audioChannels[i].channel.play();
break;
}
}
}
}
/** Returns true iff the given board dimensions are valid. If there is invalid input, then an alert will be issued. */
function validDimensions(sets, rows, columns) {
var errors = [];
if (sets < 1) errors.push("The number of sets must be at least 1.");
if (rows < 2) errors.push("The number of rows must be at least 2.");
if (columns < 2) errors.push("The number of columns must be at least 2.");
if (errors.length) {
window.alert(errors.join("\n"));
return false;
}
return true;
}
function startGame() {
document.getElementById("pipeArea").className = "";
startTime = new Date().getTime();
for (var i = 0; i < sets; i++) {
inProgress[i] = true;
}
}
/** Create the puzzle. */
function initialize() {
countCompleted = 0;
var divMessage = document.getElementById("message");
while (divMessage.firstChild) divMessage.removeChild(divMessage.firstChild);
sets = parseInt(document.getElementById("numSets").value, 10);
rows = parseInt(document.getElementById("numRows").value, 10);
columns = parseInt(document.getElementById("numColumns").value, 10);
var countdown = parseFloat(document.getElementById("timer").value) * 1000;
if (isNaN(sets) || isNaN(rows) || isNaN(columns) || isNaN(countdown)) {
window.alert("Non-numerical input detected.");
return;
}
if (!validDimensions(sets, rows, columns)) return;
var divPipe = document.getElementById("pipeArea");
divPipe.className = "setup";
while (divPipe.firstChild) divPipe.removeChild(divPipe.firstChild);
cellPipe = new Array(sets);
connected = new Array(sets);
pipeElems = new Array(sets);
chain = new Array(sets);
inProgress = new Array(sets);
for (var i = 0; i < sets; i++) {
var table = document.createElement("table");
cellPipe[i] = new Array(rows);
connected[i] = new Array(rows);
pipeElems[i] = new Array(rows);
chain[i] = [];
inProgress[i] = false;
var row = new Array(rows);
for (var j = 0; j < rows; j++)
row[j] = table.insertRow(j);
var firstCell = row[0].insertCell(0);
firstCell.rowSpan = rows;
for (var j = 0; j < rows; j++) {
cellPipe[i][j] = new Array(columns);
connected[i][j] = new Array(columns);
pipeElems[i][j] = new Array(columns);
for (var k = 0; k < columns; k++) {
var cell = row[j].insertCell(-1);
cell.setAttribute("data-set", i);
cell.setAttribute("data-row", j);
cell.setAttribute("data-col", k);
cell.className = 'unconnected';
cell.oncontextmenu = function() { return false; } // Disable right-click
pipeElems[i][j][k] = cell;
cellPipe[i][j][k] = -1;
}
}
var lastCell = row[0].insertCell(-1);
lastCell.rowSpan = rows;
lastCell.className = 'unconnected';
divPipe.appendChild(table);
randomize(i);
}
window.setTimeout(startGame, countdown);
}
/** Create a random puzzle for set i. */
function randomize(i) {
var direction = 0; // (right, up, down) = (0, 1, 2)
var j = 0, k = 0;
while (k < columns - 1) {
var aligned = Math.random() < aswh.PRB_ALIGNED;
if (j === 0 && direction === 1 || j === rows - 1 && direction === 2) {
// At the top or bottom row with a connected pipe below/above
cellPipe[i][j][k] = aligned ? 4 - direction : randomPipe(true);
direction = 0;
} else {
if (Math.random() > aswh.PRB_ELBOW) {
cellPipe[i][j][k] = aligned ? Math.ceil(direction / 2) : randomPipe(false);
} else {
if (direction > 0) {
cellPipe[i][j][k] = aligned ? direction + 1 : randomPipe(true);
direction = 0;
} else if (j === 0) {
cellPipe[i][j][k] = aligned ? 4 : randomPipe(true);
direction = 2;
} else if (j === rows - 1) {
cellPipe[i][j][k] = aligned ? 5 : randomPipe(true);
direction = 1;
} else if (Math.random() < skewPrb(j / (rows - 1))) {
cellPipe[i][j][k] = aligned ? 5 : randomPipe(true);
direction = 1;
} else {
cellPipe[i][j][k] = aligned ? 4 : randomPipe(true);
direction = 2;
}
}
}
pipeElems[i][j][k].setAttribute('data-type', pipeMap[cellPipe[i][j][k]]);
if (direction === 0) k++;
else if (direction === 1) j--;
else if (direction === 2) j++;
}
if (j === rows - 1) {
cellPipe[i][j][k] = Math.random() < aswh.PRB_ALIGNED ? 0 : randomPipe(false);
pipeElems[i][j][k].setAttribute('data-type', pipeMap[cellPipe[i][j][k]]);
} else {
cellPipe[i][j][k] = Math.random () < aswh.PRB_ALIGNED ? 4 : randomPipe(true);
pipeElems[i][j][k].setAttribute('data-type', pipeMap[cellPipe[i][j][k]]);
for (var y = j + 1; y < rows - 1; y++) {
cellPipe[i][y][k] = Math.random() < aswh.PRB_ALIGNED ? 1 : randomPipe(false);
pipeElems[i][y][k].setAttribute('data-type', pipeMap[cellPipe[i][y][k]]);
}
cellPipe[i][rows - 1][k] = Math.random() < aswh.PRB_ALIGNED ? 2 : randomPipe(true);
pipeElems[i][rows - 1][k].setAttribute('data-type', pipeMap[cellPipe[i][rows - 1][k]]);
}
for (var y = 0; y < rows; y++) {
for (var x = 0; x < columns; x++) {
if (cellPipe[i][y][x] === -1) {
cellPipe[i][y][x] = randomPipe(Math.random() < aswh.PRB_ELBOW);
pipeElems[i][y][x].setAttribute('data-type', pipeMap[cellPipe[i][y][x]]);
}
}
}
checkConnections(i, 0, 0); // Make sure puzzle isn't solved before the player touches it
}
function skewPrb(x) {
return Math.pow(x, 6 * rows / columns - 2);
}
/** Return a random type of pipe. An elbow pipe will be returned iff elbow is true. */
function randomPipe(elbow) {
if (elbow)
return Math.floor(Math.random() * 4) + 2;
return Math.floor(Math.random() * 2);
}
/** Rotate the pipe corresponding to the Image element e. */
function rotate(e) {
var i = parseInt(e.getAttribute("data-set"), 10),
j = parseInt(e.getAttribute("data-row"), 10),
k = parseInt(e.getAttribute("data-col"), 10);
cellPipe[i][j][k] = rot[cellPipe[i][j][k]];
e.setAttribute("data-type", pipeMap[cellPipe[i][j][k]]);
checkConnections(i, j, k);
playSound("sndRotate");
}
function checkConnections(i, j, k) {
var x, y;
if (connected[i][j][k]) {
// (j, k) lies in the chain
var position = [j, k];
var lastElement;
do {
// (j, k) is in the chain, so a rotation at (j, k) would break
// the chain around that point. Disconnect every pipe after it.
lastElement = chain[i].pop();
connected[i][lastElement[0]][lastElement[1]] = false;
pipeElems[i][lastElement[0]][lastElement[1]].className = 'unconnected';
} while(position[0] !== lastElement[0] || position[1] !== lastElement[1]);
}
if (chain[i].length === 0) {
// Nothing is connected
if (j === 0 && k === 0 && left[cellPipe[i][0][0]]) {
x = 0;
y = 0;
connected[i][0][0] = true;
pipeElems[i][0][0].className = '';
chain[i].push([0, 0]);
}
} else {
// Check for more pipes in the chain starting at the last pipe that is
// known to be in the chain
var lastElement = chain[i][chain[i].length - 1];
x = lastElement[1];
y = lastElement[0];
}
if (x !== undefined) {
// Check for pipes in the chain starting at (y, x)
while (true) {
var flag = true;
if (left[cellPipe[i][y][x]] && x > 0 && right[cellPipe[i][y][x - 1]]
&& !connected[i][y][x - 1]) {
x--;
} else if (right[cellPipe[i][y][x]] && x < columns - 1
&& left[cellPipe[i][y][x + 1]] && !connected[i][y][x + 1]) {
x++;
} else if (up[cellPipe[i][y][x]] && y > 0
&& down[cellPipe[i][y - 1][x]] && !connected[i][y - 1][x]) {
y--;
} else if (down[cellPipe[i][y][x]] && y < rows - 1
&& up[cellPipe[i][y + 1][x]] && !connected[i][y + 1][x]) {
y++;
} else {
flag = false;
}
if (flag) {
connected[i][y][x] = true;
pipeElems[i][y][x].className = '';
chain[i].push([y, x]);
} else {
break;
}
}
}
if (connected[i][rows - 1][columns - 1]
&& right[cellPipe[i][rows - 1][columns - 1]]) {
if (!inProgress[i]) {
// Game hasn't even started, so reconstruct puzzle
return randomize(i);
}
// Set is complete
inProgress[i] = false;
countCompleted++;
playSound("sndComplete");
pipeElems[i][0][0].parentNode.lastChild.className = '';
if (countCompleted === sets) {
// Display time elapsed
var secElapsed = (new Date().getTime() - startTime) / 1000;
var divMessage = document.getElementById("message");
divMessage.appendChild(document.createTextNode(
"Hacked in " + secElapsed + " sec."));
// Check highscore
var boardKey = [sets, rows, columns].join(",");
var data = timeStats[boardKey];
if (data === undefined) {
// First completion on this setting
timeStats[boardKey] = {count: 1, best: secElapsed, total: secElapsed};
} else {
data.count++;
data.total += secElapsed;
divMessage.appendChild(document.createElement("br"));
if (secElapsed < data.best) {
// Old record beaten
divMessage.appendChild(document.createTextNode("New best time! Old best: " + data.best));
data.best = secElapsed;
} else {
divMessage.appendChild(document.createTextNode("Best time: " + data.best));
}
divMessage.appendChild(document.createTextNode(" sec."));
}
showStats();
}
}
}
function getAverage(data) {
return (data.total / data.count).toFixed(3);
}
function showStats() {
var boardKey = [
document.getElementById("numSets").value,
document.getElementById("numRows").value,
document.getElementById("numColumns").value
].join(",");
var data = timeStats[boardKey];
var tdBest = document.getElementById("statBest");
var tdAvg = document.getElementById("statAvg");
var tdCount = document.getElementById("statCount");
if (data === undefined) {
tdBest.replaceChild(document.createTextNode("\u2013"), tdBest.firstChild);
tdAvg.replaceChild(document.createTextNode("\u2013"), tdAvg.firstChild);
tdCount.replaceChild(document.createTextNode("0"), tdCount.firstChild);
} else {
tdBest.replaceChild(document.createTextNode(data.best + " sec"), tdBest.firstChild);
tdAvg.replaceChild(document.createTextNode(getAverage(data) + " sec"), tdAvg.firstChild);
tdCount.replaceChild(document.createTextNode(data.count), tdCount.firstChild);
}
}
// Preload the images of the pipes
var imageNames = ["pipeStart", "pipeEnd"];
for (var i = 0; i < pipeMap.length; i++)
imageNames.push("pipe" + pipeMap[i]);
var container = document.getElementById("preload");
for (var i = 0; i < imageNames.length; i++) {
var img = document.createElement("img");
img.src = imageNames[i] + ".png";
container.appendChild(img);
}
document.getElementById("pipeArea").addEventListener("mousedown", function(evt) {
if (evt.target.hasAttribute("data-set") && inProgress[evt.target.getAttribute("data-set")]) {
rotate(evt.target);
}
});
showStats();
document.getElementById("initialize").addEventListener("click", initialize);
/** Update the settings fields according to what the user selected in the pulldown menu. */
document.getElementById("level").addEventListener("change", function(evt) {
var select = evt.currentTarget;
var optionValue = select.options[select.selectedIndex].value;
var notCustom = optionValue !== "custom";
if (notCustom) {
var values = optionValue.split(",");
document.getElementById("numSets").value = parseInt(values[0], 10);
document.getElementById("numRows").value = parseInt(values[1], 10);
document.getElementById("numColumns").value = parseInt(values[2], 10);
}
document.getElementById("numSets").disabled = notCustom;
document.getElementById("numRows").disabled = notCustom;
document.getElementById("numColumns").disabled = notCustom;
});
document.body.addEventListener("change", function(evt) {
if (/^(INPUT|SELECT)$/.test(evt.target.tagName)) {
showStats();
}
});
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('service_worker.js', {scope: './'});
}
});