-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
162 lines (127 loc) · 4.47 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
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
import streamlit as st
import random
import datetime
# Read words from a txt file
def read_word_list():
file_path = "id-word-list.txt"
with open(file_path, "r") as file:
word_list = file.read().splitlines()
return word_list
def generate_letters():
# Generate a list of random letters with specific conditions
vowels = ["a", "e", "i", "o", "u"]
num_vowels = 2
num_consonants = 7 - num_vowels
# Get the current date (without time) to use as the seed
today = datetime.date.today()
seed = int(today.strftime("%Y%m%d"))
random.seed(seed)
# Add vowels
letters = random.sample(vowels, num_vowels)
# Add consonants
consonants = [c for c in "bcdfghjklmnpqrstvwxyz" if c not in letters]
letters += random.sample(consonants, num_consonants)
# Reset the seed so that other random operations are not affected
random.seed(None)
return letters
def get_valid_words(letters, word_list):
# Find all valid words that can be constructed from the given letters
valid_words = []
for word in word_list:
word_letters = list(word)
if all(letter in letters for letter in word_letters) and word_letters[0] in letters:
valid_words.append(word)
return valid_words
def calculate_word_score(word):
# Calculate score for a single word
return len(word)
def calculate_total_score(words):
# Calculate total score for all words
return sum(calculate_word_score(word) for word in words)
def display_letters(letters):
center_letter = letters[0]
other_letters = letters[1:]
html = f"""
<style>
.bee-hive {{
text-align: center;
line-height: 1.6;
}}
.hexagon {{
display: inline-block;
margin: 2px;
width: 30px;
height: 30px;
border: 1px solid #000;
line-height: 30px;
font-size: 20px;
background-color: #fff;
}}
.hexagon.center {{
background-color: #fff;
}}
</style>
<div class="bee-hive">
<div></div>
<div class="hexagon">{other_letters[0]}</div>
<div class="hexagon">{other_letters[1]}</div>
<div></div>
<div class="hexagon">{other_letters[2]}</div>
<div class="hexagon center">{center_letter}</div>
<div class="hexagon">{other_letters[3]}</div>
<div></div>
<div class="hexagon">{other_letters[4]}</div>
<div class="hexagon">{other_letters[5]}</div>
<div></div>
</div>
"""
st.markdown(html, unsafe_allow_html=True)
# Streamlit app
def main():
st.title("Tebak kata")
word_list = read_word_list()
if "letters" not in st.session_state:
st.session_state["letters"] = generate_letters()
st.session_state["timestamp"] = datetime.datetime.now()
letters = st.session_state["letters"]
st.markdown("---")
# Add a button that shuffles the order of the letters
display_letters(letters)
if st.button("Acak susunan huruf"):
random.shuffle(letters)
st.session_state["letters"] = letters
st.markdown("---")
user_input = st.text_input("Kata tebakanmu:")
if user_input:
guessed_words = st.session_state.get("guessed_words", [])
score = st.session_state.get("score", 0)
if user_input in guessed_words:
st.warning("Kata telah ditebak. Coba kata lain.")
else:
valid_words = get_valid_words(letters, word_list)
if user_input in valid_words:
st.success("Betul!")
guessed_words.append(user_input)
word_score = calculate_word_score(user_input)
score += word_score
st.session_state["score"] = score
st.write(" ")
st.info(f"Skor kata: {word_score}")
else:
st.error("Ups, salah!")
st.write(" ")
st.info(f"Skor total: {calculate_total_score(guessed_words)}")
st.session_state["guessed_words"] = guessed_words
st.write(" ")
st.info(f"Daftar kata telah ditebak: {', '.join(guessed_words)}")
# Footer with your name
with st.container():
st.markdown("---")
st.markdown("Developed by MW Hidayat to help his first grader learning new words. Inspired by The NYT's Spelling Bee. Find me on [Twitter](https://twitter.com/casecrit)")
if __name__ == "__main__":
st.set_page_config(
page_title="Tebak kata",
page_icon="💡",
layout="centered",
)
main()