Exercise: a Maze Model.
Design a data structure to represent a maze using dictionaries and lists. If possible, work with a partner.
- Each place in the maze has a name, which is a string.
- Each place in the maze has zero or more people currently standing in it, identified by name.
- Each place in the maze has a maximum capacity of people that can fit in it.
- From each place in the maze, you can go from that place to a few other places, using a direction like ‘up’, ‘north’,
or ‘sideways’.
Create an example instance, in a notebook, of a simple structure for your maze:
- The living room can hold 2 people. James is currently there. You can go outside to the garden, or upstairs to the bedroom, or north to the kitchen.
- From the kitchen, you can go south to the front room. The kitchen fits 1 person.
- From the garden you can go inside to living room. The garden fits 3 people. Sue is currently there.
- From the bedroom, you can go downstairs. You can also jump out of the window to the garden. The bedroom fits 2 people.
Make sure that your model:
- Allows empty rooms
- Allows you to jump out of the upstairs window, but not to fly back up.
- Allows rooms which people can’t fit in.
Example data structure for cities and residents:
cities = [{'name': "London", "capacity": 8, "residents": ["Me","Sue"]},
{'name': "Edinburgh", "capacity": 1, "residents": ["Dave"]},
{'name': "Cardiff", "capacity": 1, "residents": []}]
len(cities[2]['residents'])
« 0
As we can see, there are currently no residents in Cardiff.
Next: Module - Control and Flow