-
Notifications
You must be signed in to change notification settings - Fork 0
/
local.mjs
130 lines (102 loc) · 2.8 KB
/
local.mjs
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
import { setupGame, grid, noOfCheeses } from './game.mjs';
var allButtons = []
var moveCounter = 0;
var cheesesFound;
var gridWidth = 10;
var gridHeight = 10;
let absoluteHour = 0;
function getAbsoluteHour(date) {
let result = (date.getUTCFullYear() * 365 * 24) +
(date.getUTCMonth() * 31 * 24) +
(date.getUTCDate() * 24) +
date.getUTCHours();
return result;
}
function showCounters() {
let counterPar = document.getElementById("counterPar");
let cheesesLeft = noOfCheeses - cheesesFound;
counterPar.textContent = "Cheeses left:" + cheesesLeft + " Tries: " + moveCounter;
}
function fillGrid(buttons) {
for (let button of buttons) {
if (button.className == "empty") {
setButtonStyle(button);
}
}
}
function setButtonStyle(button) {
let x = button.getAttribute("x");
let y = button.getAttribute("y");
button.className = grid[x][y].style;
if (button.className == "cheese") {
cheesesFound++;
if (cheesesFound == noOfCheeses) {
fillGrid(allButtons);
}
showCounters();
}
}
function newGame() {
// get the date
let date = new Date();
// get the absolute hour for this date
absoluteHour = getAbsoluteHour(date);
// this is what we want
let gameRequest = {
width: gridWidth,
height: gridHeight,
colorStyles: ["white", "red", "orange", "yellow", "yellowGreen", "lightGreen", "cyan",
"lightBlue", "blue", "purple", "magenta", "darkGray"],
minCheeses: 1,
maxCheeses: 6,
startValue: absoluteHour,
randMult: 8121,
randAdd: 28413,
randModulus: 134456789
}
setupGame(gameRequest);
}
function checkTimeout() {
// get the date
let date = new Date();
// get the absolute hour for this date
let newAbsoluteHour = getAbsoluteHour(date);
if (newAbsoluteHour != absoluteHour) {
// we have reached the end of the hour
// end the game
alert("The game in this hour has ended.");
location.reload();
}
}
function buttonClickedHandler(event) {
checkTimeout();
let button = event.target;
if (button.className != "empty") {
return;
}
setButtonStyle(button);
moveCounter++;
showCounters();
}
function doPlayGame() {
moveCounter = 0;
cheesesFound = 0;
newGame();
let container = document.getElementById("buttonPar");
for (let y = 0; y < gridHeight; y++) {
for (let x = 0; x < gridWidth; x++) {
let newButton = document.createElement("button");
newButton.className = "empty";
newButton.setAttribute("x", x);
newButton.setAttribute("y", y);
newButton.addEventListener("click", buttonClickedHandler);
newButton.textContent = "X";
container.appendChild(newButton);
allButtons.push(newButton);
}
let lineBreak = document.createElement("br");
container.appendChild(lineBreak);
}
showCounters();
}
export { doPlayGame };