import gradio as gr # --- Game Setup --- rooms = { "dark_room": { "description": "You are in a dark room. A candle lies on a table next to a key. The door is locked.", "items": ["candle", "key"], "exits": ["door", "trapdoor"], "door_locked": True, "puzzles": {"candle_lit": False} }, "hallway": { "description": "You enter a hallway with a painting and a staircase leading up.", "items": ["painting"], "exits": ["stairs", "painting"], "door_locked": False, "puzzles": {"painting_moved": False} }, } inventory = [] current_room = None game_started = False # --- Help Text --- help_text = """ 🛠 **Help Menu** 🛠 - Look around: See your surroundings. - Pick up [item]: Take an item. - Inventory: Check items you hold. - Use [item]: Use an item in your inventory. - Move [exit]: Move between rooms. - Exit: Quit the game. """ # --- Game Logic --- def game_logic(action): global current_room, inventory, game_started room = rooms.get(current_room) action = action.strip() if not game_started: if action.lower() == "start game": game_started = True current_room = "dark_room" inventory.clear() return f"🎮 Game started!\n{rooms[current_room]['description']}" elif action.lower() == "help": return help_text elif action.lower() == "exit": return "Goodbye! Thanks for playing." return "Type 'Start Game' to begin, 'Help' for instructions, or 'Exit' to quit." if action.lower() == "help": return help_text if action.lower() == "look": if current_room == "dark_room" and not room["puzzles"]["candle_lit"]: return "It’s too dark to see much. You might need to light the candle first." items = ", ".join(room["items"]) if room["items"] else "nothing" return f"{room['description']} You see: {items}." if action.lower().startswith("pick up "): item = action[8:].strip() if item in room["items"]: inventory.append(item) room["items"].remove(item) return f"You picked up the {item}." return f"No {item} here to pick up." if action.lower().startswith("use "): item = action[4:].strip() if item in inventory: if item == "candle": room["puzzles"]["candle_lit"] = True return "You light the candle. Now you can see everything clearly!" return f"You used the {item}, but nothing happened." return f"You don't have a {item}." if action.lower() in [exit.lower() for exit in room["exits"]]: exit_name = next(exit for exit in room["exits"] if exit.lower() == action.lower()) if room["door_locked"] and exit_name == "door": if "key" in inventory: room["door_locked"] = False current_room = "hallway" return f"You unlock the door with the key and enter the hallway." return "The door is locked. You need a key." current_room = exit_name return f"You move into {current_room}. {rooms[current_room]['description']}" if action.lower() == "inventory": return f"You have: {', '.join(inventory) if inventory else 'nothing'}" if action.lower() == "exit": return "Thanks for playing! Goodbye!" return "I don't understand that action. Type 'Help' for instructions." # --- Gradio UI --- with gr.Blocks() as demo: gr.Markdown("# 🏰 Escape Room Adventure (Text Only)") chatbot = gr.Chatbot(type="messages") state = gr.State() msg = gr.Textbox(label="Type your action") def respond(action, history): history = history or [] reply = game_logic(action) history.append({"role": "user", "content": action}) history.append({"role": "assistant", "content": reply}) return history, history msg.submit(respond, inputs=[msg, state], outputs=[chatbot, state]) demo.launch()