import gradio as gr import random # List of emojis for the emoji selector emojis = ["😀", "😂", "😊", "😍", "🤔", "😎", "🙌", "👍", "❤️", "🎉"] # Game state game_state = { "location": "start", "inventory": [], "game_started": False } # Game map game_map = { "start": { "description": "You're at the entrance of a mysterious cave. Paths lead north and east.", "exits": {"north": "dark_tunnel", "east": "forest_clearing"} }, "dark_tunnel": { "description": "You're in a dark, damp tunnel. There's a faint light to the north and the entrance is to the south.", "exits": {"north": "underground_lake", "south": "start"} }, "forest_clearing": { "description": "You're in a peaceful forest clearing. There's a tall tree here and paths leading west and south.", "exits": {"west": "start", "south": "ancient_ruins"}, "items": ["shiny_key"] }, "underground_lake": { "description": "You're at the edge of an underground lake. The water glows with an eerie blue light. The tunnel is to the south.", "exits": {"south": "dark_tunnel"} }, "ancient_ruins": { "description": "You're standing amidst ancient ruins. There's a locked chest here and a path leading north.", "exits": {"north": "forest_clearing"}, "items": ["locked_chest"] } } def get_game_intro(): return """ Welcome to the Cave of Wonders! You find yourself standing at the entrance of a mysterious cave, rumored to hold untold treasures and ancient secrets. Your goal is to explore the cave, solve puzzles, and uncover the hidden artifacts within. As you embark on this text-based adventure, remember these key commands: - 'look' to examine your surroundings - 'go [direction]' to move (e.g., 'go north') - 'inventory' to check your items - 'take [item]' to pick up an item - 'use [item]' to use an item Your journey begins now. Good luck, adventurer! """ def get_bot_response(message, emoji): message = message.lower().strip() # Emoji-based responses emoji_responses = { "😀": "You seem happy! That's great to see.", "😂": "I'm glad you find this amusing!", "😊": "Your smile brightens up the adventure!", "😍": "Wow, you really love this game, don't you?", "🤔": "Hmm, thinking deeply about your next move?", "😎": "Looking cool! Ready for some adventure?", "🙌": "High five! You're doing great!", "👍": "Thumbs up to you too! Keep going!", "❤️": "I'm thrilled you're enjoying the game!", "🎉": "Time to celebrate your progress!" } # If an emoji is selected, respond to it if emoji in emoji_responses: return emoji_responses[emoji] + "\n\n" + get_game_response(message) else: return get_game_response(message) def get_game_response(message): if not game_state["game_started"]: if "start game" in message or "play game" in message: game_state["game_started"] = True return get_game_intro() + "\n" + game_map[game_state["location"]]["description"] + "\nWhat would you like to do?" else: return "I'm a text adventure game bot. Type 'start game' to begin playing!" if message == "help": return "Commands: 'look' to examine your surroundings, 'go [direction]' to move, 'inventory' to check your items, 'take [item]' to pick up an item, 'use [item]' to use an item." if message == "look": return game_map[game_state["location"]]["description"] if message.startswith("go "): direction = message.split()[1] if direction in game_map[game_state["location"]]["exits"]: game_state["location"] = game_map[game_state["location"]]["exits"][direction] return game_map[game_state["location"]]["description"] else: return "You can't go that way." if message == "inventory": if game_state["inventory"]: return "You are carrying: " + ", ".join(game_state["inventory"]) else: return "Your inventory is empty." if message.startswith("take "): item = message.split()[1] if "items" in game_map[game_state["location"]] and item in game_map[game_state["location"]]["items"]: game_state["inventory"].append(item) game_map[game_state["location"]]["items"].remove(item) return f"You have taken the {item}." else: return "There's no such item here." if message.startswith("use "): item = message.split()[1] if item in game_state["inventory"]: if item == "shiny_key" and game_state["location"] == "ancient_ruins": return "You use the shiny key to unlock the chest. Inside, you find a golden amulet. Congratulations, you've won the game!" else: return f"You can't use the {item} here." else: return f"You don't have a {item} in your inventory." return "I don't understand that command. Type 'help' for a list of commands." # Function to handle user input and generate response def chat(message, history, emoji): history = history or [] # Add user message to chat history history.append((message, "")) # Get bot response bot_message = get_bot_response(message, emoji) # Add bot message to chat history history[-1] = (message, bot_message) return history, "" # Return an empty string to clear the input # Function to start a new game def new_game(): game_state["location"] = "start" game_state["inventory"] = [] game_state["game_started"] = True intro = get_game_intro() start_description = game_map[game_state["location"]]["description"] return [(None, intro + "\n" + start_description)] # Function to add selected emoji to input def add_emoji(emoji, current_input): return current_input + emoji # Function to copy the last bot message def copy_last_message(history): if history and history[-1][1]: return history[-1][1] return "No message to copy." # Create the Gradio interface with gr.Blocks() as demo: chatbot = gr.Chatbot() with gr.Row(): with gr.Column(scale=9): msg = gr.Textbox(label="Type your message here", placeholder="Enter your message...") with gr.Column(scale=1): emoji_dropdown = gr.Dropdown(choices=emojis, label="", value="", interactive=True) with gr.Row(): submit = gr.Button("Submit") clear = gr.Button("Clear") new_game_btn = gr.Button("New Game") copy_button = gr.Button("Copy Last Bot Message") copied_text = gr.Textbox(label="Copied Message") # Set up event handlers submit.click(chat, inputs=[msg, chatbot, emoji_dropdown], outputs=[chatbot, msg]) msg.submit(chat, inputs=[msg, chatbot, emoji_dropdown], outputs=[chatbot, msg]) clear.click(lambda: None, None, chatbot, queue=False) new_game_btn.click(new_game, outputs=[chatbot]) emoji_dropdown.change(add_emoji, inputs=[emoji_dropdown, msg], outputs=msg) copy_button.click(copy_last_message, inputs=[chatbot], outputs=[copied_text]) # For Hugging Face Spaces compatibility demo.launch()