-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
190 lines (174 loc) · 5.3 KB
/
index.html
File metadata and controls
190 lines (174 loc) · 5.3 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Minesweeper</title>
<style>
body {
background-color: #000;
margin: 0;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
height: 100vh;
overflow: hidden;
font-family: monospace;
color: #fff;
}
.menu {
margin-bottom: 20px;
display: flex;
gap: 10px;
align-items: center;
}
button {
background: #000;
color: #fff;
border: 1px solid #fff;
padding: 5px 10px;
cursor: pointer;
font-family: monospace;
}
button:hover {
background: #fff;
color: #000;
}
.stats {
margin-bottom: 15px;
font-size: 1.2rem;
letter-spacing: 2px;
}
#g {
display: grid;
gap: 2px;
border: 2px solid #fff;
padding: 2px;
background-color: #fff;
}
.c {
width: 25px;
height: 25px;
background-color: #333;
display: flex;
justify-content: center;
align-items: center;
cursor: pointer;
font-weight: bold;
user-select: none;
font-size: 14px;
}
.c.r {
background-color: #fff;
color: #000;
cursor: default;
}
.c.f::after {
content: "!";
color: #ff0000;
}
.c.m {
background-color: #ff0000;
}
.h {
margin-top: 20px;
text-align: center;
font-size: 0.8rem;
color: #888;
}
</style>
</head>
<body>
<div class="menu">
<button onclick="setup(8, 8, 10)">EASY</button>
<button onclick="setup(12, 12, 25)">MED</button>
<button onclick="setup(16, 16, 40)">HARD</button>
</div>
<div class="stats">MINES: <span id="m-count">0</span></div>
<div id="g"></div>
<div class="h">
LEFT CLICK : REVEAL | RIGHT CLICK : FLAG
</div>
<script>
const grid = document.getElementById('g');
const mineDisplay = document.getElementById('m-count');
let cells = [];
let rCount = 0;
let flags = 0;
let first = true;
let curSize, curMines;
function setup(w, h, m) {
curSize = w;
curMines = m;
grid.style.gridTemplateColumns = `repeat(${w}, 25px)`;
init();
}
function init() {
grid.innerHTML = '';
cells = [];
rCount = 0;
flags = 0;
first = true;
mineDisplay.innerText = curMines;
for (let i = 0; i < curSize * curSize; i++) {
const div = document.createElement('div');
div.classList.add('c');
const cell = { el: div, mine: false, r: false, f: false, i: i };
div.addEventListener('click', () => cell.r ? chord(cell) : reveal(cell));
div.addEventListener('contextmenu', (e) => { e.preventDefault(); flag(cell); });
grid.appendChild(div);
cells.push(cell);
}
}
function generate(safeIdx) {
let pos = new Set();
while(pos.size < curMines) {
let p = Math.floor(Math.random() * cells.length);
if (p !== safeIdx) pos.add(p);
}
cells.forEach((c, i) => { if (pos.has(i)) c.mine = true; });
}
function getN(i) {
const n = [];
const x = i % curSize, y = Math.floor(i / curSize);
for (let dx = -1; dx <= 1; dx++) {
for (let dy = -1; dy <= 1; dy++) {
if (dx === 0 && dy === 0) continue;
const nx = x + dx, ny = y + dy;
if (nx >= 0 && nx < curSize && ny >= 0 && ny < curSize) n.push(cells[ny * curSize + nx]);
}
}
return n;
}
function flag(c) {
if (c.r) return;
c.f = !c.f;
c.el.classList.toggle('f');
flags += c.f ? 1 : -1;
mineDisplay.innerText = curMines - flags;
}
function chord(c) {
const n = getN(c.i);
if (n.filter(x => x.f).length === n.filter(x => x.mine).length) n.forEach(x => reveal(x));
}
function reveal(c) {
if (c.r || c.f) return;
if (first) { first = false; generate(c.i); }
c.r = true;
c.el.classList.add('r');
if (c.mine) {
c.el.classList.add('m');
setTimeout(() => { alert('LOSE'); init(); }, 10);
return;
}
const n = getN(c.i);
const m = n.filter(x => x.mine).length;
if (m > 0) c.el.innerText = m;
else n.forEach(x => reveal(x));
rCount++;
if (rCount === cells.length - curMines) setTimeout(() => { alert('WIN'); init(); }, 10);
}
setup(10, 10, 15);
</script>
</body>
</html>