0

I am making a text adventure game. I'm using a named tuple to define locations.

Location = namedtuple('Location', ['desc', 'ldesc', 'func', 'dirloc'])

entrance = foyer = None 

entrance = Location('Dungeon Entrance', (
'You are in a small clearing in a forest. '          
'To the north, a large iron-studded door is visible, '          
'embedded in a small hill that rises up in the middle of the clearing. '
'The sun shines down upon you and birds sing.'), None,
{'north':foyer, 'south':None, 'east':None, 'west':None, 'up':None, 'down':None}) 

foyer = Location('Foyer', ('You are in a medium-sized room made of stone. '
'Alcoves are carved along the wall at all angles. '
'One passage leads west. Another leads north. '
'Footsteps on the ground lead towards the west passage.'),
None,
{'north':None, 'south':entrance, 'east':None, 'west':None, 'up':None, 'down':None})

I need the foyer variable in the dictionary dictloc in the entrance location to automatically update when the real foyer location is defined. How can I do this? I have scoured the internet for articles but found nothing that was related.

I have tried over and over using different data structures to see if I could find one that worked. Unfortunately, none did.

Odoul
  • 23
  • 4
  • You have a fundamental misunderstanding: variables *are not objects*. You don't have a a data structure that contains *a variable*. You have a a data structure that contains *an object*. Variables refer to objects. – juanpa.arrivillaga Apr 08 '23 at 03:29
  • 1
    Using individual variables for your locations is simply not going to work, due to the circular references between them. One possibility would be to put all the locations in a single dictionary, with the keys being location names. You'd use those names (`'entrance'`, `'foyer'`, etc.) to refer to other locations. – jasonharper Apr 08 '23 at 03:41

1 Answers1

0

Try this layout

dungeon = {
    "entrance": {
        "desc": "Dungeon Entrance",
        "ldesc": (
            "You are in a small clearing in a forest. "
            "To the north, a large iron-studded door is visible, "
            "embedded in a small hill that rises up in the middle of the clearing. "
            "The sun shines down upon you and birds sing."
        ),
        "func": None,
        "dirloc": {
            "north": "foyer",
            "south": None,
            "east": None,
            "west": None,
            "up": None,
            "down": None,
        },
    },
    "foyer": {
        "desc": "Foyer",
        "ldesc": (
            "You are in a medium-sized room made of stone. "
            "Alcoves are carved along the wall at all angles. "
            "One passage leads west. Another leads north. "
            "Footsteps on the ground lead towards the west passage."
        ),
        "func": None,
        "dirloc": {
            "north": None,
            "south": "entrance",
            "east": None,
            "west": None,
            "up": None,
            "down": None,
        },
    },
}


location_key = "entrance"

while location_key:
    location = dungeon[location_key]
    print(location["desc"])
    print("="*len(location["desc"]))
    print(location["ldesc"])

    direction = input("\nWhich direction?")
    if direction in location["dirloc"]:
        if location["dirloc"][direction] is None:
            print("You cannot go "+direction)
        else:
            location_key = location["dirloc"][direction]
            print("\nGoing "+direction)
    else:
        raise ValueError("Unexpected direction: " + direction)
ProfDFrancis
  • 8,816
  • 1
  • 17
  • 26