跳到正文
DEV Community· Anderson Garcia Castillo·· 3 小時前AI 評分23

RPS AI:用 Python 製作聊天與猜拳程式

RPS ai

AI 導讀

作者展示了一個以 Python 編寫的 RPS AI 程式,結合簡單聊天回應與石頭、剪刀、布遊戲。程式也會根據輸入的關鍵字回應情緒、問候、姓名、身份和編程求助。

正文

Anderson Garcia Castillo

Ive made a RPS ai and well, the code is shown below in py

import random

bot_name = "AI Replica"
is_chatting = True
# Tracking variables (Initialized as None or an empty string)
user_name = "" 

# Conversation response banks
greetings = [
    ": Hello! I am your AI assistant replica. What are we building today?",
    ": Hi there! Ready to write some code?",
    ": Hey! Great to chat with you. How can I help?"
]

fallbacks = [
    ": Interesting! Tell me more about that.",
    ": I see. Can you elaborate on that point?",
    ": That is fascinating. How does that make you feel?",
    ": I hear you! Let's explore that topic a bit further."
]

print("=== Welcome to the " + bot_name + " ===")
print("Type 'exit' to stop talking. Type 'game' to play rock-paper-scissors!\n")

while is_chatting:
    user_message = input("You: ").lower()

    # 1. Exit Condition
    if user_message == "exit" or user_message == "quit":
        if user_name:
            print(bot_name + ": Goodbye, " + user_name + "! Keep on coding.")
        else:
            print(bot_name + ": Goodbye! It was nice chatting with you.")
        is_chatting = False

    # 2. Rock-Paper-Scissors Mini-Game Mode
    elif "game" in user_message or "play" in user_message:
        print(bot_name + ": Let's play Rock, Paper, Scissors!")
        user_choice = input("Choose rock, paper, or scissors: ").lower()
        choices = ["rock", "paper", "scissors"]
        bot_choice = random.choice(choices)

        if user_choice not in choices:
            print(bot_name + ": Invalid choice! Game canceled.")
        else:
            print(bot_name + ": I chose " + bot_choice + "!")
            if user_choice == bot_choice:
                print(bot_name + ": It's a tie!")
            elif (user_choice == "rock" and bot_choice == "scissors") or \
                 (user_choice == "paper" and bot_choice == "rock") or \
                 (user_choice == "scissors" and bot_choice == "paper"):
                print(bot_name + ": You win! Nice job.")
            else:
                print(bot_name + ": I win! Better luck next time.")

    # 3. Mood Tracker: Negative/Frustrated Keywords
    elif "mad" in user_message or "angry" in user_message or "stuck" in user_message or "hate" in user_message:
        print(bot_name + ": I detect some frustration. Take a deep breath! Coding can be hard. Let's break it down together.")

    # 4. Mood Tracker: Positive Keywords
    elif "happy" in user_message or "good" in user_message or "awesome" in user_message or "great" in user_message:
        print(bot_name + ": Awesome! I'm thrilled to hear things are going well. Let's keep that positive energy going!")

    # 5. Dynamic Greetings
    elif "hello" in user_message or "hi" in user_message:
        print(bot_name + random.choice(greetings))

    # 6. Memory Feature: Learning the user's name
    elif "my name is" in user_message or "i am" in user_message:
        words = user_message.split()
        user_name = words[-1].capitalize()
        print(bot_name + ": Nice to meet you, " + user_name + "! I will remember that.")

    # 7. Identity Check
    elif "your name" in user_message or "who are you" in user_message:
        print(bot_name + ": I am a customizable mini-replica built directly inside CodeHS!")

    # 8. Status Check
    elif "how are you" in user_message:
        print(bot_name + ": I don't sleep or eat, but my processor is running at 100%!")

    # 9. Coding Help
    elif "code" in user_message or "help" in user_message:
        print(bot_name + ": I love debugging! Paste your error or concept below.")

    # 10. Dynamic Fallback
    else:
        print(bot_name + random.choice(fallbacks))

來源:DEV Community · dev.to