Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,8 +1,76 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
|
|
|
|
| 7 |
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
|
| 3 |
+
# Game state and logic
|
| 4 |
+
class GameState:
|
| 5 |
+
def __init__(self):
|
| 6 |
+
self.location = "start"
|
| 7 |
+
self.inventory = []
|
| 8 |
+
self.game_over = False
|
| 9 |
|
| 10 |
+
def reset(self):
|
| 11 |
+
self.__init__()
|
| 12 |
|
| 13 |
+
def process_command(command, state):
|
| 14 |
+
command = command.lower().strip()
|
| 15 |
+
|
| 16 |
+
if state.game_over:
|
| 17 |
+
if command == "restart":
|
| 18 |
+
state.reset()
|
| 19 |
+
return "Game restarted. You find yourself at the entrance of a dark cave. What do you do?"
|
| 20 |
+
return "Game Over. Type 'restart' to play again."
|
| 21 |
+
|
| 22 |
+
if state.location == "start":
|
| 23 |
+
if "enter" in command or "go" in command:
|
| 24 |
+
state.location = "cave"
|
| 25 |
+
return "You enter the dark cave. You see a glowing crystal on one side and a narrow passage on the other."
|
| 26 |
+
elif "look" in command:
|
| 27 |
+
return "You're at the entrance of a mysterious cave. The entrance looks dark but inviting."
|
| 28 |
+
else:
|
| 29 |
+
return "You can 'enter' the cave or 'look' around."
|
| 30 |
+
|
| 31 |
+
elif state.location == "cave":
|
| 32 |
+
if "crystal" in command or "take crystal" in command:
|
| 33 |
+
if "crystal" not in state.inventory:
|
| 34 |
+
state.inventory.append("crystal")
|
| 35 |
+
return "You pick up the glowing crystal. It illuminates the cave with a soft blue light."
|
| 36 |
+
return "You already have the crystal."
|
| 37 |
+
elif "passage" in command:
|
| 38 |
+
if "crystal" in state.inventory:
|
| 39 |
+
state.location = "treasure"
|
| 40 |
+
return "The crystal lights your way through the passage. You find a treasure chest!"
|
| 41 |
+
return "It's too dark to go through the passage without a light source."
|
| 42 |
+
else:
|
| 43 |
+
return "You can 'take crystal' or go through the 'passage'."
|
| 44 |
+
|
| 45 |
+
elif state.location == "treasure":
|
| 46 |
+
if "open" in command or "chest" in command:
|
| 47 |
+
state.game_over = True
|
| 48 |
+
return "Congratulations! You found the treasure! Game Over. Type 'restart' to play again."
|
| 49 |
+
else:
|
| 50 |
+
return "You can 'open' the chest."
|
| 51 |
+
|
| 52 |
+
def game_interface(command, history):
|
| 53 |
+
if history is None:
|
| 54 |
+
state = GameState()
|
| 55 |
+
response = "Welcome! You find yourself at the entrance of a dark cave. What do you do?"
|
| 56 |
+
else:
|
| 57 |
+
state = history
|
| 58 |
+
response = process_command(command, state)
|
| 59 |
+
|
| 60 |
+
return response, state
|
| 61 |
+
|
| 62 |
+
# Create Gradio interface
|
| 63 |
+
iface = gr.Interface(
|
| 64 |
+
fn=game_interface,
|
| 65 |
+
inputs=[
|
| 66 |
+
gr.Textbox(label="Enter your command"),
|
| 67 |
+
gr.State()
|
| 68 |
+
],
|
| 69 |
+
outputs=gr.Textbox(label="Game Output"),
|
| 70 |
+
title="Simple Text Adventure",
|
| 71 |
+
description="A simple cave exploration game. Try commands like 'look', 'enter', 'take crystal', etc.",
|
| 72 |
+
examples=[["look"], ["enter"], ["take crystal"], ["passage"], ["open chest"]]
|
| 73 |
+
)
|
| 74 |
+
|
| 75 |
+
if __name__ == "__main__":
|
| 76 |
+
iface.launch()
|