Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -27,10 +27,10 @@ help_text = """
|
|
27 |
🛠 **Help Menu** 🛠
|
28 |
|
29 |
- Look around: See your surroundings.
|
30 |
-
- Pick up: Take an item.
|
31 |
- Inventory: Check items you hold.
|
32 |
-
- Use: Use an item in your inventory.
|
33 |
-
- Move:
|
34 |
- Exit: Quit the game.
|
35 |
"""
|
36 |
|
@@ -39,37 +39,39 @@ def game_logic(action):
|
|
39 |
global current_room, inventory, game_started
|
40 |
room = rooms.get(current_room)
|
41 |
|
|
|
|
|
42 |
if not game_started:
|
43 |
-
if action == "
|
44 |
game_started = True
|
45 |
current_room = "dark_room"
|
46 |
inventory.clear()
|
47 |
return f"🎮 Game started!\n{rooms[current_room]['description']}"
|
48 |
-
elif action == "
|
49 |
return help_text
|
50 |
-
elif action == "
|
51 |
return "Goodbye! Thanks for playing."
|
52 |
-
return "
|
53 |
|
54 |
-
if action == "
|
55 |
return help_text
|
56 |
|
57 |
-
if action.
|
58 |
if current_room == "dark_room" and not room["puzzles"]["candle_lit"]:
|
59 |
return "It’s too dark to see much. You might need to light the candle first."
|
60 |
items = ", ".join(room["items"]) if room["items"] else "nothing"
|
61 |
return f"{room['description']} You see: {items}."
|
62 |
|
63 |
-
if action.startswith("
|
64 |
-
item = action.
|
65 |
if item in room["items"]:
|
66 |
inventory.append(item)
|
67 |
room["items"].remove(item)
|
68 |
return f"You picked up the {item}."
|
69 |
return f"No {item} here to pick up."
|
70 |
|
71 |
-
if action.startswith("
|
72 |
-
item = action.
|
73 |
if item in inventory:
|
74 |
if item == "candle":
|
75 |
room["puzzles"]["candle_lit"] = True
|
@@ -77,72 +79,41 @@ def game_logic(action):
|
|
77 |
return f"You used the {item}, but nothing happened."
|
78 |
return f"You don't have a {item}."
|
79 |
|
80 |
-
if action in room["exits"]:
|
81 |
-
|
|
|
82 |
if "key" in inventory:
|
83 |
room["door_locked"] = False
|
84 |
current_room = "hallway"
|
85 |
return f"You unlock the door with the key and enter the hallway."
|
86 |
return "The door is locked. You need a key."
|
87 |
-
current_room =
|
88 |
return f"You move into {current_room}. {rooms[current_room]['description']}"
|
89 |
|
90 |
-
if action == "
|
91 |
return f"You have: {', '.join(inventory) if inventory else 'nothing'}"
|
92 |
|
93 |
-
if action == "
|
94 |
return "Thanks for playing! Goodbye!"
|
95 |
|
96 |
-
return "I don't understand that action."
|
97 |
-
|
98 |
-
# --- Determine Which Buttons to Show ---
|
99 |
-
def get_visible_buttons():
|
100 |
-
if not game_started:
|
101 |
-
return ["Start Game", "Help", "Exit"]
|
102 |
-
room = rooms[current_room]
|
103 |
-
buttons = ["Look", "Inventory", "Help", "Exit"]
|
104 |
-
buttons += [f"Pick up {item}" for item in room["items"]]
|
105 |
-
buttons += room["exits"]
|
106 |
-
return buttons
|
107 |
|
108 |
# --- Gradio UI ---
|
109 |
with gr.Blocks() as demo:
|
110 |
-
gr.Markdown("# 🏰 Escape Room Adventure (
|
111 |
|
112 |
chatbot = gr.Chatbot(type="messages")
|
113 |
state = gr.State()
|
|
|
114 |
msg = gr.Textbox(label="Type your action")
|
115 |
|
116 |
-
# Pre-create all possible buttons
|
117 |
-
all_button_labels = [
|
118 |
-
"Start Game", "Help", "Exit", "Look", "Inventory",
|
119 |
-
"Pick up candle", "Pick up key", "Pick up painting",
|
120 |
-
"door", "trapdoor", "stairs", "painting", "Use candle"
|
121 |
-
]
|
122 |
-
buttons_dict = {}
|
123 |
-
for label in all_button_labels:
|
124 |
-
btn = gr.Button(label)
|
125 |
-
buttons_dict[label] = btn
|
126 |
-
|
127 |
-
# Respond function
|
128 |
def respond(action, history):
|
129 |
history = history or []
|
130 |
-
|
131 |
history.append({"role": "user", "content": action})
|
132 |
-
history.append({"role": "assistant", "content":
|
133 |
-
|
134 |
-
# Update button visibility
|
135 |
-
visible = get_visible_buttons()
|
136 |
-
for lbl, btn in buttons_dict.items():
|
137 |
-
btn.update(visible=(lbl in visible))
|
138 |
-
|
139 |
return history, history
|
140 |
|
141 |
-
# Connect buttons to respond
|
142 |
-
for btn in buttons_dict.values():
|
143 |
-
btn.click(respond, inputs=[btn, state], outputs=[chatbot, state])
|
144 |
-
|
145 |
-
# Manual input
|
146 |
msg.submit(respond, inputs=[msg, state], outputs=[chatbot, state])
|
147 |
|
148 |
demo.launch()
|
|
|
27 |
🛠 **Help Menu** 🛠
|
28 |
|
29 |
- Look around: See your surroundings.
|
30 |
+
- Pick up [item]: Take an item.
|
31 |
- Inventory: Check items you hold.
|
32 |
+
- Use [item]: Use an item in your inventory.
|
33 |
+
- Move [exit]: Move between rooms.
|
34 |
- Exit: Quit the game.
|
35 |
"""
|
36 |
|
|
|
39 |
global current_room, inventory, game_started
|
40 |
room = rooms.get(current_room)
|
41 |
|
42 |
+
action = action.strip()
|
43 |
+
|
44 |
if not game_started:
|
45 |
+
if action.lower() == "start game":
|
46 |
game_started = True
|
47 |
current_room = "dark_room"
|
48 |
inventory.clear()
|
49 |
return f"🎮 Game started!\n{rooms[current_room]['description']}"
|
50 |
+
elif action.lower() == "help":
|
51 |
return help_text
|
52 |
+
elif action.lower() == "exit":
|
53 |
return "Goodbye! Thanks for playing."
|
54 |
+
return "Type 'Start Game' to begin, 'Help' for instructions, or 'Exit' to quit."
|
55 |
|
56 |
+
if action.lower() == "help":
|
57 |
return help_text
|
58 |
|
59 |
+
if action.lower() == "look":
|
60 |
if current_room == "dark_room" and not room["puzzles"]["candle_lit"]:
|
61 |
return "It’s too dark to see much. You might need to light the candle first."
|
62 |
items = ", ".join(room["items"]) if room["items"] else "nothing"
|
63 |
return f"{room['description']} You see: {items}."
|
64 |
|
65 |
+
if action.lower().startswith("pick up "):
|
66 |
+
item = action[8:].strip()
|
67 |
if item in room["items"]:
|
68 |
inventory.append(item)
|
69 |
room["items"].remove(item)
|
70 |
return f"You picked up the {item}."
|
71 |
return f"No {item} here to pick up."
|
72 |
|
73 |
+
if action.lower().startswith("use "):
|
74 |
+
item = action[4:].strip()
|
75 |
if item in inventory:
|
76 |
if item == "candle":
|
77 |
room["puzzles"]["candle_lit"] = True
|
|
|
79 |
return f"You used the {item}, but nothing happened."
|
80 |
return f"You don't have a {item}."
|
81 |
|
82 |
+
if action.lower() in [exit.lower() for exit in room["exits"]]:
|
83 |
+
exit_name = next(exit for exit in room["exits"] if exit.lower() == action.lower())
|
84 |
+
if room["door_locked"] and exit_name == "door":
|
85 |
if "key" in inventory:
|
86 |
room["door_locked"] = False
|
87 |
current_room = "hallway"
|
88 |
return f"You unlock the door with the key and enter the hallway."
|
89 |
return "The door is locked. You need a key."
|
90 |
+
current_room = exit_name
|
91 |
return f"You move into {current_room}. {rooms[current_room]['description']}"
|
92 |
|
93 |
+
if action.lower() == "inventory":
|
94 |
return f"You have: {', '.join(inventory) if inventory else 'nothing'}"
|
95 |
|
96 |
+
if action.lower() == "exit":
|
97 |
return "Thanks for playing! Goodbye!"
|
98 |
|
99 |
+
return "I don't understand that action. Type 'Help' for instructions."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
100 |
|
101 |
# --- Gradio UI ---
|
102 |
with gr.Blocks() as demo:
|
103 |
+
gr.Markdown("# 🏰 Escape Room Adventure (Text Only)")
|
104 |
|
105 |
chatbot = gr.Chatbot(type="messages")
|
106 |
state = gr.State()
|
107 |
+
|
108 |
msg = gr.Textbox(label="Type your action")
|
109 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
110 |
def respond(action, history):
|
111 |
history = history or []
|
112 |
+
reply = game_logic(action)
|
113 |
history.append({"role": "user", "content": action})
|
114 |
+
history.append({"role": "assistant", "content": reply})
|
|
|
|
|
|
|
|
|
|
|
|
|
115 |
return history, history
|
116 |
|
|
|
|
|
|
|
|
|
|
|
117 |
msg.submit(respond, inputs=[msg, state], outputs=[chatbot, state])
|
118 |
|
119 |
demo.launch()
|