Wanderer Package Body
MC logo
 

Wanderer Package Body

Ada Code Examples


<<Wanderer Package Download Wanderer Package Separate Function>>
--
-- This is the city wanderer.  You can walk N, S, E, or W some number
-- of blocks, and it will tell you how to get home.
--
package body Wander is
   -- This holds an offset in two dimensions.
   type Two_D_Offset is record
      NS, EW: Integer;
   end record;

   -- Here is a map from directions to offsets.
   Offset_Map: array(Direction) of Two_D_Offset :=
     ((1, 0), (-1, 0), (0, 1), (0, -1));

   -- Walk in a particular direction a particular distance.
   procedure Walk(L: in out Locator; D: Direction; NBlock: Integer) is
      Off: Two_D_Offset := Offset_Map(D);
   begin
      L.NS := L.NS + NBlock*Off.NS;
      L.EW := L.EW + NBlock*Off.EW;
   end Walk;

   -- How far is it from home (total walking distance)?
   function How_Far_Home(L: Locator) return Integer is
   begin
      return abs(L.NS) + abs(L.EW);
   end How_Far_Home;

   function Toward_Home(L: Locator) return Integer is
   begin
      if L.NS /= 0 then
         return abs(L.NS);
      elsif L.EW /= 0 then
         return abs(L.EW);
      else
         return 0;
      end if;
   end Toward_Home;

   function Toward_Home(L: Locator) return Direction_Instruction is separate;
end Wander;
<<Wanderer Package Wanderer Package Separate Function>>