Spaces:
Sleeping
Sleeping
File size: 7,886 Bytes
1b99c6b c8c920f 759b6eb c8c920f 0df432f c8c920f b7228ad c4718ff c8c920f 23e1f24 c8c920f c4718ff c8c920f c4718ff c8c920f c4718ff c8c920f bb50953 c8c920f c4718ff c8c920f c4718ff b7228ad c8c920f c4718ff c8c920f bb50953 c8c920f c4718ff c8c920f c4718ff c8c920f c4718ff c8c920f c4718ff c8c920f c4718ff c8c920f c4718ff c8c920f c4718ff c8c920f c4718ff c8c920f c4718ff 4d94442 c8c920f c4718ff 4d94442 c4718ff 4d94442 c8c920f c4718ff c8c920f c4718ff |
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 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 |
import gradio as gr
import random
WORD_LISTS = {
"animals": ["elephant", "penguin", "giraffe", "dolphin", "tiger", "lion", "zebra", "kangaroo", "panda", "cheetah"],
"food": ["pizza", "burger", "pasta", "sushi", "taco", "cookie", "salad", "noodles", "sandwich", "chocolate"],
"colors": ["purple", "orange", "yellow", "green", "brown", "black", "white", "red", "blue", "pink"],
"countries": ["france", "japan", "india", "brazil", "egypt", "spain", "italy", "canada", "germany", "china"],
"sports": ["soccer", "cricket", "tennis", "hockey", "basketball", "baseball", "badminton", "volleyball", "golf"],
"technology": ["computer", "internet", "robotics", "software", "hardware", "algorithm", "database", "cybersecurity"],
"planets": ["earth", "mars", "venus", "jupiter", "saturn", "uranus", "neptune", "mercury", "pluto"],
"vehicles": ["car", "bicycle", "motorcycle", "bus", "truck", "airplane", "train", "ship", "submarine"],
"professions": ["doctor", "engineer", "teacher", "artist", "scientist", "lawyer", "pilot", "chef", "architect"],
"movies": ["inception", "avatar", "titanic", "interstellar", "joker", "gladiator", "frozen", "matrix", "avengers"]
}
CLUES = {
"elephant": [
"I am the largest land mammal",
"I have a long trunk",
"I have big ears and tusks",
"I never forget",
"I live in Africa or Asia"
],
"penguin": [
"I am a flightless bird",
"I love the cold",
"I waddle when I walk",
"I wear a natural tuxedo",
"I swim very well"
],
"soccer": [
"I am the most popular sport in the world",
"I am played with a round ball",
"I am known as 'football' outside the USA",
"I have 11 players in a team",
"The FIFA World Cup is my biggest event"
],
"computer": [
"I am an electronic machine",
"I process data and execute instructions",
"I can be a laptop or desktop",
"I have a CPU and memory",
"I am used in almost every field today"
],
"earth": [
"I am the third planet from the sun",
"I have life on me",
"I am 70% covered in water",
"I have one natural satellite",
"My nickname is the 'Blue Planet'"
],
"car": [
"I have four wheels",
"I run on fuel or electricity",
"I am used for transportation",
"I have an engine",
"You need a license to drive me"
],
"doctor": [
"I help sick people",
"I wear a white coat",
"I use a stethoscope",
"I work in hospitals or clinics",
"I can be a surgeon or a general physician"
],
"titanic": [
"I was a famous ship",
"I sank in 1912",
"I was called 'unsinkable'",
"A famous movie was made about me",
"I hit an iceberg"
]
}
class GameState:
def __init__(self):
self.score = 0
self.current_word = ""
self.clues_shown = 0
self.guesses_left = 2
self.category = ""
def reset(self):
self.__init__()
game = GameState()
def get_random_clues(word):
"""Get pre-written clues or generate simple ones."""
if word in CLUES:
return CLUES[word]
# Fallback clues if word not in CLUES dictionary
return [
f"This word has {len(word)} letters",
f"It starts with '{word[0]}'",
f"It ends with '{word[-1]}'",
f"It belongs to category: {game.category}",
"This is your last clue!"
]
def start_game(category):
"""Start a new game with selected category."""
if not category:
return "Please select a category first!", "", get_status(), False
game.reset()
game.category = category
game.current_word = random.choice(WORD_LISTS[category])
game.clues_shown = 0
clue = get_random_clues(game.current_word)[0]
return (
f"Category: {category}\nFirst clue: {clue}",
"",
get_status(),
True
)
def make_guess(guess, interface_state):
"""Process the player's guess."""
if not interface_state:
return "Please start a new game first!", "", get_status(), False
if not guess:
return "Please enter a guess!", "", get_status(), interface_state
guess = guess.lower().strip()
if guess == game.current_word:
score_increase = (game.guesses_left * 20) - (game.clues_shown * 5)
game.score += score_increase
return (
f"๐ Correct! +{score_increase} points! The word was: {game.current_word}",
"",
get_status(),
False
)
game.guesses_left -= 1
if game.guesses_left <= 0:
return (
f"Game Over! The word was: {game.current_word}",
"",
get_status(),
False
)
feedback = generate_feedback(guess)
return feedback, "", get_status(), interface_state
def get_hint(interface_state):
"""Provide next clue for the current word."""
if not interface_state:
return "Please start a new game first!", "", get_status(), False
clues = get_random_clues(game.current_word)
game.clues_shown += 1
if game.clues_shown >= len(clues):
return "No more clues available!", "", get_status(), interface_state
return f"Clue #{game.clues_shown + 1}: {clues[game.clues_shown]}", "", get_status(), interface_state
def generate_feedback(guess):
"""Generate helpful feedback for incorrect guesses."""
word = game.current_word
if len(guess) != len(word):
return f"The word has {len(word)} letters (your guess had {len(guess)})"
correct_pos = sum(1 for a, b in zip(guess, word) if a == b)
correct_letters = len(set(guess) & set(word))
return f"'{guess}' is not correct. {correct_pos} letters in correct position, {correct_letters} correct letters. {game.guesses_left} guesses left!"
def get_status():
"""Get current game status."""
return f"""
๐ฎ Game Status:
Score: {game.score}
Guesses Left: {game.guesses_left}
Hints Used: {game.clues_shown}
"""
# Gradio interface
with gr.Blocks(title="Word Guessing Game") as demo:
gr.Markdown("# ๐ฎ Word Guessing Game")
gr.Markdown("Guess the word from the clues! Get bonus points for using fewer hints and guesses.")
# Game state (hidden)
interface_state = gr.State(False)
# Game status
status_display = gr.Textbox(
label="Game Status",
value=get_status(),
interactive=False
)
# Category selection
category_select = gr.Dropdown(
choices=list(WORD_LISTS.keys()),
label="Choose Category"
)
# Game display
game_display = gr.Textbox(
label="Game Progress",
interactive=False,
lines=3
)
# Player input
with gr.Row():
guess_input = gr.Textbox(
label="Your Guess",
placeholder="Type your guess here..."
)
guess_button = gr.Button("๐ฏ Make Guess", variant="primary")
# Hint button
hint_button = gr.Button("๐ก Get Hint", variant="primary")
# Start new game button
start_button = gr.Button("๐ฒ Start New Game", variant="primary")
# Event handlers
start_button.click(
fn=start_game,
inputs=[category_select],
outputs=[game_display, guess_input, status_display, interface_state]
)
guess_button.click(
fn=make_guess,
inputs=[guess_input, interface_state],
outputs=[game_display, guess_input, status_display, interface_state]
)
hint_button.click(
fn=get_hint,
inputs=[interface_state],
outputs=[game_display, guess_input, status_display, interface_state]
)
demo.launch() |