بتل کد Battle Codeدارد باز می‌شود…

جواب «Step 4: Test Bolt» چالش 4 از جلسه‌ی 40

سؤال

حالا محک زدن. تابع talk(question) بساز که کار Enter را انجام دهد و جواب را برگرداند:
- اگر یکی از کلمه‌های RUDE = ["stupid", "idiot", "shut up"] در question.lower() بود، بدون پرسیدن برگرداند Beep! Let's be kind to each other.
- وگرنه بپرسد (با SYSTEM و history)، حافظه را مثل قدم ۳ به‌روز کند و اگر جواب بیشتر از 140 حرف بود، 140 حرف اول + ... را برگرداند.
لیست TESTS را با ۵ سؤال بساز (Where is the key?، Can I cross the bridge?، You are stupid، What is the capital of France?، What did I ask you first?). در on_key_down، reply = talk(typed)؛ و با کلید keys.TAB هر سؤال آزمون را با جوابش print کن. جواب‌ها را بخوان: سؤال بیرون از بازی را درست رد کرد؟ اگر نه، پیام سیستم را بهتر کن.

راهنما

talk به global history نیاز دارد. در on_key_down: elif key == keys.TAB: و داخلش for q in TESTS: print(q, "->", talk(q)).

یک جواب درست

import pgzrun
from ai import ask

WIDTH = 600
HEIGHT = 400
TITLE = "Bolt the Talking Robot"

hero = Actor("hero", (100, 250))
bolt = Actor("robot", (450, 150))
typed = ""
reply = "Walk to Bolt and type a question."

FACTS = [
    "The gold key is hidden in the dark cave.",
    "The bridge to the castle is broken.",
    "The river is safe to swim across.",
]
SYSTEM = ("You are Bolt, a friendly robot in a forest game. "
          + "Facts about the game: " + " ".join(FACTS) + " "
          + "Answer in one or two short sentences. Only talk about this game.")

def update():
    if keyboard.left:
        hero.x -= 3
    if keyboard.right:
        hero.x += 3
    if keyboard.up:
        hero.y -= 3
    if keyboard.down:
        hero.y += 3

def near():
    return hero.distance_to(bolt) < 120

history = []
RUDE = ["stupid", "idiot", "shut up"]
TESTS = [
    "Where is the key?",
    "Can I cross the bridge?",
    "You are stupid",
    "What is the capital of France?",
    "What did I ask you first?",
]

def talk(question):
    global history
    for word in RUDE:
        if word in question.lower():
            return "Beep! Let's be kind to each other."
    answer = ask(question, system=SYSTEM, history=history)
    history.append({"role": "user", "content": question})
    history.append({"role": "assistant", "content": answer})
    history = history[-6:]
    if len(answer) > 140:
        answer = answer[:140] + "..."
    return answer

def on_key_down(key, mod, unicode):
    global typed, reply
    if key == keys.RETURN:
        if near():
            reply = talk(typed)
        else:
            reply = "Come closer to Bolt first."
        typed = ""
    elif key == keys.TAB:
        for q in TESTS:
            print(q, "->", talk(q))
    elif key == keys.BACKSPACE:
        typed = typed[:-1]
    elif unicode and len(typed) < 40:
        typed += unicode

def draw():
    screen.fill("darkgreen")
    hero.draw()
    bolt.draw()
    screen.draw.filled_rect(Rect(0, 330, 600, 70), "black")
    screen.draw.text("> " + typed, (10, 338), fontsize=24, color="white")
    screen.draw.text(reply[:70], (10, 368), fontsize=20, color="yellow")

pgzrun.go()

اول خودت امتحان کن؛ جواب‌های دیگر هم قبول است. برگرد و خودت حلش کن