-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
110 lines (92 loc) · 2.7 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
const holes = document.querySelectorAll(".hole");
const scoreBoard = document.querySelector(".score");
const moles = document.querySelectorAll(".mole");
const startBtn = document.querySelector(".start-btn");
const cancelBtn = document.querySelector(".cancel-btn");
const levels = document.querySelector(".levels");
const timerInput = document.querySelector(".timer");
let lastHole;
let timeUp = false;
let score = 0;
function getDifficultyLevel() {
const selectedLevel = document.querySelector('input[name="level"]:checked');
return selectedLevel ? selectedLevel.id : null;
}
function getValidTime(time) {
const parsedTime = Number(time);
return parsedTime >= 5 && parsedTime <= 1000 ? parsedTime : 15;
}
function getRandomTime(min, max) {
return Math.round(Math.random() * (max - min) + min);
}
function getRandomHole(holes) {
const randomIndex = Math.floor(Math.random() * holes.length);
const selectedHole = holes[randomIndex];
if (selectedHole === lastHole) {
return getRandomHole(holes);
}
lastHole = selectedHole;
return selectedHole;
}
function makeMoleAppear(showDuration, hideDuration) {
const randomDuration = getRandomTime(showDuration, hideDuration);
const hole = getRandomHole(holes);
hole.classList.add("up");
setTimeout(() => {
hole.classList.remove("up");
if (!timeUp) {
makeMoleAppear(showDuration, hideDuration);
}
}, randomDuration);
}
function startGame() {
const difficulty = getDifficultyLevel();
let showDuration, hideDuration;
switch (difficulty) {
case "easy":
showDuration = 500;
hideDuration = 1500;
break;
case "medium":
showDuration = 200;
hideDuration = 1000;
break;
case "hard":
showDuration = 100;
hideDuration = 800;
break;
default:
showDuration = 500;
hideDuration = 1500;
}
scoreBoard.textContent = 0;
score = 0;
timeUp = false;
cancelBtn.style.display = "block";
startBtn.style.display = "none";
levels.style.visibility = "hidden";
timerInput.value = getValidTime(timerInput.value);
const countdownInterval = setInterval(() => {
timerInput.value--;
if (timerInput.value <= 0) {
clearInterval(countdownInterval);
timerInput.value = 0;
endGame();
}
}, 1000);
makeMoleAppear(showDuration, hideDuration);
}
function endGame() {
timeUp = true;
timerInput.value = 0;
startBtn.style.display = "block";
cancelBtn.style.display = "none";
levels.style.visibility = "visible";
}
function handleMoleHit(e) {
if (!e.isTrusted) return; // Prevent fake clicks
score++;
this.parentNode.classList.remove("up");
scoreBoard.textContent = score;
}
moles.forEach((mole) => mole.addEventListener("click", handleMoleHit));