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()