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

如何验证文本游戏房间移动输入与物品栏操作并修复异常?

蝙蝠侠文本游戏问题修复方案

问题概述

正在制作班级用的蝙蝠侠主题文本游戏,已实现房间移动、物品拾取/移除功能,但存在两个技术问题:

  1. 房间移动无有效验证:初始房间Spawn Room仅South为有效方向,输入North时触发KeyError而非错误提示;
  2. 物品栏逻辑异常:未输入「Set Down」时,仍会触发后续物品丢弃的判断语句。

问题1:房间移动KeyError修复

问题原因

当输入当前房间不存在的方向时,对应的moveroom函数直接尝试访问rooms[currentroom][direction],但该键不存在,导致KeyError。同时原代码未先验证输入方向是否在当前房间的可用移动列表中。

修复方式

  • 移除冗余的moveroom1~moveroom4函数,用统一逻辑处理移动;
  • 输入方向后先判断是否在当前房间的可用移动集合中,合法才执行移动,否则提示错误。

问题2:物品栏逻辑异常修复

问题原因

原代码条件判断逻辑错误:if itemdrop != 'Set Down' or 'set down': 等价于(itemdrop != 'Set Down') or True,永远为真,导致无论输入什么都会执行该分支;后续的if itemdrop == 'Set Down' or 'set down':也存在同样逻辑问题,且未做物品存在性检查,可能触发移除失败的报错。

修复方式

  • 将输入的itemdrop转换为小写,简化判断逻辑;
  • 用if-else结构避免多分支同时触发;
  • 移除物品前先检查是否在背包中,避免报错。

修改后的完整代码

import sys


def show_instructions():
    # 显示主菜单和命令说明
    print("Batman Text Based Game")
    print("谜语人绑架了蝙蝠侠,你必须解开谜题才能击败他。")
    print("移动命令:South, North, East, West, Confirm, Inventory, exit")

# 房间结构:键为房间名,值为可移动方向或物品
rooms = {
    'Spawn Room': {'South': 'top of the Hallway'},
    'top of the Hallway': {'East': 'Riddle One', 'West': 'Riddle Two', 'South': 'Hallway Two'},
    'Hallway Two': {'East': 'Riddle Three', 'West': 'Riddle Four', 'North': 'top of the Hallway', 'South': 'Hallway Three'},
    'Hallway Three': {'East': 'Riddle Five', 'West': 'Riddle Six', 'North': 'Hallway Two', 'South': 'Hallway Bottom'},
    'Hallway Bottom': {'East': 'Riddle Seven', 'West': 'Riddle Eight', 'North': 'Hallway Three', 'South': 'Boss Room'},
    'Riddle One': {'West': 'top of the Hallway'},
    'Riddle Two': {'East': 'top of the Hallway'},
    'Riddle Three': {'West': 'Hallway Two'},
    'Riddle Four': {'East': 'Hallway Two'},
    'Riddle Five': {'West': 'Hallway Three'},
    'Riddle Six': {'East': 'Hallway Three'},
    'Riddle Seven': {'West': 'Hallway Bottom'},
    'Riddle Eight': {'East': 'Hallway Bottom'},
    'Boss Room': {'item': 'The Riddler'}  # 反派房间
}

show_instructions()  # 显示游戏介绍和操作说明
print()

current_room = 'Spawn Room'  # 设置初始房间
inventory = []  # 玩家背包
direction = ''  # 移动输入

while direction != 'exit':
    possible_moves = rooms[current_room].keys()
    print('可用移动方向:', *possible_moves)
    print()

    # 获取玩家输入并处理
    direction = input('请输入移动方向:').strip()
    print('你输入了:', direction)
    print()

    # 处理移动逻辑
    if direction in ['South', 'North', 'East', 'West']:
        if direction in possible_moves:
            print(f'蝙蝠侠离开{current_room}')
            current_room = rooms[current_room][direction]
            print(f'你现在在{current_room}')
        else:
            print('无效的方向!请输入当前房间可用的方向。')
    elif direction == 'Inventory':
        # 处理背包逻辑
        print('当前背包物品:', inventory)
        item_drop = input('输入Set Down可丢弃物品,输入其他内容退出背包:').strip()
        if item_drop.lower() == 'set down':
            drop_item = input('请输入要丢弃的物品(如R1):').strip()
            if drop_item in inventory:
                inventory.remove(drop_item)
                print(f'你丢弃了{drop_item}')
            else:
                print('背包中没有这个物品!')
        else:
            print('退出背包')
    elif direction == 'Confirm':
        # 可添加确认逻辑,比如验证谜题答案
        print('确认操作')
    elif direction == 'exit':
        print('你退出了游戏')
        sys.exit()
    else:
        print('无效命令!请输入正确的命令。')

    # 处理谜题房间物品拾取
    if current_room.startswith('Riddle'):
        riddle_item = current_room[-1]  # 获取Riddle后的数字
        item_key = f'R{riddle_item}'
        if item_key not in inventory:
            print(f'你解开了谜题,获得{item_key}')
            inventory.append(item_key)

    # 处理Boss房间逻辑
    if current_room == 'Boss Room':
        # 示例:假设需要R2、R3、R6才能胜利
        required_items = ['R2', 'R3', 'R6']
        if all(item in inventory for item in required_items):
            print('蝙蝠侠用正确的谜题答案击败了谜语人!你赢了!')
        else:
            print('蝙蝠侠用暴力解决了谜语人,违反规则,你输了!')
        sys.exit()

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.01 08:45:46