-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.js
215 lines (161 loc) · 4.93 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
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
const scoreCounter = document.querySelector('.score-counter');
const board = document.querySelector('.game-panel');
const grid = document.querySelector('.grid');
const endGameScreen = document.querySelector('.end-game-screen');
const endGameText = document.querySelector('.end-game-text');
const playAgainButton = document.querySelector('.play-again');
const info = document.querySelector(".info");
const lifes = document.querySelector('.life');
let isInstructionsVisible = false;
info.addEventListener('pointerup', () => {
console.log("pressed");
const instructions = document.querySelector('#Instructions');
if (isInstructionsVisible) {
instructions.style.transform = "scale(0)";
isInstructionsVisible = false;
} else {
instructions.style.transform = "scale(1)";
isInstructionsVisible = true;
}
});
let tb = document.createElement('h3');
let winningScore = document.createElement('h2');
board.appendChild(winningScore);
board.appendChild(tb);
const totalCells = 100;
const totalBomb = Math.floor(Math.random() * 50) + 1;
tb.innerHTML = `Total 💣: <span class="total-bomb">${totalBomb}</span>`;
const maxScore = 20;
winningScore.innerHTML = `Winning Score:<span class = "winning"> ${maxScore}</span>`;
const start = new Audio('./audios/start.wav');
let highestScore;
try {
highestScore = JSON.parse(localStorage.getItem('highestScore')) || [];
} catch (e) {
highestScore = [];
}
// Check if highestScore is an array
if (!Array.isArray(highestScore)) {
highestScore = [];
}
// Convert the array to a string and store it in localStorage
localStorage.setItem('highestScore', JSON.stringify(highestScore));
let counter = 0;
function generateBombs(totalCells, totalBombs) {
let bombs = [];
while(bombs.length < totalBombs){
let randomNum = Math.floor(Math.random() * totalCells) + 1;
if(bombs.indexOf(randomNum) === -1) bombs.push(randomNum);
}
return bombs;
}
let bombsList = generateBombs(totalCells, totalBomb);
let score = 0;
function updateScore() {
score++;
scoreCounter.innerText = score.toString().padStart(5, '0');
if (score == maxScore) {
endGame(true);
}
}
function revealAllBombs(){
const cells = document.querySelectorAll('.cell');
for (let i = 1; i <= cells.length; i++){
const cell = cells[i-1];
if(bombsList.includes(i)) {
console.log(i);
cell.classList.add('cell-bomb');
}
}
}
function tapSound(){
const audio = new Audio("./audios/tap.wav");
audio.play();
}
function winSound(){
const audio = new Audio("./audios/win.mp3");
audio.play();
}
function showHighestScore(){
let storedHighestScore = JSON.parse(localStorage.getItem('highestScore')) || [];
// Check if storedHighestScore is an array
if (Array.isArray(storedHighestScore) && storedHighestScore.length > 0) {
let highest = Math.max(...storedHighestScore);
// Save highest score to localStorage
localStorage.setItem('highestScore', JSON.stringify(highest));
let h3 = document.createElement('h3');
h3.innerText = `Highest Score: ${highest}`;
console.log(`Highest Score: ${highest}`);
board.appendChild(h3);
} else {
console.log("No valid highest score data found.");
}
}
for (let i = 1; i <= totalCells; i++) {
const cell = document.createElement('div');
cell.classList.add('cell');
cell.addEventListener('pointerup', () => {
tapSound();
start.play();
if (bombsList.includes(i)) {
cell.classList.add('cell-bomb');
// Remove one heart
const hearts = lifes.innerText;
lifes.innerText = hearts.slice(0, -2);
lifes.classList.add('blink-vibrate');
setInterval(()=>{
lifes.classList.remove('blink-vibrate');
},2000);
// Check if all hearts are removed
if (lifes.innerText === '') {
endGame(false); // Trigger end game with loss
Loseaudio();
}
} else {
if (!cell.classList.contains('cell-clicked')) {
cell.classList.add('cell-clicked');
score++;
scoreCounter.innerText = score.toString().padStart(5, '0');
highestScore.push(score);
localStorage.setItem('highestScore', JSON.stringify(highestScore));
if (score == maxScore) {
endGame(true);
winSound();
}
}
}
});
grid.appendChild(cell);
}
function Loseaudio(){
try {
const sound = new Audio('./audios/lose.mp3');
sound.play();
start.pause();
}
catch(E){
console.log(`error:${E}`);
}
}
if(endGame == false){
showHighestScore(highestScore);
}
function endGame(isVictory) {
if (isVictory) {
winSound();
endGameText.innerHTML = 'YOU<br>WON';
endGameScreen.classList.add('win');
}
revealAllBombs();
Loseaudio();
endGameScreen.classList.remove('hidden');
}
playAgainButton.addEventListener('pointerup', () => {
window.location.reload();
});
playAgainButton.addEventListener('pointerup', () => {
window.location.reload();
});
window.onload = function() {
showHighestScore();
}