-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
196 lines (144 loc) · 5.23 KB
/
Copy pathscript.js
File metadata and controls
196 lines (144 loc) · 5.23 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
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
document.addEventListener('DOMContentLoaded', function () {
const grid = document.querySelector('.grid');
let width = 10;
let bombs = 20;
let square = [];
let isGameOver = false;
// shuffle array to set bombs
function shuffle(arra1) {
let ctr = arra1.length;
let temp;
let index;
// While there are elements in the array
while (ctr > 0) {
// Pick a random index
index = Math.floor(Math.random() * ctr);
// Decrease ctr by 1
ctr--;
// And swap the last element with it
temp = arra1[ctr];
arra1[ctr] = arra1[index];
arra1[index] = temp;
}
return arra1;
}
// creating grid
function createGrid() {
// creating blueprint and assigning bombs
let bomb = Array(bombs).fill("bomb");
let safe = Array(100 - bombs).fill("safe");
let bluePrint = bomb.concat(safe);
bluePrint = shuffle(bluePrint);
console.log(bluePrint);
// adding box to grid
for (let i = 0; i < 100; i++) {
let box = document.createElement('div');
box.id = i;
box.classList.add(bluePrint[i]);
grid.appendChild(box);
square.push(box);
}
// adding number to each box
for (let i = 0; i < 100; i++) {
let count = 0;
let isLeft = (i % 10 == 0);
let isRight = (i % 10 == 9);
let isTop = (i < 10)
let isBottom = (i >= 90);
if ((!isLeft) && square[i - 1].classList.contains('bomb')) count++;
if ((!isRight) && square[i + 1].classList.contains('bomb')) count++;
if ((!isBottom) && square[i + 10].classList.contains('bomb')) count++;
if ((!isTop) && square[i - 10].classList.contains('bomb')) count++;
if ((!isTop) && (!isRight) && square[i - 10 + 1].classList.contains('bomb')) count++;
if ((!isBottom) && (!isRight) && square[i + 10 + 1].classList.contains('bomb')) count++;
if ((!isTop) && (!isLeft) && square[i - 10 - 1].classList.contains('bomb')) count++;
if ((!isBottom) && (!isLeft) && square[i + 10 - 1].classList.contains('bomb')) count++;
square[i].setAttribute('data', count);
// adding event to display number
square[i].addEventListener('click', function () {
clicking(square[i]);
});
}
}
createGrid();
function sleep(milliseconds) {
const date = Date.now();
let currentDate = null;
do {
currentDate = Date.now();
} while (currentDate - date < milliseconds);
}
function clicking(ele) {
if(isGameOver)return;
if(ele.classList.contains('clicked'))return;
if (ele.classList.contains('bomb')) {
// alert('game over');
isGameOver=true;
for(let r=0;r<100;r++)
{
if(square[r].classList.contains('bomb'))
{
square[r].innerHTML="💣";
square[r].style.backgroundColor="#FFDEAD";
}
}
return;
} else {
let total = ele.getAttribute('data');
ele.classList.add('clicked');
if (total != 0) {
ele.innerHTML = total;
return;
}
doCheck(ele);
}
}
//
function doCheck(ele)
{
setTimeout(()=>{
let i=parseInt(ele.id);
if(i<0)
{
return;
}
console.log(i);
let isLeft = ((i % 10) == 0);
let isRight = ((i % 10) == 9);
let isTop = (i < 10)
let isBottom = (i >= 90);
if ((!isLeft) && square[i - 1].classList.contains('safe'))
{
clicking(square[i-1]);
}
if ((!isRight) && square[i + 1].classList.contains('safe'))
{
clicking(square[i+1]);
}
if ((!isBottom) && square[i + 10].classList.contains('safe'))
{
clicking(square[i+10]);
}
if ((!isTop) && square[i - 10].classList.contains('safe'))
{
clicking(square[i-10]);
}
if ((!isTop) && (!isRight) && square[i - 10 + 1].classList.contains('safe'))
{
clicking(square[i-10+1]);
}
if ((!isBottom) && (!isRight) && square[i + 10 + 1].classList.contains('safe'))
{
clicking(square[i+10+1]);
}
if ((!isTop) && (!isLeft) && square[i - 10 - 1].classList.contains('safe'))
{
clicking(square[i-1-10]);
}
if ((!isBottom) && (!isLeft) && square[i + 10 - 1].classList.contains('safe'))
{
clicking(square[i-1+10]);
}
},10);
}
});