jsakshi commited on
Commit
4d94442
ยท
verified ยท
1 Parent(s): 23e1f24

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -183
app.py CHANGED
@@ -14,66 +14,6 @@ WORD_LISTS = {
14
  "movies": ["inception", "avatar", "titanic", "interstellar", "joker", "gladiator", "frozen", "matrix", "avengers"]
15
  }
16
 
17
- CLUES = {
18
- "elephant": [
19
- "I am the largest land mammal",
20
- "I have a long trunk",
21
- "I have big ears and tusks",
22
- "I never forget",
23
- "I live in Africa or Asia"
24
- ],
25
- "penguin": [
26
- "I am a flightless bird",
27
- "I love the cold",
28
- "I waddle when I walk",
29
- "I wear a natural tuxedo",
30
- "I swim very well"
31
- ],
32
- "soccer": [
33
- "I am the most popular sport in the world",
34
- "I am played with a round ball",
35
- "I am known as 'football' outside the USA",
36
- "I have 11 players in a team",
37
- "The FIFA World Cup is my biggest event"
38
- ],
39
- "computer": [
40
- "I am an electronic machine",
41
- "I process data and execute instructions",
42
- "I can be a laptop or desktop",
43
- "I have a CPU and memory",
44
- "I am used in almost every field today"
45
- ],
46
- "earth": [
47
- "I am the third planet from the sun",
48
- "I have life on me",
49
- "I am 70% covered in water",
50
- "I have one natural satellite",
51
- "My nickname is the 'Blue Planet'"
52
- ],
53
- "car": [
54
- "I have four wheels",
55
- "I run on fuel or electricity",
56
- "I am used for transportation",
57
- "I have an engine",
58
- "You need a license to drive me"
59
- ],
60
- "doctor": [
61
- "I help sick people",
62
- "I wear a white coat",
63
- "I use a stethoscope",
64
- "I work in hospitals or clinics",
65
- "I can be a surgeon or a general physician"
66
- ],
67
- "titanic": [
68
- "I was a famous ship",
69
- "I sank in 1912",
70
- "I was called 'unsinkable'",
71
- "A famous movie was made about me",
72
- "I hit an iceberg"
73
- ]
74
- }
75
-
76
-
77
  class GameState:
78
  def __init__(self):
79
  self.score = 0
@@ -81,28 +21,13 @@ class GameState:
81
  self.clues_shown = 0
82
  self.guesses_left = 2
83
  self.category = ""
84
-
85
  def reset(self):
86
  self.__init__()
87
 
88
  game = GameState()
89
 
90
- def get_random_clues(word):
91
- """Get pre-written clues or generate simple ones."""
92
- if word in CLUES:
93
- return CLUES[word]
94
-
95
- # Fallback clues if word not in CLUES dictionary
96
- return [
97
- f"This word has {len(word)} letters",
98
- f"It starts with '{word[0]}'",
99
- f"It ends with '{word[-1]}'",
100
- f"It belongs to category: {game.category}",
101
- "This is your last clue!"
102
- ]
103
-
104
  def start_game(category):
105
- """Start a new game with selected category."""
106
  if not category:
107
  return "Please select a category first!", "", get_status(), False
108
 
@@ -110,17 +35,11 @@ def start_game(category):
110
  game.category = category
111
  game.current_word = random.choice(WORD_LISTS[category])
112
  game.clues_shown = 0
113
- clue = get_random_clues(game.current_word)[0]
114
 
115
- return (
116
- f"Category: {category}\nFirst clue: {clue}",
117
- "",
118
- get_status(),
119
- True
120
- )
121
 
122
  def make_guess(guess, interface_state):
123
- """Process the player's guess."""
124
  if not interface_state:
125
  return "Please start a new game first!", "", get_status(), False
126
 
@@ -130,121 +49,37 @@ def make_guess(guess, interface_state):
130
  guess = guess.lower().strip()
131
 
132
  if guess == game.current_word:
133
- score_increase = (game.guesses_left * 20) - (game.clues_shown * 5)
134
- game.score += score_increase
135
- return (
136
- f"๐ŸŽ‰ Correct! +{score_increase} points! The word was: {game.current_word}",
137
- "",
138
- get_status(),
139
- False
140
- )
141
 
142
  game.guesses_left -= 1
143
 
144
  if game.guesses_left <= 0:
145
- return (
146
- f"Game Over! The word was: {game.current_word}",
147
- "",
148
- get_status(),
149
- False
150
- )
151
 
152
- feedback = generate_feedback(guess)
153
- return feedback, "", get_status(), interface_state
154
-
155
- def get_hint(interface_state):
156
- """Provide next clue for the current word."""
157
- if not interface_state:
158
- return "Please start a new game first!", "", get_status(), False
159
-
160
- clues = get_random_clues(game.current_word)
161
- game.clues_shown += 1
162
-
163
- if game.clues_shown >= len(clues):
164
- return "No more clues available!", "", get_status(), interface_state
165
-
166
- return f"Clue #{game.clues_shown + 1}: {clues[game.clues_shown]}", "", get_status(), interface_state
167
-
168
- def generate_feedback(guess):
169
- """Generate helpful feedback for incorrect guesses."""
170
- word = game.current_word
171
-
172
- if len(guess) != len(word):
173
- return f"The word has {len(word)} letters (your guess had {len(guess)})"
174
-
175
- correct_pos = sum(1 for a, b in zip(guess, word) if a == b)
176
- correct_letters = len(set(guess) & set(word))
177
-
178
- return f"'{guess}' is not correct. {correct_pos} letters in correct position, {correct_letters} correct letters. {game.guesses_left} guesses left!"
179
 
180
  def get_status():
181
- """Get current game status."""
182
- return f"""
183
- ๐ŸŽฎ Game Status:
184
- Score: {game.score}
185
- Guesses Left: {game.guesses_left}
186
- Hints Used: {game.clues_shown}
187
- """
188
 
189
- # Gradio interface
190
  with gr.Blocks(title="Word Guessing Game") as demo:
191
  gr.Markdown("# ๐ŸŽฎ Word Guessing Game")
192
  gr.Markdown("Guess the word from the clues! Get bonus points for using fewer hints and guesses.")
193
 
194
- # Game state (hidden)
195
  interface_state = gr.State(False)
196
 
197
- # Game status
198
- status_display = gr.Textbox(
199
- label="Game Status",
200
- value=get_status(),
201
- interactive=False
202
- )
203
 
204
- # Category selection
205
- category_select = gr.Dropdown(
206
- choices=list(WORD_LISTS.keys()),
207
- label="Choose Category"
208
- )
209
-
210
- # Game display
211
- game_display = gr.Textbox(
212
- label="Game Progress",
213
- interactive=False,
214
- lines=3
215
- )
216
-
217
- # Player input
218
  with gr.Row():
219
- guess_input = gr.Textbox(
220
- label="Your Guess",
221
- placeholder="Type your guess here..."
222
- )
223
- guess_button = gr.Button("๐ŸŽฏ Make Guess")
224
-
225
- # Hint button
226
- hint_button = gr.Button("๐Ÿ’ก Get Hint")
227
 
228
- # Start new game button
229
- start_button = gr.Button("๐ŸŽฒ Start New Game")
230
 
231
- # Event handlers
232
- start_button.click(
233
- fn=start_game,
234
- inputs=[category_select],
235
- outputs=[game_display, guess_input, status_display, interface_state]
236
- )
237
 
238
- guess_button.click(
239
- fn=make_guess,
240
- inputs=[guess_input, interface_state],
241
- outputs=[game_display, guess_input, status_display, interface_state]
242
- )
243
-
244
- hint_button.click(
245
- fn=get_hint,
246
- inputs=[interface_state],
247
- outputs=[game_display, guess_input, status_display, interface_state]
248
- )
249
-
250
- demo.launch()
 
14
  "movies": ["inception", "avatar", "titanic", "interstellar", "joker", "gladiator", "frozen", "matrix", "avengers"]
15
  }
16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  class GameState:
18
  def __init__(self):
19
  self.score = 0
 
21
  self.clues_shown = 0
22
  self.guesses_left = 2
23
  self.category = ""
24
+
25
  def reset(self):
26
  self.__init__()
27
 
28
  game = GameState()
29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
  def start_game(category):
 
31
  if not category:
32
  return "Please select a category first!", "", get_status(), False
33
 
 
35
  game.category = category
36
  game.current_word = random.choice(WORD_LISTS[category])
37
  game.clues_shown = 0
38
+ clue = f"This word has {len(game.current_word)} letters"
39
 
40
+ return f"Category: {category}\nFirst clue: {clue}", "", get_status(), True
 
 
 
 
 
41
 
42
  def make_guess(guess, interface_state):
 
43
  if not interface_state:
44
  return "Please start a new game first!", "", get_status(), False
45
 
 
49
  guess = guess.lower().strip()
50
 
51
  if guess == game.current_word:
52
+ game.score += 20
53
+ return f"๐ŸŽ‰ Correct! The word was: {game.current_word}", "", get_status(), False
 
 
 
 
 
 
54
 
55
  game.guesses_left -= 1
56
 
57
  if game.guesses_left <= 0:
58
+ return f"Game Over! The word was: {game.current_word}", "", get_status(), False
 
 
 
 
 
59
 
60
+ return f"Incorrect! {game.guesses_left} guesses left.", "", get_status(), interface_state
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
 
62
  def get_status():
63
+ return f"\n๐ŸŽฎ Game Status:\nScore: {game.score}\nGuesses Left: {game.guesses_left}\nHints Used: {game.clues_shown}\n"
 
 
 
 
 
 
64
 
 
65
  with gr.Blocks(title="Word Guessing Game") as demo:
66
  gr.Markdown("# ๐ŸŽฎ Word Guessing Game")
67
  gr.Markdown("Guess the word from the clues! Get bonus points for using fewer hints and guesses.")
68
 
 
69
  interface_state = gr.State(False)
70
 
71
+ status_display = gr.Textbox(label="Game Status", value=get_status(), interactive=False)
72
+ category_select = gr.Dropdown(choices=list(WORD_LISTS.keys()), label="Choose Category")
73
+ game_display = gr.Textbox(label="Game Progress", interactive=False, lines=3)
 
 
 
74
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75
  with gr.Row():
76
+ guess_input = gr.Textbox(label="Your Guess", placeholder="Type your guess here...")
77
+ guess_button = gr.Button("๐ŸŽฏ Make Guess", variant="primary")
 
 
 
 
 
 
78
 
79
+ hint_button = gr.Button("๐Ÿ’ก Get Hint", variant="primary")
80
+ start_button = gr.Button("๐ŸŽฒ Start New Game", variant="primary")
81
 
82
+ start_button.click(fn=start_game, inputs=[category_select], outputs=[game_display, guess_input, status_display, interface_state])
83
+ guess_button.click(fn=make_guess, inputs=[guess_input, interface_state], outputs=[game_display, guess_input, status_display, interface_state])
 
 
 
 
84
 
85
+ demo.launch()