Python:不能在特定的arrayNumber之间更改

2024-09-22 10:32:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在我的程序上得到一些帮助。这是一个基于文本的游戏,有房间。 我希望玩家在房间之间跳跃,但只有当玩家在某些房间时

#-------------***********GAME************-------------
#This module implements pseudo-random number generators for various    distributions.
from random import randint
import random

#--------------------------------------------------------------------

def showInstructions():
   #print a main menu and the commands
   print ("---------------------------")
   print ("***Game***")
   print ("---------------------------")
   print ("Commands:")
   print ("'[direction]'")
   print ("'get [item]'")

def showStatus():
    #print the player's current status
    print ("---------------------------")
    #print the current inventory
    print ("Inventory : " , inventory)
    #print an item if there is one
    print("You are in the " , currentRoom)
    '''
    if "item" in rooms[currentRoom]:
        print( "You see a " + rooms[currentRoom]["item"])
    print ("---------------------------")
'''

#---------------------------------------------------------------------

inventory = []

# A list of rooms
rooms = [
        ["Hall","Discription of the Hall", "0", "6", "3",],
        ["Living Room","Discription of the Living Room", "1", "5", "3", "4"],
        ["Kitchen","Discription of the Kitchen", "0", "2"],
        ["Master Bedroom", "Discription of the Master Bedroom", "0"],
        ["Bedroom", "Discription of the Bedroom", "0"],
        ["Bathroom", "Discription of the Bathroom", "1"],
    ]

currentRoom = rooms[0]

#---------------------------------------------------------------------

showInstructions()
showStatus()

lastCommand = []
commandList = []

numberOfRounds = []

inputList = []

cannotDoThat = ["<<< You cannot do that\n", "<<< Cannot be done\n", "<<<    Impossible\n", "<<< I can't do that\n"]
dontUnderstand = ["<<< I didn't quite get that\n", "<<< I don't understand your    command.\n", "<<< Refrase that command.\n", "<<< What do you mean?\n", "<<< Please    refrase that command.\n", "<<< I do not understand that command.\n", "<<< I don't    know what you mean.\n"]
commands = ["command", "commands", "input list", "inputs", "previous input", "last input", "input", "previous commands", "last command", "turn", "rounds", "round", ]

#--------------------------------------------------------------

while True:

print "---------------------------"
inputUser = raw_input(">>> ")


lastCommand = inputUser
inputList.append(lastCommand)

# Rooms

if(currentRoom == [0]):
    print currentRoom[0][1]
    print "<<< You have following options:"
    print "<<< Go EAST and enter the Bathroom, or"
    print "<<< Go SOUTH and enter the Living Room, or"
    print "<<< Go WEST and enter the Kitchen."

if(currentRoom == [1]):
    print currentRoom[1][1]
    print "<<< You have following options:"
    print "<<< Go NORTH and enter the Hall, or"
    print "<<< Go EAST and enter the Bedroom, or"
    print "<<< Go WEST and enter the Master Bedroom."

if(currentRoom == [2]):
    print currentRoom[2][1]
    print "<<< You have following options:"
    print "<<< Go EAST and enter the Hall."

if(currentRoom == [3]):
    print currentRoom[3][1]
    print "<<< You have following options:"
    print "<<< Go EAST and enter the Living."

if(currentRoom == [4]):
    print currentRoom[4][1]
    print "<<< You have following options:"
    print "<<< Go WEST and enter the Hall."

if(currentRoom == [5]):
    print currentRoom[5][1]
    print "<<< You have following options:"
    print "<<< Go WEST and enter the Living Room."

#方向指示 #---------------------------------------------------------------------你知道吗

if(inputUser=="S" or inputUser=="s" or inputUser=="south" or    inputUser=="South" or inputUser=="SOUTH") and (currentRoom==[0]):
    commandList.append(lastCommand)
    currentRoom = [1]
    print "<<< You are now in the " , currentRoom , "\n"

#--------------------------------------------------------------

elif(inputUser=="N" or inputUser=="n" or inputUser=="north" or inputUser=="North" or inputUser=="NORTH") and (currentRoom == [1]):
    commandList.append(lastCommand)   
    currentRoom = [0]
    print("<<< You are now in the " + currentRoom + "\n")

#--------------------------------------------------------------

elif(inputUser=="E" or inputUser=="e" or inputUser=="east" or inputUser=="East" or inputUser=="EAST"):
    commandList.append(lastCommand)
    if(currentRoom == [2]):
        currentRoom = [0]   
    elif(currentRoom == [3]):
        currentRoom = [1]
        print("<<< You are now in the " + currentRoom + "\n")

#--------------------------------------------------------------

elif(inputUser=="w" or inputUser=="W" or inputUser=="west" or inputUser=="West" or inputUser=="WEST"):
    commandList.append(lastCommand)
    if(currentRoom == [4]):
        currentRoom = [0]
    elif(currentRoom == [5]):
        currentRoom = [1]

#--------------------------------------------------------------

#项目和库存

elif(inputUser=="inventory"):
    print "<<< Here is a list of your inventory:"
    print "<<< " , inventory

我在哪里

elif(inputUser=="Where" or inputUser=="where" or inputUser=="where am i" or inputUser=="Where Am I" or inputUser=="Where?" or inputUser=="Where am i?"):
    print "<<< You are in the ", "'",currentRoom,"'"

#命令列表和循环

elif(inputUser=="commands" or inputUser=="command" or inputUser=="command list"):
    print "Here is a list of your previous commands:"
    print commandList


elif(inputUser=="last command" or inputUser=="previous command"):

    print "<<< Here is your last command:"
    print "<<< '" ,  commandList[-1] , "'"


elif(inputUser=="round" or inputUser=="rounds" or inputUser=="turn"):
    if commandList == None:
        print "<<< Here is the number of your actionable entries:"
        print "<<< 0"
    else:
        print "<<< Here is the number of your actionable entries:"
        print "<<< " , len(commandList)


elif(inputUser=="input list" or inputUser=="input"):
    print "<<< Here is the list of all user inputs:"
    print "<<< " , inputList


else:

    print (random.choice(dontUnderstand))

#-------------------------------------

我知道它还不完整,但除非我解决这个问题,否则我就不能走得更远

我认为,问题主要出在代码的这一部分:

# Direction Comamnds

#--------------------------------------------------------------

if(inputUser=="S" or inputUser=="s" or inputUser=="south" or inputUser=="South" or inputUser=="SOUTH") and (currentRoom==[0]):
    commandList.append(lastCommand)
    currentRoom = [1]
    print "<<< You are now in the " , currentRoom , "\n"

#--------------------------------------------------------------

elif(inputUser=="N" or inputUser=="n" or inputUser=="north" or inputUser=="North" or inputUser=="NORTH") and (currentRoom == [1]):
    commandList.append(lastCommand)   
    currentRoom = [0]
    print("<<< You are now in the " + currentRoom + "\n")

#--------------------------------------------------------------

elif(inputUser=="E" or inputUser=="e" or inputUser=="east" or inputUser=="East" or inputUser=="EAST"):
    commandList.append(lastCommand)
    if(currentRoom == [2]):
        currentRoom = [0]   
    elif(currentRoom == [3]):
        currentRoom = [1]
        print("<<< You are now in the " + currentRoom + "\n")

#--------------------------------------------------------------

elif(inputUser=="w" or inputUser=="W" or inputUser=="west" or                               inputUser=="West" or inputUser=="WEST"):
    commandList.append(lastCommand)
    if(currentRoom == [4]):
        currentRoom = [0]
    elif(currentRoom == [5]):
        currentRoom = [1]

#--------------------------------------------------------------

我希望代码可以这样说: 示例:如果您进入南方向,并且您在大厅中,请进入客厅

任何帮助都将不胜感激


Tags: orandoftheyougoifcommand
1条回答
网友
1楼 · 发布于 2024-09-22 10:32:34

我建议建立一个词典系统,而不仅仅是单子。然后,您可以创建一组房间,并为它们提供一个特殊的连接键,该键将存储您可以从那里搬进的房间

bedroom = {'name':'bedroom','decription':'blah'*4}
hall = {'name':'hall','decription':'blah'*4}
bathroom = {'name':'bathroom','decription':'blah'*4}
kitchen = {'name':'kitchen','decription':'blah'*4}
hall['connections'] = {'SOUTH':bedroom, 'NORTH':bathroom, 'EAST':kitchen}
kitchen['connections'] = {'WEST':hall}
bedroom['connections'] = {'NORTH':hall}
bathroom['connections'] = {'SOUTH':hall}

这样,您就可以轻松地检查与给定房间的所有连接

for key in hall['connections']:
    print ("To the {} is the {}".format(key, connection[key]['name']))

它也更容易编辑,而不是重新调整一堆不同的数字,你重新分配文本值,而不是相互依赖

相关问题 更多 >