-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.go
More file actions
414 lines (362 loc) · 8.88 KB
/
Copy pathmodel.go
File metadata and controls
414 lines (362 loc) · 8.88 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
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
package minesweeper
import (
"fmt"
"strings"
"time"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)
type phase int
const (
phaseDifficulty phase = iota
phasePlaying
phaseGameOver
)
type tickMsg struct{}
func tickCmd() tea.Cmd {
return tea.Tick(time.Second, func(time.Time) tea.Msg {
return tickMsg{}
})
}
// HighScoreFunc returns the best time for a given difficulty, or 0 if none.
type HighScoreFunc func(difficulty string) int
// Model is the Bubbletea model for the Minesweeper game.
type Model struct {
game *Game
cursorRow int
cursorCol int
width int
height int
done bool
phase phase
elapsed int
ticking bool
diff Difficulty
HighScore int
HighScoreFunc HighScoreFunc
}
// New creates a fresh Minesweeper model at the difficulty selection screen.
func New() Model {
return Model{
phase: phaseDifficulty,
}
}
// Init returns nil; no initial command needed.
func (m Model) Init() tea.Cmd {
return nil
}
// Done returns true when the player wants to exit to the menu.
func (m Model) Done() bool {
return m.done
}
// FinalScore returns the elapsed seconds, or -1 if not won.
func (m Model) FinalScore() int {
if m.game == nil || m.game.State != Won {
return -1
}
return m.elapsed
}
// DifficultyName returns the difficulty as a string for score tracking.
func (m Model) DifficultyName() string {
switch m.diff {
case Beginner:
return "beginner"
case Intermediate:
return "intermediate"
case Expert:
return "expert"
}
return "unknown"
}
// Update handles input and advances game state.
func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.WindowSizeMsg:
m.width = msg.Width
m.height = msg.Height
return m, nil
case tickMsg:
if m.phase == phasePlaying && m.ticking && m.game.State == Playing {
m.elapsed++
return m, tickCmd()
}
return m, nil
case tea.KeyMsg:
key := msg.String()
if key == "ctrl+c" {
return m, tea.Quit
}
switch m.phase {
case phaseDifficulty:
return m.updateDifficulty(key)
case phasePlaying:
return m.updatePlaying(key)
case phaseGameOver:
return m.updateGameOver(key)
}
}
return m, nil
}
func (m Model) updateDifficulty(key string) (tea.Model, tea.Cmd) {
switch key {
case "1":
return m.startGame(Beginner)
case "2":
return m.startGame(Intermediate)
case "3":
return m.startGame(Expert)
case "q", "esc":
m.done = true
}
return m, nil
}
func (m Model) startGame(diff Difficulty) (tea.Model, tea.Cmd) {
m.diff = diff
m.game = NewGame(diff)
m.phase = phasePlaying
m.cursorRow = 0
m.cursorCol = 0
m.elapsed = 0
m.ticking = false
m.HighScore = 0
if m.HighScoreFunc != nil {
m.HighScore = m.HighScoreFunc(m.DifficultyName())
}
return m, nil
}
func (m Model) updatePlaying(key string) (tea.Model, tea.Cmd) {
switch key {
case "up", "k":
if m.cursorRow > 0 {
m.cursorRow--
}
case "down", "j":
if m.cursorRow < m.game.Rows-1 {
m.cursorRow++
}
case "left", "h":
if m.cursorCol > 0 {
m.cursorCol--
}
case "right", "l":
if m.cursorCol < m.game.Cols-1 {
m.cursorCol++
}
case "enter", " ":
if m.game.State != Playing {
return m, nil
}
wasFirstClick := m.game.FirstClick
m.game.Reveal(m.cursorRow, m.cursorCol)
if wasFirstClick && !m.game.FirstClick {
m.ticking = true
if m.game.State == Playing {
return m, tickCmd()
}
}
if m.game.State != Playing {
m.ticking = false
m.phase = phaseGameOver
}
case "f":
if m.game.State == Playing {
m.game.ToggleFlag(m.cursorRow, m.cursorCol)
}
case "n":
return m.startGame(m.diff)
case "q", "esc":
m.done = true
}
return m, nil
}
func (m Model) updateGameOver(key string) (tea.Model, tea.Cmd) {
switch key {
case "n":
return m.startGame(m.diff)
case "d":
m.phase = phaseDifficulty
m.game = nil
case "q", "esc":
m.done = true
}
return m, nil
}
// View renders the complete game screen.
func (m Model) View() string {
switch m.phase {
case phaseDifficulty:
return m.viewDifficulty()
case phasePlaying, phaseGameOver:
return m.viewGame()
}
return ""
}
func (m Model) viewDifficulty() string {
sections := make([]string, 0, 9)
sections = append(sections,
titleStyle.Render("M I N E S W E E P E R"),
"",
headerStyle.Render("Select Difficulty"),
"",
optionStyle.Render(" [1] Beginner 9 x 9 10 mines"),
optionStyle.Render(" [2] Intermediate 16 x 16 40 mines"),
optionStyle.Render(" [3] Expert 16 x 30 99 mines"),
"",
footerStyle.Render("Q Quit"),
)
content := lipgloss.JoinVertical(lipgloss.Center, sections...)
return lipgloss.Place(m.width, m.height, lipgloss.Center, lipgloss.Center, content)
}
func (m Model) viewGame() string {
if m.game == nil {
return ""
}
var sections []string
// Title with difficulty
diffNames := map[Difficulty]string{
Beginner: "Beginner",
Intermediate: "Intermediate",
Expert: "Expert",
}
title := titleStyle.Render(fmt.Sprintf("Minesweeper - %s", diffNames[m.diff]))
sections = append(sections, title, "")
// Status bar
remaining := m.game.TotalMines - m.game.FlagsUsed
status := statusStyle.Render(fmt.Sprintf("Mines: %d Flags: %d Time: %d", remaining, m.game.FlagsUsed, m.elapsed))
sections = append(sections, status, "",
m.renderGrid(), "",
)
// Game over message
if m.phase == phaseGameOver {
switch m.game.State {
case Won:
mins := m.elapsed / 60
secs := m.elapsed % 60
switch {
case m.HighScore > 0 && m.elapsed < m.HighScore:
sections = append(sections, winStyle.Render(fmt.Sprintf("YOU WIN! Time: %d:%02d — NEW BEST!", mins, secs)))
case m.HighScore > 0:
bestM := m.HighScore / 60
bestS := m.HighScore % 60
sections = append(sections, winStyle.Render(fmt.Sprintf("YOU WIN! Time: %d:%02d (Best: %d:%02d)", mins, secs, bestM, bestS)))
default:
sections = append(sections, winStyle.Render(fmt.Sprintf("YOU WIN! Time: %d:%02d", mins, secs)))
}
case Lost:
sections = append(sections, loseStyle.Render("GAME OVER - Mine hit!"))
case Playing:
// Still playing; no game-over message.
}
sections = append(sections, "")
}
// Footer
var footer string
if m.phase == phaseGameOver {
footer = "N New Game | D Difficulty | Q Quit"
} else {
footer = "Arrows Move | Enter Reveal | F Flag | N New | Q Quit"
}
sections = append(sections, footerStyle.Render(footer))
content := lipgloss.JoinVertical(lipgloss.Center, sections...)
return lipgloss.Place(m.width, m.height, lipgloss.Center, lipgloss.Center, content)
}
func (m Model) renderGrid() string {
var rows []string
for r := 0; r < m.game.Rows; r++ {
var cells []string
for c := 0; c < m.game.Cols; c++ {
cell := m.game.Grid[r][c]
isCursor := r == m.cursorRow && c == m.cursorCol
text := m.renderCell(cell)
style := m.cellStyle(cell, isCursor)
cells = append(cells, style.Render(text))
}
rows = append(rows, strings.Join(cells, ""))
}
return strings.Join(rows, "\n")
}
func (m Model) renderCell(cell Cell) string {
switch cell.State {
case Hidden:
return "##"
case Flagged:
return "FF"
case Revealed:
if cell.Mine {
return "* "
}
if cell.Adjacent == 0 {
return " "
}
return fmt.Sprintf("%d ", cell.Adjacent)
}
return "##"
}
func (m Model) cellStyle(cell Cell, isCursor bool) lipgloss.Style {
base := lipgloss.NewStyle().Width(2)
if isCursor && m.phase == phasePlaying {
return base.
Background(lipgloss.Color("#444444")).
Bold(true).
Foreground(m.cellForeground(cell))
}
return base.Foreground(m.cellForeground(cell))
}
func (m Model) cellForeground(cell Cell) lipgloss.Color {
switch cell.State {
case Hidden:
return lipgloss.Color("#808080")
case Flagged:
return lipgloss.Color("#FF0000")
case Revealed:
if cell.Mine {
return lipgloss.Color("#FF0000")
}
return numberColor(cell.Adjacent)
}
return lipgloss.Color("#808080")
}
func numberColor(n int) lipgloss.Color {
switch n {
case 1:
return lipgloss.Color("#0000FF")
case 2:
return lipgloss.Color("#008200")
case 3:
return lipgloss.Color("#FF0000")
case 4:
return lipgloss.Color("#000084")
case 5:
return lipgloss.Color("#840000")
case 6:
return lipgloss.Color("#008284")
case 7:
return lipgloss.Color("#840084")
case 8:
return lipgloss.Color("#808080")
default:
return lipgloss.Color("#FFFFFF")
}
}
// --- Styles ---
var (
titleStyle = lipgloss.NewStyle().
Bold(true).
Foreground(lipgloss.Color("15"))
headerStyle = lipgloss.NewStyle().
Bold(true).
Foreground(lipgloss.Color("15")).
Underline(true)
statusStyle = lipgloss.NewStyle().
Foreground(lipgloss.Color("242"))
optionStyle = lipgloss.NewStyle().
Foreground(lipgloss.Color("#00E632"))
footerStyle = lipgloss.NewStyle().
Foreground(lipgloss.Color("240"))
winStyle = lipgloss.NewStyle().
Bold(true).
Foreground(lipgloss.Color("#00E632"))
loseStyle = lipgloss.NewStyle().
Bold(true).
Foreground(lipgloss.Color("#FF0000"))
)