-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
647 lines (491 loc) · 19.5 KB
/
script.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
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
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
const debugStage = 0; //0 = Disable Debug Mode, 1 = Skip to Selecting Number Of Cubes, 2 = Skip to Main Game Area, 3 = Skip to Early Choose Screen, 4 = Skip to Last Screen
function exitGame()
{
window.close();
}
function restartGame()
{
location.reload();
}
function chooseInputValidation(inputID)
{
var inputElement = document.getElementById(inputID);
//Validation bug fixed using parseInt()
var inputCubeNumber = parseInt(inputElement.value);
if(inputCubeNumber < 1 || inputCubeNumber > numberOfCubes)
{
inputElement.style.borderBottomColor = "rgb(255, 0, 0)";
return 0;
}
else
{
inputElement.style.borderBottomColor = "rgb(0, 0, 0)";
return inputCubeNumber;
}
}
function lastScreen()
{
var chosenCubeLightestNumber = chooseInputValidation("lightestCubeNumberInput");
var chosenCubeHeaviestNumber = chooseInputValidation("heaviestCubeNumberInput");
//chooseInputValidation returns 0 if value is invalid
if (!chosenCubeLightestNumber || !chosenCubeHeaviestNumber)
{
// Function will exit if values are invalid
return;
}
document.getElementById("game").setAttribute("style", "display: none;");
document.getElementById("chooseScreen").setAttribute("style", "display: none;");
document.getElementById("lastScreen").removeAttribute("style");
var winStatusMessage = document.getElementById("winStatusMessage");
if (secretWeights[chosenCubeLightestNumber - 1] == lightestWeight && secretWeights[chosenCubeHeaviestNumber - 1] == heaviestWeight)
{
winStatusMessage.innerText = "You Win!!!";
winStatusMessage.style.color = "rgb(133, 255, 51)";
}
else
{
winStatusMessage.innerHTML = "You Lose...";
winStatusMessage.style.color = "rgb(255, 32, 32)";
}
//Generate cubesRevealedMessage
//First we find all cube numbers that correspond to lightestWeight and heaviestWeight
var lightestCubeNumbers = [];
var heaviestCubeNumbers = [];
for(var i = 0; i < numberOfCubes; i++)
{
if(secretWeights[i] == lightestWeight)
{
lightestCubeNumbers.push([i + 1]);
}
if(secretWeights[i] == heaviestWeight)
{
heaviestCubeNumbers.push([i + 1]);
}
}
var cubesRevealedMessageString = "The lightest cube" + (lightestCubeNumbers.length > 1 ? "s are " : " is ");
for (var i = 0; i < lightestCubeNumbers.length; i++)
{
cubesRevealedMessageString += lightestCubeNumbers[i];
if(lightestCubeNumbers.length > 1 && i + 1 != lightestCubeNumbers.length)
{
if(i + 2 == lightestCubeNumbers.length)
{
cubesRevealedMessageString += " and ";
}
else
{
cubesRevealedMessageString += ", ";
}
}
}
cubesRevealedMessageString += "<br>You chose cube " + chosenCubeLightestNumber + "<br><br>The heaviest cube" + (heaviestCubeNumbers.length > 1 ? "s are " : " is ");
for (var i = 0; i < heaviestCubeNumbers.length; i++)
{
cubesRevealedMessageString += heaviestCubeNumbers[i];
if(heaviestCubeNumbers.length > 1 && i + 1 != heaviestCubeNumbers.length)
{
if(i + 2 == heaviestCubeNumbers.length)
{
cubesRevealedMessageString += " and ";
}
else
{
cubesRevealedMessageString += ", ";
}
}
}
cubesRevealedMessageString += "<br>You chose cube " + chosenCubeHeaviestNumber;
document.getElementById("cubesRevealedMessage").innerHTML = cubesRevealedMessageString;
}
function closeChooseScreen()
{
document.getElementById("close").setAttribute("style", "display: none;");
document.getElementById("chooseScreen").setAttribute("style", "display: none;");
}
function choose(early)
{
var lightestCubeNumberInputElement = document.getElementById("lightestCubeNumberInput");
var heaviestCubeNumberInputElement = document.getElementById("heaviestCubeNumberInput");
document.getElementById("chooseScreen").removeAttribute("style");
lightestCubeNumberInputElement.defaultValue = 1;
lightestCubeNumberInputElement.min = 1;
lightestCubeNumberInputElement.max = numberOfCubes;
heaviestCubeNumberInputElement.defaultValue = 1;
heaviestCubeNumberInputElement.min = 1;
heaviestCubeNumberInputElement.max = numberOfCubes;
if(early)
{
document.getElementById("close").removeAttribute("style");
}
}
var haltWeighing = false;
function weigh()
{
//This is here to prevent double weighing
if (haltWeighing == false)
{
haltWeighing = true;
currentWeighings = currentWeighings - 1;
if (currentWeighings <= 0)
{
currentWeighings = 0;
choose(false);
}
weighingsCounter.innerText = "Weighings left: " + (currentWeighings <= 9 ? "0": "") + currentWeighings;
var leftPanTotalWeight = 0;
var rightPanTotalWeight = 0;
for (var i = 0; i < numberOfCubes; i++)
{
if(cubeLocations[i][0] == 1)
{
leftPanTotalWeight += secretWeights[i];
}
else if (cubeLocations[i][0] == 2)
{
rightPanTotalWeight += secretWeights[i];
}
}
if (leftPanTotalWeight > rightPanTotalWeight)
{
leftPanTextElement.innerText = "Left Pan: Heavier";
rightPanTextElement.innerText = "Right Pan: Lighter";
}
else if (leftPanTotalWeight < rightPanTotalWeight)
{
leftPanTextElement.innerText = "Left Pan: Lighter";
rightPanTextElement.innerText = "Right Pan: Heavier";
}
else
{
leftPanTextElement.innerText = "Left Pan: Equal Weight";
rightPanTextElement.innerText = "Right Pan: Equal Weight";
}
}
}
function placeCube(cubeIndex, gridIndex, rowIndex, columnIndex)
{
cubes[cubeIndex].style.bottom = rowIndex * cubeLength + gridsBottomOffset[gridIndex] + "px";
cubes[cubeIndex].style.left = columnIndex * cubeLength + gridsLeftOffset[gridIndex] + "px";
cubeLocations[cubeIndex] = [gridIndex, rowIndex, columnIndex];
}
function closestCell(cubeIndex)
{
var grabbedCubeMiddleCoordsX = cubes[cubeIndex].getBoundingClientRect().left + cubeLength / 2;
var grabbedCubeMiddleCoordsY = window.innerHeight - cubes[cubeIndex].getBoundingClientRect().top - cubeLength / 2;
//To stop wasting compute, lets see if the grabbed cube is even inside a grid and record which grid it is in
gridIndex = -1;
for (var i = 0; i < grids.length; i++)
{
if(grabbedCubeMiddleCoordsX >= gridsLeftOffset[i] && grabbedCubeMiddleCoordsX <= gridsLeftOffset[i] + gridsWidth[i] &&
grabbedCubeMiddleCoordsY >= gridsBottomOffset[i] && grabbedCubeMiddleCoordsY <= gridsBottomOffset[i] + gridsHeight[i])
{
gridIndex = i;
break;
}
}
if (gridIndex != -1)
{
var columnIndex = Math.floor(((grabbedCubeMiddleCoordsX - gridsLeftOffset[gridIndex]) / gridsWidth[gridIndex]) * gridsNumberOfColumns[gridIndex]);
//Find highest row in the this column that has a cube
var highestRow = 0;
for(var i = 0; i < numberOfCubes; i++)
{
if(cubeLocations[i][0] == gridIndex && cubeLocations[i][2] == columnIndex && cubeLocations[i][1] >= highestRow && i != cubeIndex)
{
highestRow = cubeLocations[i][1] + 1;
}
}
return [gridIndex, highestRow, columnIndex];
}
else
{
return null;
}
}
function reverseDisplacements(cubeIndex)
{
var gridIndex = cubeLocations[cubeIndex][0];
var rowIndex = cubeLocations[cubeIndex][1];
var columnIndex = cubeLocations[cubeIndex][2];
//Collect indexes of all cubes currently at or above grabbed cube's original cell:
var atOrAboveCubesIndexes = [];
for(var i = 0; i < numberOfCubes; i++)
{
if(cubeLocations[i][0] == gridIndex && cubeLocations[i][2] == columnIndex && cubeLocations[i][1] >= rowIndex && i != cubeIndex)
{
atOrAboveCubesIndexes.push(i);
}
}
for(var i = 0; i < atOrAboveCubesIndexes.length; i++)
{
placeCube(atOrAboveCubesIndexes[i], gridIndex, cubeLocations[atOrAboveCubesIndexes[i]][1] + 1, columnIndex);
}
placeCube(cubeIndex, gridIndex, rowIndex, columnIndex);
}
function displaceCubes(cubeIndex)
{
var gridIndex = cubeLocations[cubeIndex][0];
var rowIndex = cubeLocations[cubeIndex][1];
var columnIndex = cubeLocations[cubeIndex][2];
//Collect indexes of all cubes above grabbed cube:
var aboveCubesIndexes = [];
for(var i = 0; i < numberOfCubes; i++)
{
if(cubeLocations[i][0] == gridIndex && cubeLocations[i][2] == columnIndex && cubeLocations[i][1] > rowIndex)
{
aboveCubesIndexes.push(i);
}
}
for(var i = 0; i < aboveCubesIndexes.length; i++)
{
placeCube(aboveCubesIndexes[i], gridIndex, cubeLocations[aboveCubesIndexes[i]][1] - 1, columnIndex);
}
}
function showGrids(show)
{
for(var i = 0; i < grids.length; i++)
{
if (show)
{
grids[i].style.filter = "opacity(0.4)";
}
else
{
grids[i].style.filter = "opacity(0)";
}
}
}
var isCubeCurrentlyGrabbed = false;
function cubeGrab(cubeIndex)
{
if (isCubeCurrentlyGrabbed == false) //Prevents bugs if somehow more than 1 cube was selected
{
isCubeCurrentlyGrabbed = true;
haltWeighing = false;
leftPanTextElement.innerText = "Left pan: _______";
rightPanTextElement.innerText = "Right pan: _______";
const cubeMoveListener = new AbortController();
const cubeUnGrabListener = new AbortController();
showGrids(true);
cubes[cubeIndex].style.zIndex = 1;
//Cube move functionality
document.addEventListener("mousemove", (e) =>
{
//Set the middle of the grabbed cube to mouse coords
cubes[cubeIndex].style.bottom = window.innerHeight - e.clientY - (cubeLength / 2) + "px";
cubes[cubeIndex].style.left = e.clientX - (cubeLength / 2) + "px";
}, { signal: cubeMoveListener.signal });
displaceCubes(cubeIndex);
//Cube un-grab functionality
cubes[cubeIndex].addEventListener("mouseup", () =>
{
cubes[cubeIndex].style.removeProperty("z-index");
var closestCellVar = null;
closestCellVar = closestCell(cubeIndex);
cubeMoveListener.abort();
if (closestCellVar != null)
{
placeCube(cubeIndex, closestCellVar[0], closestCellVar[1], closestCellVar[2]);
}
else
{
reverseDisplacements(cubeIndex);
}
showGrids(false);
isCubeCurrentlyGrabbed = false;
cubeUnGrabListener.abort();
}, { signal: cubeUnGrabListener.signal });
}
}
function settleAllCubes()
{
//Check if half of the cubes can fit in each grid
if (areaHalfOfTheCubesTake > areaOfGrids[0] || areaHalfOfTheCubesTake > areaOfGrids[1])
{
alert("Error: Screen dimensions are incompatible with game. Resize window, decrease number of cubes or try a different montior.");
restartGame();
return;
}
var currentGrid;
var rowsIndex = [0, 0, 0, 0];
var columnsIndex = [0, 0, 0, 0];
for (var i = 0; i < numberOfCubes; i++)
{
currentGrid = cubeLocations[i][0];
placeCube(i, currentGrid, rowsIndex[currentGrid], columnsIndex[currentGrid]);
columnsIndex[currentGrid] = (columnsIndex[currentGrid] + 1) % gridsNumberOfColumns[currentGrid];
if (columnsIndex[currentGrid] % gridsNumberOfColumns[currentGrid] == 0)
{
rowsIndex[currentGrid]++;
}
}
}
function setGrids()
{
for (var i = 0; i < grids.length; i++)
{
grids[i].style.width = gridsWidth[i] + "px";
grids[i].style.left = gridsLeftOffset[i] + "px";
grids[i].style.bottom = gridsBottomOffset[i] + "px";
grids[i].style.height = gridsHeight[i] + "px";
}
}
var cubeLength;
var gridsHeight = [];
var gridsWidth = [];
var gridsBottomOffset = [];
var gridsLeftOffset = [];
var gridsNumberOfRows = [];
var gridsNumberOfColumns = [];
var areaHalfOfTheCubesTake;
var areaOfGrids = [];
function setDimensionVariables()
{
var digitalScaleLeftCoord = digitalScaleImage.getBoundingClientRect().left;
var digitalScaleRightCoord = digitalScaleImage.getBoundingClientRect().right;
var digitalScaleWidth = digitalScaleRightCoord - digitalScaleLeftCoord;
var digitalScaleHeight = window.innerHeight - digitalScaleImage.getBoundingClientRect().top;
cubeLength = cubesContainer.lastChild.getBoundingClientRect().right - cubesContainer.lastChild.getBoundingClientRect().left;
var maxSpaceOnEachSide = (window.innerWidth - digitalScaleWidth) / 2;
//Keep in mind that grid3 is the mirror of grid0, and grid2 is the mirror of grid1
gridsNumberOfColumns[0] = gridsNumberOfColumns[3] = Math.floor(maxSpaceOnEachSide/cubeLength);
gridsWidth[0] = gridsWidth[3] = gridsNumberOfColumns[0] * cubeLength;
gridsLeftOffset[0] = (maxSpaceOnEachSide - gridsWidth[0]) / 2;
gridsLeftOffset[3] = window.innerWidth - (gridsWidth[3] + gridsLeftOffset[0]);
var maxSpaceOnEachPan = (445 / 1080) * digitalScaleWidth; // 445px/1080px is derrived from ratios of actual digital scale image from a photo editor
gridsNumberOfColumns[1] = gridsNumberOfColumns[2] = Math.floor(maxSpaceOnEachPan/cubeLength);
gridsWidth[1] = gridsWidth[2] = gridsNumberOfColumns[1] * cubeLength;
gridsLeftOffset[1] = (digitalScaleLeftCoord + (305 / 1080) * digitalScaleWidth) - gridsWidth[1] / 2; // 305px is the length from the left of the image to the middle of the left pan
gridsLeftOffset[2] = window.innerWidth - (gridsWidth[2] + gridsLeftOffset[1]);
gridsNumberOfRows[0] = gridsNumberOfRows[3] = Math.floor(window.innerHeight/cubeLength);
gridsHeight[0] = gridsHeight[3] = gridsNumberOfRows[0] * cubeLength;
gridsBottomOffset[0] = gridsBottomOffset[3] = 0;
gridsBottomOffset[1] = gridsBottomOffset[2] = (138 / 169) * digitalScaleHeight; // 138px the bottom edge of the pans in the image and 169px is total height of pan in the image
gridsNumberOfRows[1] = gridsNumberOfRows[2] = Math.floor((window.innerHeight - gridsBottomOffset[1]) / cubeLength);
gridsHeight[1] = gridsHeight[2] = gridsNumberOfRows[1] * cubeLength;
areaHalfOfTheCubesTake = Math.ceil(numberOfCubes / 2) * Math.pow(cubeLength, 2);
areaOfGrids[0] = areaOfGrids[3] = gridsHeight[0] * gridsWidth[0];
areaOfGrids[1] = areaOfGrids[2] = gridsHeight[1] * gridsWidth[1];
}
var digitalScaleImage;
var leftPanTextElement;
var rightPanTextElement;
var weighingsCounter;
var cubesContainer;
var heaviestWeight = 0;
var lightestWeight = Infinity;
var secretWeights = [];
var cubes;
var grids;
var cubeLocations = [];
function initGame()
{
document.getElementById("selectNumber").setAttribute("style", "display: none;");
document.getElementById("game").removeAttribute("style");
weighingsCounter = document.getElementById("weighingsCounter");
digitalScaleImage = document.getElementById("digitalScaleImage");
leftPanTextElement = document.getElementById("left");
rightPanTextElement = document.getElementById("right");
if (debugStage >= 2)
{
numberOfCubes = 20;
currentWeighings = Math.ceil(3 * numberOfCubes / 2 - 2);
}
weighingsCounter.innerText = "Weighings left: " + (currentWeighings <= 9 ? "0": "") + currentWeighings;
cubesContainer = document.getElementById("cubesContainer");
var cube = document.createElement("div");
cube.classList.add("cube");
var cubeText = document.createElement("h1");
cubeText.classList.add("cubeText");
var currentSecretWeight;
for(var i = 0; i < numberOfCubes; i++)
{
currentSecretWeight = Math.floor(Math.random() * 100) + 1;
secretWeights[i] = currentSecretWeight;
if(currentSecretWeight < lightestWeight)
{
lightestWeight = currentSecretWeight;
}
if(currentSecretWeight > heaviestWeight)
{
heaviestWeight = currentSecretWeight;
}
cube.setAttribute("onmousedown", "cubeGrab(" + i + ")");
if (debugStage > 0)
{
cube.setAttribute("data-secretWeight", secretWeights[i]);
}
//Add 1 to value since humans normally count from 1
cubeText.innerText = (i + 1 <= 9 ? "0": "") + (i + 1);
cubesContainer.appendChild(cube.cloneNode(true));
cubesContainer.lastChild.appendChild(cubeText.cloneNode(true));
}
cubes = cubesContainer.getElementsByClassName("cube");
grids = document.getElementsByClassName("grid");
setDimensionVariables();
setGrids();
//Fancy thing I did: Just place cubes initially in grid0 and grid3, then use settleCubes() (which was intially used only when resizing screen) to just place them neatly in the start aswell
var halfCubesIndex = Math.ceil(numberOfCubes / 2);
for (var i = 0; i < halfCubesIndex; i++)
{
cubeLocations.push([0, 0, 0]);
}
for (var i = halfCubesIndex; i < numberOfCubes; i++)
{
cubeLocations.push([3, 0, 0]);
}
settleAllCubes();
}
var numberOfCubes;
var currentWeighings;
function updateSliderValue()
{
sliderValue.innerText = (sliderElement.value <= 9 ? "0": "") + sliderElement.value + " ➤";
numberOfCubes = sliderElement.value;
currentWeighings = Math.ceil(3 * numberOfCubes / 2 - 2);
document.getElementById("challengeText").innerText = "Challenge: Guess the correct cubes in " + currentWeighings + " or less weighings";
}
var sliderElement;
var sliderValue;
function clickToStart()
{
document.getElementById("titleScreen").setAttribute("style", "display: none;");
document.getElementById("selectNumber").removeAttribute("style");
window.removeEventListener("click", clickToStart);
sliderElement = document.getElementById("slider");
sliderValue = document.getElementById("sliderValue");
if (debugStage < 2)
{
updateSliderValue();
}
}
window.onload = () => {
if (debugStage < 1)
{
window.addEventListener("click", clickToStart);
}
}
window.onresize = () => {
if (window.getComputedStyle(document.getElementById("game")).display != "none")
{
setDimensionVariables();
setGrids();
settleAllCubes();
}
}
if (debugStage >= 1)
{
clickToStart();
}
if (debugStage >= 2)
{
initGame();
}
if (debugStage >= 3)
{
choose();
}
if (debugStage >= 4)
{
lastScreen();
}