-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.go
More file actions
347 lines (301 loc) · 7.94 KB
/
Copy pathmodel.go
File metadata and controls
347 lines (301 loc) · 7.94 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
package wordle
import (
"fmt"
"strings"
"unicode"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)
type phase int
const (
phaseTyping phase = iota
phaseGameOver
)
// Model is the Bubbletea model for the Wordle game.
type Model struct {
game *Game
currentGuess string
phase phase
width int
height int
done bool
message string
HighScore int
}
// New creates a fresh Wordle model.
func New() Model {
return Model{
game: NewGame(),
phase: phaseTyping,
}
}
// Init returns nil; no initial command needed.
func (m Model) Init() tea.Cmd {
return nil
}
// 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 tea.KeyMsg:
key := msg.String()
if key == "ctrl+c" {
return m, tea.Quit
}
switch m.phase {
case phaseTyping:
return m.updateTyping(key)
case phaseGameOver:
return m.updateGameOver(key)
}
}
return m, nil
}
func (m Model) updateTyping(key string) (tea.Model, tea.Cmd) {
switch key {
case "backspace":
if m.currentGuess != "" {
m.currentGuess = m.currentGuess[:len(m.currentGuess)-1]
m.message = ""
}
case "enter":
if len(m.currentGuess) < 5 {
m.message = "Not enough letters"
return m, nil
}
_, err := m.game.Guess(m.currentGuess)
if err != nil {
m.message = err.Error()
return m, nil
}
m.currentGuess = ""
m.message = ""
if m.game.IsOver() {
m.phase = phaseGameOver
if m.game.Won {
guesses := len(m.game.Guesses)
switch {
case m.HighScore > 0 && guesses < m.HighScore:
m.message = fmt.Sprintf("Correct in %d/6! %s — NEW HIGH SCORE!", guesses, m.game.Target)
case m.HighScore > 0:
m.message = fmt.Sprintf("Correct in %d/6! %s (Best: %d/6)", guesses, m.game.Target, m.HighScore)
default:
m.message = fmt.Sprintf("Correct! The word was %s", m.game.Target)
}
} else {
m.message = fmt.Sprintf("The word was %s", m.game.Target)
}
}
case "q", "esc":
m.done = true
default:
if len(key) == 1 && unicode.IsLetter(rune(key[0])) && len(m.currentGuess) < 5 {
m.currentGuess += strings.ToUpper(key)
m.message = ""
}
}
return m, nil
}
func (m Model) updateGameOver(key string) (tea.Model, tea.Cmd) {
switch key {
case "n":
m.game = NewGame()
m.phase = phaseTyping
m.currentGuess = ""
m.message = ""
case "q", "esc":
m.done = true
}
return m, nil
}
// Done returns true when the player wants to exit.
func (m Model) Done() bool {
return m.done
}
// FinalScore returns the number of guesses used, or -1 if the game wasn't won.
func (m Model) FinalScore() int {
if m.game == nil || !m.game.Won {
return -1
}
return len(m.game.Guesses)
}
// View renders the complete game screen.
func (m Model) View() string {
var sections []string
sections = append(sections,
titleStyle.Render("W O R D L E"),
"",
m.renderGrid(),
"",
m.renderKeyboard(),
"",
)
if m.message != "" {
var msgStyled string
switch {
case m.game.Won:
msgStyled = winStyle.Render(m.message)
case m.game.Over:
msgStyled = lossStyle.Render(m.message)
default:
msgStyled = messageStyle.Render(m.message)
}
sections = append(sections, msgStyled)
} else {
sections = append(sections, "")
}
var footer string
switch m.phase {
case phaseTyping:
footer = "Type a word | Enter Submit | Q Quit"
case phaseGameOver:
footer = "N New Game | 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)
}
// renderGrid draws the 6-row guess grid with colored cells.
func (m Model) renderGrid() string {
var rows []string
for i := 0; i < m.game.MaxGuesses; i++ {
switch {
case i < len(m.game.Guesses):
rows = append(rows, m.renderGuessRow(m.game.Guesses[i], m.game.Results[i]))
case i == len(m.game.Guesses) && m.phase == phaseTyping:
rows = append(rows, m.renderCurrentRow())
default:
rows = append(rows, m.renderEmptyRow())
}
}
return strings.Join(rows, "\n")
}
// renderGuessRow renders a completed guess with colored backgrounds.
func (m Model) renderGuessRow(guess string, result GuessResult) string {
cells := make([]string, 0, len(guess))
for i, ch := range guess {
letter := string(ch)
var cell string
switch result[i] {
case Correct:
cell = correctCellStyle.Render(" " + letter + " ")
case Present:
cell = presentCellStyle.Render(" " + letter + " ")
default:
cell = absentCellStyle.Render(" " + letter + " ")
}
cells = append(cells, cell)
}
return strings.Join(cells, " ")
}
// renderCurrentRow renders the row being typed with cursor indicators.
func (m Model) renderCurrentRow() string {
var cells []string
for i := 0; i < 5; i++ {
if i < len(m.currentGuess) {
letter := string(m.currentGuess[i])
cells = append(cells, inputCellStyle.Render(" "+letter+" "))
} else {
cells = append(cells, emptyCellStyle.Render(" "))
}
}
return strings.Join(cells, " ")
}
// renderEmptyRow renders a blank row placeholder.
func (m Model) renderEmptyRow() string {
cells := make([]string, 0, 5)
for range 5 {
cells = append(cells, emptyCellStyle.Render(" "))
}
return strings.Join(cells, " ")
}
// renderKeyboard draws the on-screen keyboard with per-key coloring.
func (m Model) renderKeyboard() string {
rows := []string{"QWERTYUIOP", "ASDFGHJKL", "ZXCVBNM"}
lines := make([]string, 0, len(rows))
for i, row := range rows {
var keys []string
for _, ch := range row {
letter := string(ch)
state, exists := m.game.KeyboardState[ch]
var key string
if !exists {
key = untestedKeyStyle.Render(" " + letter + " ")
} else {
switch state {
case Correct:
key = correctKeyStyle.Render(" " + letter + " ")
case Present:
key = presentKeyStyle.Render(" " + letter + " ")
default:
key = absentKeyStyle.Render(" " + letter + " ")
}
}
keys = append(keys, key)
}
line := strings.Join(keys, "")
// Indent the second and third rows for staggered keyboard look.
switch i {
case 1:
line = " " + line
case 2:
line = " " + line
}
lines = append(lines, line)
}
return strings.Join(lines, "\n")
}
// --- Styles ---
var (
titleStyle = lipgloss.NewStyle().
Bold(true).
Foreground(lipgloss.Color("15"))
correctCellStyle = lipgloss.NewStyle().
Bold(true).
Foreground(lipgloss.Color("15")).
Background(lipgloss.Color("#538D4E"))
presentCellStyle = lipgloss.NewStyle().
Bold(true).
Foreground(lipgloss.Color("15")).
Background(lipgloss.Color("#B59F3B"))
absentCellStyle = lipgloss.NewStyle().
Bold(true).
Foreground(lipgloss.Color("15")).
Background(lipgloss.Color("#3A3A3C"))
inputCellStyle = lipgloss.NewStyle().
Bold(true).
Foreground(lipgloss.Color("15")).
Border(lipgloss.NormalBorder()).
BorderForeground(lipgloss.Color("242"))
emptyCellStyle = lipgloss.NewStyle().
Foreground(lipgloss.Color("240")).
Border(lipgloss.NormalBorder()).
BorderForeground(lipgloss.Color("238"))
correctKeyStyle = lipgloss.NewStyle().
Bold(true).
Foreground(lipgloss.Color("15")).
Background(lipgloss.Color("#538D4E"))
presentKeyStyle = lipgloss.NewStyle().
Bold(true).
Foreground(lipgloss.Color("15")).
Background(lipgloss.Color("#B59F3B"))
absentKeyStyle = lipgloss.NewStyle().
Foreground(lipgloss.Color("240")).
Background(lipgloss.Color("#3A3A3C"))
untestedKeyStyle = lipgloss.NewStyle().
Foreground(lipgloss.Color("15")).
Background(lipgloss.Color("#818384"))
winStyle = lipgloss.NewStyle().
Bold(true).
Foreground(lipgloss.Color("#538D4E"))
lossStyle = lipgloss.NewStyle().
Bold(true).
Foreground(lipgloss.Color("#FF4444"))
messageStyle = lipgloss.NewStyle().
Foreground(lipgloss.Color("#DCFFDC"))
footerStyle = lipgloss.NewStyle().
Foreground(lipgloss.Color("240"))
)