-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
248 lines (236 loc) · 6.92 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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
let minesAmmount = null
let boardSize = null
let diff = null
let flags = null
function setDifficulty (element) {
diff = element.className;
switch (diff) {
case "easy":
boardSize = "9x9"
minesAmmount = 10
break;
case "normal":
boardSize = "16x16"
minesAmmount = 40
break;
case "hard":
boardSize = "16x30"
minesAmmount = 99
break;
}
document.querySelector(".game").style.display = "flex";
document.querySelector(".choose").style.display = "none";
flags = minesAmmount;
generateBoard()
}
function generateBoard () {
let board = document.querySelector(".board");
let style = document.createElement('style')
switch (boardSize) {
case "9x9":
board.style.width = "360px";
board.style.height = "360px";
style.innerHTML = `.row {
width: 360px;
height: 40px;
display: flex;
flex-direction: row;
}
.cell {
width: 40px;
height: 40px;
}`
break;
case "16x16":
board.style.width = "640px";
board.style.height = "640px";
style.innerHTML = `.row {
width: 640px;
height: 40px;
display: flex;
flex-direction: row;
}
.cell {
width: 40px;
height: 40px;
}`
break;
case "16x30":
board.style.width = "960px";
board.style.height = "640px";
style.innerHTML = `.row {
width: 960px;
height: 40px;
display: flex;
flex-direction: row;
}
.cell {
width: 40px;
height: 40px;
}`
break;
}
document.body.appendChild(style)
board.innerHTML = "";
let ss = boardSize.split("x")[0]
let ss2 = boardSize.split("x")[1]
for (let i = 0; i < ss; i++) {
let row = document.createElement("div");
row.className = "row";
for (let j = 0; j < ss2; j++) {
let cell = document.createElement("div");
cell.className = "cell";
cell.id = `${i}-${j}`;
cell.onclick = function () {
checkCell(cell);
}
cell.addEventListener("contextmenu", function (e) {
e.preventDefault();
if (cell.textContent == "🚩") {
cell.classList.remove("flag");
cell.textContent = "";
flags++;
} else if (cell.textContent == "") {
cell.classList.add("flag");
cell.textContent = "🚩"
flags--;
checkFlags()
}
})
row.appendChild(cell);
}
board.appendChild(row);
}
generateMines(minesAmmount, diff)
}
function isMine (cell) {
return cell.classList.contains("mine");
}
function isClicked (cell) {
return cell.classList.contains("clicked");
}
function getNeighbors (cell) {
let n = [];
let row = +cell.id.split("-")[0]
let col = +cell.id.split("-")[1]
let height = +boardSize.split("x")[0]
let width = +boardSize.split("x")[1]
if (row > 0) {
if (col > 0)
n.push(document.getElementById(`${row - 1}-${col - 1}`));
n.push(document.getElementById(`${row - 1}-${col}`));
if (col + 1 < width)
n.push(document.getElementById(`${row - 1}-${col + 1}`));
}
if (row + 1 < height) {
if (col > 0)
n.push(document.getElementById(`${row + 1}-${col - 1}`));
n.push(document.getElementById(`${row + 1}-${col}`));
if (col + 1 < width)
n.push(document.getElementById(`${row + 1}-${col + 1}`));
}
if (col > 0)
n.push(document.getElementById(`${row}-${col - 1}`));
if (col + 1 < width)
n.push(document.getElementById(`${row}-${col + 1}`));
return n
}
function checkCell (cell) {
let classes = cell.classList
if (isClicked(cell)) return
if (isMine(cell)) return;
classes.add("clicked");
let neighbors = getNeighbors(cell);
let mines = 0;
neighbors.forEach(neighbor => {
if (isMine(neighbor))
mines++;
});
if (mines == 0) {
neighbors.forEach(checkCell);
} else {
cell.textContent = mines;
}
}
function generateMines (ammount, difficulty) {
let mines = [];
let boardHeight = boardSize.split("x")[0]
let boardWidth = boardSize.split("x")[1]
for (let i = 0; i < ammount; i++) {
let x = Math.floor(Math.random() * boardHeight);
let y = Math.floor(Math.random() * boardWidth);
let mine = `${x}-${y}`;
if (mines.includes(mine)) {
i--;
} else {
mines.push(mine);
}
}
mines.forEach(mine => {
let cell = document.getElementById(`${mine}`);
cell.classList.add("mine");
cell.addEventListener('click', (e) => {
gameOver(cell)
})
})
document.querySelector(".info").innerHTML = `${mines.length} mines`
}
function gameOver (cell) {
showMines()
cell.classList.add("died")
//remove all event listeners
let cells = document.querySelectorAll(".cell")
cells.forEach(cell => {
cell.removeEventListener("contextmenu", function (e) {
e.preventDefault();
if (cell.textContent == "🚩") {
cell.classList.remove("flag");
cell.textContent = "";
flags++;
}
else {
cell.classList.add("flag");
cell.textContent = "🚩"
flags--;
checkFlags()
}
})
cell.removeEventListener("click", function (e) {
e.preventDefault();
checkCell(cell)
})
})
let mines = document.querySelectorAll(".mine")
mines.forEach(mine => {
mine.removeEventListener("mousedown", function (e) {
e.preventDefault();
mine.classList.add("died")
gameOver()
})
})
checkCell = () => { }
gameOver = () => { }
alert("You died!")
}
function showMines () {
let mines = document.querySelectorAll(".mine");
mines.forEach(mine => {
mine.textContent = "💣"
})
}
function checkFlags () {
let miness = 0
if (flags == 0) {
let flagElements = document.querySelectorAll('.flag')
flagElements.forEach(flag => {
if (flag.classList.contains("mine")) {
flag.classList.remove("flag")
miness++
}
})
if (miness == minesAmmount) {
alert("You won!")
checkCell = () => { }
}
}
}