-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
205 lines (185 loc) · 5.4 KB
/
index.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
<!doctype html>
<html>
<head>
<meta charset="utf-8"/>
<title>BQNsweeper</title>
<style>
@font-face{
font-family: BQN386;
src: url(BQN386.subset.woff2) format('woff2');
}
pre,code,textarea {
font-family: BQN386, monospace;
}
textarea, button {
font-size: 1.2rem;
line-height: 1.4;
}
button[data-value=" "] { background-color: #ccc; border: 0; }
button[data-value="1"] { color: olive; }
button[data-value="2"] { color: teal; }
button[data-value="3"] { color: blue; }
button[data-value="4"] { color: navy; }
button[data-value="5"] { color: purple; }
button[data-value="6"] { color: fuchsia; }
button[data-value="7"] { color: maroon; }
button[data-value="8"] { color: red; }
#grid > button {
width: 30px;
height: 30px;
padding: 0;
vertical-align: bottom;
font-weight: bold;
}
input {
width: 3em;
}
#timer {
height: 1em;
font-size: 1.2em;
margin-top: 8px;
}
</style>
</head>
<body>
<div id="try_again">
<h1 id="ending_text">BQNsweeper</h1>
<button onClick="restartGame()">Generate</button>
<input id="h" type="number" value="8">×<input id="v" type="number" value="8">,
<input id="m" type="number" value="10"> mines
<div id="timer"></div>
<hr/>
</div>
<div id="grid"></div>
<hr/>
<h2><a href="https://github.com/dancek/bqnsweeper">Source code</a></h2>
<pre id="src"></pre>
See page source for the javascript implementing this UI on top of the BQN logic.
<br>
The BQNsweeper code is CC0 licensed. It runs on bqn.js by Marshall Lochbaum, ISC license.
<script src="bqn.js"></script>
<script>
const src =
`Grid ← {𝕩⥊(↕×´𝕩)∊𝕨•rand.Deal ×´𝕩}
Pad ← {0∾˘⍉⌽𝕩}⍟4
Adjacent ← +´∘⥊⎉2 3‿3↕Pad
Spread ← {⍉⌽𝕩∨»𝕩}⍟4
Flood ← ∧⟜Spread⍟{+´≢𝕩}
Neighbors ← <⎉2 3‿3↕⊢
Game ← {count 𝕊 size:
repr ← ".x 12345678ff"
mines ← count Grid size
adj ← Adjacent mines
seen ← size⥊0
flags ← size⥊0
Clamp ← size⊸{h‿w 𝕊 y‿x: ⟨(h-1)(0⌈⌊)y, (w-1)(0⌈⌊)x⟩}
Guess ⇐ {
g ← 1⌾(𝕩⊸⊑)size⥊0
seen ∨↩(0=adj)Spread∘Flood⍟(0=+´⥊g×adj) g׬flags
flags ×↩¬seen
}
Render ⇐ {𝕊: ⊑⟜repr⎊'/'¨(flags×2-˜≠repr)+seen×seen+(¬mines)×1+adj}
Result ⇐ {𝕊: (seen≡¬mines) - ∨´⥊seen∧mines}
Flag ⇐ {flags ¬⌾(𝕩⊸⊑)⍟(0=⊑⟜seen 𝕩)↩}
Chord ⇐ {
p←𝕩⊑adj×seen
Valid←{(×p) ∧ p=+´+˝𝕩⊑Neighbors Pad flags}
ChordCoords←⥊·∾⌜´((↕3)+1-˜⊢)¨
(Guess∘Clamp¨ChordCoords)⍟Valid 𝕩
}
ShowAll ⇐ {𝕊: seen∨↩1}
}`
const bytecode = compile(src)
document.getElementById("src").innerText = src
let timer
let start
function updateTime() {
let t = "0:00.0"
if (start) {
const ms = Date.now() - start
const m = Math.floor(ms / 60000)
const s = Math.floor(ms / 1000 % 60)
const d = Math.floor(ms / 100 % 10)
t = `${m}:${s < 10 ? "0" : ""}${s}.${d}`
}
document.getElementById("timer").innerText = t
}
function restartGame() {
const h = parseInt(document.getElementById("v").value)
const v = parseInt(document.getElementById("h").value)
const m = parseInt(document.getElementById("m").value)
if(timer) {
start = undefined
clearInterval(timer)
}
timer = window.setInterval(updateTime, 100)
const Game = run.apply(null, bytecode)
const obj = Game(list([v,h]), m)
const ns = nsget(obj)
const render = ns("render")
const result = ns("result")
const guess = ns("guess")
const flag = ns("flag")
const showall = ns("showall")
const chord = ns("chord")
const grid = document.getElementById("grid")
while(grid.firstChild) grid.removeChild(grid.firstChild)
document.getElementById("ending_text").innerText = "BQNsweeper"
for (let i=0; i<v; i++) {
for (let j=0; j<h; j++) {
const btn = document.createElement("button")
const idx = i*h+j
btn.id = "btn-" + idx
btn.innerText = "."
btn.dataset.value = btn.innerText
btn.addEventListener("mouseup", e => {
const p = list([i,j])
if (!start) start = Date.now()
if (e.button == 1) {
chord(p)
} else if (e.button == 2) {
flag(p)
} else {
guess(p)
}
update()
})
btn.addEventListener("contextmenu", e => e.preventDefault())
grid.appendChild(btn)
}
grid.appendChild(document.createElement("br"))
}
function char(value, gameOver) {
const isMine = value === 'x'
const isFlag = value === 'f'
const isFalseFlag = value === '/'
if (" 12345678".indexOf(value)>=0)
return value
if (isFalseFlag) return "🏴"
if (isFlag) return "🚩"
if (isMine) return "💣"
return value
}
function update() {
const res = result()
const gameOver = res !== 0
if(gameOver)
showall()
const g = render()
for (let i=0; i<v*h; i++) {
const btn = document.getElementById("btn-" + i)
btn.innerText = char(g[i], gameOver)
btn.dataset.value = g[i]
btn.disabled = gameOver
}
const text = res === 1 ? "You win" : "You lose"
if (gameOver) {
document.getElementById("ending_text").innerText = text
clearInterval(timer)
}
}
}
window.onLoad = restartGame()
</script>
</body>
</html>