-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
wordle.py
90 lines (76 loc) · 3.27 KB
/
wordle.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
import random
import string
from funix import funix, funix_class, funix_method
from funix.hint import HTML
import json
with open("./words.json", "r") as f:
words = json.load(f)
@funix(
title="Get hint",
description="Get 5 random letters"
)
def random_5_letters() -> str:
"""
Get random 5 letters
"""
return "".join(random.choices(string.ascii_lowercase, k=5))
@funix_class()
class Wordle:
@funix_method(
title="Start a game",
argument_labels={
"random_word": "Random 5-letter"
},
print_to_web=True
)
def __init__(self, random_word: bool = False):
"""
Start a game
"""
self.random_word = random_word
self.word = random_5_letters() if random_word else random.choice(words)
self.history = []
self.mismatch = False
print("Game started. Now go to Play to play. ")
def __reset(self):
self.word = random_5_letters() if self.random_word else random.choice(words)
self.history = []
def __push(self, word: str):
if len(word) != 5 or not word.isalpha():
self.mismatch = True
else:
self.mismatch = False
if len(self.history) > 6:
self.__reset()
self.history.append(word.lower())
def __generate(self) -> HTML:
html_code = ""
for i in range(6):
html_code += f"<div id='line-{i+1}' style='width: 548px; display: flex; flex-wrap: wrap; justify-content: space-around; {'margin-top: 12px' if i > 0 else ''}'>"
if i < len(self.history):
for single_word_index in range(5):
single_word = self.history[i][single_word_index]
single_word_color = "rgb(156, 163, 175)"
if single_word in self.word:
single_word_color = "rgb(251, 191, 36)"
if single_word == self.word[single_word_index]:
single_word_color = "rgb(34, 197, 94)"
html_code += f"<div id='line-{i+1}-{single_word_index+1}' style='width: 100px; height: 100px; background-color: {single_word_color}; display: flex; justify-content: center; color: #fff; line-height: 1; font-size: 60px; font-weight: 700; align-items: center;'>{single_word}</div>"
else:
for j in range(5):
html_code += f"<div id='line-{i+1}-{j+1}' style='width: 100px; height: 100px; background-color: rgb(147, 197, 253); display: flex; justify-content: center; color: #fff; line-height: 1; font-size: 60px; font-weight: 700; align-items: center;'></div>"
html_code += "</div>"
if self.mismatch:
html_code += "<span>Word mismatch, please enter a 5-letter word</span>"
if len(self.history) > 0 and self.history[-1] == self.word:
self.__reset()
html_code += "<span>Good game, you win!</span>"
elif len(self.history) == 6:
html_code += f"<span>Game Over, answer is: <strong>{self.word}</strong></span>"
return html_code
def Play(self, word: str) -> HTML:
"""
Enter a five-letter word and click the "Run" button to see your guess result.
"""
self.__push(word)
return self.__generate()