|
| 1 | +export class Game { |
| 2 | + |
| 3 | + #rows |
| 4 | + #columns |
| 5 | + #numOfMines |
| 6 | + #gridMeta |
| 7 | + #isGameWon |
| 8 | + #isGameOver |
| 9 | + |
| 10 | + constructor(height, width, numOfMines, gridId) { |
| 11 | + this.#rows = height; |
| 12 | + this.#columns = width; |
| 13 | + this.#numOfMines = numOfMines; |
| 14 | + this.#gridMeta = []; |
| 15 | + this.buildBoard(gridId); |
| 16 | + this.#isGameWon = false; |
| 17 | + this.#isGameOver = false; |
| 18 | + } |
| 19 | + |
| 20 | + buildBoard(gridId) { |
| 21 | + const table = document.querySelector(gridId); |
| 22 | + const cellsToBeMined = []; |
| 23 | + for (let rowIndex = 0; rowIndex < this.#rows; rowIndex++) { |
| 24 | + const row = document.createElement('tr'); |
| 25 | + table.appendChild(row); |
| 26 | + this.#gridMeta.push([]) |
| 27 | + for (let colIndex = 0; colIndex < this.#columns; colIndex++) { |
| 28 | + const cell = document.createElement('td'); |
| 29 | + cell.innerText = "⬛"; |
| 30 | + cell.dataset.number = 0; |
| 31 | + cell.dataset.bomb = false; |
| 32 | + cell.dataset.revealed = false; |
| 33 | + cell.addEventListener('contextmenu', event => event.preventDefault(), false); |
| 34 | + cell.addEventListener('mousedown', event => { |
| 35 | + if (event.button === 0) { |
| 36 | + this.reveal(cell); |
| 37 | + } else if (event.button === 2) { |
| 38 | + this.flag(cell); |
| 39 | + } |
| 40 | + }); |
| 41 | + |
| 42 | + cellsToBeMined.push({ rowIndex, colIndex, cell }); |
| 43 | + this.#gridMeta[rowIndex].push(cell); |
| 44 | + |
| 45 | + row.appendChild(cell); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + cellsToBeMined |
| 50 | + .sort(() => 0.5 - Math.random()) |
| 51 | + .slice(0, this.#numOfMines) |
| 52 | + .forEach(cellData => { |
| 53 | + cellData.cell.dataset.bomb = true; |
| 54 | + this.safeIncrement(cellData.rowIndex+1, cellData.colIndex); |
| 55 | + this.safeIncrement(cellData.rowIndex+1, cellData.colIndex+1); |
| 56 | + this.safeIncrement(cellData.rowIndex+1, cellData.colIndex-1); |
| 57 | + this.safeIncrement(cellData.rowIndex-1, cellData.colIndex); |
| 58 | + this.safeIncrement(cellData.rowIndex-1, cellData.colIndex+1); |
| 59 | + this.safeIncrement(cellData.rowIndex-1, cellData.colIndex-1); |
| 60 | + this.safeIncrement(cellData.rowIndex, cellData.colIndex+1); |
| 61 | + this.safeIncrement(cellData.rowIndex, cellData.colIndex-1); |
| 62 | + }); |
| 63 | + |
| 64 | + //this.revealAll(); |
| 65 | + } |
| 66 | + |
| 67 | + safeIncrement(row, col) { |
| 68 | + if (row >= this.#rows || col >= this.#columns || row < 0 || col < 0) { |
| 69 | + return; |
| 70 | + } |
| 71 | + this.#gridMeta[row][col].dataset.number++; |
| 72 | + } |
| 73 | + |
| 74 | + revealAll() { |
| 75 | + this.#gridMeta.forEach(row => |
| 76 | + row.forEach(cell => |
| 77 | + this.reveal(cell))) |
| 78 | + } |
| 79 | + |
| 80 | + reveal(cell) { |
| 81 | + const nums = [ "🟦", "1️⃣", "2️⃣", "3️⃣", "4️⃣", "5️⃣", "6️⃣", "7️⃣", "8️⃣" ]; |
| 82 | + if (cell.dataset.bomb === "true" && this.#isGameWon) { |
| 83 | + cell.innerText = "💣"; |
| 84 | + } else if (cell.dataset.bomb === "true") { |
| 85 | + cell.innerText = "💥"; |
| 86 | + this.#isGameOver = true; |
| 87 | + } else { |
| 88 | + cell.innerText = nums[cell.dataset.number]; |
| 89 | + } |
| 90 | + |
| 91 | + cell.dataset.revealed = "true"; |
| 92 | + } |
| 93 | + |
| 94 | + flag(cell) { |
| 95 | + if (cell.dataset.revealed === "true") { |
| 96 | + return; |
| 97 | + } |
| 98 | + cell.innerText = "🚩"; |
| 99 | + } |
| 100 | +} |
0 commit comments