|
6 | 6 |
|
7 | 7 | tea "github.com/charmbracelet/bubbletea" |
8 | 8 | "github.com/charmbracelet/lipgloss" |
| 9 | + "github.com/f-gillmann/wordle-ssh/internal/wordle" |
9 | 10 | ) |
10 | 11 |
|
11 | 12 | const ( |
@@ -44,6 +45,7 @@ type GameModel struct { |
44 | 45 | state GameState |
45 | 46 | errorMessage string |
46 | 47 | letterMap map[rune]LetterState |
| 48 | + invalidWord bool |
47 | 49 | } |
48 | 50 |
|
49 | 51 | func NewGameModel(targetWord string) GameModel { |
@@ -81,12 +83,20 @@ func (m GameModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { |
81 | 83 | } |
82 | 84 |
|
83 | 85 | if len([]rune(m.currentGuess)) != WordLength { |
84 | | - m.errorMessage = fmt.Sprintf("Word must be %d letters", WordLength) |
| 86 | + m.errorMessage = fmt.Sprintf("Word must be %d letters\n", WordLength) |
| 87 | + return m, nil |
| 88 | + } |
| 89 | + |
| 90 | + // Validate the guess against the wordlist |
| 91 | + if !wordle.IsValidWord(strings.ToLower(m.currentGuess)) { |
| 92 | + m.invalidWord = true |
| 93 | + m.errorMessage = "Invalid word\n" |
85 | 94 | return m, nil |
86 | 95 | } |
87 | 96 |
|
88 | 97 | // Process the guess |
89 | 98 | m.errorMessage = "" |
| 99 | + m.invalidWord = false |
90 | 100 | m.guesses = append(m.guesses, m.currentGuess) |
91 | 101 | result := m.evaluateGuess(m.currentGuess) |
92 | 102 | m.guessResults = append(m.guessResults, result) |
@@ -119,6 +129,7 @@ func (m GameModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { |
119 | 129 | if len(m.currentGuess) > 0 { |
120 | 130 | m.currentGuess = m.currentGuess[:len(m.currentGuess)-1] |
121 | 131 | m.errorMessage = "" |
| 132 | + m.invalidWord = false |
122 | 133 | } |
123 | 134 |
|
124 | 135 | default: |
@@ -223,7 +234,12 @@ func (m GameModel) View() string { |
223 | 234 | // Render current guess being typed |
224 | 235 | for j := 0; j < WordLength; j++ { |
225 | 236 | if j < len([]rune(m.currentGuess)) { |
226 | | - tiles = append(tiles, TileStyleEmpty.Render(string([]rune(m.currentGuess)[j]))) |
| 237 | + // Use red style if word is invalid |
| 238 | + style := TileStyleEmpty |
| 239 | + if m.invalidWord { |
| 240 | + style = TileStyleInvalid |
| 241 | + } |
| 242 | + tiles = append(tiles, style.Render(string([]rune(m.currentGuess)[j]))) |
227 | 243 | } else { |
228 | 244 | tiles = append(tiles, TileStyleEmpty.Render(" ")) |
229 | 245 | } |
|
0 commit comments