Skip to content

Commit dcb8302

Browse files
committedDec 9, 2022
Initial Version from 2022-12-07 Stream
1 parent 149e555 commit dcb8302

File tree

3 files changed

+118
-0
lines changed

3 files changed

+118
-0
lines changed
 

‎app.js

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { Game } from "./game.js";
2+
3+
const game = new Game(10, 10, 10, '#grid');
4+

‎game.js

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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+
}

‎index.html

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7+
<title>Minesweeper in JavaScript</title>
8+
<script type="module" src="app.js" defer></script>
9+
</head>
10+
<body>
11+
<table id="grid" style="font-size: 20pt;">
12+
</table>
13+
</body>
14+
</html>

0 commit comments

Comments
 (0)
Please sign in to comment.