-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
573 lines (459 loc) · 16.9 KB
/
index.js
File metadata and controls
573 lines (459 loc) · 16.9 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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
'use strict'
$(document).ready(function () {
$("#endPointer").fadeOut();
$("#darkOverlay").fadeOut();
var checkingPoints = []
//Generate a grid of squares
const body = document.body;
for (var i = 0; i < 13; i++) {
let row = `<tr>`;
for (let j = 0; j < 18; j++) {
let col = `<td data-x="${i}" data-y="${j}" class="item">${i},${j}</td>`
row += col;
}
row += `</tr>`
$("#myTable").append(row);
}
// idk what this does
$("body").addClass('h_block');
$("#reset").click(function (e) {
e.preventDefault();
location.reload();
});
// flags
var start = false;
var end = false;
var block = true;
var clear = false
// start coordinates
var startCoord = { x: 0, y: 0 }
var endCoord = { x: 0, y: 0 }
// function to get and store the start coordinates in the global variable
function storeStartCoord(value) {
startCoord = {
x: value.x,
y: value.y
}
console.log(startCoord);
}
// function to get and store end coordinates in the global variable
function storeEndCoord(value) {
endCoord = {
x: value.x,
y: value.y
}
}
// function to add class to coordinates
function AddClassToCoord(coord, className) {
$(`[data-x="${coord.x}"][data-y="${coord.y}"]`).addClass(className);
}
// function to remove class form coordinates
function removeClassFromCoord(coord, className) {
$(`[data-x="${coord.x}"]+[data-y="${coord.y}"]`).removeClass(className);
}
// this function will just return 0 if any value is below zero
function preventNegative(value) {
if (value < 0) {
return 0;
} else {
return value
}
}
var counter = 0; // idk why this is here
// function to to check if the inputed coordinate is a block or the end
function checkBlockOrEnd(coord) {
// if the input is the end return end and then fade the finishing items
if ($(`[data-x="${coord.x}"][data-y="${coord.y}"]`).hasClass("end")) {
$("#endPointer").fadeIn();
$("#darkOverlay").fadeIn();
return "end"
};
// state blocked
if ($(`[data-x="${coord.x}"][data-y="${coord.y}"]`).hasClass("block")) {
return "blocked"
};
// state checking
if ($(`[data-x="${coord.x}"][data-y="${coord.y}"]`).hasClass("checking")) {
return "checking"
};
// if the block is not a checked, end, or blocked coord then then make it a checked block
if ($(`[data-x="${coord.x}"][data-y="${coord.y}"]`).hasClass("item")) {
//create a lag with the timeer
setTimeout(() => {
removeClassFromCoord(coord, "checking")
$(`[data-x="${coord.x}"][data-y="${coord.y}"]`).addClass("checked")
}, 1000);
return false
}
}
// function to check the northern coodinate
function checkN(currentCoord) {
let coordX = currentCoord.x
let coordY = currentCoord.y
coordX = preventNegative(coordX - 1)
let coord = { x: coordX, y: coordY }
if (checkBlockOrEnd(coord) == "blocked" || checkBlockOrEnd(coord) == "checking" || checkBlockOrEnd(coord) == "checked") {
return { x: "blocked", y: "blocked" }
}
if (checkBlockOrEnd(coord) == "end") {
return { x: "end", y: "end" }
}
AddClassToCoord(coord, 'checking')
return coord;
}
// function to check the north-estern coodinate
function checkNE(currentCoord) {
let coordX = currentCoord.x;
let coordY = currentCoord.y;
coordX = preventNegative(coordX - 1)
coordY = preventNegative(coordY + 1)
let coord = { x: coordX, y: coordY }
if (checkBlockOrEnd(coord) == "blocked" || checkBlockOrEnd(coord) == "checking" || checkBlockOrEnd(coord) == "checked") {
return { x: "blocked", y: "blocked" }
}
if (checkBlockOrEnd(coord) == "end") {
return { x: "end", y: "end" }
}
AddClassToCoord(coord, 'checking')
return coord;
}
// function to check the north-western coodinate
function checkNW(currentCoord) {
let coordX = currentCoord.x
let coordY = currentCoord.y
coordX = preventNegative(coordX - 1)
coordY = preventNegative(coordY - 1)
let coord = { x: coordX, y: coordY }
if (checkBlockOrEnd(coord) == "blocked" || checkBlockOrEnd(coord) == "checking" || checkBlockOrEnd(coord) == "checked") {
return { x: "blocked", y: "blocked" }
}
if (checkBlockOrEnd(coord) == "end") {
return { x: "end", y: "end" }
}
AddClassToCoord(coord, 'checking')
return coord;
}
// function to check the southern coodinate
function checkS(currentCoord) {
let coordX = currentCoord.x
let coordY = currentCoord.y
coordX = preventNegative(coordX + 1)
let coord = { x: coordX, y: coordY }
if (checkBlockOrEnd(coord) == "blocked" || checkBlockOrEnd(coord) == "checking" || checkBlockOrEnd(coord) == "checked") {
return { x: "blocked", y: "blocked" }
}
if (checkBlockOrEnd(coord) == "end") {
return { x: "end", y: "end" }
}
AddClassToCoord(coord, 'checking')
return coord;
}
// function to check the south-eastern coodinate
function checkSE(currentCoord) {
let coordX = currentCoord.x
let coordY = currentCoord.y
coordX = preventNegative(coordX + 1)
coordY = preventNegative(coordY + 1)
let coord = { x: coordX, y: coordY }
if (checkBlockOrEnd(coord) == "blocked" || checkBlockOrEnd(coord) == "checking" || checkBlockOrEnd(coord) == "checked") {
return { x: "blocked", y: "blocked" }
}
if (checkBlockOrEnd(coord) == "end") {
return { x: "end", y: "end" }
}
AddClassToCoord(coord, 'checking')
return coord;
}
// function to check the south-western coodinate
function checkSW(currentCoord) {
let coordX = currentCoord.x
let coordY = currentCoord.y
coordX = preventNegative(coordX + 1)
coordY = preventNegative(coordY - 1)
let coord = { x: coordX, y: coordY }
if (checkBlockOrEnd(coord) == "blocked" || checkBlockOrEnd(coord) == "checking" || checkBlockOrEnd(coord) == "checked") {
return { x: "blocked", y: "blocked" }
}
if (checkBlockOrEnd(coord) == "end") {
return { x: "end", y: "end" }
}
AddClassToCoord(coord, 'checking')
return coord;
}
// function to check the eastern coodinate
function checkE(currentCoord) {
let coordX = currentCoord.x
let coordY = currentCoord.y
coordY = preventNegative(coordY + 1)
let coord = { x: coordX, y: coordY }
if (checkBlockOrEnd(coord) == "blocked" || checkBlockOrEnd(coord) == "checking" || checkBlockOrEnd(coord) == "checked") {
return { x: "blocked", y: "blocked" }
}
if (checkBlockOrEnd(coord) == "end") {
return { x: "end", y: "end" }
}
AddClassToCoord(coord, 'checking')
return coord;
}
// function to check the western coodinate
function checkW(currentCoord) {
let coordX = currentCoord.x
let coordY = currentCoord.y
coordY = preventNegative(coordY - 1)
let coord = { x: coordX, y: coordY }
if (checkBlockOrEnd(coord) == "blocked" || checkBlockOrEnd(coord) == "checking" || checkBlockOrEnd(coord) == "checked") {
return { x: "blocked", y: "blocked" }
}
if (checkBlockOrEnd(coord) == "end") {
return { x: "end", y: "end" }
}
AddClassToCoord(coord, 'checking')
return coord;
}
// remove the current start point used when the user is interracting with the interface
function removeStart() {
$(`[data-x="${startCoord.x}"]+[data-y="${startCoord.y}"]`).removeClass('start');
console.log("startRemoved");
}
// remove the current end point used when the user is interracting with the interface
function removeEnd() {
$(`[data-x="${endCoord.x}"],[data-y="${endCoord.y}"]`).removeClass('end');
console.log("endRemoved");
}
function selectEndCoord(newCoord) {
$(selector).removeClass("start");
}
// when clicked the body element will change the color of blocks when hovered over
$("#startbtn").click(function (e) {
e.preventDefault();
start = true;
end = false;
block = false;
clear = false;
if ($(body).hasClass('h_end')) {
body.classList.replace("h_end", "h_start")
}
if ($(body).hasClass('h_block')) {
body.classList.replace("h_block", "h_start")
}
});
// when clicked the body element will change the color of blocks when hovered over
$("#endbtn").click(function (e) {
e.preventDefault();
start = false;
end = true;
block = false;
clear = false;
if ($(body).hasClass('h_start')) {
body.classList.replace("h_start", "h_end")
}
if ($(body).hasClass('h_block')) {
body.classList.replace("h_block", "h_end")
}
});
// when clicked the body element will change the color of blocks when hovered over
$("#blockbtn").click(function (e) {
e.preventDefault();
start = false;
end = false;
block = true;
clear = false;
if ($(body).hasClass('h_end')) {
body.classList.replace("h_end", "h_block")
}
if ($(body).hasClass('h_start')) {
body.classList.replace("h_start", "h_block")
}
});
// when clicked the body element will change the color of blocks when hovered over
$("#clearbtn").click(function (e) {
e.preventDefault();
clear = true;
});
// based on which type of button is activated at a particular time perform a different function
function activateClicks() {
$('#myTable td.item').click(function (e) {
// if the current coord does not have the class start and start is activated
if (!$(this).hasClass("start") && start) {
removeStart();
let x = $(this).attr("data-x");
let y = $(this).attr("data-y");
let value = { x: parseInt(x), y: parseInt(y) }
storeStartCoord(value)
$(this).addClass("start");
$(this).removeClass('block');
$(this).removeClass('end');
console.log("start set");
}
// if the current coord does not have the class "block" and "block" is activated
if (!$(this).hasClass("block") && block) {
$(this).addClass("block");
$(this).removeClass('start');
$(this).removeClass('end');
console.log("block set");
}
// if the current coord does not have the class end and end is activated
if (!$(this).hasClass("end") && end) {
removeEnd()
let x = $(this).attr("data-x");
let y = $(this).attr("data-y");
let value = { x: x, y: y }
storeEndCoord(value)
$(this).addClass("end");
$(this).removeClass('start');
$(this).removeClass('block');
console.log("end set");
}
if (($(this).hasClass("end") || $(this).hasClass("start") || $(this).hasClass("block")) && clear) {
$(this).removeClass("end");
$(this).removeClass('start');
$(this).removeClass('block');
console.log("clear block");
}
})
}
var mouseIsDown = false;
// when a coord is clicked and held down
window.addEventListener('mousedown', function () {
mouseIsDown = true;
setTimeout(() => {
if (mouseIsDown) {
console.log("Mouse is down");
$("#myTable td.item").hover(function () {
// over
if (!$(this).hasClass("block") && block) {
$(this).addClass("block");
$(this).removeClass('start');
$(this).removeClass('end');
console.log("block");
}
if (($(this).hasClass("end") || $(this).hasClass("start") || $(this).hasClass("block")) && clear) {
$(this).removeClass("end");
$(this).removeClass('start');
$(this).removeClass('block');
console.log("clear");
}
}, function () {
// out
}
);
}
}, 50);
if (start == true) {
startSound()
}
if (end == true) {
endSound()
}
})
// listen and play click events
window.addEventListener('mouseup', function () {
mouseIsDown = false;
console.log("Mouse is up");
$("#myTable td.item").off();
activateClicks()
$("td").mouseenter(function () {
playHoverUI()
});
$("td").mouseout(function () {
stopHoverUI()
});
})
var infected = new Set();
var infectedCount = 0
function asyncMethod(coord) {
return new Promise(function (resolve, reject) {
setTimeout(() => {
//for each one check to see if they are not infected
let N = checkN(coord)
let NE = checkNE(coord)
let NW = checkNW(coord)
let S = checkS(coord)
let SE = checkSE(coord)
let SW = checkSW(coord)
let E = checkE(coord)
let W = checkW(coord)
let currentInfected = [N, NE, NW, S, SE, SW, W, E];
let l;
let count = 0;
// loop over the currentInfected and add them to the infected
for (l = 0; l < currentInfected.length; l++) {
const crd = currentInfected[l];
if (crd.x == "blocked" || crd.y == "blocked") {
resolve()
continue;
} else if (crd.x == "end" || crd.y == "end") {
reject(crd)
} else {
if (!infected.has(JSON.stringify(crd))) {
infected.add(JSON.stringify(crd));
}
console.log(infected);
infectedCount += 1;
resolve()
}
}
}, 100);
})
}
var mainCounter = 0;
async function main() {
for (const coordinate of infected) {
await asyncMethod(JSON.parse(coordinate)).then(function (data) {
console.log(data);
if (data === 7) {
console.log("Program will continue");
main();
}
}, function (data) {
console.log(`Program has stopped end coordinate is ${data}`);
throw new Error("Completed");
});
console.log(infectedCount);
if (infectedCount > 250) {
throw new Error("Fail")
}
}
}
$("#playbtn").click(function (e) {
e.preventDefault();
infected.add(JSON.stringify(startCoord))
main();
$("#stopBtn").click(function (e) {
e.preventDefault();
throw new Error("Completed")
});
});
$("td").mouseenter(function () {
playHoverUI()
});
$("td").mouseout(function () {
stopHoverUI()
});
function playHoverUI(soundObj) {
var sound = document.getElementById("hoverTD")
sound.play()
}
function stopHoverUI() {
var sound = document.getElementById("hoverTD")
sound.pause()
sound.currentTime = 0
}
function startSound() {
var sound = document.getElementById("startSFX")
sound.play()
}
function stop_startSound() {
var sound = document.getElementById("startSFX")
sound.pause()
sound.currentTime = 0
}
function endSound() {
var sound = document.getElementById("endSFX")
sound.play()
}
function stop_endSound() {
var sound = document.getElementById("endSFX")
sound.play()
}
});