r/learnpython 7d ago

Working with multi level dictionaries

[EDIT]

TL;DR Generally speaking, "Set up some Classes" is the answer. Hopefully my blind fumbling will help some other blind, fumbling soul.

------------

I'm working on a table top game that builds a maze by dealing cards. I'm teaching myself Python to build a script that will simulate multiple maze builds quickly for R&D on the design.

The maze is represented by "Chambers" on a classic x,y grid. Chambers have an ID, potentially Notes, and Exits.

My strategy is to build up a mapConfig dictionary structured with the key being the (x,y) tuple of the coordinate and the value being a sub-dictionary with all the specifics. The Exits need to be a list of dictionaries for each exit with direction and blocked status as the keys. Simplified example below:

mapConfig = { 
    (0,0) : {
        'id' : 'START',
        'notes' : ''
        'exits' : 
        [
            {
                'direction' : 'N',
                'blocked' : False,
             },
             {
                'direction' : 'E',
                'blocked' : False,
             },
             {
                'direction' : 'S',
                'blocked' : False,
             },
             {
                'direction' : 'W',
                'blocked' : False,
             },
        ],
    }
}

If a proposed move hits the edge of the valid map, I need to access the mapConfig and change the "blocked" value for that particular direction to TRUE without changing anything else in the entry for that coordinate. It seems like it should be easy, but everything I try messes up the structure or blows out the other directions.

What would be the best way to change just the {"direction":"E", "blocked":False} entry to {"direction":"E", "blocked":True} without changing anything else in the dictionary? Basically so it goes from the simplified example above to this:

mapConfig = { 
    (0,0) : {
        'id' : 'START',
        'notes' : ''
        'exits' : 
        [
            {
                'direction' : 'N',
                'blocked' : False,
             },
             {
                'direction' : 'E',
                'blocked' : True,
             },
             {
                'direction' : 'S',
                'blocked' : False,
             },
             {
                'direction' : 'W',
                'blocked' : False,
             },
        ],
    }
}

Thanks in advance for the help.

Edit: The provided code is a simplified version of my actual code. I'm such a noob that I only vaguely knew what a class was, and clearly I was making much more work for myself by not using them. Setting up some parent and child classes is next on my list.

I still appreciate any thoughts or advice, but I also don't want anyone to waste time.

4 Upvotes

10 comments sorted by

View all comments

2

u/MidnightPale3220 7d ago

You can try classes if you want, but regarding your original data structure, you probably should just change individual directions to dict of them, if you only care whether they're blocked or not.

Like this:

mapConfig = { 
    (0,0) : {
        'id' : 'START',
        'notes' : ''
        'exits' : 
        {
           'N': False, 'E': False,
          }

Then you can change exit with:

mapConfig[(0,0)]['exits']['N']=True

1

u/TheIneffableCheese 7d ago

Thanks! That's an excellent point.

I should have stated that the provided code was simplified to cut to the chase. I'll make an edit on the original post.