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

文本冒险游戏中进入Villain's Lair房间时if条件语句未触发的问题求助

文本冒险游戏中进入Villain's Lair房间时if条件语句未触发的问题求助

嘿,我一眼就发现问题出在代码执行的顺序上!你现在的逻辑里,检查是否进入Villain's Lair的代码块,居然在处理移动、更新current_room之前就执行了——这就导致当你输入East进入反派房间时,程序还没来得及把current_room更新为Villain's Lair,就已经跳过了胜负判断;等current_room真的更新后,要等到下一次循环才会再检查,但下一次循环又先显示房间状态,然后等着你的下一个输入,自然不会自动触发胜负提示。

具体问题拆解

你当前的循环流程是这样的:

  1. 显示当前房间状态
  2. 获取你的移动指令
  3. 处理「Get Item」操作
  4. 检查是否在Villain's Lair(此时current_room还是上一个房间,因为移动还没处理!)
  5. 处理「Exit」指令
  6. 处理移动,更新current_room

所以当你输入East从Troll Region进入Villain's Lair时,步骤6才会把current_room改成Villain's Lair,但这时候步骤4的检查早就做完了,这次循环里不会触发胜负逻辑;而下一次循环启动时,先显示你在Villain's Lair的状态,然后就等着你输入新指令,不会主动判断胜负。

修复方案:调整代码顺序

我们只需要把「检查是否进入反派房间」的逻辑,移到每次循环的最开头,这样只要current_room变成Villain's Lair,下一次循环启动时就会立刻触发胜负判断,完全符合你的需求。

修改后的完整代码如下(关键调整部分已标注):

def display_instructions():
    # print a main menu and the commands
    print('Welcome to Colossal Cave Adventure Remake')
    print('Move commands: South, North, East, West')
    print('Collect 9 items before encountering the Dragon or be eaten by the Dragon.')
    print('Move commands: South, North, East, West, Exit & Get Item')
    print('To add item to inventory use: Get item')
    
display_instructions()

# The starting room
current_room = 'Cave Entrance'
inventory = []
move = ''
directions = ['North', 'South', 'East', 'West', 'Exit', 'Get Item']
item_key = 'item'
villain = 'Dragon'

# Define room connections
rooms = {
  'Cave Entrance': {'South': 'Troll Region', 'North': 'Giants Room', 'East': 'Magnificent Caverns', 'West': 'Pearl Room' },
  'Giants Room': {'item': 'Golden Eggs', 'East': 'Hall of Mists', 'West': 'Forest Region', 'South': 'Cave Entrance'},
  'Hall of Mists': {'item': 'Diamonds', 'West': 'Giants Room'},
  'Forest Region': {'item': 'Magic Staff', 'South': 'Volcano Region', 'East': 'Giants Room'},
  'Volcano Region': {'item': 'Ring of Doom', 'North': 'Forest Region'},
  'Pearl Room': {'item': 'Magic Pearl', 'East': 'Cave Entrance', 'North': 'Pirates Room'},
  'Pirates Room': {'item': 'Pirates Chest', 'South': 'Pearl Room'},
  'Magnificent Caverns': {'item':'Trident', 'South': 'Plover Room', 'West': 'Cave Entrance'},
  'Plover Room': {'item': 'Emeralds', 'North': 'Magnificent Caverns'},
  'Troll Region': {'item': 'Golden Chain', 'North': 'Cave Entrance', 'East': "Villain's Lair"},
  "Villain's Lair": {'villain': 'Dragon', 'West': 'Troll Region'} # Villain's Lair
}

def user_status():  # indicate room and inventory contents
    print('\n-------------------------')
    print(f'You are in the {current_room}.')
    print(f'Your inventory is {inventory}.')

    if 'item' in rooms[current_room]:
         print(f"In this room you see {rooms[current_room]['item']}.")
    else:
        print("This room is empty.")

    print('-------------------------------')

#While loop for game play
while move != 'Exit':
    # 【关键调整:把胜负判断移到循环最开头】
    if current_room == "Villain's Lair":
         # If You Lose
        if len(inventory) < 9:
            print('You lost the fight with the Dragon.')
            break
        #If You Win
        else:
            print('You won! You beat the Dragon! Congratulations!')
            break

    user_status()

    move = str(input('Make a move, Get item or Exit to quit: \n')).title().strip()

    if move not in directions:
        print('Invalid Input. Try Again!')
        continue

    # Code to get item
    if move == 'Get Item':
        if 'item' in rooms[current_room]:
            inventory.append(rooms[current_room]['item'])
            # Delete item from dictionary
            del rooms[current_room]['item']
        else:
            print("There's no item here.")

    # If you want to exit game
    if move == 'Exit':
        print("You've exited the game. Bye! Thanks for playing!")
        break

    # If Move Is Right Direction
    if move in rooms[current_room]:
        current_room = rooms[current_room][move]
    else:
        # If Wrong Direction
        print("You have a wall in the way. Try again")

修复后的流程

现在循环的流程变成:

  1. 检查是否在Villain's Lair → 是就触发胜负,结束游戏
  2. 显示当前房间状态
  3. 获取用户输入
  4. 处理无效输入、Get Item、Exit
  5. 处理移动,更新current_room
  6. 回到循环开头,再次检查房间

这样当你输入East进入Villain's Lair后,current_room被更新,下一次循环启动时,第一步就会检测到你在反派房间,立刻根据 inventory 数量显示胜负提示,完美解决你的问题!

你可以测试一下原来的输入顺序:South → East,现在一进入Villain's Lair就会直接显示You lost the fight with the Dragon.,完全符合预期。

备注:内容来源于stack exchange,提问作者Dcommand

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.14 17:48:09