|
|
import gradio as gr |
|
|
import random |
|
|
|
|
|
|
|
|
emojis = ["π", "π", "π", "π", "π€", "π", "π", "π", "β€οΈ", "π"] |
|
|
|
|
|
|
|
|
game_state = { |
|
|
"location": "start", |
|
|
"inventory": [], |
|
|
"game_started": False |
|
|
} |
|
|
|
|
|
|
|
|
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_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 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." |
|
|
|
|
|
|
|
|
def chat(message, history, emoji): |
|
|
history = history or [] |
|
|
|
|
|
history.append((message, "")) |
|
|
|
|
|
|
|
|
bot_message = get_bot_response(message, emoji) |
|
|
|
|
|
|
|
|
history[-1] = (message, bot_message) |
|
|
|
|
|
return history, "" |
|
|
|
|
|
|
|
|
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)] |
|
|
|
|
|
|
|
|
def add_emoji(emoji, current_input): |
|
|
return current_input + emoji |
|
|
|
|
|
|
|
|
def copy_last_message(history): |
|
|
if history and history[-1][1]: |
|
|
return history[-1][1] |
|
|
return "No message to copy." |
|
|
|
|
|
|
|
|
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") |
|
|
|
|
|
|
|
|
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]) |
|
|
|
|
|
|
|
|
demo.launch() |