|
| 1 | +# Learn Python |
| 2 | +[Home](../README.md) | [Zombie House](README.md) | Dictionaries |
| 3 | + |
| 4 | +### Zombie House Game |
| 5 | + |
| 6 | +**About Dictionaries** |
| 7 | + |
| 8 | +Compared to a list, where items are consectively ordered and accessed by the postion (0, 1, 2, 3, etc), a dictionary is a mapping of items by a key. These key:value pairs are not maintained in any particular order like in a list. The values are simply mapped to the associated key. Like a list, dictionaries are mutable - values can be added and subtracted in the program. This is important to the game because items that can be picked up need to be removed from the associated room by taking it out of the dictionary. |
| 9 | + |
| 10 | +Looking at the original code as an example: |
| 11 | + |
| 12 | +```python |
| 13 | +# a dictionary linking a room to other rooms |
| 14 | +rooms = { |
| 15 | + 'Hall' : { |
| 16 | + 'south' : 'Kitchen', |
| 17 | + 'item' : 'key' |
| 18 | + }, |
| 19 | + |
| 20 | + 'Kitchen' : { |
| 21 | + 'north' : 'Hall', |
| 22 | + 'item' : 'sword' |
| 23 | + } |
| 24 | + } |
| 25 | +``` |
| 26 | + |
| 27 | +The "rooms" dictionary contains two keys (Hall and Kitchen) that each represents a room. The value for each key is itself a dictionary. This is an important concept - lists and dictionaries both can contain other Python objects. I like to call this "nesting" the dictionaries. It's very handy in developing the game. |
| 28 | + |
| 29 | +One problem with this dictionary structure was discovered. If the player tries a command like "go item" the game would halt. That's because in the way the game logic works, going to a room should return a key:value pair that has information about the room. An item (such as a sword) does not provide this and causes the crash. |
| 30 | + |
| 31 | +The solution to this was to break out the directions into a separate key:value pair with the linked rooms inside another dictionary. |
| 32 | + |
| 33 | +```python |
| 34 | +rooms = { |
| 35 | + # Each room has a directions key with another dictionary containing the linked rooms |
| 36 | + 'Hall' : { |
| 37 | + 'directions' : { |
| 38 | + 'south' : 'Kitchen', |
| 39 | + 'east' : 'Dining Room', |
| 40 | + 'north' : 'Atrium', |
| 41 | + 'west' : 'Library' |
| 42 | + }, |
| 43 | + 'item' : 'key' |
| 44 | + }, |
| 45 | + |
| 46 | + 'Kitchen' : { |
| 47 | + 'directions' : { |
| 48 | + 'north' : 'Hall', |
| 49 | + 'east' : 'Pantry' |
| 50 | + }, |
| 51 | + 'monster' : 'Zombie', |
| 52 | + 'poison' : 'Hemlock', |
| 53 | + 'item' : 'flower' |
| 54 | + }, |
| 55 | +``` |
| 56 | + |
| 57 | +Now, to display the available directions and other items in the room, the "showStatus()" function needs to be modified: |
| 58 | + |
| 59 | +```python |
| 60 | +# print the available directions |
| 61 | + for direction in rooms[currentRoom]['directions']: |
| 62 | + print(f"The {rooms[currentRoom]['directions'][direction]} is {direction}") |
| 63 | + # print an item, monster or poison if there is one |
| 64 | + if "item" in rooms[currentRoom]: |
| 65 | + print(f"You see a {rooms[currentRoom]['item']}") |
| 66 | + if "monster" in rooms[currentRoom]: |
| 67 | + print(f"There is a {rooms[currentRoom]['monster']} in the room!") |
| 68 | + if "poison" in rooms[currentRoom]: |
| 69 | + print(f"There is some {rooms[currentRoom]['poison']} in the room!") |
| 70 | +``` |
| 71 | + |
| 72 | +This same technique could be taken even further to add new items to a room, such as monsters, poisons and other non-playable characters. The downside is, at some point, the entire dictionary becomes hard to manage and maintain. This leads to the next concept of Object Oriented Programming (OOP). The objects in the game can be inside a Class and instances created in the program. |
| 73 | + |
| 74 | +More to follow ... |
0 commit comments