-
Notifications
You must be signed in to change notification settings - Fork 0
/
MineSweeper.html
257 lines (256 loc) · 7.97 KB
/
MineSweeper.html
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
<!-- @format -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>MineSweeper</title>
<style>
#mine-sweeper {
--n: 32;
--m: 32;
width: max-content;
display: flex;
flex-direction: column;
outline: 0.1em solid gray;
padding: 0.2em;
gap: 0.5em;
}
#mine-sweeper header {
display: flex;
align-items: end;
flex-direction: row;
justify-content: space-around;
max-width: 100vw;
}
#mine-sweeper header .info {
display: grid;
grid-template: repeat(2, 1fr) / repeat(2, auto);
text-align: right;
}
#mine-sweeper header .info * {
margin: 0;
padding: 0 0.1em;
}
#mine-sweeper .board {
display: grid;
grid-template: repeat(var(--m), 1fr) / repeat(var(--n), auto);
gap: 0.1em;
width: min-content;
}
#mine-sweeper .board .cell {
display: block;
text-align: center;
outline: 0.1em solid gray;
border-radius: 0.2em;
width: 1.5em;
height: 1.5em;
line-height: 1.5em;
-webkit-user-select: none;
user-select: none;
}
#mine-sweeper .board .cell.flagged {
outline: 0.1em dotted #8008;
}
#mine-sweeper .board .cell:focus,
#mine-sweeper .board .cell:hover {
background-color: #0008;
}
#mine-sweeper .board .cell.dug {
background-color: #0008;
outline: none;
}
</style>
<!-- prettier-ignore -->
<style>
/* State Colors */
#mine-sweeper.win .board .cell { --bomb-color: green; }
#mine-sweeper.lose .board .cell { --bomb-color: red; }
#mine-sweeper .board .cell.dug.value0 { color: transparent; }
#mine-sweeper .board .cell.dug.value1 { color: blue; }
#mine-sweeper .board .cell.dug.value2 { color: green; }
#mine-sweeper .board .cell.dug.value3 { color: red; }
#mine-sweeper .board .cell.dug.value4 { color: purple; }
#mine-sweeper .board .cell.dug.value5 { color: yellow; }
#mine-sweeper .board .cell.dug.value6 { color: orange; }
#mine-sweeper .board .cell.dug.value7 { color: darkred; }
#mine-sweeper .board .cell.dug.value8 { color: black; }
#mine-sweeper .board .cell.dug.value💣 { background-color: var(--bomb-color); }
</style>
<script>
const mineSweeper = {
/**@typedef {{
* i : number,
* j : number,
* value : string,
* div : HTMLElement,
* flagged : boolean,
* mine : boolean,
* dug : boolean,
* }} cell
*/
n: 32,
m: 32,
/**@type {cell[]}*/
cells: [],
/**@param {number} i
* @param {number} j
*/
cellAt(i, j) {
const { cells, n, m } = this
if (i < 0 || n <= i || j < 0 || m <= j) return
return cells[i + j * n]
},
/**@param {{i:number,j:number}}
* @returns {cell[]}
*/
cellsAround({ i, j }) {
const offsets = Array.from({ length: 3 }, (_, offset) => -1 + offset)
return offsets
.flatMap(offsetI => offsets.map(offsetJ => this.cellAt(offsetI + i, offsetJ + j)))
.filter(cell => cell && cell !== this.cellAt(i, j))
},
/**@param {number} [n]
* @param {number} [m]
* @param {number} [mineCount]
*/
reset(n, m, mineCount) {
this.n = n ??= this.n
this.m = m ??= this.m
mineCount ??= Math.floor(n * m * 0.2)
const { templateDomCell } = this
// Create Cells
/**@type {cell[]}*/
const cells = (this.cells = Array.from({ length: n * m }, (_, flatI) => ({
i: Math.floor(flatI % n),
j: Math.floor(flatI / n),
value: 0,
div: null,
get flagged() { return this._flagged }, // prettier-ignore
set flagged(flagged) {
this.div.classList.toggle('flagged', (this._flagged = flagged))
this.div.textContent = flagged ? '🚩' : templateDomCell.textContent
},
get mine() { return this.value === '💣' }, // prettier-ignore
set mine(mine) { this.value = mine ? '💣' : this.mine ? '' : this.value }, // prettier-ignore
get dug() { return this._dug }, // prettier-ignore
set dug(dug) {
this.div.classList.toggle('dug', (this._dug = dug))
this.div.classList.toggle(`value${this.value}`, dug)
this.div.textContent = this.value
},
_flagged: false,
_dug: false,
})))
// Place bombs
/**@type {Set<cell>}*/ const mines = new Set()
while (mines.size < mineCount) mines.add(cells[Math.floor(Math.random() * cells.length)])
for (const mine of mines) mine.mine = true
// Calculate cell values
for (const cell of cells) {
if (cell.mine) continue
cell.value = this.cellsAround(cell).filter(c => c.mine).length
}
// Reset DOM
/**@type {HTMLElement}*/
const gameDom = document.getElementById('mine-sweeper')
gameDom.classList.remove('win', 'lose')
gameDom.style.setProperty('--n', n)
gameDom.style.setProperty('--m', m)
/**@type {HTMLElement}*/
const boardDom = gameDom.querySelector('.board')
/**@type {NodeListOf<HTMLElement>}*/
const reusableDomCells = boardDom.querySelectorAll('.cell')
const domCells = cells.map((cell, i) => {
const div = (cell.div = reusableDomCells[i] ?? templateDomCell.cloneNode())
div.textContent = templateDomCell.textContent
div.className = templateDomCell.className
div.gameCell = cell
return div
})
boardDom.replaceChildren(...domCells)
this.displayInfo()
console.log('reset', { n, m, cells })
},
/**@param {cell} cell*/
dig(cell, testEnd = true) {
if (cell.flagged) return
const wasDug = cell.dug
cell.dug = true
const around = this.cellsAround(cell)
if (wasDug && cell.value != around.filter(c => c.flagged).length) return
if (wasDug || cell.value == 0) for (const cell of around) if (!cell.dug) this.dig(cell, false)
if (testEnd) {
this.displayInfo()
this.testEnd()
}
},
/**@param {cell} cell*/
flag(cell) {
if (cell.dug) return
cell.flagged = !cell.flagged
this.displayInfo()
this.testEnd()
},
testEnd() {
const { cells } = this
if (cells.some(c => c.mine && c.dug)) return this.end('lose', 'A mine is dug')
if (cells.every(c => c.mine !== c.dug)) return this.end('win', 'Every non-mine dug')
if (cells.every(c => c.mine === c.flagged)) return this.end('win', 'Every and only mines are flagged')
},
/**@param {string} state
* @param {string} message
*/
end(state, message) {
const { cells } = this
const gameDom = document.getElementById('mine-sweeper')
gameDom.classList.remove('win', 'lose')
gameDom.classList.toggle(state, true)
for (const cell of cells) {
if (cell.dug) continue
cell.dug = true
}
console.log('end', { state, message })
},
/**@type {HTMLElement}*/
templateDomCell: null,
displayInfo() {
const { cells } = this
const [total, flagged] = document.querySelectorAll('#mine-sweeper header .info p')
total.textContent = cells.filter(c => c.mine).length
flagged.textContent = cells.filter(c => c.flagged).length
},
cheatPeek() {
for (const cell of this.cells) cell.div.textContent = cell.value
},
}
</script>
<script>
document.addEventListener('DOMContentLoaded', onDOMContentLoaded, { once: true })
function onDOMContentLoaded() {
const templateDomCell = (mineSweeper.templateDomCell = document.querySelector('.cell'))
templateDomCell.parentElement.removeChild(templateDomCell)
mineSweeper.reset()
}
</script>
</head>
<body>
<div id="mine-sweeper">
<header>
<div class="info">
<b>Total Mines:</b>
<p>#</p>
<b>Flagged Mines:</b>
<p>#</p>
</div>
<button type="button" onclick="mineSweeper.reset()">Reset</button>
</header>
<div class="board">
<div
class="cell"
onclick=" mineSweeper.dig (event.target.gameCell) ;event.preventDefault()"
oncontextmenu="mineSweeper.flag(event.target.gameCell) ;event.preventDefault()"></div>
</div>
</div>
</body>
</html>