Section 18.4 - Attributes and Operations of Objects

Well, we have the general structure laid out. Now we need to figure out what data values and operations apply to the different tagged types.

Type Thing should probably have the attributes Name and Description. It should also have a way of referring to its container, so we'll add a "Container" value in its implementation. For example, if Player "Fred" is the Room "kitchen", then Fred's Container would refer to the kitchen.

Let's completely hide these attributes with other procedures; that way we can control what happens to these values. In fact, to change "where" an object is, let's only provide the operation "Place", which places a given Thing inside another thing. That way we can make sure that the internal data values are kept consistent.

Let's implement "looking" as a pair of procedures. The first procedure, called "Look", is a message sent to the object doing the looking (i.e. a player). That procedure, should it consider looking okay (for example, if it determines that the player can see), will then call Put_View of whatever object the the player is looking at (i.e. the player's room).

I suspect that, if a player is inside a room, the view is different than if the player is inside a dragon's belly. Therefore Put_View should be a dispatching operation so that it can do different things depending on the type of the object being viewed. In Ada 95, to make a subprogram dispatchable (primitive), place the subprogram's declaration in the specification declaring the type.

By the same reasoning "Look" should be a primitive operation; perhaps different players have different kinds of vision (such as X-ray vision or vision that doesn't require light), so we'll want that to dispatch depending on the "looker" as well.

We'll also need to handle getting and dropping objects. Many of the operations in an adventure game involve both the Actor (i.e. the Player) and the object being acted on. That's similar to how we handled looking, since looking involves both the looker and the looked-at object. For example, for a "Get" to work, the player must be able to get things, and the object being gotten must agree that it can be acquired by the player. So let's define pairs of operations for Get: Get itself, which asks a given actor to get an object, and May_I_Get, which asks the object if the given actor can get it. The same argument applies to drop, which will have the pair Drop and May_I_Drop.


Quiz:

If Item box and Player Fred are both in Room Kitchen, and Item knife is in Item box, what is the container of Item box?

  1. Room Kitchen
  2. Player Fred
  3. Item knife
  4. Item box
  5. None of the above

You may also:

PREVIOUS Go back to the previous section

NEXT     Skip to the next section

OUTLINE  Go up to lesson 18 outline

David A. Wheeler (dwheeler@dwheeler.com)

The master copy of this file is at "http://www.adahome.com/Tutorials/Lovelace/s18s4.htm".