-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
448 lines (421 loc) · 11.7 KB
/
script.js
File metadata and controls
448 lines (421 loc) · 11.7 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
// Basic tasks (12 pts)
// [ ] Map selector: at least three different maps can be selected and started correctly (1 pt)
// [x] Map selector: the player's name can be entered which is shown during the game and on the scoreboard (1 pt)
// [x] Game board: the elapsed time is always shown and updated (1 pt)
// [x] Game board: all illuminated tiles (including the tiles containing the light bulbs) get yellow background colour (1 pt)
// [x] Game board: the propagation of light is animated, the yellow background colour spreads from the light source after it has been placed (1 pt)
// [x] Game board: show with a different style (e.g. green text colour) if a black tile is surrounded by the correct number of light bulbs (1 pt)
// [x] Game board: show with a different style (e.g. red colour or icon) if two light bulbs illuminate each other (1 pt)
// [ ] Map selector: the latest results can be seen - player's name, map name, time elapsed (1 pt)
// [ ] Map selector: the latest results are stored persistently after the page is closed (1 pt)
// [ ] Map selector: the saved game is shown and can be loaded properly (1 pt)
// [x] Other: nice design (1 pt)
// [x] Game board: the game can be interrupted and saved (1 pt)
// Starting Button Trigger
function startGame(){
toggleScreen('start-menu', false);
toggleScreen('name-input', true)
inputname(true);
}
function toggleScreen(id, toggle)
{
let elem = document.getElementById(id);
let display = (toggle) ? 'block' : 'none';
elem.style.display = display;
}
function inputname(isStart)
{
let s = document.getElementById('name-input');
let inp = document.createElement("input");
inp.id = "inp";
inp.type = "text";
inp.placeholder = `Username`
let b = document.createElement("button");
b.innerHTML = `Start`;
b.id = "inp-submit";
if(isStart)
{
b.addEventListener('click', putUsername);
inp.addEventListener('keypress', function (e)
{
console.log(e.key);
if(e.key === 'Enter')
{
putUsername();
}
})
}
else
{
b.addEventListener('click', putUsernameLoad);
inp.addEventListener('keypress', (e) =>
{
if(e.key === 'Enter')
{
putUsernameLoad();
}
})
}
s.appendChild(inp);s.appendChild(b);
}
//Username putting hadling
function putUsername()
{
username = document.getElementById("inp").value
toggleScreen('name-input', false);
toggleScreen('game-space', true);
level(7);
run();
}
function putUsernameLoad()
{
username = document.getElementById("inp").value
toggleScreen('name-input', false);
toggleScreen('game-space', true);
let temp = window.localStorage.getItem("Game0");
console.log(temp);
let json = JSON.parse(temp);
//Create info block
let info = document.getElementById('info');
info.innerHTML = '';
//loop time and fix the interval
let p = document.createElement('p');
info.appendChild(p);
p.id = 'timer';
time = json["time"];
p.innerHTML = time;
//Username
if(username.length == 0)
username = "Guest";
let uname = document.createElement('p');
uname.innerHTML = username;
uname.id = "username";
info.appendChild(uname);
min = parseInt(json["time"].substring(0,2)); sec = parseInt(json["time"].substring(3,5));
interval = setInterval(timer, 1000);
//Save button
let save = document.createElement('p');
save.innerHTML = "Save Game";
save.id = "save"
save.addEventListener('click', saveGame);
info.appendChild(save);
//create table
let board = document.getElementById('board');
let tbody = document.createElement('tbody');
board.innerHTML = '';
for(let i = 0; i < 7; i++)
{
let newTr = document.createElement('tr');
for(let j = 0; j < 7; j++)
{
if(!json["cells"][7 * i + j]["class"].includes("wall"))
{
blackSquares[i][j] = -1;
let newTd = document.createElement('td');
newTd.setAttribute('class', 'cell');
newTd.dataset.row = i;
newTd.dataset.col = j;
newTd.dataset.bulbs = parseInt(json["cells"][7 * i + j]["data-bulbs"]);
if(json["cells"][7 * i + j]["class"].includes("taken"))
{
newTd.classList.add("taken");
let img = document.createElement('img');
img.src = "/bulb.png";
img.style.width = '50px';
img.style.height = '50px';
img.classList = 'bulb';
newTd.appendChild(img);
}
if(json["cells"][7 * i + j]["class"].includes("lightcell"))
{
newTd.classList.add("lightcell");
}
newTr.append(newTd);
}
else
{
let newTd = document.createElement('td');
newTd.setAttribute('class', 'wall');
newTd.dataset.row = i;
newTd.dataset.col = j;
newTd.dataset.bCount = parseInt(json["cells"][7 * i + j]["data-b-count"]);
blackSquares[i][j] = parseInt(json["cells"][7 * i + j]["data-black-squares"]);
//set the numbers on the black squares
switch(blackSquares[i][j])
{
case 0:
newTd.innerHTML = `0`;
newTd.classList.add('fit');
break;
case 1:
newTd.innerHTML = `1`;
break;
case 2:
newTd.innerHTML = `2`;
break;
case 3:
newTd.innerHTML = `3`;
break;
default:
}
newTr.append(newTd);
}
}
delegate(newTr, 'td', 'click', putBulb)
tbody.appendChild(newTr);
}
board.appendChild(tbody);
isGame = true;
}
//Loading game function
function loadGame()
{
if(window.localStorage.length > 0)
{
toggleScreen('start-menu', false);
toggleScreen('name-input', true);
inputname(false);
}
}
//Saving game function
function saveGame()
{
console.log("saved");
let tbody = document.querySelector('tbody');
let trs = tbody.children;
let json = `{"cells":[`;
for(let i = 0; i < 7; i++)
{
let rep = "";
for(let j = 0; j < 7; j++)
{
rep += "{";
let td = trs[i].children[j];
let cnt = 0;
for(let data of td.getAttributeNames())
{
let temp = "";
if(cnt != 0)
temp += `, `;
cnt = 1;
temp += `"${data}"`;
temp += `:"${td.getAttribute(data)}"`;
rep += temp;
}
rep += "}";
if(j != 6)
rep += ","
}
json += rep;
if(i != 6)
json += ",";
}
json += "]";
let ttime = `,"time" : "${time}"}`
window.localStorage.setItem(`Game0`, json + ttime);
}
// Game
function run(){
//Create info block
let info = document.getElementById('info');
info.innerHTML = '';
//loop time and fix the interval
let p = document.createElement('p');
info.appendChild(p);
p.id = 'timer';
time = "00:00";
p.innerHTML = time;
if(min != 0 && sec != 0) min = 0; sec = 0;
interval = setInterval(timer, 1000);
//Username
if(username.length == 0)
username = "Guest";
let uname = document.createElement('p');
uname.innerHTML = username;
uname.id = "username";
info.appendChild(uname);
//Save button
let save = document.createElement('p');
save.innerHTML = "Save Game";
save.id = "save";
save.addEventListener('click', saveGame);
info.appendChild(save);
//create table
let board = document.getElementById('board');
let tbody = document.createElement('tbody');
board.innerHTML = '';
for(let i = 0; i < 7; i++)
{
let newTr = document.createElement('tr');
for(let j = 0; j < 7; j++)
{
if(blackSquares[i][j] == -1)
{
let newTd = document.createElement('td');
newTd.setAttribute('class', 'cell');
newTd.dataset.row = i;
newTd.dataset.col = j;
newTd.dataset.bulbs = 0;
newTr.append(newTd);
}
else
{
let newTd = document.createElement('td');
newTd.setAttribute('class', 'wall');
newTd.dataset.row = i;
newTd.dataset.col = j;
newTd.dataset.bCount = 0;
newTd.dataset.blackSquares = blackSquares[i][j];
//set the numbers on the black squares
switch(blackSquares[i][j])
{
case 0:
newTd.innerHTML = `0`;
newTd.classList.add('fit');
break;
case 1:
newTd.innerHTML = `1`;
break;
case 2:
newTd.innerHTML = `2`;
break;
case 3:
newTd.innerHTML = `3`;
break;
default:
}
newTr.append(newTd);
}
}
delegate(newTr, 'td', 'click', putBulb)
tbody.appendChild(newTr);
}
board.appendChild(tbody);
isGame = true;
}
// Bulb putting function
function putBulb(event, elem)
{
if(isGame)
{
if(!elem.classList.contains('wall'))
{
let class_list = elem.classList;
if(!class_list.contains('taken'))
{
addWallBulb(elem);
let img = document.createElement('img');
img.src = "./bulb.png";
img.style.width = '50px';
img.style.height = '50px';
img.classList = 'bulb';
elem.classList.add('taken');
elem.classList.add('lightcell');
elem.appendChild(img);
elem.dataset.bulbs++;
lightup(elem);
allBsCount++;
}
else{
removeWallBulb(elem);
elem.innerHTML = "";
elem.dataset.bulbs--;
if(elem.dataset.bulbs == 0)
elem.classList = 'cell';
else
elem.classList = 'cell lightcell'
unlight(elem)
allBsCount--;
}
}
}
}
//function for lighting up the bulb
function lightup(elem)
{
let tbody = document.querySelector('tbody');
let trs = tbody.children;
let tds = trs[elem.dataset.row].children;
//right of the bulb
lightRight(elem, tds, parseInt(elem.dataset.col) + 1);
//left of the bulb
lightLeft(elem, tds, parseInt(elem.dataset.col) - 1);
//top of the bulb
lightTop(elem, trs, parseInt(elem.dataset.row) - 1);
//bottom of the bulb
lightBottom(elem, trs, parseInt(elem.dataset.row) + 1);
}
//function for unlighting bulbs
function unlight(elem)
{
let tbody = document.querySelector('tbody');
let trs = tbody.children;
let tds = trs[elem.dataset.row].children;
//right of the bulb
unlightRight(elem, tds, parseInt(elem.dataset.col) + 1);
//left of the bulb
unlightLeft(elem, tds, parseInt(elem.dataset.col) - 1);
//top of the bulb
unlightTop(elem, trs, parseInt(elem.dataset.row) - 1);
//bottom of the bulb
unlightBottom(elem, trs, parseInt(elem.dataset.row) + 1);
}
//Checks the board if the solution was found
function check()
{
let tbody = document.querySelector('tbody');
let win = true;
let trs = tbody.children
for(let tr of trs)
{
let tds = tr.children
for(let td of tds)
{
if(!td.classList.contains('wall') && (!td.classList.contains('lightcell') || td.classList.contains('wrong')))
{
win = false;
break;
}
if(td.classList.contains('wall') && blackSquares[td.dataset.row][td.dataset.col] != 4
&& (td.classList.contains('unfit') || !td.classList.contains('fit'))
)
{
win = false;
break;
}
if(td.classList.contains('illuminated'))
{
win = false;
break;
}
}
if(!win)
{
break;
}
}
if(win)
{
p = document.createElement('p');
button = document.createElement('button');
p.innerText = 'You have won!';
p.id = 'winningText';
button.classlist = 'button';
button.id = 'restart';
button.innerText = 'Restart';
button.addEventListener('click', restart);
res = document.getElementById('result');
res.appendChild(p);
res.appendChild(button);
toggleScreen('result', true);
isGame = false;
}
}
//Restart button click event
function restart(e)
{
res = document.getElementById('result');
res.innerHTML = '';
toggleScreen('result', false);
run();
}