SanaAdeel commited on
Commit
8fb62eb
·
verified ·
1 Parent(s): d1bd450

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -72
app.py CHANGED
@@ -1,83 +1,58 @@
1
  import streamlit as st
 
2
 
3
- # List of objects and related questions (example)
4
- objects_and_questions = {
5
- "Cat": [
6
- "Is it alive?",
7
- "Is it a pet?",
8
- "Does it have fur?",
9
- "Is it a small animal?",
10
- "Does it meow?"
11
- ],
12
- "Car": [
13
- "Is it something that moves on wheels?",
14
- "Can it be driven?",
15
- "Is it used for transportation?",
16
- "Does it have an engine?",
17
- "Is it a four-wheeled vehicle?"
18
- ],
19
- "Banana": [
20
- "Is it a fruit?",
21
- "Is it yellow?",
22
- "Does it grow on trees?",
23
- "Is it edible?",
24
- "Is it sweet?"
25
- ]
26
- }
27
 
28
  # Function to start the game
29
  def start_game():
30
- st.title("Think of a Thing and I'll Try to Guess It!")
31
-
32
- # Initial instructions
33
- st.write("Think of something, and I will try to guess it by asking you up to 10 yes/no questions.")
34
-
35
- # Initialize game state
36
- questions_asked = 0
37
- possible_objects = list(objects_and_questions.keys()) # Start with all objects
38
- current_questions = {obj: 0 for obj in possible_objects} # Initialize the index for each object
 
 
 
 
39
 
40
- while questions_asked < 10 and len(possible_objects) > 1:
41
- question = None
42
- for obj in possible_objects:
43
- # Make sure we have not exhausted all questions for the object
44
- if current_questions[obj] < len(objects_and_questions[obj]):
45
- question = objects_and_questions[obj][current_questions[obj]]
46
- break
47
-
48
- if not question:
49
- break
50
 
51
- # Ask the question
52
- answer = st.radio(question, ('Yes', 'No'), key=questions_asked)
53
-
54
- # Filter objects based on the answer
55
- if answer == 'Yes':
56
- possible_objects = [obj for obj in possible_objects if objects_and_questions[obj][current_questions[obj]] == question]
57
- else:
58
- possible_objects = [obj for obj in possible_objects if objects_and_questions[obj][current_questions[obj]] != question]
59
-
60
- # Move to the next question for the filtered objects
61
- for obj in possible_objects:
62
- current_questions[obj] += 1
63
-
64
- questions_asked += 1
65
 
66
- # After 10 questions or fewer, try to guess
67
- if len(possible_objects) == 1:
68
- st.write(f"I think you were thinking of a **{possible_objects[0]}**!")
69
- elif len(possible_objects) > 1:
70
- st.write("I couldn't guess your object with certainty. Maybe you can narrow it down a bit?")
71
- else:
72
- st.write("I couldn't guess anything based on the questions asked.")
73
 
74
- # Main function for Streamlit
75
- def main():
76
- st.title("Guess My Thing Game")
77
 
78
- # Start button
79
- if st.button("Start Game"):
80
- start_game()
81
 
82
- if __name__ == "__main__":
83
- main()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
+ import random
3
 
4
+ # List of words for the game
5
+ words = ['apple', 'banana', 'cherry', 'date', 'elderberry', 'fig', 'grape']
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
 
7
  # Function to start the game
8
  def start_game():
9
+ # Choose a random word from the list
10
+ secret_word = random.choice(words)
11
+ st.session_state.secret_word = secret_word
12
+ st.session_state.guesses_left = 6
13
+ st.session_state.guessed_letters = []
14
+
15
+ # Function to update the game state after each guess
16
+ def update_game_state(guess):
17
+ # Check if the guess is correct or not
18
+ if guess in st.session_state.secret_word:
19
+ st.session_state.guessed_letters.append(guess)
20
+ else:
21
+ st.session_state.guesses_left -= 1
22
 
23
+ # Streamlit app layout
24
+ st.title("Word Guessing Game")
 
 
 
 
 
 
 
 
25
 
26
+ if 'secret_word' not in st.session_state:
27
+ start_game()
 
 
 
 
 
 
 
 
 
 
 
 
28
 
29
+ # Display the current guessed word with blanks for unguessed letters
30
+ current_word = ''.join([letter if letter in st.session_state.guessed_letters else '_' for letter in st.session_state.secret_word])
31
+ st.write(f"Word: {current_word}")
 
 
 
 
32
 
33
+ # Display number of guesses left
34
+ st.write(f"Guesses Left: {st.session_state.guesses_left}")
 
35
 
36
+ # Input for user guess
37
+ guess = st.text_input("Guess a letter:", max_chars=1)
 
38
 
39
+ if guess:
40
+ guess = guess.lower()
41
+ if guess.isalpha() and len(guess) == 1:
42
+ if guess not in st.session_state.guessed_letters:
43
+ update_game_state(guess)
44
+ else:
45
+ st.warning("You've already guessed that letter.")
46
+ else:
47
+ st.warning("Please enter a valid letter.")
48
+
49
+ # Check if the game is over
50
+ if st.session_state.guesses_left == 0:
51
+ st.write("Game Over! The word was:", st.session_state.secret_word)
52
+ elif set(st.session_state.secret_word) == set(st.session_state.guessed_letters):
53
+ st.write("Congratulations! You've guessed the word:", st.session_state.secret_word)
54
+
55
+ # Button to restart the game
56
+ if st.button("Restart Game"):
57
+ start_game()
58
+ st.experimental_rerun()