File size: 7,318 Bytes
5aa8708
 
 
 
 
 
2c16c22
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
215a46b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ca0c79b
2c16c22
 
ca0c79b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2c16c22
 
 
215a46b
2c16c22
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5aa8708
 
ca0c79b
5aa8708
 
ae92619
5aa8708
 
ca0c79b
5aa8708
 
ca0c79b
5aa8708
ca0c79b
5aa8708
215a46b
 
 
 
 
 
 
 
 
5aa8708
 
 
 
 
 
ae92619
 
5aa8708
 
 
 
 
 
 
ae92619
5aa8708
ae92619
ca0c79b
5aa8708
 
 
 
215a46b
5aa8708
 
 
 
 
ca0c79b
 
ae92619
215a46b
5aa8708
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
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()