Section 18.6 - Starting Implementation

Okay, let's start implementing program Small. I started by writing a version of procedure body Small, package Parser, package Thing, and package World, just enough to make it possible to run "look" and "quit". Rather than looking at everything in program Small, let's quickly examine some specific areas. For example, let's see how we can now define package Things more specifically:

package Things is

 -- "Thing" is the root class for all things in this small world.
 -- Rooms, Players, Items, and Monsters are derived from Thing.

 type Thing is abstract new Limited_Controlled with private;
 type Thing_Access is access all Thing'Class;

 -- Public Dispatching operations.

 procedure Put_View(T : access Thing; Agent : access Thing'Class) is abstract;
  -- Put what Agent sees inside T.

  -- Public non-Dispatching operations:

 procedure Set_Description(T : access Thing'Class;
                           Description : in String);
 function Long_Description(T : access Thing'Class) return Unbounded_String;
 function Short_Description(T : access Thing'Class) return Unbounded_String;

 -- ...
end Things;

We now have a root type called "Thing" defined in package "Things". We can set its description using procedure Set_Description, and retrieve descriptions using functions Long_Description or Short_Description. Any player who successfully looks at some thing will call procedure Put_View to print whatever that Thing looks like. Now we can implement procedure "Look" in package Occupants; it will figure out what you're looking at and call the corresponding Put_View.

For Get and Drop we'll add the corresponding operations to package Occupants. However, to implement Get and Drop we need to be able to move objects around. To do this, let's add this operation to package Thing:

 procedure Place(T : access Thing'Class; Into : Thing_Access);
   -- Place T inside "Into" (removing it from wherever it was).
   -- Attempting to place T into itself will print an error message
   -- and fail.

Procedure Place doesn't need to dispatch, and we're passing access values around, so the first parameter is access Thing'Class (if we wanted this to dispatch, we could change it to "access Thing"). The second parameter is Thing_Access, not Thing'Class, because "null" should be a valid value for "Into" (that would mean we'll move T so it will have no container).

I've added support for directions - that way players can go north, south, east, west, up, or down. To do so, I've added a "Direction" type that has the values North, South, East, West, Up, and Down. Rooms have data on what's at a given direction, Occupants have commands to go in a given direction, and the Parser recognizes those commands. Since different packages (Rooms, Occupants, and Parser) need to know about directions, I've added another package (Directions) to define this new type Direction.


Between Look and Put_View, which would be called first?

  1. Look
  2. Put_View

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 (

The master copy of this file is at "".