Home > Software engineering >  Moving between rooms in python text based game
Moving between rooms in python text based game

Time:02-15

Whenever I type my input for going South or any direction, I stay in the same room. The point of the game is to move from room to room and get an item.

rooms = {
    'Front Lobby': {'West': 'Ice Rink'},
    'Ice Rink': {'North': 'Away Locker Room', 'East': 'Front Lobby', 'West': 'Mens Locker Room', 'South': 'Lounge'},
    'Away Locker Room': {'East': 'Pro Shop', 'South': 'Ice Rink'},
    'Pro Shop': {'West': 'Away Locker Room'},
    'Mens Locker Room': {'West': 'Ice Rink'},
    'Lounge': {'West': 'Womens Locker Room', 'East': 'Skate Rentals', 'North': 'Ice Rink'},
    'Womens Locker Room': {'East': 'Lounge'},
    'Skate Rentals': {'West': 'Lounge'}
}
gameOn = True
inventory = ['place holder']

# moves player from room to room
def move(player, direction):

    current_room = player

    # check if there is a room in the specified direction
    if direction in rooms[current_room]:
        current_room = rooms[current_room][direction]

    # error handling
    else:
        print("There is nothing in that direction!")

    # return the player state
        return current_room

# displays rules at start of game 
def showRules():

    print("- Collect 6 items to win the game, or have to get clowned by the other goalie.\n" "Move commands: go South, go North, go East, go West.\n" "Add to Inventory: get 'item name'. Once all items collected type: ‘Finish’")


def main():
    showRules()
    player = ("Front Lobby", [])

    while gameOn:

        current_room = player

        # output
        print(f"\nYou are in the {current_room}")

        # Goalie got you
        if player[0] == 'Pro Shop':
                print('You make more excuses than saves. GAME OVER')
                break

        # input validation and parsing
        print("----------------")
        move = input("Enter your move:\n")

        # invalid move syntax
        if 'go ' in move or 'Finish' in move:
            continue
        else:
            print('Invalid Command')
            continue

        # split string 
        action = move.split()

        # move 
        if action[0] == 'go ':
            move(player, arg)
        elif action == 'Finish':
            print('Awesome! Now get on the ice! You Win.')

        # invalid action
        else:
            print("Invalid command!")

main()

CodePudding user response:

I think the

return current_room

is one indentation to far. This way it will only return when the else-statement is reached.

But also, maybe you should assign the value to current_room in the main function.

current_room = move(player, arg)

instead of this:

move(player, arg)

Then you should also change the move function so it returns the current room:

def move(player, direction):
    current_room = player
    # check if there is a room in the specified direction
    if direction in rooms[current_room]:
        return rooms[current_room][direction]
    # error handling
    else:
        print("There is nothing in that direction!")

This has to do with global and local variables. When changing the variable of a value within a function, that only changes within the function. Try this as a test (output a):

def move(x):
    x = 'b'

def main():
    x = 'a'
    move(x)
    print(x)

main()

And this (output b):

def move(x):
    x = 'b'
    return x

def main():
    x = 'a'
    x = move(x)
    print(x)

main()

CodePudding user response:

Another problem:

if 'go ' in move or 'Finish' in move:
    continue

Continue will make the execution go to the top of the while loop, you can use pass here instead, or remove this if statement and change the else-statement to:

if direction not in rooms[current_room]:
    print("There is nothing in that direction!")
    return player # Returning the same room if nothing Exists in the direction

CodePudding user response:

So there were several errors, I marked them in the program:

rooms = {
    'Front Lobby': {'West': 'Ice Rink'},
    'Ice Rink': {'North': 'Away Locker Room', 'East': 'Front Lobby', 'West': 'Mens Locker Room', 'South': 'Lounge'},
    'Away Locker Room': {'East': 'Pro Shop', 'South': 'Ice Rink'},
    'Pro Shop': {'West': 'Away Locker Room'},
    'Mens Locker Room': {'West': 'Ice Rink'},
    'Lounge': {'West': 'Womens Locker Room', 'East': 'Skate Rentals', 'North': 'Ice Rink'},
    'Womens Locker Room': {'East': 'Lounge'},
    'Skate Rentals': {'West': 'Lounge'}
}
gameOn = True
inventory = ['place holder']

#moves player from room to room
def move(player, direction):
    global rooms
    current_room = player 

    # check if there is a room in the specified direction
    if direction in rooms[current_room]:
        current_room = rooms[current_room][direction]

    # error handling
    else:
        print("There is nothing in that direction!")
        return player # Returning the same room if nothing Exists in the direction

    # return the player state
    return current_room # Indent Error

#displays rules at start of game 
def showRules():

    print("- Collect 6 items to win the game, or have to get clowned by the other goalie.\n" "Move commands: go South, go North, go East, go West.\n" "Add to Inventory: get 'item name'. Once all items collected type: ‘Finish’")


def main():
    showRules()
    player = "Front Lobby" # Acc. to me player should be a str not  tuple

    while gameOn:

        current_room = player

        # output
        print(f"\nYou are in the {current_room}")

        # Goalie got you
        if player == 'Pro Shop':
            print('You make more excuses than saves. GAME OVER')
            break

        # input validation and parsing
        print("----------------")
        player_move = input("Enter your move:\n") # because move is already a function

        # invalid move syntax
        if 'go' in player_move or 'Finish' in player_move:
                
    
            # split string 
            
            action = player_move.split(' ')
            print(action)        # move 
            if action[0] == 'go':
                player = move(player, action[1]) # Assigning the value to player 
                                                 # and giving a value since arg 
                                                 # isn't anything
            elif action == 'Finish':
                print('Awesome! Now get on the ice! You Win.')
    
            # invalid action
            else:
                print("Invalid command!")
        
        else:
            print('Invalid Command')
            continue


    

main()
  • Related