-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
289 lines (233 loc) · 6.87 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
package main
import (
"bufio"
"embed"
"fmt"
"io"
"io/ioutil"
"log"
"math/rand"
"net/http"
"os"
"strings"
"time"
)
const (
maxGuesses int = 6
wordLength int = 5
emptyGuessChar string = "*"
)
type evaluation int
const (
absent evaluation = iota
present
correct
)
var (
// passed in at build time
version string
// Embed the words directory in the compiled binary.
//go:embed words
words embed.FS
)
// history homes historical data.
// TODO: While not currently used, in the future it could be saved as a
// JSON file (or perhaps pluggible backend?) to support a `wordle -statistics`
// feature that displays a visualization of historical data, or something similar-ish.
// For comparison, an example of the original statics JSON:
// {"currentStreak":1,"maxStreak":1,"guesses":{"1":1,"2":0,"3":0,"4":0,"5":0,"6":0,"fail":0},"winPercentage":100,"gamesPlayed":1,"gamesWon":1,"averageGuesses":1}
// However, history diverges from the original wordle and represents all this a
// bit differently...
type history struct {
// currentStreak is the current streak.
currentStreak int
// maxStreak is the maximum streak, historically.
maxStreak int
// games is a slice of past games played.
games []game
}
// game is the historical representation of a particular game played.
type game struct {
// id is the index identifying of the game (i.e. the first game played, the second game played, etc.).
id int
// guessCount is the total number of guesses used before game completion.
guessCount int
// success represents whether the player successfully guessed the word.
success bool
// complete represents whether a game was played until completion.
complete bool
// time represents when the game was last played.
time time.Time
}
// wordle is a word guessing game based on Josh Wardle's Wordle (https://powerlanguage.co.uk/wordle/).
// It represents an instance of the wordle game.
type wordle struct {
in io.Reader
out io.Writer
// guesses is a slice of word guesses.
// example: []string{"BEACH", "", "", "", "", ""}
guesses [maxGuesses]string
// evaluations is a slice of slices, representing an evaluation of each character of each guess.
evaluations [maxGuesses][wordLength]evaluation
// guessIndex is the current guess index.
guessIndex int
// solution is the solution word.
solution string
}
func (w *wordle) displaySolution() {
for _, char := range w.solution {
w.displayGreenTile(char)
}
w.write("\n")
}
func (w *wordle) displayGrid() {
for i, guess := range w.guesses {
for j, guessLetter := range guess {
switch w.evaluations[i][j] {
case correct:
w.displayGreenTile(guessLetter)
case present:
w.displayYellowTile(guessLetter)
case absent:
w.displayGrayTile(guessLetter)
}
}
w.write("\n")
}
}
func (w *wordle) displayGreenTile(char rune) {
w.write("\033[42m\033[1;30m")
w.displayOnTile(char)
}
func (w *wordle) displayYellowTile(char rune) {
w.write("\033[43m\033[1;30m")
w.displayOnTile(char)
}
func (w *wordle) displayGrayTile(char rune) {
w.write("\033[40m\033[1;37m")
w.displayOnTile(char)
}
func (w *wordle) displayOnTile(char rune) {
w.write(fmt.Sprintf(" %c ", char))
w.write("\033[m\033[m")
}
func (w *wordle) evaluateGuess(guess string) [wordLength]evaluation {
evaluation := [wordLength]evaluation{}
for i := 0; i < wordLength; i++ {
evaluation[i] = absent
}
for j, guessLetter := range guess {
for k, letter := range w.solution {
if guessLetter == letter {
if j == k {
evaluation[j] = correct
break
}
evaluation[j] = present
}
}
}
return evaluation
}
func (w *wordle) write(str string) {
w.out.Write([]byte(str))
}
func (w *wordle) run() {
reader := bufio.NewScanner(w.in)
solution := w.solution
w.write(fmt.Sprintf("Version: \t%s\n", version))
w.write("Info: \t\thttps://github.com/mdb/wordle\n")
w.write("About: \t\tA CLI adaptation of Josh Wardle's Wordle (https://powerlanguage.co.uk/wordle/)\n\n")
w.write(fmt.Sprintf("Guess a %v-letter word within %v guesses...\n", wordLength, maxGuesses))
for w.guessIndex = 0; w.guessIndex < maxGuesses; w.guessIndex++ {
w.write(fmt.Sprintf("\nGuess (%v/%v): ", w.guessIndex+1, maxGuesses))
reader.Scan()
guess := strings.ToUpper(reader.Text())
if guess == "STOP" {
break
}
if len(guess) != len(solution) {
w.write(fmt.Sprintf("%s is not a %v-letter word. Try again...\n", guess, wordLength))
w.guessIndex--
}
if len(guess) == len(solution) {
w.guesses[w.guessIndex] = guess
w.evaluations[w.guessIndex] = w.evaluateGuess(guess)
w.displayGrid()
}
if guess == solution {
break
}
if w.guessIndex == maxGuesses-1 {
fmt.Println()
w.displaySolution()
os.Exit(1)
}
}
}
func newWordle(word string, in io.Reader, out io.Writer) *wordle {
// TODO: Consider configuring wordle with a 'history' that includes 'games'.
// This could allow the wordle to render with a pre-populated grid showing
// the current day's game state if the game is still in-progress and incomplete.
w := &wordle{
in: in,
out: out,
solution: word,
}
emptyGuess := ""
emptyGuessEvaluation := [wordLength]evaluation{}
for i := 0; i < wordLength; i++ {
emptyGuess = emptyGuess + emptyGuessChar
emptyGuessEvaluation[i] = absent
}
// By seeding with dummy guesses and dummy evaluations,
// displayGrid displays remaining rows with each grid rendering.
for i := 0; i < maxGuesses; i++ {
w.evaluations[i] = emptyGuessEvaluation
w.guesses[i] = emptyGuess
}
return w
}
func getWordFromFile() string {
data, err := words.ReadFile("words/words.txt")
if err != nil {
log.Fatalln(err)
}
today := time.Now().UTC()
startDay := time.Date(2021, time.Month(6), 19, 0, 0, 0, 0, time.UTC)
daysSinceStart := int(today.Sub(startDay).Hours() / 24)
return strings.ToUpper(strings.Split(string(data), ",")[daysSinceStart])
}
// getWordFromURL populates the wordle word via a random word chosen
// from those listed at a remote URL, rather than via the in-baked
// per-day list of wordle words.
// While it's not currently used, it could be used in the future to
// enable something like a `wordle -for-sport` feature that allows
// users to play multiple games/day "for sport."
func getWordFromURL() string {
// NOTE: this list inludes many uncommon and seemingly not-English words. Is there a better data source?
res, err := http.Get("https://raw.githubusercontent.com/dwyl/english-words/master/words_alpha.txt")
if err != nil {
log.Fatalln(err)
}
body, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Fatalln(err)
}
words := strings.Split(string(body), "\r\n")
candidates := []string{}
for _, word := range words {
if len(word) == wordLength {
candidates = append(candidates, strings.ToUpper(word))
}
}
rand.Seed(time.Now().Unix())
return candidates[rand.Intn(len(candidates))]
}
func main() {
word := getWordFromFile()
f := os.Stdin
defer f.Close()
w := newWordle(word, os.Stdin, f)
w.run()
}