-
Notifications
You must be signed in to change notification settings - Fork 163
/
Copy pathscript.js
177 lines (145 loc) · 4.37 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
var board = [];
var rows = 8;
var columns = 8;
var minesCount = 10;
var minesLocation = []; // "2-2", "3-4", "2-1"
var tilesClicked = 0; //goal to click all tiles except the ones containing mines
var flagEnabled = false;
var gameOver = false;
window.onload = function() {
startGame();
}
function setMines() {
// minesLocation.push("2-2");
// minesLocation.push("2-3");
// minesLocation.push("5-6");
// minesLocation.push("3-4");
// minesLocation.push("1-1");
let minesLeft = minesCount;
while (minesLeft > 0) {
let r = Math.floor(Math.random() * rows);
let c = Math.floor(Math.random() * columns);
let id = r.toString() + "-" + c.toString();
if (!minesLocation.includes(id)) {
minesLocation.push(id);
minesLeft -= 1;
}
}
}
function startGame() {
document.getElementById("mines-count").innerText = minesCount;
document.getElementById("flag-button").addEventListener("click", setFlag);
setMines();
//populate our board
for (let r = 0; r < rows; r++) {
let row = [];
for (let c = 0; c < columns; c++) {
//<div id="0-0"></div>
let tile = document.createElement("div");
tile.id = r.toString() + "-" + c.toString();
tile.addEventListener("click", clickTile);
document.getElementById("board").append(tile);
row.push(tile);
}
board.push(row);
}
console.log(board);
}
function setFlag() {
if (flagEnabled) {
flagEnabled = false;
document.getElementById("flag-button").style.backgroundColor = "lightgray";
}
else {
flagEnabled = true;
document.getElementById("flag-button").style.backgroundColor = "darkgray";
}
}
function clickTile() {
if (gameOver || this.classList.contains("tile-clicked")) {
return;
}
let tile = this;
if (flagEnabled) {
if (tile.innerText == "") {
tile.innerText = "🚩";
}
else if (tile.innerText == "🚩") {
tile.innerText = "";
}
return;
}
if (minesLocation.includes(tile.id)) {
// alert("GAME OVER");
gameOver = true;
revealMines();
return;
}
let coords = tile.id.split("-"); // "0-0" -> ["0", "0"]
let r = parseInt(coords[0]);
let c = parseInt(coords[1]);
checkMine(r, c);
}
function revealMines() {
for (let r= 0; r < rows; r++) {
for (let c = 0; c < columns; c++) {
let tile = board[r][c];
if (minesLocation.includes(tile.id)) {
tile.innerText = "💣";
tile.style.backgroundColor = "red";
}
}
}
}
function checkMine(r, c) {
if (r < 0 || r >= rows || c < 0 || c >= columns) {
return;
}
if (board[r][c].classList.contains("tile-clicked")) {
return;
}
board[r][c].classList.add("tile-clicked");
tilesClicked += 1;
let minesFound = 0;
//top 3
minesFound += checkTile(r-1, c-1); //top left
minesFound += checkTile(r-1, c); //top
minesFound += checkTile(r-1, c+1); //top right
//left and right
minesFound += checkTile(r, c-1); //left
minesFound += checkTile(r, c+1); //right
//bottom 3
minesFound += checkTile(r+1, c-1); //bottom left
minesFound += checkTile(r+1, c); //bottom
minesFound += checkTile(r+1, c+1); //bottom right
if (minesFound > 0) {
board[r][c].innerText = minesFound;
board[r][c].classList.add("x" + minesFound.toString());
}
else {
//top 3
checkMine(r-1, c-1); //top left
checkMine(r-1, c); //top
checkMine(r-1, c+1); //top right
//left and right
checkMine(r, c-1); //left
checkMine(r, c+1); //right
//bottom 3
checkMine(r+1, c-1); //bottom left
checkMine(r+1, c); //bottom
checkMine(r+1, c+1); //bottom right
}
if (tilesClicked == rows * columns - minesCount) {
document.getElementById("mines-count").innerText = "Cleared";
gameOver = true;
}
}
function checkTile(r, c) {
if (r < 0 || r >= rows || c < 0 || c >= columns) {
return 0;
}
if (minesLocation.includes(r.toString() + "-" + c.toString())) {
return 1;
}
return 0;
}