Skip to content
Advertisement

How can I add items and avoid duplicates to my inventory in a text based adventure game?

So I am trying to design a text based adventure game for one of my classes. I have the movement from room to room down, but have been struggling when it comes to getting items added to the inventory for the escape. At first when I tried to enter “get ‘item'” I was prompted with ‘invalid move’ which was happening because there was an issue with the sequence I was calling my functions. Now that I have everything fixed, the items are automatically being added to the inventory without the user entering the get item function, and every time you enter a room, the item is being added despite it already being in the inventory. Here is my code:

def show_instructions():
    print('Escape From Work Text Based Game')
    print('Movement commands: South, North, East, West')
    print("To pick up an item, type 'get [item]'")
    print()
    print('Your boss is asking you to come in on your days off. To escape without being seen, you will need to get your'
          ' vest, backpack, lunchbox, paycheck, name badge, and car keys.')
    print('Do not enter the room that has your boss, or you can kiss your days off goodbye!')


def player_stat(current_room, inventory):
    print('----------------------')
    print('You are in the {}'.format(current_room))
    print('Item in this room: {}'.format(rooms[current_room]['Item']))
    print('Inventory: ', inventory)
    print('----------------------')


def move(direction, current_room, rooms):
    if direction in rooms[current_room]:
        current_room = rooms[current_room][direction]
    else:
        print('You run into the wall, customers are staring. Try again.')
    return current_room


rooms = {
    'Garden Center': {'South': 'Hardware Dept.', 'West': 'Customer Service', 'North': 'HR Office',
                      'East': 'Storage Room', 'Item': 'None'},
    'Customer Service': {'East': 'Garden Center', 'Item': 'lunchbox'},
    'Hardware Dept.': {'East': 'Break Room', 'North': 'Garden Center', 'Item': 'backpack'},
    'HR Office': {'East': 'Lounge', 'South': 'Garden Center', 'Item': 'paycheck'},
    'Lounge': {'West': 'HR Office', 'Item': 'name badge'},
    'Break Room': {'West': 'Hardware Dept.'},
    'Storage Room': {'West': 'Garden Center', 'North': 'Admin Office', 'Item': 'vest'},
    'Admin Office': {'South': 'Storage Room', 'Item': 'keys'}
}

directions = ['North', 'East', 'South', 'West']
current_room = 'Garden Center'
inventory = []
show_instructions()

while current_room != 'Exit':
    player_stat(current_room, inventory)
    player_move = input('What is your command?: ').lower().title()
    if player_move == 'exit':
        current_room = 'Exit'
        print('Sorry to see you go, play again soon!')
    elif player_move in directions:
        current_room = move(player_move, current_room, rooms)

    item = rooms[current_room].get('Item')

    if item is not None and player_move == 'get ' + item:
        if item in inventory:
            print('You already have this item in your inventory!')
    else:
        inventory.append(item)

    if current_room == 'Break Room':
        print('Oh no! You got caught. Have fun working this weekend.')
        exit()
    else:
        print('Invalid move')

And this is what the output looks like after I have moved from a few of the rooms. I don’t understand why it is printing invalid move after every move even if it is valid either.

You are in the HR Office
Item in this room: paycheck
Inventory:  ['paycheck', 'paycheck']
----------------------
Where would you like to go?: south
Invalid move
----------------------
You are in the Garden Center
Item in this room: None
Inventory:  ['paycheck', 'paycheck', 'None']
----------------------
Where would you like to go?: south
Invalid move
----------------------
You are in the Hardware Dept.
Item in this room: backpack
Inventory:  ['paycheck', 'paycheck', 'None', 'backpack']
----------------------

I also still have to add a way to exit the game when you win after collecting all 6 items, and i was heading in the direction of ‘if inventory == [ all 6 items in list ]: print( you win) followed by exit’ Any help anyone has to offer would be greatly appreciated.

Advertisement

Answer

Try

#Loop continues if user has not entered 'exit' or inventory is not up to 6 items
while current_room != 'Exit' and len(inventory)!= 6:
    player_stat(current_room, inventory)
    player_move = input('What is your command?: ').lower().title()
    if player_move == 'Exit':
        current_room = 'Exit'
        print('Sorry to see you go, play again soon!')
        exit()
    elif player_move in directions:
        current_room = move(player_move, current_room, rooms)

    item = rooms[current_room].get('Item')
    #As the input has .title() this condition will accept input of item in that format
    if item is not None and player_move == 'Get ' + item.title():
        if item in inventory:
            print('You already have this item in your inventory!')
        #Was adding repeat items because of prior incorrect indentation
        #Correct indentation with the else applying after checking inventory
        else:
            inventory.append(item)

    if current_room == 'Break Room':
        print('Oh no! You got caught. Have fun working this weekend.')
        exit()
    #Replaced the 'else' statement with this so the console doesn't return 'Invalid move' every time
    if "get " + item not in player_move and player_move not in directions:
        print('Invalid move')
#When loop breaks, user will have won
print("You won!")
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement