-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
133 lines (98 loc) · 3.01 KB
/
index.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
let main = document.getElementById("main");
let gameActive = true;
let id = 1;
for(var i=1; i<=81; i++){
let gridItem = document.createElement("div");
gridItem.className = "grid-item";
gridItem.id = `cell_${id}`;
id++;
main.appendChild(gridItem);
}
//Generating 10 random places for bombs
window.random = [];
function bombs(){
while(random.length < 10){
let randomNum = Math.floor(Math.random() * 81) + 1;
if(random.indexOf(randomNum) === -1){
random.push(randomNum);
}
}
}
bombs(); //initial placement
let SafeArray = [];
let points = 0;
let messageDisplay = document.getElementById("resultDisplay");
let score = document.getElementById("score");
//function for displaying result
function displayResult(result){
if(result == 0){
messageDisplay.innerText = "Game Over";
gameActive = false;
}
else if(result == 71){
score.innerText = result;
messageDisplay.innerText = "You Win";
gameActive = false;
}
else{
score.innerText = result;
}
}
//after every click do this
function handleEvent(clickedEvent){
if(!gameActive){
return;
}
const clickedCell = clickedEvent.target;
const id = parseInt(clickedCell.getAttribute("id").substring(5));
if(random.includes(id)){
for(let i=0; i<random.length; i++){
let bombElem = document.getElementById(`cell_${random[i]}`);
bombElem.style.backgroundImage = "url('https://img.icons8.com/emoji/48/000000/bomb-emoji.png')";
bombElem.style.backgroundColor = "red";
displayResult(0);
}
}
else{
//condition to check if the cell is already clicked or not
if(!SafeArray.includes(id)) {
SafeArray.push(id);
clickedCell.style.backgroundColor = "green";
points++;
displayResult(points);
}
}
}
let cellArray = document.querySelectorAll(".grid-item");
//function to restart game
function reset(){
gameActive = true;
SafeArray = [];
points = 0;
score.innerText = 0;
messageDisplay.innerText = "";
cellArray.forEach((cell) => cell.style.backgroundColor = "white");
for(let i=0;i<random.length;i++){
let bombElem = document.getElementById(`cell_${random[i]}`);
bombElem.style.backgroundImage = "none";
}
random = [];
bombs();
}
document.querySelectorAll(".grid-item").forEach((cell) => cell.addEventListener("click", handleEvent));
// for(var i=0;i<9;i++){
// var row = document.createElement("div");
// row.className = "row";
// for(var j=1;j<=9;j++){
// var cell = document.createElement("div");
// cell.className = "col";
// row.appendChild(cell);
// }
// main.appendChild(row);
// // const column = document.getElementsByClassName("col");
// }
// document.querySelectorAll(".col").forEach(item => {
// item.addEventListener("click" , event =>{
// this.style.color = "red";
// })
// });