Jozlen G
Jozlen G

Reputation: 1

Text based game: Movement between rooms

I am creating a text based game where the character has to move between rooms to defeat the final boss: HUNGER. However, I am not sure how to create the code needed to move between rooms. This is what I have so far. How do I go about making the character move from Great Hall to Bedroom?


rooms = {
        'Great Hall': {'South': 'Bedroom'},
        'Bedroom': {'North': 'Great Hall', 'East': 'Cellar'},
        'Cellar': {'West': 'Bedroom'}
    }

directions = ['North', 'South', 'East', 'West']
location = 'Great Hall'
Inventory = []

print("Welcome to Trials of Harvest. You can go North, East, South, or West. Type 'Exit' to leave the game.")


while True: #gameplay loop
    print('You are in the ', location)
    print('------------------------------')
    direction = input("Type North, East, South, West, or Exit. ")
    if direction == 'North' or 'South' or 'East' or 'West':
        print("You are in the", location)
    elif direction == "Exit":
        print('Thanks for playing!')
        exit()
    else:
        print('Invalid Direction!!')

Upvotes: -1

Views: 52

Answers (2)

required_banana
required_banana

Reputation: 9

Your direction == "North" or "South" or "East" or "West" isn't actually doing what you think it is. (There was a reply about this, but it didn't explain fully what was going on.) Every string, in logic, is equivalent to True. So this actually means direction == True or True or True or True. This collapses down to direction == True, which is always false. Instead, you should do direction == "North" or direction == "South" or direction == "East" or direction == "West". Python is like English, but it doesn't have all the properties of it. I'm a beginner at Python, can do most basic things, but I don't understand a lot of what these answers are, and I'm glad to help if any more problems are in my expertise. Feel free to edit my typos or anything else. Please don't edit the answer, though, unless it is a vital caveat that I missed.

Upvotes: -1

AstralHex
AstralHex

Reputation: 101

As it is unclear how your map looks like I got an idea of the map which looks like this ->

--------------
| Great Hall |
|            |
|            |
--------------------------
|            |           |
| Bedroom    | Cellar    |
|            |           |
--------------------------

Your if statement in the code is not correct it will always evaluate true because non-empty strings like 'South' returns true in python.

The corrected code according to your implementation ->

rooms = {
    'Great Hall': {'South': 'Bedroom'},
    'Bedroom': {'North': 'Great Hall', 'East': 'Cellar'},
    'Cellar': {'West': 'Bedroom'}
}

directions = ['North', 'South', 'East', 'West']
location = 'Great Hall'
Inventory = []

print("Welcome to Trials of Harvest. You can go North, East, South, or West. Type 'Exit' to leave the game.")

while True:  # Gameplay loop
    print(f'You are in the {location}')
    print('------------------------------')
    
    direction = input("Type North, East, South, West, or Exit: ").capitalize()
    
    if direction in rooms[location]:  # Check if the direction exists in the current room
        location = rooms[location][direction]  # Move to the new room
    elif direction == "Exit":
        print('Thanks for playing!')
        break
    else:
        print('Invalid direction or you cannot go that way!')

If I were you, I would do this (Use graphs) instead of creating a string based dictionary of locations:

_map = {
    (0, 0): "Bedroom",  # Center position
    (0, 1): "Cellar",  # East of Bedroom
    (1, 0): "Great Hall"  # North of Bedroom
}

location = (0, 0)

# Directions based on standard graphs
_directions = {
    "East": (0, 1),
    "West": (0, -1),
    "North": (1, 0),
    "South": (-1, 0)
}

print("Welcome to Trials of Harvest. You can go North, East, South, or West. Type 'Exit' to leave the game.")

while True:
    print('You are in the', _map[location])
    print('------------------------------')
    
    user_input = input("Type North, East, South, West, or Exit: ")
    if user_input == "Exit":
        print("Thanks for playing")
        break

    if user_input not in _directions:
        print("Invalid direction. Try again.")
        continue

    # Calculate new position
    direction = _directions[user_input]
    new_location = (location[0] + direction[0], location[1] + direction[1])

    # Check if the new location exists
    if new_location in _map:
        location = new_location
    else:
        print("You can't go that way!")

with this implementation you can easily have movement in between the rooms

Upvotes: 2

Related Questions