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

文本类游戏输出不符合预期:房间识别错误且无退出条件

问题修复:房间导航代码的错误修正

问题诊断

  • 房间识别逻辑错误:每次循环未更新当前房间的可用移动列表,导致判断始终基于初始房间(Entrance)的移动选项
  • 缺少退出机制:用户无法主动终止程序
  • 具体异常表现:
    • 从South Hall输入East时,系统错误跳转至Locker Room(因一直沿用Entrance的East指向关系)
    • 从South Hall输入North时提示无效移动(因用初始房间的移动列表判断,Entrance无North选项)

修复后的代码

rooms = {
        'Entrance': {'East': 'Locker Room', 'West': 'South Hall'},
        'Locker Room': {'West': 'Entrance'},
        'South Hall': {'North': 'Main Room', 'East': 'Entrance'},
        'Main Room': {'East': 'Power Grid', 'South': 'South Hall'},
        'Power Grid': {'West': 'Main Room', 'East': 'Restroom', 'North': 'North Hall'},
        'North Hall': {'South': 'Power Grid', 'West': 'Control Room', 'East': 'Loading Bay'},
        'Control Room': {'East': 'North Hall'},
        'Loading Bay': {'West': 'North Hall'}
    }

room = 'Entrance'

print('你必须赶紧去救你的朋友!')
print(f'你现在在 {room}\n你想去哪里?(输入"quit"可退出)')

while True:
    move = input().strip()
    # 处理退出逻辑
    if move.lower() == 'quit':
        print('退出游戏')
        break
    # 获取当前房间的可用移动选项
    possible_moves = rooms[room]
    if move in possible_moves:
        room = possible_moves[move]
        print(f'你现在在 {room}\n你想去哪里?(输入"quit"可退出)')
    else:
        print(f'从{room}无法进行{move}方向的移动,请重新输入')

修复说明

  1. 修复房间识别错误:将possible_moves = rooms[room]移至循环内部,每次循环都根据当前所在房间更新可用移动选项,确保判断逻辑匹配当前位置
  2. 添加退出条件:增加对quit指令的判断,用户输入后可直接退出程序
  3. 优化交互体验:在提示文本中加入退出说明,同时优化提示内容的可读性
  4. 输入容错处理:使用strip()去除输入的前后空格,避免因误输入空格导致的无效判断

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.05 07:46:26