Text Adventure: Mentioning the dictionary inside itself

Now that you’ve laid down some code, you might want to take a fresh look at your data structure. It looks pretty complex and perhaps a bit organic (design-as-you-go). Computers work much better with a consistent structure. And programmers trying to maintain and extend a program–which includes you–also prefer good structure. Consider these definitions:

  • complex: a collection (system) of simple elements with simple relationships between them.
  • complicated: a “system” of arbitrary elements with arbitrary relationships between them.

Complex is okay, and you get there by thinking ‘simple’.

My suggestion is to design the data structure you plan to use before you try to program it. You might find, for example, that a master list containing separate dictionaries for {rooms} and {actions} (with each dictionary entry being a list of rooms, actions, etc.) is better than a single large dictionary.

Of course, if this project is to learn about how to design and program dictionaries, then continue programming. Just know that it’s very common to realize after you’ve programmed something how to optimize the program, because finishing the first version gave you a deep understanding of the system you’re creating.

A well-designed data structure can be enlarged for a very long time. For this reason, a best practice is to design the data structures that will give you the behavior you want and be as simple as possible to program.

The principle is: Design comes before build. If this is an exercise to learn Python, then that’s a great thing. The more interest you have in completing a project, the more you’ll get out of the project and the more fun it will be.

Consider this data structure:
[EDITED to correct the nested Room lists and replace the poor choice of map, which is a Python function (now ‘BOARD’)]

  1. BOARD[]
    This might need to be a grid list[] with Y rows of map locations and X rooms per row, each containing `Room[]` (see item 'B' below)
  2. Room[]
    1. roomName' (string)
    2. exits[] (N, E, S, W)
    3. Contents/Objects
    4. Puzzles
    5. etc. ...
  3. ACTIONS[]
    1. move
    2. open
    3. take
    4. drop
    5. etc. ...

See this post for how to create a grid of lists. Once you have a grid, you can reference the coordinates of the grid with BOARD[Y][X]. (Note the reversed coordinates. The grid origin is at top left and goes DOWN then RIGHT. The origin is map[0][0].

2 Likes