-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
394 lines (313 loc) · 10.3 KB
/
script.js
File metadata and controls
394 lines (313 loc) · 10.3 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
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
// Game configuration
const ROWS = 14;
const COLS = 18;
const MINES = 40;
const API_URL = 'https://script.google.com/macros/s/AKfycbxGmBC7asHnpngfgSwQqtxig3notnM4CxTBhTQlUh5duSOHidgBKPSnvCg4ha0oC71GrQ/exec';
// Initialize Leaderboard Manager
let leaderboardManager;
if (API_URL && API_URL !== 'YOUR_GOOGLE_APPS_SCRIPT_URL_HERE') {
leaderboardManager = new LeaderboardManager(API_URL);
}
// Game state
let board = [];
let mineLocations = new Set();
let revealedCells = 0;
let flaggedCells = 0;
let gameStarted = false;
let gameOver = false;
let startTime = null;
let timerInterval = null;
let timeStamps = [];
// DOM elements
const boardElement = document.getElementById('board');
const mineCountElement = document.getElementById('mine-count');
const timerElement = document.getElementById('timer');
const resetBtn = document.getElementById('reset-btn');
const finalTimeElement = document.getElementById('final-time');
const playAgainBtn = document.getElementById('play-again-btn');
// Initialize game
function initGame() {
board = [];
mineLocations = new Set();
revealedCells = 0;
flaggedCells = 0;
gameStarted = false;
gameOver = false;
startTime = null;
timeStamps = [];
if (timerInterval) {
clearInterval(timerInterval);
timerInterval = null;
}
timerElement.textContent = '0.00';
mineCountElement.textContent = MINES;
//gameOverOverlay.classList.remove('show');
createBoard();
renderBoard();
}
// Create the board structure
function createBoard() {
for (let row = 0; row < ROWS; row++) {
board[row] = [];
for (let col = 0; col < COLS; col++) {
board[row][col] = {
isMine: false,
isRevealed: false,
isFlagged: false,
neighborMines: 0
};
}
}
}
// Place mines randomly (avoiding first clicked cell)
function placeMines(firstRow, firstCol) {
let minesPlaced = 0;
while (minesPlaced < MINES) {
const row = Math.floor(Math.random() * ROWS);
const col = Math.floor(Math.random() * COLS);
// Don't place mine on first clicked cell or its neighbors
if (Math.abs(row - firstRow) <= 1 && Math.abs(col - firstCol) <= 1) {
continue;
}
const key = `${row},${col}`;
if (!mineLocations.has(key)) {
mineLocations.add(key);
board[row][col].isMine = true;
minesPlaced++;
}
}
calculateNeighborMines();
}
// Calculate neighbor mine counts
function calculateNeighborMines() {
for (let row = 0; row < ROWS; row++) {
for (let col = 0; col < COLS; col++) {
if (!board[row][col].isMine) {
board[row][col].neighborMines = countNeighborMines(row, col);
}
}
}
}
// Count mines in neighboring cells
function countNeighborMines(row, col) {
let count = 0;
for (let dRow = -1; dRow <= 1; dRow++) {
for (let dCol = -1; dCol <= 1; dCol++) {
if (dRow === 0 && dCol === 0) continue;
const newRow = row + dRow;
const newCol = col + dCol;
if (isValidCell(newRow, newCol) && board[newRow][newCol].isMine) {
count++;
}
}
}
return count;
}
// Check if cell coordinates are valid
function isValidCell(row, col) {
return row >= 0 && row < ROWS && col >= 0 && col < COLS;
}
// Render the board
function renderBoard() {
boardElement.innerHTML = '';
for (let row = 0; row < ROWS; row++) {
for (let col = 0; col < COLS; col++) {
const cell = document.createElement('div');
cell.className = 'cell';
cell.dataset.row = row;
cell.dataset.col = col;
// Add staggered animation delay
cell.style.animationDelay = `${(row * COLS + col) * 0.001}s`;
cell.addEventListener('click', () => handleCellClick(row, col));
cell.addEventListener('contextmenu', (e) => handleRightClick(e, row, col));
updateCellDisplay(cell, row, col);
boardElement.appendChild(cell);
}
}
}
// Update cell display
function updateCellDisplay(cellElement, row, col) {
const cell = board[row][col];
cellElement.classList.remove('revealed', 'flagged', 'mine', 'wrong-flag');
cellElement.textContent = '';
cellElement.removeAttribute('data-count');
if (cell.isFlagged) {
cellElement.classList.add('flagged');
} else if (cell.isRevealed) {
cellElement.classList.add('revealed');
if (cell.isMine) {
cellElement.classList.add('mine');
} else if (cell.neighborMines > 0) {
cellElement.textContent = cell.neighborMines;
cellElement.dataset.count = cell.neighborMines;
}
}
}
// Handle cell click
function handleCellClick(row, col) {
if (gameOver) return;
const cell = board[row][col];
// If cell is revealed with a number, try chord (auto-reveal neighbors)
if (cell.isRevealed && cell.neighborMines > 0) {
chordCell(row, col);
return;
}
if (cell.isFlagged || cell.isRevealed) return;
// Start game on first click
if (!gameStarted) {
gameStarted = true;
placeMines(row, col);
startTimer();
}
revealCell(row, col);
}
// Chord function: auto-reveal neighbors if flags match the number
function chordCell(row, col) {
const cell = board[row][col];
if (!cell.isRevealed || cell.neighborMines === 0) return;
// Count neighboring flags
let flagCount = 0;
const neighbors = [];
for (let dRow = -1; dRow <= 1; dRow++) {
for (let dCol = -1; dCol <= 1; dCol++) {
if (dRow === 0 && dCol === 0) continue;
const newRow = row + dRow;
const newCol = col + dCol;
if (isValidCell(newRow, newCol)) {
const neighborCell = board[newRow][newCol];
neighbors.push({ row: newRow, col: newCol });
if (neighborCell.isFlagged) {
flagCount++;
}
}
}
}
// Only chord if flag count matches the number
if (flagCount === cell.neighborMines) {
// Check if any flags are incorrect (on non-mine cells)
let hasIncorrectFlag = false;
for (let dRow = -1; dRow <= 1; dRow++) {
for (let dCol = -1; dCol <= 1; dCol++) {
if (dRow === 0 && dCol === 0) continue;
const newRow = row + dRow;
const newCol = col + dCol;
if (isValidCell(newRow, newCol)) {
const neighborCell = board[newRow][newCol];
// If flagged but not a mine, game over
if (neighborCell.isFlagged && !neighborCell.isMine) {
hasIncorrectFlag = true;
break;
}
}
}
if (hasIncorrectFlag) break;
}
if (hasIncorrectFlag) {
// End game - incorrect flag
endGame(false);
return;
}
// Reveal all non-flagged neighbors
for (const neighbor of neighbors) {
const neighborCell = board[neighbor.row][neighbor.col];
if (!neighborCell.isFlagged && !neighborCell.isRevealed) {
revealCell(neighbor.row, neighbor.col);
}
}
}
}
// Handle right click (flag)
function handleRightClick(event, row, col) {
event.preventDefault();
if (gameOver || !gameStarted) return;
const cell = board[row][col];
if (cell.isRevealed) return;
cell.isFlagged = !cell.isFlagged;
if (cell.isFlagged) {
flaggedCells++;
} else {
flaggedCells--;
}
mineCountElement.textContent = MINES - flaggedCells;
const cellElement = boardElement.querySelector(`[data-row="${row}"][data-col="${col}"]`);
updateCellDisplay(cellElement, row, col);
}
// Reveal a cell
function revealCell(row, col) {
if (!isValidCell(row, col)) return;
const cell = board[row][col];
if (cell.isRevealed || cell.isFlagged) return;
cell.isRevealed = true;
revealedCells++;
timeStamps.push(Date.now());
const cellElement = boardElement.querySelector(`[data-row="${row}"][data-col="${col}"]`);
updateCellDisplay(cellElement, row, col);
if (cell.isMine) {
endGame(false);
return;
}
// Auto-reveal neighbors if no neighboring mines
if (cell.neighborMines === 0) {
for (let dRow = -1; dRow <= 1; dRow++) {
for (let dCol = -1; dCol <= 1; dCol++) {
if (dRow === 0 && dCol === 0) continue;
revealCell(row + dRow, col + dCol);
}
}
}
// Check for win
if (revealedCells === ROWS * COLS - MINES) {
endGame(true);
}
}
// Start timer
function startTimer() {
startTime = Date.now();
timerInterval = setInterval(() => {
const elapsed = (Date.now() - startTime) / 1000;
timerElement.textContent = elapsed.toFixed(2);
}, 10);
}
// End game
function endGame(won) {
gameOver = true;
if (timerInterval) {
clearInterval(timerInterval);
}
const now = Date.now();
const finalTime = ((now - startTime) / 1000);
timeStamps.push(now);
// Reveal all mines
for (let row = 0; row < ROWS; row++) {
for (let col = 0; col < COLS; col++) {
const cell = board[row][col];
const cellElement = boardElement.querySelector(`[data-row="${row}"][data-col="${col}"]`);
if (cell.isMine && !won) {
cell.isRevealed = true;
updateCellDisplay(cellElement, row, col);
}
if (cell.isFlagged && !cell.isMine) {
cellElement.classList.add('wrong-flag');
}
}
}
// Show game over overlay briefly, then modal
setTimeout(() => {
if (leaderboardManager) {
leaderboardManager.showGameOverModal(won, finalTime, now, startTime, timeStamps, MINES);
}
}, 300);
}
// Event listeners
resetBtn.addEventListener('click', initGame);
document.addEventListener('keydown', (e) => {
if (e.key === "r" || e.key === "R") {
initGame();
}
})
// Initialize game on load
initGame();
// Initialize permanent leaderboard if API is configured
if (leaderboardManager) {
leaderboardManager.createPermanentLeaderboard();
}