0

I want a room to be removed from my dictionary from the start of the game while snowboots = False. When snowboots = True, I want the room to be reachable, and I want picking up the snowboots to make them True.

if that makes sense.

roomDirections = {
    "hallEnt":{"e":"hallMid"},
    "hallMid":{"s":"snowRoom", "e":"giantNature", "w":"hallEnt"},
    "snowRoom":{"n":"hallMid"},
    "giantNature":{"s":"strangeWall", "e":"riverBank", "w":"hallMid"},
    "strangeWall":{"s":"hallOuter", "e":"riverBank", "n":"giantNature"},
    "riverBank":{"e":"lilyOne", "w":"giantNature"},
    "lilyOne":{"e":"lilyTwo", "w":"riverBank", "n":"riverBank", "s":"riverBank"},
    "lilyTwo":{"e":"riverBank", "w":"lilyThree", "n":"riverBank", "s":"riverBank"},
    "lilyThree":{"e":"riverBank", "w":"lilyFour", "n":"riverBank", "s":"riverBank"},
    "lilyFour":{"e":"riverBank", "w":"treasureRoom", "n":"riverBank", "s":"riverBank"},
    "treasureRoom":{"w":"hallEnt"},
}


roomItems = {
    "hallEnt":["snowboots"],
    "snowRoom":["lamp"],
    "treasureRoom":["treasure"],
    }

snowboots = lamp = treasure = False

these are my dictionaries and my alleged variables.

if "snowboots" == False:
            del roomDirections["hallMid"]
        else:
            print ("you cannot go that way")

this was meant to remove hallMid from roomDirections so movement from it is impossible, until...

elif playerInput in roomItems[currentRoom]:
        print("picked up", playerInput)
        invItems.append(playerInput)
        playerInput == True
        for i in range(0, len(roomItems[currentRoom])):
            if playerInput == roomItems[currentRoom][i]:
                del roomItems[currentRoom][i]
                break

the snowboots = True, which is what this chunk was suppose to do but it doesn't seem to be working, am I close or am I completely off track?

EDIT -- My main game loop --

while True:
    playerInput = input("What do you want to do? ")
    playerInput = playerInput.lower()
    if playerInput == "quit":
        break

    elif playerInput == "look":
        print(roomDescriptions[currentRoom])




    elif playerInput in dirs:
        playerInput = playerInput[0]
        if playerInput in roomDirections[currentRoom]:


            currentRoom = roomDirections[currentRoom][playerInput]
            print(roomEntrance [currentRoom])
        else:
            print("You can't go that way")

    elif playerInput == "lookdown":
        if currentRoom in roomItems.keys():
            print ("You see", roomItems[currentRoom])
        else:
            print ("You see nothing on the ground")

    elif playerInput == "inventory" or playerInput == "inv":
        print (invItems)




    elif playerInput in roomItems[currentRoom]:
        print("picked up", playerInput)
        invItems.append(playerInput)       
        for i in range(0, len(roomItems[currentRoom])):
            if playerInput == roomItems[currentRoom][i]:
                del roomItems[currentRoom][i]
                break

    elif playerInput in invItems:
        print("dropped", playerInput)
        roomItems[currentRoom].append (playerInput)
        for i in range (0, len(invItems)):
            if playerInput == invItems[i]:
                del invItems[i]
                break
    else:
        print ("I don't understand")
kebab
  • 17
  • 5
  • Are you saying that you want that ```elif``` suite to assign ```True``` to ```snowboots```? If so just add an assignment statement. Is ```playerInput == True``` the statement that is not working? – wwii Nov 19 '14 at 04:51
  • I don't know what an assignment statement is sorry I am very new to python. EDIT: the 'elif' suite is also for picking up all three items in the game, but only if the player is in the right room – kebab Nov 19 '14 at 04:52
  • ```a = 3``` is an assignment statement - it assigns 3 to `a`. – wwii Nov 19 '14 at 05:01
  • okay yea, I have my snowboots = False in the first code window. this is set from the start so that the room is not reachable at the start. but then when the snowboots get picked up i need them to become True, then "snowboots = True" will bring back the room making it reachable. – kebab Nov 19 '14 at 05:07
  • Maybe change ```playerInput == True``` to ```playerInput = True```. – wwii Nov 19 '14 at 14:46

3 Answers3

1

As I understand it, you want to add some conditions to decide whether going through a particular exit is allowed. You currently have dictionaries mapping the directions from each room, variables intended to hold whether the player has each item, and lists of items in each room and the player inventory. Note that the item variables are redundant; you could simply check the inventory.

Your proposed method is to add and remove exits from the rooms when the required items are acquired or lost. This can be done, but the complexity of finding which exits to remove is all you need to disregard them in the first place; restoring them if they were removed is harder than filtering them out as needed. Here's one approach:

requirements = {'snowRoom': 'snowboots', 'darkCave': 'lamp'}
reasons = {'snowboots': "You'd sink into the snow.",
           'lamp': "It would be too dark to see."}

You can then use these to ignore a direction if the condition was not satisfied:

elif playerInput in dirs:
    playerInput = playerInput[0]
    if playerInput in roomDirections[currentRoom]:
        newRoom = roomDirections[currentRoom][playerInput]
        required = requirements.get(newRoom)
        if required and required not in invItems:
            print("You can't go that way. " + reasons[required])
        else:
            currentRoom = newRoom
            print(roomEntrance [currentRoom])
    else:
        print("You can't go that way")

You could also make it so players can't remove the required item in the room:

elif playerInput in invItems:
    if playerInput != requirements[currentRoom]:
        print("dropped", playerInput)
        roomItems[currentRoom].append (playerInput)
        invItems.remove(playerInput)
    else:
        print("You still need " + playerInput + ". " + reasons[required])

It may make sense to have a more object oriented approach, where a room actually contains its list of items, links to the other rooms, and requirements. You could also do things like add synonyms to items.

Yann Vernier
  • 15,414
  • 2
  • 28
  • 26
0

Let me point out your mistake here. You have snowboots (which is an object) but you are comparing "snowboots" (which is a string). So when you are comparing "snowboots" in if condition it'll return false and as a result it'll go to else part always. Try the following code and let me know if it helps you.

snowboots = False
if snowboots == False:              ## notice the absence of double quotes here 
    del roomDirections["hallMid"]
else:
    print ("you cannot go that way")

EDIT: For the sake of more clarity I've edited your code to show that del works. I have removed

roomDirections = {
    "hallMid":{"s":"snowRoom", "e":"giantNature", "w":"hallEnt"},
}

print roomDirections  ## print dictionary before your deleting.

snowboots = lamp = treasure = False

if snowboots == False:              ## notice the absence of double quotes here 
    del roomDirections["hallMid"]
else:
    print ("you cannot go that way")

print roomDirections                ## print dictionary after deleting

Output:

    {'hallMid': {'s': 'snowRoom', 'e': 'giantNature', 'w': 'hallEnt'}}   ## before deleting

     {}     ## after deleting.
d-coder
  • 12,813
  • 4
  • 26
  • 36
  • It is still letting me to move through the room (hallMid) and to the following rooms – kebab Nov 19 '14 at 05:00
  • @kebab "letting me to move through the room" I don't understand. – d-coder Nov 19 '14 at 05:26
  • I know that it works Im using it in other parts of my project, but once hallMid is removed from roomDirections I should not be able to move to a new room from hallMid, but I can still? – kebab Nov 19 '14 at 05:28
  • Could you provide us some more part of your code ? or is this all ? – d-coder Nov 19 '14 at 05:32
  • I added the rest of my game loop, thats most of the code, the rest is just descriptive text – kebab Nov 19 '14 at 05:42
  • I'm sorry I can't figure it out why. This is how far I could help – d-coder Nov 19 '14 at 06:06
  • okay thank you anyway :) you have helped me I should be able to get it right if i fiddle with it a bit – kebab Nov 19 '14 at 06:12
0

playerInput is a string. It looks like you need to map strings to boolean values.

items = {'snowboots' : False,
         'lamp' : False,
         'treasure' : False}

then change

if "snowboots" == False to if not items['snowboots']

playerInput == True to items[playerInput] = True

snowboots = lamp = treasure = False to

for item in items:
    items[item] = False

Looks like you have a conceptual problem with strings and names and seem to be mixing them up in your code. "foo" is not the same as foo.

>>> 
>>> foo = 2
>>> "foo" = 2
SyntaxError: can't assign to literal
>>> foo = "foo"
>>> foo
'foo'
>>> "foo" = foo
SyntaxError: can't assign to literal
>>> foo = False
>>> bool("foo")
True
>>> bool(foo)
False
>>>

You will need to go through all your code and resolve all similar code.

wwii
  • 23,232
  • 7
  • 37
  • 77