-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
77 lines (69 loc) · 1.86 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
const board = document.querySelector(".gameboard");
const bossLevel = 10;
let steps = 2;
let lastClicked = 0;
let seatsTaken = [];
function buildBoard() {
// clear board
document.body.classList.remove("fail", "mask", "success");
board.innerHTML = "";
// reset values
lastClicked = 0;
seatsTaken = [];
for (let i = 1; i <= steps; i++) {
let seats = findSeats();
board.innerHTML += `
<div class="number" style="top: ${seats[0]}%; left: ${seats[1]}%">${i}</div>
`;
}
}
function findSeats() {
// let seats = [getRandomInt(1,9)*10, getRandomInt(1,9)*10];
let seats = [getRandomInt(1, 4) * 20, getRandomInt(1, 4) * 20];
if (seatsTaken.find(s => JSON.stringify(s) == JSON.stringify(seats))) {
// seat taken try again
return findSeats();
} else {
seatsTaken.push(seats);
return seats;
}
}
function getRandomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min;
}
board.addEventListener("click", e => {
if (document.body.classList.contains("fail")) return;
if (!e.target.classList.contains("number")) return;
let clickedNum = Number(e.target.innerHTML);
if (clickedNum == 1) {
document.body.classList.add("mask");
}
if (clickedNum == lastClicked + 1) {
lastClicked = clickedNum;
e.target.remove();
if (clickedNum == steps) {
if (steps == bossLevel) {
// won - game over
board.innerHTML = '<div class="chimp">🐒</div>';
} else {
// next level
document.body.classList.add("success");
steps++;
setTimeout(function() {
buildBoard();
}, 600);
}
}
} else {
// lost - game over
document.body.classList.add("fail");
document.body.classList.remove("mask");
steps = 2;
setTimeout(function() {
buildBoard();
}, 2000);
}
});
buildBoard();