Repository navigation
Expand file tree
/
Copy pathminesweeper.html
More file actions
131 lines (114 loc) · 3.82 KB
/
Copy pathminesweeper.html
File metadata and controls
131 lines (114 loc) · 3.82 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Minesweeper Mini - Java Terminal Demo</title>
<link rel="stylesheet" href="./styles/main.css">
<link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;600&display=swap" rel="stylesheet">
</head>
<body>
<div class="nav-bar">
<a href="./index.html">🏠 Home</a>
<a href="./word-scramble.html">🔤 Word Scramble</a>
<a href="./number-converter.html">🔢 Number Converter</a>
<a href="./memory-game.html">🧠 Memory Game</a>
<a href="./minesweeper.html" class="active">💣 Minesweeper</a>
<a href="https://github.com/Janani-V219/Java_Mini_Projects" target="_blank">📄 GitHub Repo</a>
</div>
<div class="terminal" id="Terminal">
<div class="console-line">
Click <button id="run-button">Run</button> to run Minesweeper Mini.
</div>
<div class="content" id="Content"></div>
</div>
<script src="./terminal.js"></script>
<script>
let isRunning = false;
document.getElementById("run-button").addEventListener("click", async () => {
if (isRunning) return;
isRunning = true;
clearScreen();
const size = 5;
const mines = 5;
const mineGrid = Array.from({ length: size }, () => Array(size).fill(false));
const revealed = Array.from({ length: size }, () => Array(size).fill(false));
let placedMines = 0;
while (placedMines < mines) {
const r = Math.floor(Math.random() * size);
const c = Math.floor(Math.random() * size);
if (!mineGrid[r][c]) {
mineGrid[r][c] = true;
placedMines++;
}
}
let safeCells = 0;
const totalSafeCells = size * size - mines;
print("================================");
print(" MINESWEEPER MINI");
print("================================");
while (true) {
print("");
let header = " ";
for (let col = 1; col <= size; col++) {
header += col + " ";
}
print(header);
for (let r = 0; r < size; r++) {
let line = (r + 1) + " ";
for (let c = 0; c < size; c++) {
if (revealed[r][c]) {
line += "✓ ";
} else {
line += "■ ";
}
}
print(line);
}
print("");
const rowInput = await readLine("Enter row (1-5): ");
const row = parseInt(rowInput.trim(), 10) - 1;
const colInput = await readLine("Enter column (1-5): ");
const col = parseInt(colInput.trim(), 10) - 1;
if (isNaN(row) || isNaN(col) || row < 0 || row >= size || col < 0 || col >= size) {
print("❌ Invalid position!");
continue;
}
if (revealed[row][col]) {
print("⚠️ You already selected this cell!");
continue;
}
if (mineGrid[row][col]) {
print("");
print("💣 BOOM!");
print("You hit a mine!");
print("\nMine locations:");
for (let r = 0; r < size; r++) {
let line = "";
for (let c = 0; c < size; c++) {
line += mineGrid[r][c] ? "💣 " : "■ ";
}
print(line);
}
print("\nGame Over!");
break;
} else {
revealed[row][col] = true;
safeCells++;
print("✅ Safe cell!");
if (safeCells === totalSafeCells) {
print("");
print("🎉 Congratulations!");
print("You cleared the entire board!");
print("You avoided all " + mines + " mines!");
break;
}
}
}
print("");
print("Press Run to play again.");
isRunning = false;
});
</script>
</body>
</html>