-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
85 lines (69 loc) · 2.62 KB
/
app.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
// <Whack a Mole: An implementation of the classic game.>
// Copyright (C) <2022> <Cromega>
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
const content = document.querySelector("main"),
header = document.querySelector("h1"),
blocks = []
let points = 0, time = 60, mole_timer = null, count_timer = null
function create_table() {
let block;
content.innerHTML = ""
for (let index = 0; index < 9; ++index) {
block = document.createElement("article")
block.setAttribute("class", "mole")
block.setAttribute("data-id", index)
block.addEventListener("click", whack_mole)
content.append(block)
blocks.push(block)
}
random_mole()
start()
}
function whack_mole() {
if (this.classList.contains("mole")) {
points += 1
this.setAttribute("class", "whack")
}
}
function counter() {
header.innerHTML = `Time Left: ${time}`
time === 0? clear():time -= 1
}
function clear() {
let point_text = document.createElement("p")
point_text.innerHTML = `Points: ${points}`
header.innerHTML = set_cleared_header()
content.innerHTML = ""
content.append(point_text)
clearInterval(mole_timer)
clearInterval(count_timer)
}
function set_cleared_header() {
let end_text = points === 0? "You've done nothing":
points > 0 && points <= 30? "Better luck next time":
points > 30 && points <= 58? "Well done":
points === 59? "So close to perfection...":
points === 60? "Perfect, you're the master": "Fucking Hacker"
return end_text
}
function start() {
mole_timer = setInterval(random_mole, 1100)
count_timer = setInterval(counter, 1100)
}
function random_mole() {
blocks.forEach(block => {
block.classList.remove("mole")
block.classList.remove("whack")
});
const random_position = Math.floor(Math.random() * 9)
blocks[random_position].setAttribute("class", "mole")
}