LangBuddy / src /pages /4_Language_games.py
sailadachetansurya
stream lit changes first commit
30837f6
import streamlit as st
import random
st.title("🎲 Language Games")
# Instructions for the user
st.markdown("""
**Instructions:**
- **Word Scramble Game:**
- You will be shown a scrambled word. Your task is to **unscramble** it and type the correct word.
- Click on **Start New Round** to begin, and keep playing to improve your score.
- **Hangman Game:**
- In this game, you need to guess a hidden word by **guessing one letter at a time**.
- You have a limited number of tries, so be careful!
- Once you've guessed the word, you will get feedback on your performance.
- Enjoy playing and improving your vocabulary!
""")
# Initialize session state variables for Word Scramble
if "ws_score" not in st.session_state:
st.session_state.ws_score = 0
if "ws_word" not in st.session_state:
st.session_state.ws_word = ""
if "ws_scrambled" not in st.session_state:
st.session_state.ws_scrambled = ""
if "ws_round_active" not in st.session_state:
st.session_state.ws_round_active = False
# Initialize session state variables for Hangman
if "hm_word" not in st.session_state:
st.session_state.hm_word = ""
if "hm_display" not in st.session_state:
st.session_state.hm_display = ""
if "hm_guesses" not in st.session_state:
st.session_state.hm_guesses = []
if "hm_tries_left" not in st.session_state:
st.session_state.hm_tries_left = 6
if "hm_game_over" not in st.session_state:
st.session_state.hm_game_over = False
games = ["Word Scramble", "Hangman"]
game_choice = st.selectbox("Choose a game", games)
# Word Scramble Game: Helper Functions
def start_new_round():
words = ["python", "algorithm", "function", "variable", "loop", "recursion", "array", "debug"]
word = random.choice(words)
scrambled = "".join(random.sample(word, len(word)))
st.session_state.ws_word = word
st.session_state.ws_scrambled = scrambled
st.session_state.ws_round_active = True
def handle_word_scramble():
if not st.session_state.ws_round_active:
if st.button("Start New Round"):
start_new_round()
if st.session_state.ws_round_active:
st.write(f"Unscramble this word: **{st.session_state.ws_scrambled}**")
guess = st.text_input("Your guess:", key="ws_guess")
if st.button("Submit Guess"):
if guess.lower() == st.session_state.ws_word:
st.success("πŸŽ‰ Correct!")
st.session_state.ws_score += 1
st.session_state.ws_round_active = False
else:
st.error("Oops! That's not correct. Try again.")
st.write(f"Current Score: {st.session_state.ws_score}")
# Hangman Game: Helper Functions
def start_hangman():
words = ["streamlit", "python", "developer", "computer", "function", "variable", "syntax", "algorithm"]
word = random.choice(words)
st.session_state.hm_word = word
st.session_state.hm_display = "_" * len(word)
st.session_state.hm_guesses = []
st.session_state.hm_tries_left = 6
st.session_state.hm_game_over = False
def handle_hangman():
hangman_stages = [
"""
-----
| |
|
|
|
|
=========
""",
"""
-----
| |
O |
|
|
|
=========
""",
"""
-----
| |
O |
| |
|
|
=========
""",
"""
-----
| |
O |
/| |
|
|
=========
""",
"""
-----
| |
O |
/|\\ |
|
|
=========
""",
"""
-----
| |
O |
/|\\ |
/ |
|
=========
""",
"""
-----
| |
O |
/|\\ |
/ \\ |
|
=========
"""
]
st.write(hangman_stages[6 - st.session_state.hm_tries_left])
st.write(f"Word: {' '.join(st.session_state.hm_display)}")
st.write(f"Guesses: {', '.join(st.session_state.hm_guesses)}")
st.write(f"Tries left: {st.session_state.hm_tries_left}")
if not st.session_state.hm_game_over:
guess = st.text_input("Guess a letter:", max_chars=1, key="hm_input").lower()
if st.button("Submit Guess", key="hm_submit"):
if guess and guess.isalpha() and len(guess) == 1:
if guess in st.session_state.hm_guesses:
st.warning("You already guessed that letter!")
else:
st.session_state.hm_guesses.append(guess)
if guess in st.session_state.hm_word:
# Update display
new_display = list(st.session_state.hm_display)
for i, c in enumerate(st.session_state.hm_word):
if c == guess:
new_display[i] = guess
st.session_state.hm_display = "".join(new_display)
st.success(f"Good guess! Letter '{guess}' is in the word.")
else:
st.session_state.hm_tries_left -= 1
st.error(f"Letter '{guess}' is NOT in the word.")
# Check win/loss
if st.session_state.hm_display == st.session_state.hm_word:
st.balloons()
st.success(f"πŸŽ‰ You guessed the word: {st.session_state.hm_word.upper()}!")
st.session_state.hm_game_over = True
elif st.session_state.hm_tries_left == 0:
st.error(f"Game over! The word was: {st.session_state.hm_word.upper()}")
st.session_state.hm_game_over = True
else:
st.error("Please enter a single alphabet letter.")
else:
if st.button("Play Again"):
start_hangman()
# Game Logic
if game_choice == "Word Scramble":
handle_word_scramble()
elif game_choice == "Hangman":
if st.button("Start New Hangman Game") or st.session_state.hm_word == "":
start_hangman()
handle_hangman()