-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
427 lines (387 loc) · 9.21 KB
/
main.go
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
package main
import (
"flag"
"fmt"
"math/rand"
"os"
"strings"
"time"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/lipgloss/table"
)
const (
DEFAULT_WIDTH = 30
DEFAULT_HEIGHT = 30
DEFAULT_MINES = 99
)
var wFlag, hFlag, numMinesFlag int
var shouldUseEmoji bool
func main() {
flag.IntVar(&wFlag, "w", DEFAULT_WIDTH, "minefield width")
flag.IntVar(&hFlag, "h", DEFAULT_HEIGHT, "minefield height")
flag.IntVar(&numMinesFlag, "n", DEFAULT_MINES, "number of mines")
flag.BoolVar(&shouldUseEmoji, "e", false, "use emoji characters")
flag.Parse()
m := initialModel(wFlag, hFlag, numMinesFlag, shouldUseEmoji)
p := tea.NewProgram(m)
if _, err := p.Run(); err != nil {
fmt.Printf("There's been an error: %v", err)
os.Exit(1)
}
}
func initialModel(width int, height int, numMines int, shouldUseEmoji bool) model {
positions := make(stack[point], 0, width*height)
minefield := make([][]cell, height)
for y := range minefield {
minefield[y] = make([]cell, width)
for x := range minefield[y] {
minefield[y][x] = cell{}
positions.push(point{x: x, y: y})
}
}
// TODO instantiate the mines after the first sweep to make sure first
// click never hits a mine
r := rand.New(rand.NewSource(time.Now().UnixNano()))
r.Shuffle(len(positions), func(i, j int) {
positions[i], positions[j] = positions[j], positions[i]
})
numMines = min(numMines, width*height)
for i := 0; i < numMines && i < width*height; i++ {
p := positions.pop()
minefield[p.y][p.x].isMine = true
}
return model{
prefs: preferences{
width: width,
height: height,
numberOfMines: numMines,
showHelp: true,
shouldUseEmoji: shouldUseEmoji,
},
minefield: minefield,
cursorX: width/2 - 1,
cursorY: height/2 - 1,
}
}
type tickMsg struct{}
func (m model) Init() tea.Cmd {
return tick()
}
func tick() tea.Cmd {
return func() tea.Msg {
time.Sleep(1 * time.Second)
return tickMsg{}
}
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
cursorMine := &m.minefield[m.cursorY][m.cursorX]
switch msg := msg.(type) {
case tickMsg:
if m.isGameOver {
break
}
m.secondsElapsed++
return m, tick()
case tea.KeyMsg:
switch msg.String() {
case "ctrl+c", "q":
return m, tea.Quit
case "up", "k":
m.cursorY--
if m.cursorY < 0 {
m.cursorY = m.prefs.height - 1
}
case "down", "j":
m.cursorY++
if m.cursorY > m.prefs.height-1 {
m.cursorY = 0
}
case "left", "h":
m.cursorX--
if m.cursorX < 0 {
m.cursorX = m.prefs.width - 1
}
case "right", "l":
m.cursorX++
if m.cursorX > m.prefs.width-1 {
m.cursorX = 0
}
case "r":
isDebug := m.prefs.isDebug
y, x := m.cursorY, m.cursorX
showHelp := m.prefs.showHelp
isEmoji := m.prefs.shouldUseEmoji
m = initialModel(wFlag, hFlag, numMinesFlag, isEmoji)
m.prefs.isDebug = isDebug
m.cursorY, m.cursorX = y, x
m.prefs.showHelp = showHelp
break
case "enter", " ":
if m.isGameOver {
break
}
sweep(m.cursorX, m.cursorY, &m, true, make(set[point]))
if checkDidWin(m) {
m.isGameOver = true
}
case "f":
if m.isGameOver {
break
}
if cursorMine.isRevealed {
sweep(m.cursorX, m.cursorY, &m, true, make(set[point]))
} else {
cursorMine.isFlagged = !cursorMine.isFlagged
}
case "d":
m.prefs.isDebug = !m.prefs.isDebug
case "?":
m.prefs.showHelp = !m.prefs.showHelp
case "e":
m.prefs.shouldUseEmoji = !m.prefs.shouldUseEmoji
}
}
return m, nil
}
func (m model) View() string {
var sb strings.Builder
writeHeader(&sb, m)
sb.WriteString("\n\n")
if m.prefs.shouldUseEmoji {
writeMinefield(&sb, m)
} else {
writeAsciiMinefield(&sb, m)
}
sb.WriteString("\n\n")
writeHelp(&sb, m)
return sb.String()
}
func writeHeader(sb *strings.Builder, m model) {
if m.isGameOver {
sb.WriteString("Game Over! ")
if checkDidWin(m) {
sb.WriteString("You WON!!")
} else {
sb.WriteString("You lost...")
}
} else {
sb.WriteString("...go sweep...")
sb.WriteString(fmt.Sprintf("\n%v mines left", minesLeft(m)))
}
sb.WriteString(fmt.Sprintf("\n%v seconds elapsed", m.secondsElapsed))
}
func writeMinefield(sb *strings.Builder, m model) {
for y, row := range m.minefield {
for x, mine := range row {
switch {
case x == m.cursorX && y == m.cursorY:
sb.WriteString("🔳")
case (m.isGameOver || m.prefs.isDebug) && mine.isMine:
sb.WriteString("💣")
case mine.isRevealed:
sb.WriteString(viewForMineAtPosition(x, y, m))
case mine.isFlagged:
sb.WriteString("🟨")
default:
sb.WriteString("⬜️")
}
}
sb.WriteString("\n")
}
}
func writeAsciiMinefield(sb *strings.Builder, m model) {
strs := make([][]string, m.prefs.height)
for y, row := range m.minefield {
strs[y] = make([]string, m.prefs.width)
for x, mine := range row {
switch {
case x == m.cursorX && y == m.cursorY:
strs[y][x] = "*"
case (m.isGameOver || m.prefs.isDebug) && mine.isMine:
strs[y][x] = "B"
case mine.isRevealed:
strs[y][x] = asciiViewForMineAtPosition(x, y, m)
case mine.isFlagged:
strs[y][x] = "F"
default:
strs[y][x] = " "
}
}
}
t := table.New().
Border(lipgloss.NormalBorder()).
BorderRow(true).
BorderColumn(true).
Rows(strs...).
StyleFunc(func(row, col int) lipgloss.Style {
var fg lipgloss.TerminalColor = lipgloss.NoColor{}
var bg lipgloss.TerminalColor = lipgloss.NoColor{}
switch strs[row-1][col] {
case "0":
fg = lipgloss.Color("#292929")
case "1":
fg = lipgloss.Color("#74adf2")
case "2":
fg = lipgloss.Color("#00FF00")
case "3":
fg = lipgloss.Color("#FF0000")
case "4":
fg = lipgloss.Color("#28706d")
case "5":
fg = lipgloss.Color("#b06446")
case "6":
fg = lipgloss.Color("#FF0000")
case "7":
fg = lipgloss.Color("#8a7101")
case "8":
fg = lipgloss.Color("#111")
bg = lipgloss.Color("#bfbfbf")
case "*":
fg = lipgloss.Color("#FF33FF")
case "F":
bg = lipgloss.Color("#ffee00")
fg = lipgloss.Color("#111")
case "B":
bg = lipgloss.Color("#FF0000")
fg = lipgloss.Color("#111")
case " ":
bg = lipgloss.Color("#bfbfbf")
}
return lipgloss.NewStyle().
Foreground(fg).
Background(bg).
Padding(0, 1)
})
sb.WriteString(t.Render())
}
func writeHelp(sb *strings.Builder, m model) {
if m.prefs.showHelp {
if !m.isGameOver {
sb.WriteString("Press h/j/k/l or ←↓↑→ to move\n")
sb.WriteString("Press enter or space to sweep\n")
sb.WriteString("Press f to toggle flag.\n")
sb.WriteString("Press d to toggle debug.\n")
}
sb.WriteString("Press q to quit.\n")
sb.WriteString("Press r to start a new game.\n")
sb.WriteString("Press ? to toggle help text\n")
sb.WriteString("Press a to toggle ascii view\n")
}
}
func sweep(x, y int, m *model, userInitiatedSweep bool, swept set[point]) {
cell := &m.minefield[y][x]
if cell.isRevealed && userInitiatedSweep {
adjMines := countAdjacentMines(x, y, *m)
adjFlags := countAdjacentFlags(x, y, *m)
if adjFlags >= adjMines {
autoSweep(x, y, m)
}
return
}
if cell.isMine {
if userInitiatedSweep {
m.isGameOver = true
}
return
}
touching := countAdjacentMines(x, y, *m)
p := point{x: x, y: y}
if touching == 0 && !swept.has(p) {
swept.add(p)
forEachSurroundingCellDo(x, y, m, func(x, y int, m *model) {
sweep(x, y, m, false, swept)
})
}
cell.isRevealed = true
}
func minesLeft(m model) int {
flags := 0
for y := range m.minefield {
for _, mine := range m.minefield[y] {
if mine.isFlagged && !mine.isRevealed {
flags++
}
}
}
return m.prefs.numberOfMines - flags
}
func checkDidWin(m model) bool {
for y := range m.minefield {
for _, mine := range m.minefield[y] {
if !mine.isMine && !mine.isRevealed {
return false
}
}
}
return true
}
func asciiViewForMineAtPosition(x, y int, m model) string {
if m.minefield[y][x].isMine {
return "B"
}
return fmt.Sprint(countAdjacentMines(x, y, m))
}
func viewForMineAtPosition(x, y int, m model) string {
if m.minefield[y][x].isMine {
return "💣"
}
touching := countAdjacentMines(x, y, m)
numViewMap := map[int]string{
0: "⬛️",
1: "1️⃣",
2: "2️⃣",
3: "3️⃣",
4: "4️⃣",
5: "5️⃣",
6: "6️⃣",
7: "7️⃣",
8: "8️⃣",
}
return numViewMap[touching]
}
func forEachSurroundingCellDo(x, y int, m *model, do func(x, y int, m *model)) {
w := m.prefs.width
h := m.prefs.height
for dx := -1; dx <= 1; dx++ {
for dy := -1; dy <= 1; dy++ {
if (dx == 0 && dy == 0) || x+dx < 0 || x+dx > w-1 || y+dy < 0 || y+dy > h-1 {
continue
}
do(x+dx, y+dy, m)
}
}
}
func autoSweep(x, y int, m *model) {
forEachSurroundingCellDo(x, y, m, func(x, y int, m *model) {
cell := m.minefield[y][x]
if !cell.isRevealed && !cell.isFlagged {
sweep(x, y, m, true, make(set[point]))
}
})
}
func countAdjacentFlags(x, y int, m model) int {
adj := 0
forEachSurroundingCellDo(x, y, &m, func(x, y int, m *model) {
if m.minefield[y][x].isFlagged {
adj++
}
})
return adj
}
func countAdjacentMines(x, y int, m model) int {
adj := 0
forEachSurroundingCellDo(x, y, &m, func(x, y int, m *model) {
if m.minefield[y][x].isMine {
adj++
}
})
return adj
}
func min(x, y int) int {
if x < y {
return x
} else {
return y
}
}