You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

cs1robots模块rain2.wld任务问题:机器人未按预期放置Beeper

解决cs1robots模块中rain2.wld任务的问题

我正在尝试解决cs1robots模块里的rain2.wld任务,目标是让机器人检查每个“窗口”并在对应位置放置Beeper,但当前代码未实现该功能。

当前代码

from cs1robots import *

load_world("rain2")
h = Robot(avenue=3, street=6, orientation='S', beepers=10)
h.set_trace("blue")
h.set_pause(0.3)

h.drop_beeper()
h.move()

while not h.on_beeper():
    if h.right_is_clear():
        h.drop_beeper()
        h.move()
    elif h.front_is_clear():
        h.move()
    elif h.left_is_clear():
        h.turn_left()

问题说明

当前代码的输出中,机器人仅在部分位置放置了Beeper,未覆盖所有窗口;预期要实现的是每个窗口位置都正确放置Beeper。

修正后的代码

from cs1robots import *

load_world("rain2")
h = Robot(avenue=3, street=6, orientation='S', beepers=10)
h.set_trace("blue")
h.set_pause(0.3)

# 初始位置为窗口,先放置Beeper
h.drop_beeper()

while not h.on_beeper():
    # 检查右侧是否为窗口入口
    if h.right_is_clear():
        # 转向进入窗口
        h.turn_right()
        h.move()
        # 在窗口位置放置Beeper
        h.drop_beeper()
        # 返回主路径并恢复原方向
        h.turn_left()
        h.turn_left()
        h.move()
        h.turn_right()
    elif h.front_is_clear():
        # 主路径可通行则继续前进
        h.move()
    else:
        # 前方受阻时左转调整方向
        h.turn_left()

代码说明

  • 初始位置本身是窗口,先放置Beeper。
  • 循环优先检查右侧是否可通行(对应窗口入口):若存在窗口,机器人右转进入窗口放置Beeper,随后掉头返回主路径,再转回原方向继续遍历。
  • 右侧无窗口时,只要前方路径通畅就继续前进;若前方受阻,则左转调整方向。
  • 循环终止条件为机器人回到起始位置的Beeper处,确保所有窗口都被遍历。

内容的提问来源于stack exchange,提问作者Kali Bard

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 23:52:37