File size: 4,431 Bytes
df505eb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import random
import re


# List of 5-letter words
word_list = [
    "apple", "grape", "pearl", "flame", "stone",
    "ocean", "brave", "crane", "plume", "spark",
    "blaze", "globe", "quest", "vivid", "prism",
    "frost", "clock", "honey", "maple", "whale",
    "daisy", "shark", "charm", "smile", "peach",
    "cloud", "drift", "creek", "flour", "flock",
    "grove", "haunt", "jolly", "knack", "lemon",
    "lunar", "mango", "night", "olive", "piano",
    "quilt", "raven", "scout", "swirl", "tango",
    "unite", "vapor", "whirl", "xenon", "yacht",
    "zebra", "amber", "brick", "cedar", "dwell",
    "fable", "gleam", "haven", "ivory", "jazzy",
    "karma", "latte", "medal", "noble", "orbit",
    "petal", "quack", "reign", "spike", "truce",
    "umbra", "valor", "whisk", "xylem", "yeast",
    "zesty", "abode", "broil", "cling", "dunce",
    "ember", "frost", "glare", "hiker", "inbox",
    "jiffy", "knoll", "lilac", "mirth", "niece",
    "opine", "pluck", "quill", "rouge", "stark",
    "throb", "usher", "vista", "wrath", "xenon"
]


def reinitialize_game():
    st.session_state['game_active'] = True
    st.session_state['target_word'] = random.choice(word_list)
    st.session_state['attempts'] = []
    st.session_state['score'] = 0


def initialize_game():
    if 'target_word' not in st.session_state:
        st.session_state['target_word'] = random.choice(word_list)
    if 'attempts' not in st.session_state:
        st.session_state['attempts'] = []
    if 'game_active' not in st.session_state:
        st.session_state['game_active'] = False
    if 'score' not in st.session_state:
        st.session_state['score'] = 0

    st.title('Wordle game')
    st.sidebar.write(f"**Score:** {st.session_state['score']}")


def calculate_score(attempt_number):
    score = 100 * (1/2) ** (attempt_number - 1) if attempt_number <= 5 else 0
    return score


def is_valid_word(word):
    return bool(re.match("^[a-zA-Z]{5}$", word))


def give_feedback(str1, str2):
    if str1[i] == str2[i]:
        feedback = "🟩"  # correct letter and position
    elif str1[i] in str2:
        feedback = "🟨"  # correct letter but wrong position
    else:
        feedback = "⬛"  # letter not in word
    return feedback


def lose_actions():
    st.session_state['game_active'] = False
    st.write(f"**Game over!** You've reached the maximum number of \
                 attempts. The answer is **{target.upper()}**")
    st.session_state['score'] += calculate_score(len(attempts))
    st.button("Restart Game")  # Allow restart


def win_actions():
    st.write(f"**YOU WIN!** The answer was **{target.upper()}**.")
    st.session_state['score'] += calculate_score(len(attempts))
    st.button("Restart Game")  # Allow restart


def giveup_actions():
    st.write(f"**You gave up!** The answer was **{target.upper()}**.")
    st.session_state['score'] += calculate_score(len(attempts))
    st.session_state['game_active'] = False
    st.button("Restart Game")  # Allow restart


def answer_str(target):
    return f'The answer was **{target.upper()}**.'

# ======================================================
# MAIN


initialize_game()

# Handle game logic
if st.session_state['game_active']:
    target = st.session_state['target_word']
    attempts = st.session_state['attempts']

    # Limit to 6 attempts
    if len(attempts) >= 6:
        lose_actions()

    input_word = st.text_input("**Enter your 5-letter word guess:**",
                               max_chars=5,
                               placeholder="e.g. apple",
                               label_visibility="visible")

    input_word = input_word.lower()

    if st.button("Submit"):
        if len(input_word) == 5 and is_valid_word(input_word):
            attempts.append(input_word)
        elif not is_valid_word(input_word):
            st.write("**Invalid input! Please enter a 5-letter word.**")

    # Display guesses and feedback
    for attempt in attempts:
        feedback = ""
        for i in range(5):
            feedback += give_feedback(attempt, target)

        st.write(f"{attempt.upper()} {feedback}")

        if feedback == "🟩" * 5:
            win_actions()
            break

    # Button to give up
    if st.button("Give Up"):
        giveup_actions()

else:
    if st.button("Play the game"):
        reinitialize_game()
        st.write("**Game has started! Press the button again.**")