-
Notifications
You must be signed in to change notification settings - Fork 0
/
wordle.go
209 lines (160 loc) · 4.19 KB
/
wordle.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
package wordle
import (
"fmt"
"math/rand"
"strings"
"time"
)
type GuessResult int
const (
GuessResultNotGuessed GuessResult = iota
GuessResultWrong
GuessResultWrongPosition
GuessResultCorrect
)
type void struct{}
type Wordle struct {
word []rune // Correct word
dictionary map[string]void // All valid words
guessesAllowed int // Number of guesses allowed
wordLength int // Length of word
guesses [][]rune // Guesses
guessResults [][]GuessResult // Character guesses
letters [26]GuessResult // Letters guessed
cancelled bool
}
func New(wordLength, guessesAllowed int, dictionary, common []string) (*Wordle, error) {
r := rand.New(rand.NewSource(time.Now().Unix()))
// Initialize Wordle
w := &Wordle{
dictionary: make(map[string]void),
guessesAllowed: guessesAllowed,
wordLength: wordLength,
guesses: make([][]rune, 0),
guessResults: make([][]GuessResult, 0),
}
for _, word := range dictionary {
word = strings.ToLower(word)
newWord := make([]rune, 0)
for _, c := range word {
if isLowercaseLetter(c) {
newWord = append(newWord, c)
}
}
if len(newWord) == wordLength {
w.dictionary[string(newWord)] = void{}
}
}
cleanedCommon := make([]string, 0)
for _, word := range common {
word = strings.ToLower(word)
newWord := make([]rune, 0)
for _, c := range word {
if isLowercaseLetter(c) {
newWord = append(newWord, c)
}
}
if len(newWord) == wordLength {
cleanedCommon = append(cleanedCommon, string(newWord))
w.dictionary[string(newWord)] = void{}
}
}
if len(cleanedCommon) == 0 {
return nil, fmt.Errorf("invalid word length: no words with length %d in common words", wordLength)
}
randWord := cleanedCommon[r.Intn(len(cleanedCommon))]
w.word = []rune(randWord)
return w, nil
}
func (w *Wordle) Guess(guess string) ([]GuessResult, error) {
if w.Done() {
return nil, fmt.Errorf("game is done")
}
guess = strings.ToLower(guess)
guessRunes := []rune(guess)
if guessRunes == nil || len(guessRunes) != w.wordLength {
return nil, fmt.Errorf("invalid word length: wanted %d, got %d", w.wordLength, len(guessRunes))
}
if _, ok := w.dictionary[guess]; !ok {
return nil, fmt.Errorf("invalid word: %s was not found in the dictionary", guess)
}
guessResults := make([]GuessResult, w.wordLength)
charCounts := [26]int{}
for _, c := range w.word {
if contains(guessRunes, c) {
charCounts[c-'a']++
}
}
for i, g := range guessRunes {
if g < 'a' || g > 'z' {
return nil, fmt.Errorf("invalid word: %c is not a letter", g)
}
if g == w.word[i] {
guessResults[i] = GuessResultCorrect
charCounts[g-'a']--
if w.letters[g-'a'] < GuessResultCorrect {
w.letters[g-'a'] = GuessResultCorrect
}
} else {
guessResults[i] = GuessResultWrong
if w.letters[g-'a'] < GuessResultWrong {
w.letters[g-'a'] = GuessResultWrong
}
}
}
for i, g := range guessRunes {
if charCounts[g-'a'] > 0 && guessResults[i] == GuessResultWrong {
guessResults[i] = GuessResultWrongPosition
charCounts[g-'a']--
if w.letters[g-'a'] < GuessResultWrongPosition {
w.letters[g-'a'] = GuessResultWrongPosition
}
}
}
w.guesses = append(w.guesses, guessRunes)
w.guessResults = append(w.guessResults, guessResults)
return guessResults, nil
}
func (w *Wordle) Word() string {
if w.Done() {
return string(w.word)
}
return ""
}
func (w *Wordle) WordLength() int {
return w.wordLength
}
func (w *Wordle) Guesses() []string {
guesses := make([]string, len(w.guesses))
for i, g := range w.guesses {
guesses[i] = string(g)
}
return guesses
}
func (w *Wordle) GuessResults() [][]GuessResult {
return w.guessResults
}
func (w *Wordle) GuessesLeft() int {
return w.guessesAllowed - len(w.guesses)
}
func (w *Wordle) Letters() [26]GuessResult {
return w.letters
}
func (w *Wordle) Cancel() {
w.cancelled = true
}
func (w *Wordle) Cancelled() bool {
return w.cancelled
}
func (w *Wordle) Won() bool {
if len(w.guesses) == 0 {
return false
}
return equalsSlice(w.guesses[len(w.guesses)-1], w.word)
}
func (w *Wordle) Lost() bool {
return len(w.guesses) >= w.guessesAllowed
}
func (w *Wordle) Done() bool {
return w.Won() || w.Lost() || w.Cancelled()
}