-
Notifications
You must be signed in to change notification settings - Fork 3
/
termle.go
258 lines (235 loc) · 5.06 KB
/
termle.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
package main
import (
"bufio"
"embed"
"flag"
"fmt"
"math/rand"
"os"
"regexp"
"strconv"
"strings"
"time"
)
const (
totalWordles = 2315
blackSquare = `⬛`
yellowSquare = `🟨`
greenSquare = `🟩`
)
var (
//go:embed guesses.txt
//go:embed answers.txt
files embed.FS
dayFlag = flag.Int("day", daysSinceFirstWordle(), "select a specific wordle by day")
randomFlag = flag.Bool("random", false, "pick a random wordle")
// UTC to avoid DST
firstDay = time.Date(2021, time.June, 19, 0, 0, 0, 0, time.UTC)
valid = regexp.MustCompile(`^[A-Za-z]{5}$`)
)
type game struct {
day int
currentTurn int
turnsRemaining int
complete bool
won bool
answer string
validGuesses map[string]struct{}
board [][]cell
}
type cell struct {
color string
letter string
}
func main() {
flag.Parse()
rand.Seed(time.Now().UnixNano())
var day int
if *randomFlag {
day = randomDay()
} else {
day = *dayFlag
}
g := newGame(day)
s := bufio.NewScanner(os.Stdin)
g.printTurn()
for !g.complete && s.Scan() {
guess := strings.ToUpper(strings.TrimSpace(s.Text()))
if !valid.MatchString(guess) {
g.printTurnWithError("Please enter a 5 letter word")
continue
}
if _, ok := g.validGuesses[guess]; !ok {
g.printTurnWithError("Not in word list")
continue
}
g.addGuess(guess)
g.printTurn()
}
if s.Err() != nil {
panic(s.Err())
}
// prevents answer from printing if user used a signal to end the program
// scanner.Err() returns nil if io.EOF
if g.complete {
g.printShareableScore()
}
}
func (g *game) printShareableScore() {
var turnS string
if g.won {
turnS = strconv.Itoa(g.currentTurn)
fmt.Println("you won!")
} else {
turnS = "X"
fmt.Println("you lose!")
fmt.Println("Answer was", g.answer)
}
fmt.Printf("Wordle %v %v/6\n\n", g.day, turnS)
for i := 0; i < g.currentTurn; i++ {
for _, x := range g.board[i] {
fmt.Print(x.color)
}
fmt.Println()
}
}
func green(l string) string {
return "\033[37;102m" + l + "\033[0m"
}
func yellow(l string) string {
return "\033[37;103m" + l + "\033[0m"
}
func white(l string) string {
return "\033[0;107m" + l + "\033[0m"
}
func black(l string) string {
return "\033[37;100m" + l + "\033[0m"
}
func clearBoard() {
fmt.Print("\033c")
}
func prompt() {
fmt.Print(">")
}
func newGame(day int) *game {
b := make([][]cell, 6)
for i := range b {
b[i] = make([]cell, 5)
for j := range b[i] {
b[i][j] = cell{
color: blackSquare,
letter: "_",
}
}
}
return &game{
day: day,
currentTurn: 0,
turnsRemaining: 6,
complete: false,
won: false,
answer: answerForDay(day),
validGuesses: guessesSet(),
board: b,
}
}
func (g *game) addGuess(guess string) {
// A cell is green if the letters by index match.
// A cell is yellow if
// the cell is not green
// the letter exists somewhere in the word
// the sum of green cells and yellow cells for the letter is less than the frequency of the letter
// Otherwise, the cell is black.
freq := make(map[rune]int)
for _, r := range g.answer {
freq[r]++
}
for i, r := range guess {
if rune(g.answer[i]) == r {
freq[r]--
g.board[g.currentTurn][i].color = greenSquare
}
g.board[g.currentTurn][i].letter = string(r)
}
for i, r := range guess {
// not green, it exists somewhere, and there is room left to color
if rune(g.answer[i]) != r && strings.ContainsRune(g.answer, r) && freq[r] > 0 {
freq[r]--
g.board[g.currentTurn][i].color = yellowSquare
g.board[g.currentTurn][i].letter = string(r)
}
}
g.turnsRemaining--
g.currentTurn++
if guess == g.answer || g.turnsRemaining == 0 {
g.complete = true
}
if guess == g.answer {
g.won = true
}
}
func (g *game) print() {
fmt.Printf("Wordle %v\n", g.day)
for _, y := range g.board {
for _, x := range y {
l := x.letter
switch x.color {
case greenSquare:
l = green(l)
case yellowSquare:
l = yellow(l)
default:
l = black(l)
}
fmt.Print(" " + l)
}
fmt.Println()
}
}
func (g *game) printTurn() {
clearBoard()
g.print()
prompt()
}
func (g *game) printTurnWithError(err string) {
clearBoard()
g.print()
fmt.Println(err)
prompt()
}
func guessesSet() map[string]struct{} {
guessesFile, err := files.Open("guesses.txt")
if err != nil {
panic(err)
}
defer guessesFile.Close()
validGuesses := make(map[string]struct{})
guessesReader := bufio.NewScanner(guessesFile)
for guessesReader.Scan() {
validGuesses[strings.ToUpper(guessesReader.Text())] = struct{}{}
}
if guessesReader.Err() != nil {
panic(err)
}
return validGuesses
}
func answerForDay(day int) string {
answersFile, err := files.Open("answers.txt")
if err != nil {
panic(err)
}
defer answersFile.Close()
s := bufio.NewScanner(answersFile)
for i := 0; i <= day; i++ {
s.Scan()
}
return strings.ToUpper(s.Text())
}
func daysSinceFirstWordle() int {
year, month, day := time.Now().Date()
today := time.Date(year, month, day, 0, 0, 0, 0, time.UTC)
return int(today.Sub(firstDay).Hours() / 24)
}
func randomDay() int {
return rand.Intn(totalWordles)
}