Python:无法在特定数组编号之间更改
我想在我的程序上得到一些帮助。这是一个基于文本的游戏,有房间。 我希望玩家在房间之间跳跃,但只有当玩家在某些房间时Python:无法在特定数组编号之间更改,python,arrays,Python,Arrays,我想在我的程序上得到一些帮助。这是一个基于文本的游戏,有房间。 我希望玩家在房间之间跳跃,但只有当玩家在某些房间时 #-------------***********GAME************------------- #This module implements pseudo-random number generators for various distributions. from random import randint import random #--------
#-------------***********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]
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
#---------------------------------------------------------------------
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]
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]
#---------------------------------------------------------------------
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]
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]
我希望代码可以这样说:
示例:如果您进入向南的方向,并且您在大厅中,则进入客厅
任何帮助都将不胜感激 我建议建立一个字典系统,而不仅仅是列表。然后,您可以创建一组房间,并为所有房间提供一个特殊的连接键,该键将存储您可以从那里搬进的房间
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']))
而且它也更容易编辑,而不是重新调整一堆不同的数字,您重新分配的文本值彼此不依赖。欢迎使用SO!你可能想复习你的问题以符合标准,这不是一个简单的问题。你知道字典吗?你可以用字典把所有的房间连接起来,这对你来说可能是一个更具可塑性的系统。我知道这要求很多,但你能帮我多一点吗?我试图实现它,但它没有正常工作。到底出了什么问题?我可以帮忙,但我需要知道出了什么问题。如果我知道你对字典有多熟悉,也会更容易。