-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
84 lines (62 loc) · 1.94 KB
/
script.js
File metadata and controls
84 lines (62 loc) · 1.94 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
// Game settings
const numberOfCells = 100;
const numberOfBombs = 20;
const maxScore = 10;
const bombsList = [16, 21, 3];
// Player score to keep tabs
let score = 0;
// Update score
function updateScore() {
score++;
scoreAmount.innerText = score.toString().padStart(5, '0');
if (score === maxScore) {
endGameWon()
}
}
// Declaring HTML variables
const scoreAmount = document.querySelector('.score-amount');
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 bombsAmount = document.querySelector('.bombs-amount')
// Display amount of bombs
bombsAmount.innerText = numberOfBombs.toString().padStart(5, '0');
// Building grid html from JavaScript
for (let i = 0; i < numberOfCells; i++) {
const cell = document.createElement('div');
cell.classList.add('cell');
cell.addEventListener('click', function () {
if (bombsList.includes(i)) {
cell.classList.add('cell-bomb');
cell.innerText = '💣'
endGameLost();
} else {
updateScore();
}
cell.classList.add('cell-clicked');
})
grid.appendChild(cell);
}
// Add random bombs
while (bombsList.length < numberOfBombs) {
// Generate random number
const randomNumber = Math.floor(Math.random() * numberOfCells) + 1;
if (!bombsList.includes(randomNumber)) {
bombsList.push(randomNumber)
}
}
// End game
function endGameWon() {
endGameText.innerHTML = 'YOU<br>WON';
endGameScreen.classList.add('win');
endGameScreen.classList.remove('hidden');
}
function endGameLost() {
endGameText.innerHTML = 'GAME<br>OVER';
endGameScreen.classList.remove('hidden');
}
// Play again button
playAgainButton.addEventListener('click', function() {
window.location.reload()
})