-
Notifications
You must be signed in to change notification settings - Fork 0
/
minesweeper.js
315 lines (277 loc) · 10.5 KB
/
minesweeper.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
// create a new grid
const createGrid = (x,y,n) => {
let grid = [];
for(i=0; i<x; i++){
grid[i] = [];
for(j=0; j<y; j++){
grid[i][j] = ["hidden", 0];
}
}
placeBombs(grid, n); // place bombs in the grid
return {"grid": grid, "nb_bomb": n, "hidden_cells": x*y};
}
// place bombs in the grid
const placeBombs = (grid, n) => {
let remain = n;
let x = grid.length;
let y = grid[0].length;
while(remain > 0){
// select a random cell
randX = Math.floor(Math.random() * x);
randY = Math.floor(Math.random() * y);
if(grid[randX][randY][1] != -1){
grid[randX][randY][1] = -1;
updateNeighboors(grid, randX, randY); // Update adjacent cells value
remain--;
}
}
}
// Return the list of the non-bomb neighboors of cell at coordinate (x, y)
const neighboors = (grid, x, y) => {
let neighboors = []
let n = grid.length;
let m = grid[0].length;
for (i=-1; i<= 1; i++){
for (j=-1; j<= 1; j++){
if (x+i >= 0 && x+i < n && y+j >= 0 && y+j < m && grid[x+i][y+j][1] != -1){
neighboors.push([x+i,y+j]);
}
}
}
return neighboors;
}
// Update value of the neighboors of a bombs
const updateNeighboors = (grid, x, y) => {
let neighs = neighboors(grid, x, y);
for(neigh of neighs){
grid[neigh[0]][neigh[1]][1] += 1;
}
}
// Display the grid
const displayGrid = (grid) => {
let gridHTML = document.getElementById("grid");
let x = grid.length;
let y = grid[0].length;
let html = "<table class=\"grid\">";
for(j=0; j<y; j++){
gridHTML.innerHTML += "<tr>";
for(i=0; i<x; i++){
let cell = grid[i][j];
if(cell[0] == "hidden"){
html += "<td id=\'{" + "\"x\":" + i + ", \"y\":" + j + "}\' oncontextmenu=\"javascript:handleRightClick(this);return false;\" onclick=\"handleLeftClick(this)\" class=\"hidden\"></td>";
}else if(cell[0] == "flagged"){
html += "<td id=\'{" + "\"x\":" + i + ", \"y\":" + j + "}\' oncontextmenu=\"javascript:handleRightClick(this);return false;\" class=\"flagged\"><img src=\"assets/flag.png\" class=\"image\"></img></td>"
}else{
switch(cell[1]){
case -1:
html += "<td id=\'{" + "\"x\":" + i + ", \"y\":" + j + "}\' oncontextmenu=\"javascript:return false;\" class=\"discovered\"><img src=\"assets/bomb.png\" class=\"image\"></img></td>"
break;
case 0:
html += "<td id=\'{" + "\"x\":" + i + ", \"y\":" + j + "}\' oncontextmenu=\"javascript:return false;\" class=\"discovered number0\"></td>"
break;
default:
html += "<td id=\'{" + "\"x\":" + i + ", \"y\":" + j + "}\' oncontextmenu=\"javascript:return false;\" class=\"discovered number" + cell[1] + "\">" + cell[1] + "</td>"
break;
}
}
}
html += "</tr>";
}
html += "</table>"
gridHTML.innerHTML = html;
}
// Display the grid when the game is finished
const displayStaticGrid = (grid) => {
let gridHTML = document.getElementById("grid");
let x = grid.length;
let y = grid[0].length;
let html = "<table class=\"grid\">";
for(j=0; j<y; j++){
gridHTML.innerHTML += "<tr>";
for(i=0; i<x; i++){
let cell = grid[i][j];
if(cell[0] == "hidden"){
html += "<td oncontextmenu=\"javascript:return false;\" class=\"hidden\"></td>";
}else if(cell[0] == "flagged"){
html += "<td oncontextmenu=\"javascript:return false;\" class=\"flagged\"><img src=\"assets/flag.png\" class=\"image\"></img></td>"
}else{
switch(cell[1]){
case -1:
html += "<td oncontextmenu=\"javascript:return false;\" class=\"discovered\"><img src=\"assets/bomb.png\" class=\"image\"></img></td>"
break;
case 0:
html += "<td oncontextmenu=\"javascript:return false;\" class=\"discovered number0\"></td>"
break;
default:
html += "<td oncontextmenu=\"javascript:return false;\" class=\"discovered number" + cell[1] + "\">" + cell[1] + "</td>"
break;
}
}
}
html += "</tr>";
}
html += "</table>"
gridHTML.innerHTML = html;
}
// Discover cells with no bomb nearby
const discover = (grid, x, y) => {
pile = neighboors(grid["grid"], x, y); // compute neighboors of the cell
while(pile.length){ // while the pile is not empty
let cell = pile.pop(); // get the head of the pile
if(grid["grid"][cell[0]][cell[1]][1] == 0 && grid["grid"][cell[0]][cell[1]][0] == "hidden"){ // if the cell is empty and hidden, discover the cell and add its neighboors to the pile
pile = pile.concat(neighboors(grid["grid"], cell[0], cell[1]));
}
if(grid["grid"][cell[0]][cell[1]][0] == "hidden"){// if the case has a value, discover it but not its neighboors
grid["hidden_cells"] -= 1;// Decrement total hidden cells
}
grid["grid"][cell[0]][cell[1]][0] = "discovered";
}
}
// Handle left click event on cell
const handleLeftClick = (e) => {
// get coordinates from id
let coor = JSON.parse(e.id);
let x = coor["x"];
let y = coor["y"];
// discover the cell
grid["grid"][x][y][0] = "discovered";
grid["hidden_cells"] -= 1;
// if it is the first play of the game, start the timer
if(timer == 0){
let d = new Date();
timer = d.getTime();
timerInterval = setInterval(updateTimer, 10);
}
// If the case is empty, discover is neighboors
if(grid["grid"][x][y][1] == 0){
discover(grid, x, y);
displayGrid(grid["grid"]);
}else if(grid["grid"][x][y][1] == -1){ // if it is a bomb, finish the game
// Display modal
let score = document.getElementById("score");
score.innerHTML = "YOU LOOSE";
score.style.display = "block";
// Display final grid
displayStaticGrid(grid["grid"]);
// End the timer and print the total time
let d = new Date();
t = d.getTime();
let time = document.getElementById("timer");
time.innerHTML = String("00" + Math.round((t-timer)/1000)).slice(-3);
timer = 0;
clearInterval(timerInterval);
}else if(checkWin(grid)){ // if the player win the game
// Display the modal
let score = document.getElementById("score");
score.innerHTML = "YOU WIN";
score.style.display = "block";
// Display final grid
displayStaticGrid(grid["grid"]);
// End the timer and print the total time
let d = new Date();
t = d.getTime();
let time = document.getElementById("timer");
time.innerHTML = String("00" + Math.round((t-timer)/1000)).slice(-3);
clearInterval(timerInterval);
// Update score page
updateScore(Math.round((t-timer)/10)/100);
timer = 0;
}else{
// Display the updated grid
displayGrid(grid["grid"]);
}
}
// Handle right click on cell
const handleRightClick = (e) => {
// get coordinates from id
let coor = JSON.parse(e.id);
let x = coor["x"];
let y = coor["y"];
// If it is the first play of the game, start timer
if(timer == 0){
let d = new Date();
timer = d.getTime();
timerInterval = setInterval(updateTimer, 1000);
}
// If the cell is already flagged, de-flag it
if(grid["grid"][x][y][0] == "flagged"){
grid["grid"][x][y][0] = "hidden";
}else{ // else flag it
grid["grid"][x][y][0] = "flagged";
}
// Display updated grid
displayGrid(grid["grid"]);
}
// Check if the game is winned
const checkWin = (grid) => {
return (grid["hidden_cells"] == grid["nb_bomb"]); // Compare hidden cells number to bomb number
}
// Start a new game
const newGame = () => {
// Create score table in localSotrage if not already set
let scores = JSON.parse(localStorage.getItem("bestScores"));
if(scores == null){
scores = {"easy": [], "normal": [], "hard": []};
}
localStorage.setItem("bestScores", JSON.stringify(scores));
// get conf from localStorage
let conf = JSON.parse(localStorage.getItem("conf"));
timer = 0;
clearInterval(timerInterval);
if(conf != null){
let n = conf["n"];
let m = conf["m"];
let bombs = conf["bombs"];
grid = createGrid(n,m,bombs);
}else{ // default value
grid = createGrid(9,9,10);
localStorage.setItem("conf", JSON.stringify({"n": 9, "m": 9, "bombs": 10}));
}
// Reset score and timer
let score = document.getElementById("score");
score.innerHTML = "";
score.style.display = "none";
let time = document.getElementById("timer");
time.innerHTML = "000";
displayGrid(grid["grid"]);
}
// Update timer value
const updateTimer = () => {
let d = new Date();
t = d.getTime();
let time = document.getElementById("timer");
time.innerHTML = String("00" + Math.round((t-timer)/1000)).slice(-3);
}
// Update score table
const updateScore = (score) => {
let scores = JSON.parse(localStorage.getItem("bestScores"));
let conf = JSON.parse(localStorage.getItem("conf"));
if(conf["n"] == 9 && conf["m"] == 9 && conf["bombs"] == 10){
scores["easy"] = insertScore(scores["easy"], score).slice(0, 9);
}else if(conf["n"] == 16 && conf["m"] == 16 && conf["bombs"] == 40){
scores["normal"] = insertScore(scores["normal"], score).slice(0, 9);
}else if(conf["n"] == 30 && conf["m"] == 16 && conf["bombs"] == 99){
scores["hard"] = insertScore(scores["hard"], score).slice(0, 9);
}
localStorage.setItem("bestScores", JSON.stringify(scores));
}
// insert score in the table recursively
const insertScore = (list, score) => {
if(list.length == 0){
return [score];
}else{
let worst = list.pop();
if(score < worst){
return insertScore(list,score).concat(worst);
}else{
return list.concat(worst).concat(score);
}
}
}
// Create global variable (grid and timer) to be accessible anywhere
let grid;
let timer;
timer = 0;
let timerInterval;
// Start a new game when player arrive on the website
newGame();