home *** CD-ROM | disk | FTP | other *** search
/ BURKS 2 / BURKS_AUG97.ISO / BURKS / LANGUAGE / ADA / LOVELACE / small.txt < prev    next >
Text File  |  1997-04-10  |  42KB  |  1,312 lines

  1. --
  2. -- Copyright (C) 1996 Ada Resource Association (ARA), Columbus, Ohio.
  3. -- Author: David A. Wheeler
  4. --
  5.  
  6. with Occupants;
  7. use  Occupants;
  8.  
  9. package Creatures is
  10.  type Creature is abstract new Occupant with private;
  11.  type Creature_Access   is access Creature'Class;
  12. private
  13.  type Creature is abstract new Occupant with null record;
  14. end Creatures;
  15.  
  16. --
  17. -- Permission to use, copy, modify, and distribute this software and its
  18. -- documentation for any purpose and without fee is hereby granted,
  19. -- provided that the above copyright and authorship notice appear in all
  20. -- copies and that both that copyright notice and this permission notice
  21. -- appear in supporting documentation.
  22. -- 
  23. -- The ARA makes no representations about the suitability of this software
  24. -- for any purpose.  It is provided "as is" without express
  25. -- or implied warranty.
  26. -- 
  27. --
  28. -- Copyright (C) 1996 Ada Resource Association (ARA), Columbus, Ohio.
  29. -- Author: David A. Wheeler
  30. --
  31.  
  32. with Ada.Characters.Handling;
  33. use  Ada.Characters.Handling;
  34.  
  35. package body Directions is
  36.  
  37.  Abbreviations : constant String := "nsewud";
  38.  
  39.  procedure To_Direction(Text : in Unbounded_String;
  40.                         Is_Direction : out Boolean;
  41.                         Dir  : out Direction) is
  42.   Lower_Text : String := To_Lower(To_String(Text));
  43.   -- Attempt to turn "Text" into a direction.
  44.   -- If successful, set "Is_Direction" True and "Dir" to the value.
  45.   -- If not successful, set "Is_Direction" False and "Dir" to arbitrary value.
  46.  begin
  47.    if Length(Text) = 1 then
  48.      -- Check if it's a one-letter abbreviation.
  49.      for D in Direction'Range loop
  50.        if Lower_Text(1) = Abbreviations(Direction'Pos(D) + 1) then
  51.          Is_Direction := True;
  52.          Dir := D;
  53.          return;
  54.        end if;
  55.      end loop;
  56.      Is_Direction := False;
  57.      Dir := North;
  58.      return;
  59.  
  60.    else
  61.      -- Not a one-letter abbreviation, try a full name.
  62.      for D in Direction'Range loop
  63.        if Lower_Text = To_Lower(Direction'Image(D)) then
  64.          Is_Direction := True;
  65.          Dir := D;
  66.          return;
  67.        end if;
  68.      end loop;
  69.      Is_Direction := False;
  70.      Dir := North;
  71.      return;
  72.    end if;
  73.  end To_Direction;
  74.  
  75.  function To_Direction(Text : in Unbounded_String) return Direction is
  76.    Is_Direction : Boolean;
  77.    Dir          : Direction;
  78.  begin
  79.    To_Direction(Text, Is_Direction, Dir);
  80.    if Is_Direction then
  81.       return Dir;
  82.    else
  83.       raise Constraint_Error;
  84.    end if;
  85.  end To_Direction;
  86.  
  87.  function Is_Direction(Text : in Unbounded_String) return Boolean is
  88.    Is_Direction : Boolean;
  89.    Dir          : Direction;
  90.  begin
  91.    To_Direction(Text, Is_Direction, Dir);
  92.    return Is_Direction;
  93.  end Is_Direction;
  94.  
  95. end Directions;
  96.  
  97. --
  98. -- Permission to use, copy, modify, and distribute this software and its
  99. -- documentation for any purpose and without fee is hereby granted,
  100. -- provided that the above copyright and authorship notice appear in all
  101. -- copies and that both that copyright notice and this permission notice
  102. -- appear in supporting documentation.
  103. -- 
  104. -- The ARA makes no representations about the suitability of this software
  105. -- for any purpose.  It is provided "as is" without express
  106. -- or implied warranty.
  107. -- 
  108. --
  109. -- Copyright (C) 1996 Ada Resource Association (ARA), Columbus, Ohio.
  110. -- Author: David A. Wheeler
  111. --
  112.  
  113. with Ada.Strings.Unbounded;
  114. use  Ada.Strings.Unbounded;
  115.  
  116. package Directions is
  117.  
  118.  type Direction is (North, South, East, West, Up, Down);
  119.  
  120.  Reverse_Direction : constant array(Direction) of Direction :=
  121.                     (North => South, South => North,
  122.                      East =>West, West => East,
  123.                      Up => Down, Down => Up);
  124.  
  125.  function To_Direction(Text : Unbounded_String) return Direction;
  126.  -- Converts Text to Direction; raises Constraint_Error if it's not
  127.  -- a legal direction.
  128.  
  129.  function Is_Direction(Text : Unbounded_String) return Boolean;
  130.  -- Returns TRUE if Text is a direction, else false.
  131.  
  132. end Directions;
  133.  
  134. --
  135. -- Permission to use, copy, modify, and distribute this software and its
  136. -- documentation for any purpose and without fee is hereby granted,
  137. -- provided that the above copyright and authorship notice appear in all
  138. -- copies and that both that copyright notice and this permission notice
  139. -- appear in supporting documentation.
  140. -- 
  141. -- The ARA makes no representations about the suitability of this software
  142. -- for any purpose.  It is provided "as is" without express
  143. -- or implied warranty.
  144. -- 
  145. --
  146. -- Copyright (C) 1996 Ada Resource Association (ARA), Columbus, Ohio.
  147. -- Author: David A. Wheeler
  148. --
  149.  
  150. package body Items is
  151.  
  152.  function May_I_Get(Direct_Object : access Item;
  153.                     Agent : access Occupant'Class) return Boolean is
  154.  begin
  155.   return True;
  156.  end May_I_Get;
  157.  
  158. end Items;
  159.  
  160. --
  161. -- Permission to use, copy, modify, and distribute this software and its
  162. -- documentation for any purpose and without fee is hereby granted,
  163. -- provided that the above copyright and authorship notice appear in all
  164. -- copies and that both that copyright notice and this permission notice
  165. -- appear in supporting documentation.
  166. -- 
  167. -- The ARA makes no representations about the suitability of this software
  168. -- for any purpose.  It is provided "as is" without express
  169. -- or implied warranty.
  170. -- 
  171. --
  172. -- Copyright (C) 1996 Ada Resource Association (ARA), Columbus, Ohio.
  173. -- Author: David A. Wheeler
  174. --
  175.  
  176. with Occupants;
  177. use  Occupants;
  178.  
  179. package Items is
  180.  type Item     is new Occupant with private;
  181.  type Item_Access       is access Item'Class;
  182.  function May_I_Get(Direct_Object : access Item;
  183.                     Agent : access Occupant'Class) return Boolean;
  184.  
  185. private
  186.  type Item     is new Occupant with null record;
  187.  
  188. end Items;
  189.  
  190. --
  191. -- Permission to use, copy, modify, and distribute this software and its
  192. -- documentation for any purpose and without fee is hereby granted,
  193. -- provided that the above copyright and authorship notice appear in all
  194. -- copies and that both that copyright notice and this permission notice
  195. -- appear in supporting documentation.
  196. -- 
  197. -- The ARA makes no representations about the suitability of this software
  198. -- for any purpose.  It is provided "as is" without express
  199. -- or implied warranty.
  200. -- 
  201. --
  202. -- Copyright (C) 1996 Ada Resource Association (ARA), Columbus, Ohio.
  203. -- Author: David A. Wheeler
  204. --
  205.  
  206. with Creatures;
  207. use  Creatures;
  208.  
  209. package Monsters is
  210.  type Monster is new Creature with private;
  211.  type Monster_Access    is access Monster'Class;
  212. private
  213.  type Monster is new Creature with null record;
  214. end Monsters;
  215.  
  216. --
  217. -- Permission to use, copy, modify, and distribute this software and its
  218. -- documentation for any purpose and without fee is hereby granted,
  219. -- provided that the above copyright and authorship notice appear in all
  220. -- copies and that both that copyright notice and this permission notice
  221. -- appear in supporting documentation.
  222. -- 
  223. -- The ARA makes no representations about the suitability of this software
  224. -- for any purpose.  It is provided "as is" without express
  225. -- or implied warranty.
  226. -- 
  227. --
  228. -- Copyright (C) 1996 Ada Resource Association (ARA), Columbus, Ohio.
  229. -- Author: David A. Wheeler
  230. --
  231.  
  232. with Text_IO, Ada.Strings.Unbounded, Ustrings, Rooms;
  233. use  Text_IO, Ada.Strings.Unbounded, Ustrings, Rooms;
  234.  
  235. package body Occupants is
  236.  
  237.  
  238.  procedure Put_View(T : access Occupant; Agent : access Thing'Class) is
  239.  begin
  240.   Put("You are inside ");
  241.   Put_Line(Short_Description(T));
  242.   Put_Line(".");
  243.   Put_Contents(T, Agent, "You see:");
  244.  end Put_View;
  245.  
  246.  procedure Look(T : access Occupant) is
  247.  -- T is running a "look" command; tell T what he views.
  248.  begin
  249.   if Container(T) = null then
  250.     Put("You are inside nothing at all.");
  251.   else
  252.     Put_View(Container(T), T);
  253.   end if;
  254.  end Look;
  255.  
  256.  
  257.  procedure Get(Agent : access Occupant; Direct_Object : access Occupant'Class)
  258.  is
  259.  begin
  260.    if May_I_Get(Direct_Object, Agent) then
  261.      Place(T => Direct_Object, Into => Thing_Access(Agent));
  262.    end if;
  263.  end Get;
  264.  
  265.  function May_I_Get(Direct_Object : access Occupant;
  266.                     Agent : access Occupant'Class)
  267.           return Boolean is
  268.  begin
  269.    Sorry("get", Name(Direct_Object));  -- Tell the getter sorry, can't get it
  270.    return False;
  271.  end May_I_Get;
  272.  
  273.  procedure Drop(Agent : access Occupant;
  274.                 Direct_