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"apple", "grape", "pearl", "flame", "stone",
|
| 9 |
+
"ocean", "brave", "crane", "plume", "spark",
|
| 10 |
+
"blaze", "globe", "quest", "vivid", "prism",
|
| 11 |
+
"frost", "clock", "honey", "maple", "whale",
|
| 12 |
+
"daisy", "shark", "charm", "smile", "peach",
|
| 13 |
+
"cloud", "drift", "creek", "flour", "flock",
|
| 14 |
+
"grove", "haunt", "jolly", "knack", "lemon",
|
| 15 |
+
"lunar", "mango", "night", "olive", "piano",
|
| 16 |
+
"quilt", "raven", "scout", "swirl", "tango",
|
| 17 |
+
"unite", "vapor", "whirl", "xenon", "yacht",
|
| 18 |
+
"zebra", "amber", "brick", "cedar", "dwell",
|
| 19 |
+
"fable", "gleam", "haven", "ivory", "jazzy",
|
| 20 |
+
"karma", "latte", "medal", "noble", "orbit",
|
| 21 |
+
"petal", "quack", "reign", "spike", "truce",
|
| 22 |
+
"umbra", "valor", "whisk", "xylem", "yeast",
|
| 23 |
+
"zesty", "abode", "broil", "cling", "dunce",
|
| 24 |
+
"ember", "frost", "glare", "hiker", "inbox",
|
| 25 |
+
"jiffy", "knoll", "lilac", "mirth", "niece",
|
| 26 |
+
"opine", "pluck", "quill", "rouge", "stark",
|
| 27 |
+
"throb", "usher", "vista", "wrath", "xenon"
|
| 28 |
+
]
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
def reinitialize_game():
|
| 32 |
+
st.session_state['game_active'] = True
|
| 33 |
+
st.session_state['target_word'] = random.choice(word_list)
|
| 34 |
+
st.session_state['attempts'] = []
|
| 35 |
+
st.session_state['score'] = 0
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
def initialize_game():
|
| 39 |
+
if 'target_word' not in st.session_state:
|
| 40 |
+
st.session_state['target_word'] = random.choice(word_list)
|
| 41 |
+
if 'attempts' not in st.session_state:
|
| 42 |
+
st.session_state['attempts'] = []
|
| 43 |
+
if 'game_active' not in st.session_state:
|
| 44 |
+
st.session_state['game_active'] = False
|
| 45 |
+
if 'score' not in st.session_state:
|
| 46 |
+
st.session_state['score'] = 0
|
| 47 |
+
|
| 48 |
+
st.title('Wordle game')
|
| 49 |
+
st.sidebar.write(f"**Score:** {st.session_state['score']}")
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
def calculate_score(attempt_number):
|
| 53 |
+
score = 100 * (1/2) ** (attempt_number - 1) if attempt_number <= 5 else 0
|
| 54 |
+
return score
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
def is_valid_word(word):
|
| 58 |
+
return bool(re.match("^[a-zA-Z]{5}$", word))
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
def give_feedback(str1, str2):
|
| 62 |
+
if str1[i] == str2[i]:
|
| 63 |
+
feedback = "🟩" # correct letter and position
|
| 64 |
+
elif str1[i] in str2:
|
| 65 |
+
feedback = "🟨" # correct letter but wrong position
|
| 66 |
+
else:
|
| 67 |
+
feedback = "⬛" # letter not in word
|
| 68 |
+
return feedback
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
def lose_actions():
|
| 72 |
+
st.session_state['game_active'] = False
|
| 73 |
+
st.write(f"**Game over!** You've reached the maximum number of \
|
| 74 |
+
attempts. The answer is **{target.upper()}**")
|
| 75 |
+
st.session_state['score'] += calculate_score(len(attempts))
|
| 76 |
+
st.button("Restart Game") # Allow restart
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
def win_actions():
|
| 80 |
+
st.write(f"**YOU WIN!** The answer was **{target.upper()}**.")
|
| 81 |
+
st.session_state['score'] += calculate_score(len(attempts))
|
| 82 |
+
st.button("Restart Game") # Allow restart
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
def giveup_actions():
|
| 86 |
+
st.write(f"**You gave up!** The answer was **{target.upper()}**.")
|
| 87 |
+
st.session_state['score'] += calculate_score(len(attempts))
|
| 88 |
+
st.session_state['game_active'] = False
|
| 89 |
+
st.button("Restart Game") # Allow restart
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
def answer_str(target):
|
| 93 |
+
return f'The answer was **{target.upper()}**.'
|
| 94 |
+
|
| 95 |
+
# ======================================================
|
| 96 |
+
# MAIN
|
| 97 |
+
|
| 98 |
+
|
| 99 |
+
initialize_game()
|
| 100 |
+
|
| 101 |
+
# Handle game logic
|
| 102 |
+
if st.session_state['game_active']:
|
| 103 |
+
target = st.session_state['target_word']
|
| 104 |
+
attempts = st.session_state['attempts']
|
| 105 |
+
|
| 106 |
+
# Limit to 6 attempts
|
| 107 |
+
if len(attempts) >= 6:
|
| 108 |
+
lose_actions()
|
| 109 |
+
|
| 110 |
+
input_word = st.text_input("**Enter your 5-letter word guess:**",
|
| 111 |
+
max_chars=5,
|
| 112 |
+
placeholder="e.g. apple",
|
| 113 |
+
label_visibility="visible")
|
| 114 |
+
|
| 115 |
+
input_word = input_word.lower()
|
| 116 |
+
|
| 117 |
+
if st.button("Submit"):
|
| 118 |
+
if len(input_word) == 5 and is_valid_word(input_word):
|
| 119 |
+
attempts.append(input_word)
|
| 120 |
+
elif not is_valid_word(input_word):
|
| 121 |
+
st.write("**Invalid input! Please enter a 5-letter word.**")
|
| 122 |
+
|
| 123 |
+
# Display guesses and feedback
|
| 124 |
+
for attempt in attempts:
|
| 125 |
+
feedback = ""
|
| 126 |
+
for i in range(5):
|
| 127 |
+
feedback += give_feedback(attempt, target)
|
| 128 |
+
|
| 129 |
+
st.write(f"{attempt.upper()} {feedback}")
|
| 130 |
+
|
| 131 |
+
if feedback == "🟩" * 5:
|
| 132 |
+
win_actions()
|
| 133 |
+
break
|
| 134 |
+
|
| 135 |
+
# Button to give up
|
| 136 |
+
if st.button("Give Up"):
|
| 137 |
+
giveup_actions()
|
| 138 |
+
|
| 139 |
+
else:
|
| 140 |
+
if st.button("Play the game"):
|
| 141 |
+
reinitialize_game()
|
| 142 |
+
st.write("**Game has started! Press the button again.**")
|