-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
52 lines (43 loc) · 1.2 KB
/
index.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
import init, {
getGameState,
openCell,
toggleFlag,
} from "../pkg/minesweeper.js";
const render = () => {
let gameData = getGameState()
.split("\n")
.map((row) => row.trim().split(" "));
const app = document.getElementById("app");
app.innerHTML = ""; // Clear the app
app.className = "app";
app.style.display = "inline-grid";
app.style.gridTemplate = `repeat(${gameData.length}, 1fr) / repeat(${gameData[0].length}, 1fr)`;
app.style.columnGap = "0.7em";
app.style.rowGap = "0.7em";
gameData.forEach((row, columnIndex) => {
row.forEach((cell, rowIndex) => {
const cellEl = document.createElement("a");
cellEl.href = "#";
cellEl.className = "cell";
cellEl.textContent = cell === "0" ? "" : cell;
cellEl.addEventListener("click", (e) => {
e.preventDefault();
openCell(rowIndex, columnIndex);
// Re-render the game
render();
});
cellEl.addEventListener("contextmenu", (e) => {
e.preventDefault();
toggleFlag(rowIndex, columnIndex);
// Re-render the game
render();
});
app.appendChild(cellEl);
});
});
};
async function run() {
await init();
render();
}
run();