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

Python房屋文本游戏房间移动问题求助:仅可南北移动

问题描述

当前Python文本游戏仅支持南北方向移动,其他方向无法识别,需要实现所有房间的自由往返。涉及房间及移动规则如下:

  • 阁楼(起始点):向南到客厅
  • 客厅:向西到餐厅、向北回阁楼、向东到门厅
  • 门厅:向西到客厅、向北到卧室、向南到前廊
  • 卧室:向南到门厅
  • 前廊:向北到门厅
  • 餐厅:向东到客厅、向北到厨房、向南到地下室
  • 厨房:向南到餐厅
  • 地下室:向北到餐厅

原代码如下:

rooms = {
'Attic': {'South': ' Living Room'},

'Living Room': {'East': 'Foyer', 'West': 'Dining Room','North': 'Attic'},
      
'Foyer': {'North': 'Bedroom', 'South': 'Front Porch', 'West': 'Living Room'},
     
'Bedroom': {'South': 'Foyer'},
    
'Dining Room': {'East': 'Living Room', 'North': 'Kitchen', 'South': 'Basement'},
     
'Kitchen': {'South': 'Dining Room'},
     
'Basement': {'North': 'Dining Room'},
    
'Front Porch': {'North': 'Foyer'},
    
}

def possible_moves():
  moves = rooms[location].keys()
  print("Option:", *moves)

location = 'Attic'

Direction = ''

while True:
  print('You are in the', location)

  possible_moves()

  direction = input("Which way would you like to go? ")

  if direction == 'Exit' or Direction == 'exit':

    print('Thank you for playing!')
    break
  elif direction == 'South' or direction == 'south':
      location = 'Living Room'
      print('You are in the', location)

      possible_moves()

      direction = input("Which way would you like to go? ")

      if direction == 'East' or direction == 'east':

        location = 'Foyer'
        print('You are in the', location)
        possible_moves()
        direction = input("Which way would you like to go? ")

  elif direction == 'North' or direction == 'north':

        location = 'Attic'
        print('You are in the', location)
        possible_moves()
        direction = input("Which way would you like to go? ")

  else:
   print('Invalid')
修改后的代码
rooms = {
    'Attic': {'South': 'Living Room'},
    'Living Room': {'East': 'Foyer', 'West': 'Dining Room', 'North': 'Attic'},
    'Foyer': {'North': 'Bedroom', 'South': 'Front Porch', 'West': 'Living Room'},
    'Bedroom': {'South': 'Foyer'},
    'Dining Room': {'East': 'Living Room', 'North': 'Kitchen', 'South': 'Basement'},
    'Kitchen': {'South': 'Dining Room'},
    'Basement': {'North': 'Dining Room'},
    'Front Porch': {'North': 'Foyer'},
}

def possible_moves(current_location):
    moves = rooms[current_location].keys()
    print("可选方向:", *moves)

location = 'Attic'

while True:
    print(f'你现在在:{location}')
    possible_moves(location)
    
    direction = input("你想往哪个方向走?输入Exit退出游戏:").strip()
    
    # 处理退出
    if direction.lower() == 'exit':
        print('感谢游玩!')
        break
    
    # 统一方向格式为首字母大写,匹配字典键
    direction_formatted = direction.capitalize()
    
    # 检查方向是否可用
    if direction_formatted in rooms[location]:
        location = rooms[location][direction_formatted]
    else:
        print('无效的方向,请重新输入!')
关键修改点
  1. 重构移动逻辑:原代码用硬编码的elif逐个判断方向,只能支持有限路径。现在直接通过rooms字典查询当前房间的可用方向,自动获取目标房间,所有规则内的方向都能识别。
  2. 修复输入循环:原代码每次移动后重复调用input和打印,导致流程混乱。现在整个游戏在一个while循环内,每次循环只做一次状态展示、输入判断,逻辑清晰。
  3. 统一大小写处理:将用户输入的方向转为首字母大写(比如south转成South),匹配字典中的键,避免大小写输入错误。
  4. 修正字典错误:原Attic对应的Living Room前有多余空格,改成'Living Room',防止房间名称不匹配。
  5. 修复退出判断:原代码错误使用未赋值的Direction变量,改为判断输入的direction是否为exit(不区分大小写)。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.24 17:54:53