0

I have a text-based game using python. My issue is that I can't move between rooms due to a variable not getting changed like it needs to.

I've used global and that results in another error. The code is as follows:

#The dictionary links a room to other rooms and items.
rooms = {
        'Holding Cell': {'South': 'Training room','North': 'Cupboard', 'West': 'Study' },
        'Cupboard': {'South': 'Holding Cell', 'Item': 'Health Potion'},
        'Study': {'West': 'Armory', 'East': 'Holding Cell', 'Item': 'Mind key'},
        'Armory': {'East': 'Study', 'Item': 'Sword and shield'},
        'Training room': {'North': 'Holding Cell', 'East': 'Storage room', 'Item': 'Body key'},
        'Storage room': {'West': 'Training room', 'Item': 'Armor set'},
        'Prayer room': {'North': 'Dungeon Exit', 'Item': 'Soul key'},
        'Dungeon Exit': {}
    }
starting_room = 'Holding Cell'
current_room = starting_room

inventory = []
inventory1 = ['Health Potion', 'Sword and shield', 'Mind key', 'Soul key', 'Armor set', 'Body key']
inventory1.sort()
health = 100



def status():
    inventory.sort()
    print('-----------------------------')
    print("Inventory: ", inventory)
    print("Health: ", str(health))
    print("Current room: ", current_room)

def main():
    rooms = {
        'Holding Cell': {'South': 'Training room', 'North': 'Cupboard', 'West': 'Study'},
        'Cupboard': {'South': 'Holding Cell', 'Item': 'Health Potion'},
        'Study': {'West': 'Armory', 'East': 'Holding Cell', 'Item': 'Mind key'},
        'Armory': {'East': 'Study', 'Item': 'Sword and shield'},
        'Training room': {'North': 'Holding Cell', 'East': 'Storage room', 'Item': 'Body key'},
        'Storage room': {'West': 'Training room', 'Item': 'Armor set'},
        'Prayer room': {'North': 'Dungeon Exit', 'Item': 'Soul key'},
        'Dungeon Exit': {}
    }
    status()


    current_room = starting_room

    direction = input("Enter 'North/South/East/West' to move or 'Exit': ")

        # user to exit
    if direction == 'Exit':
        print("Thanks for playing!")
        exit(0)


        # a valid move
    elif direction in rooms[current_room]:
        current_room = rooms[current_room][direction]

        # invalid move
    else:
        print("Invalid Move. There's no room to the {}".format(direction))



def show_instructions():
    print("Type 'North', 'South', 'East', 'North' to go in a direction. Type 'Exit' to leave the game!")
    print("To get an item type 'Get (item).")
#provides instructions to player
show_instructions()

while 1:
    main()
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • 1
    `rooms` in main is local to that function, and does not override the one outside that function. Same for `current_room` – OneCricketeer Feb 16 '22 at 20:34
  • _I've used global and that results in another error._... And what is the error you get? Because that seems to be what you need – OneCricketeer Feb 16 '22 at 20:35
  • SyntaxError: name 'current_room' is assigned to before global declaration – Matthew Lowerre Feb 16 '22 at 21:35
  • You need to put `global current_room` inside main function, before its usage, rather than outside the function. You should be able to do the same for `rooms` dictionary, or remove from the main function, since I don't see the need for it twice. See example here - https://stackoverflow.com/questions/13881395/in-python-what-is-a-global-statement – OneCricketeer Feb 16 '22 at 22:47

1 Answers1

0

First problem: Use global correctly within the main function.

def main():   
    status()

    global current_room  # <--- here
    current_room = starting_room  # this is already defined outside the function, so really isn't needed

    direction = input("Enter 'North/South/East/West' to move or 'Exit': ")

    if direction == 'Exit':
        print("Thanks for playing!")
        exit(0)
    elif direction in rooms[current_room]:
        current_room = rooms[current_room][direction]
    else:
        print("Invalid Move. There's no room to the {}".format(direction))

Second problem: you are constantly resetting the current_room = starting_room each time through the main function, and not maintaining your state correctly.

The fix for that seems to be removing that line and just prompting for the direction input, and letting the flow pass to the elif block.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245