|
| 1 | +const gridSize = 10; |
| 2 | +const totalMines = 20; |
| 3 | +let remainingFlags = totalMines; |
| 4 | +let revealedCount = 0; |
| 5 | +let highscore = parseInt(localStorage.getItem("highscore")) || 0; |
| 6 | + |
| 7 | +const grid = document.getElementById("grid"); |
| 8 | +const bombCountDisplay = document.getElementById("bomb-count"); |
| 9 | +const resultDisplay = document.getElementById("result"); |
| 10 | +const retryButton = document.getElementById("retry-button"); |
| 11 | +const highscoreDisplay = document.getElementById("highscore"); |
| 12 | + |
| 13 | +let mines = Array(gridSize).fill(null).map(() => Array(gridSize).fill(false)); |
| 14 | +let revealed = Array(gridSize).fill(null).map(() => Array(gridSize).fill(false)); |
| 15 | +let flagged = Array(gridSize).fill(null).map(() => Array(gridSize).fill(false)); |
| 16 | + |
| 17 | +function resetGame() { |
| 18 | + mines = Array(gridSize).fill(null).map(() => Array(gridSize).fill(false)); |
| 19 | + revealed = Array(gridSize).fill(null).map(() => Array(gridSize).fill(false)); |
| 20 | + flagged = Array(gridSize).fill(null).map(() => Array(gridSize).fill(false)); |
| 21 | + plantMines(); |
| 22 | + remainingFlags = totalMines; |
| 23 | + revealedCount = 0; |
| 24 | + resultDisplay.textContent = ""; |
| 25 | + bombCountDisplay.textContent = `Bombs: ${remainingFlags}`; |
| 26 | + updateHighscore(); |
| 27 | + createGrid(); |
| 28 | +} |
| 29 | + |
| 30 | +retryButton.addEventListener("click", resetGame); |
| 31 | + |
| 32 | +function updateHighscore() { |
| 33 | + if (revealedCount === gridSize * gridSize - totalMines && remainingFlags >= 0) { |
| 34 | + if (remainingFlags > highscore) { |
| 35 | + highscore = remainingFlags; |
| 36 | + localStorage.setItem("highscore", highscore); |
| 37 | + } |
| 38 | + highscoreDisplay.textContent = `Highscore: ${highscore}`; |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +function plantMines() { |
| 43 | + let plantedMines = 0; |
| 44 | + while (plantedMines < totalMines) { |
| 45 | + const x = Math.floor(Math.random() * gridSize); |
| 46 | + const y = Math.floor(Math.random() * gridSize); |
| 47 | + if (!mines[x][y]) { |
| 48 | + mines[x][y] = true; |
| 49 | + plantedMines++; |
| 50 | + } |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +function countAdjacentMines(x, y) { |
| 55 | + let count = 0; |
| 56 | + for (let i = -1; i <= 1; i++) { |
| 57 | + for (let j = -1; j <= 1; j++) { |
| 58 | + const newX = x + i; |
| 59 | + const newY = y + j; |
| 60 | + if (newX >= 0 && newX < gridSize && newY >= 0 && newY < gridSize && mines[newX][newY]) { |
| 61 | + count++; |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + return count; |
| 66 | +} |
| 67 | + |
| 68 | +function revealCell(x, y) { |
| 69 | + if (x < 0 || x >= gridSize || y < 0 || y >= gridSize || revealed[x][y] || flagged[x][y]) { |
| 70 | + return; |
| 71 | + } |
| 72 | + revealed[x][y] = true; |
| 73 | + const cell = document.getElementById(`cell-${x}-${y}`); |
| 74 | + cell.classList.add("revealed"); |
| 75 | + revealedCount++; |
| 76 | + |
| 77 | + if (mines[x][y]) { |
| 78 | + cell.classList.add("mine"); |
| 79 | + resultDisplay.textContent = "Game Over! You stepped on a mine!"; |
| 80 | + return; |
| 81 | + } |
| 82 | + |
| 83 | + const minesNearby = countAdjacentMines(x, y); |
| 84 | + if (minesNearby > 0) { |
| 85 | + cell.textContent = minesNearby; |
| 86 | + } else { |
| 87 | + for (let i = -1; i <= 1; i++) { |
| 88 | + for (let j = -1; j <= 1; j++) { |
| 89 | + revealCell(x + i, y + j); |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + if (revealedCount === gridSize * gridSize - totalMines) { |
| 95 | + resultDisplay.textContent = "Congratulations! You Win!"; |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +function handleCellClick(x, y) { |
| 100 | + if (revealed[x][y] || flagged[x][y]) { |
| 101 | + return; |
| 102 | + } |
| 103 | + revealCell(x, y); |
| 104 | +} |
| 105 | + |
| 106 | +function handleCellRightClick(event, x, y) { |
| 107 | + event.preventDefault(); |
| 108 | + if (!revealed[x][y]) { |
| 109 | + flagged[x][y] = !flagged[x][y]; |
| 110 | + const cell = document.getElementById(`cell-${x}-${y}`); |
| 111 | + if (flagged[x][y]) { |
| 112 | + cell.classList.add("flag"); |
| 113 | + remainingFlags--; |
| 114 | + } else { |
| 115 | + cell.classList.remove("flag"); |
| 116 | + remainingFlags++; |
| 117 | + } |
| 118 | + bombCountDisplay.textContent = `Bombs: ${remainingFlags}`; |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +function createGrid() { |
| 123 | + for (let x = 0; x < gridSize; x++) { |
| 124 | + for (let y = 0; y < gridSize; y++) { |
| 125 | + const cell = document.createElement("div"); |
| 126 | + cell.className = "grid-item"; |
| 127 | + cell.id = `cell-${x}-${y}`; |
| 128 | + cell.addEventListener("click", () => handleCellClick(x, y)); |
| 129 | + cell.addEventListener("contextmenu", (e) => handleCellRightClick(e, x, y)); |
| 130 | + grid.appendChild(cell); |
| 131 | + } |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +plantMines(); |
| 136 | +createGrid(); |
0 commit comments