CCRss commited on
Commit
5702df1
·
verified ·
1 Parent(s): 6bc94d7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -63
app.py CHANGED
@@ -1,76 +1,58 @@
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()
 
1
  import gradio as gr
2
 
3
+ # Initialize game state
4
+ game_state = {"step": 0}
 
 
 
 
5
 
6
+ # Game logic
7
+ def play_game(user_input):
8
+ step = game_state["step"]
9
+ user_input = user_input.strip().lower()
 
10
 
11
+ if step == 0:
12
+ game_state["step"] = 1
13
+ return "You are in a dark forest. Do you go 'left' or 'right'?"
14
+
15
+ elif step == 1:
16
+ if user_input == "left":
17
+ game_state["step"] = 2
18
+ return "You see a river. Do you 'swim' across or 'build a raft'?"
19
+ elif user_input == "right":
20
+ game_state["step"] = 3
21
+ return "You meet a wizard. Do you 'talk' to him or 'run' away?"
 
22
  else:
23
+ return "Please type 'left' or 'right'."
24
+
25
+ elif step == 2:
26
+ if user_input == "swim":
27
+ game_state["step"] = 4
28
+ return "You tried to swim and got swept away! Game Over. Type anything to restart."
29
+ elif user_input == "build a raft":
30
+ game_state["step"] = 4
31
+ return "You safely crossed the river. You win! 🏆 Type anything to restart."
 
 
 
 
32
  else:
33
+ return "Please choose 'swim' or 'build a raft'."
34
+
35
+ elif step == 3:
36
+ if user_input == "talk":
37
+ game_state["step"] = 4
38
+ return "The wizard gives you a treasure. You win! 🧙‍♂️ Type anything to restart."
39
+ elif user_input == "run":
40
+ game_state["step"] = 4
41
+ return "The wizard curses you! Game Over. Type anything to restart."
42
  else:
43
+ return "Please choose 'talk' or 'run'."
44
 
 
 
 
 
45
  else:
46
+ game_state["step"] = 0
47
+ return "The game is restarted. Do you want to go 'left' or 'right'?"
 
 
48
 
49
+ # Gradio interface
50
  iface = gr.Interface(
51
+ fn=play_game,
52
+ inputs=gr.Textbox(placeholder="Type your choice here..."),
53
+ outputs="text",
54
+ title="🕹️ Simple Text Adventure Game",
55
+ description="Make your choices and see where the story goes!"
 
 
 
 
56
  )
57
 
58
+ iface.launch()