So, I've been fascinated with Python for a while. About a week or so ago I got a sudden burst of inspiration and started making a text-based RPG engine, using newfound knowledge from a few excellent tutorials. However, I'm having a bit of a problem with the room handler module that I wrote.
Starting room
This is the starting room.
There is an exit to the north.
There is no exit to the south.
There is no exit to the east.
There is an exit to the west.
go n
Second room
This is the other room.
There is no exit to the north.
There is an exit to the south.
There is no exit to the east.
There is no exit to the west.
go s
Second room
This is the other room.
There is no exit to the north.
There is an exit to the south.
There is no exit to the east.
There is no exit to the west.
As you can see, even though I should return to the first room after typing "go s", I'm staying in the second room. As far as I can tell, my command interpreter works fine and there is no fault in the test setup itself. Thus, the problem is probably in my roomhandler.py module. The module itself is clunky and probably larger than it needs to be, but AFAIK it should work as written. I've tried the debugger, but it provides no usable information. Help?
import roomhandler
import interpreter as interpret
# The starting room, used to initialize the others
startroom = roomhandler.rooms("", "",
0, 0, 0, 0,
"", "", "", "")
# Rooms defined using the class found inside the
# roomhandler module. See that module for
# more information.
room2 = roomhandler.rooms("Second room", "This is the other room.",
0, 1, 0, 0,
"", startroom, "", "")
room3 = roomhandler.rooms("Third room", "This is another room.",
0, 0, 1, 0,
"", "", startroom, "")
spawnroom = roomhandler.rooms("Starting room", "This is the starting room.",
1, 0, 0, 1,
room2, "", "", room3)
# Updating attributes since all the rooms are defined now.
room2.connectedS = spawnroom
room3.connectedE = spawnroom
# Describes the room for the first time. Outside the function so that
# it doesn't describe it twice.
spawnroom.describe()
def start():
inp = interpret.interpret(input())
roomhandler.move(spawnroom,inp)
spawnroom.describe()
start()
start()
grabverb = ["grab", "take", "acquire", "pick up"]
moveverb = ["go", "move", "step", "walk", "run", "sprint", "dash"]
north = ["north", "n"]
south = ["south", "s"]
east = ["east", "e"]
west = ["west", "w"]
directions = {"N":1, "S":2, "E":3, "W":4}
def interpret(inputstring):
tointerpret = inputstring.lower()
tointerpret = tointerpret.split()
command1 = tointerpret[0]
command2 = tointerpret[1]
if command1 in moveverb:
if command2 in north:
return directions["N"]
elif command2 in south:
return directions["S"]
elif command2 in east:
return directions["E"]
elif command2 in west:
return directions["W"]
else:
print("You cannot do that!")
interpret(inputstring)
class rooms:
def __init__(self,name,describestring,connectsN,connectsS,connectsE,connectsW,
connectedN,connectedS,connectedE,connectedW):
self.name = name # Name. Self explanatory.
self.describestring = describestring # A description of the room as it will appear to the player.
self.connectsN = connectsN # A variable that determines wether there is an exit to the north or not.
self.connectedN = connectedN # The actual room that is connected to the northern end.
self.connectsS = connectsS # As self.connectsN, but for south.
self.connectedS = connectedS # And so on.
self.connectsE = connectsE
self.connectedE = connectedE
self.connectsW = connectsW
self.connectedW = connectedW
def describe(self): # A function for describing the room and listing all exits.
print("\n" + self.name + "\n")
print(self.describestring + "\n")
if self.connectsN == 1:
print("There is an exit to the north. ") # I forgot how escape characters work, can't be arsed for
elif self.connectsN == 0: # a version that currently doesn't work.
print("There is no exit to the north. ")
if self.connectsS == 1:
print("There is an exit to the south. ")
elif self.connectsS == 0:
print("There is no exit to the south. ")
if self.connectsE == 1:
print("There is an exit to the east. ")
elif self.connectsE == 0:
print("There is no exit to the east. ")
if self.connectsW == 1:
print("There is an exit to the west. ")
elif self.connectsW == 0:
print("There is no exit to the west. \n")
def move(room,direction): # A function that "moves" the player by replacing all attributes with those of the new room.
if direction == 1: # As far as I can tell, the source of the problem.
if room.connectsN == 1:
newroom = room.connectedN
# For example, replacing the name of the current room (room.name) with
# that of the new one:
room.name = newroom.name
room.describestring = newroom.describestring
room.connectsN = newroom.connectsN
room.connectsS = newroom.connectsS
room.connectsE = newroom.connectsE
room.connectsW = newroom.connectsW
room.connectedN = newroom.connectedN
room.connectedS = newroom.connectedS
room.connectedE = newroom.connectedE
room.connectedW = newroom.connectedW
elif room.connectsN == 0:
print("You cannot go there!\n") # Or, if room.connectsN == 0, you get an error message and do not move.
if direction == 2:
if room.connectsS == 1:
newroom = room.connectedS
room.name = newroom.name
room.describestring = newroom.describestring
room.connectsN = newroom.connectsN
room.connectsS = newroom.connectsS
room.connectsE = newroom.connectsE
room.connectsW = newroom.connectsW
room.connectedN = newroom.connectedN
room.connectedS = newroom.connectedS
room.connectedE = newroom.connectedE
room.connectedW = newroom.connectedW
elif room.connectsS == 0:
print("You cannot go there!")
if direction == 3:
if room.connectsE == 1:
newroom = room.connectedE
room.name = newroom.name
room.describestring = newroom.describestring
room.connectsN = newroom.connectsN
room.connectsS = newroom.connectsS
room.connectsE = newroom.connectsE
room.connectsW = newroom.connectsW
room.connectedN = newroom.connectedN
room.connectedS = newroom.connectedS
room.connectedE = newroom.connectedE
room.connectedW = newroom.connectedW
elif room.connectsE == 0:
print("You cannot go there!")
if direction == 4:
if room.connectsW == 1:
newroom = room.connectedW
room.name = newroom.name
room.describestring = newroom.describestring
room.connectsN = newroom.connectsN
room.connectsS = newroom.connectsS
room.connectsE = newroom.connectsE
room.connectsW = newroom.connectsW
room.connectedN = newroom.connectedN
room.connectedS = newroom.connectedS
room.connectedE = newroom.connectedE
room.connectedW = newroom.connectedW
elif room.connectsW == 0:
print("You cannot go there!")