MS 3: Reading and distributing items

The most important extension that separates the Adventure game from the teaching machine application is the introduction of items like keys and treasures. The items are specified in the Items file described here. For example, the first item in the SmallItems.txt data file is the set of keys, for which the description consists of the following three lines:

KEYS
a set of keys
InsideBuilding

Your first task in this milestone is to implement the AdvItem class, which is given to you in skeletal form in the repository. The AdvItem class defines a constructor and the getter methods get_name, get_description, and get_initial_location, along with a read_item function that reads an item from a data file. Your model for this file is the AdvRoom class, which implements the same mechanism for the more complicated data structure used for rooms. Reading in the information for the items should be significantly more simple. You also will then need to add the necessary code to the AdvGame class to read in all the items at the start and store them in a dictionary, just as you do for the rooms. It is not an error if the Items file is missing; a missing file just means that there are no items.

Your second task is to add methods to the AdvRoom class so that rooms can keep track of the items they contain. The easiest strategy for doing so is to keep track of the names of the items in the room using a Python set stored in the AdvRoom object as an attribute. In keeping with the principles of data abstraction, clients should not look at this set directly but should instead obtain information through method calls. The following methods are sufficient to get you through all the milestones:

The third task is to add code to AdvGame so that it places the items in the appropriate rooms at the start of the game. You could do this either at the end of the constructor once the rooms and items have been read in, or at the start of the run method. This code should iterate through the dictionary containing all the items and then call add_item to put it in the room specified by its initial location. Your code should simply skip over any items whose location is PLAYER until you implement Milestone 4.

The fourth and final task is to extend the code that describes a room so that it also prints out the descriptions of the items contained in that room. The items are listed on single lines, one for each item that the room contains.

Because Milestone 3 does not yet allow you to pick up and drop items, the only thing you can do to test whether this part is working is to check and see whether the items are listed as part of the room descriptions. For example, you should make sure that the keys are listed inside the building, as shown in the following session: