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

Python文本游戏胜利条件触发失效问题求助

问题修复方案

核心问题梳理

  1. 胜利条件判断逻辑错误

    • 房间名称笔误:Theater被写成了Thearter,导致无法正确识别最终房间。
    • 房间判断错误:你试图用current_room['name'] == 'Warlock'判断是否进入最终房间,但Warlock是Theater房间的物品字段值,不是房间名称。
    • 物品数量判断错误:用字典Inventory和整数6比较,字典类型永远不可能等于整数,且这个字典完全多余,直接用inventory列表的长度即可判断是否收集满6件物品。
  2. 其他代码问题

    • 结束指令错误:command in quit 是错误写法,应改为command == 'end',和游戏提示的结束指令一致。
    • 冗余代码:move函数未被使用且逻辑错误,Inventory字典和add_to函数完全多余,且add_to里的object变量未定义,会触发报错。

修复后的完整代码

def game():
    answer = input('Would you like to help Galatrix save Halloween Town?(y/n)')
    if answer.lower() == 'y':
        print('Great! Welcome to Halloween Town!')
        instructions()
    else:
        print('Too bad they really need your help.')
        exit()

# define rules
def instructions():
    print('Galatrix and her mentor have been noticing some strange goings on in their home of Halloween Town.')
    print('Help Galatrix find all 6 items she needs to finish the potion to charge the Ancient wand and defeat the Warlock.')
    print('Collect 6 items around Halloween Town. Then head to the theater to face off with the Warlock.')
    print('Move commands: North, South, East, West')
    print("Add objects to inventory: type 'get'.")
    print("To end the game at any time, just type 'end'.")
    print('*****************************')

# 定义房间,修正Theater的名称拼写
rooms = {
    'Town Square': {'name': 'Town Square> Home of the Jack-o-lantern.', 'North': 'Dentist', 'South': 'General Store', 'East': 'Rec Center', 'West': 'Galatrix House', 'object': '0'},
    'Galatrix House': {'name': 'Galatrix House', 'East': 'Town Square', 'object': 'Cloak'},
    'Dentist': {'name': 'Dentist', 'South': 'Town Square', 'East': 'Hair Salon', 'object': 'Vampire tooth'},
    'Hair Salon': {'name': 'Hair Salon', "West": 'Dentist', 'object': 'Werewolf hair'},
    'General Store': {'name': "General Store", 'North': 'Town Square', 'East': 'Mentor House', 'object': 'Cauldron'},
    'Mentor House': {'name': 'Mentor House', 'West': 'General Store', 'object': 'Ancient Wand'},
    'Rec Center': {'name': 'Rec Center', 'North': 'Theater', 'West': 'Town Square', 'object': 'Ghost sweat'},
    'Theater': {'name': 'Theater', 'South': 'Rec Center', 'object': 'Warlock'}
}
current_room = rooms['Town Square']
inventory = []
directions = {'North', 'South', 'East', 'West'}

game()
while True:
    # 修正胜利条件判断:当前房间是Theater,且物品栏有6件物品
    if current_room['name'] == 'Theater' and len(inventory) == 6:
        print('CONGRATULATIONS!!! YOU SAVED HALLOWEEN TOWN!!!!!')
        exit('The End')
    elif len(inventory) != 6 and current_room['name'] == 'Theater':
        print('Keep searching. You need all 6 items to defeat the Warlock!')

    print('You are in the {}.'.format(current_room['name']))
    print('Inventory: {}'.format(inventory))
    command = input('\nWhat do you do?').strip()
    
    if command in directions:
        if command in current_room:
            current_room = rooms[current_room[command]]
            # 只有当前房间物品不是0和Warlock时才提示看到的物品
            if current_room['object'] != '0' and current_room['object'] != 'Warlock':
                print('You see the {}.'.format(current_room['object']))
        else:
            print("You can't go that way.")
    elif command == 'get':
        if current_room['object'] != '0' and current_room['object'] != 'Warlock':
            print('You grabbed the {}.'.format(current_room['object']))
            inventory.append(current_room['object'])
            current_room['object'] = '0'
        else:
            print("I don't see anything here.")
    elif command == 'end':
        print('Thanks for playing!')
        break
    else:
        print('Invalid command. Try again.')

关键修复说明

  1. 修正了Theater的名称拼写错误,确保房间判断准确。
  2. 胜利条件改为current_room['name'] == 'Theater' and len(inventory) ==6,直接通过列表长度判断物品数量。
  3. 删除了冗余的move函数、Inventory字典和add_to函数,简化逻辑。
  4. 修正了结束游戏的指令判断,改为command == 'end',和游戏提示一致。
  5. 优化了物品提示逻辑,进入Theater时不会提示看到Warlock,避免混淆。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.16 21:40:27