forked from nolenroyalty/wardle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
104 lines (88 loc) · 3.16 KB
/
app.py
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
from flask import Flask, request
from collections import defaultdict
import re
import random
GREEN ="🟩"
YELLOW ="🟨"
WHITE ="⬜"
def get_answers():
with open("allowed_answers.txt") as f:
answers = set(l for l in f.read().splitlines() if l)
return answers
def get_guesses():
guesses = get_answers()
with open("allowed_guesses.txt") as f:
for l in f.read().splitlines():
if l: guesses.add(l)
return guesses
app = Flask(__name__, static_folder="static")
app.answers = get_answers()
app.guesses = get_guesses()
word = random.choice(list(app.answers))
print(f"The word is {word}")
def with_header(content):
return f"""
<html>
<head>
<link rel="search" type="application/opensearchdescription+xml" title="searchGame" href="http://searchgame:5000/static/opensearch.xml" />
</head>
<body> {content} </body></html>"""
@app.route("/")
def home():
return with_header("<p>Right click on the address bar to install the search engine.</p>")
@app.route("/search")
def search():
return with_header(f"Content: {request.args.get('q')}")
def to_result(guess, answer):
chars = [WHITE] * 5
count = defaultdict(int)
for idx, (g, a) in enumerate(zip(guess, answer)):
if g == a: chars[idx] = GREEN
else: count[a] += 1
for idx, g in enumerate(guess):
if g in count and count[g] > 0 and chars[idx] == WHITE:
chars[idx] = YELLOW
count[g] -= 1
return "".join(chars)
def maybe_error(guess):
if len(guess) < 5: return f"less than 5 characters"
if len(guess) > 5: return f"greater than 5 characters"
if guess not in app.guesses: return f"not in wordlist"
return None
def get_fixed_width_char(letter):
fixed_width_chars = {
'a': 'a', 'b': 'b', 'c': 'c', 'd': 'd', 'e': 'e',
'f': 'f', 'g': 'g', 'h': 'h', 'i': 'i', 'j': 'j',
'k': 'k', 'l': 'l', 'm': 'm', 'n': 'n', 'o': 'o',
'p': 'p', 'q': 'q', 'r': 'r', 's': 's', 't': 't',
'u': 'u', 'v': 'v', 'w': 'w', 'x': 'x', 'y': 'y',
'z': 'z'
}
return fixed_width_chars.get(letter, letter)
@app.route("/game")
def game():
query = request.args.get("q")
guesses = [x for x in re.split("[. ]", query) if x]
response = []
if not guesses:
response.append("Enter 5-letter guesses separated by spaces")
else:
most_recent = guesses[-1]
# Don't show "too short" error for most recent guess
if len(most_recent) < 5: guesses = guesses[:-1]
if not guesses:
response.append("Enter a wordle guess")
for guess in guesses[::-1]:
error = maybe_error(guess)
fixed_width = "".join(get_fixed_width_char(c) for c in guess)
if error is None:
result = to_result(guess, word)
s = f"{fixed_width} | {result}"
if result == GREEN * 5:
s = f"{fixed_width} | CORRECT!"
response.append(s)
else:
response.append(f"{fixed_width} | ERROR: {error}")
return [query, response]
if __name__ == "__main__":
app.run(debug=True)