Spider | Previous | Next | Full Table of Contents | Index | Program List | Copyright

10.6 Continuing Saga: Writing an ADT for Multiple Spiders

In our continuing study of the spider, all packages and programs have assumed that only one spider lives in the room. How could we emulate a situation in which the room contains an entire village of spiders? As we saw in Chapter 7, the spider package implements what we now know as an abstract data object. To provide for multiple spiders, we need an abstract data type. Program 10.19 gives a specification for an ADT package Spiders. This package provides all the operations of Spider and also a type, allowing a client program to declare spider variables. As a close reading will show, additional operations are provided as well.

Program 10.19
Package Specification for Multiple Spiders

PACKAGE Spiders IS
------------------------------------------------------------------------
--| This package provides procedures to emulate multiple           
--| spiders. The spiders can move around        
--| the screen drawing simple patterns.                           
--| Author: John Dalbey, Cal Poly San Luis Obispo, 1992
--| Adapted by: Michael B. Feldman, The George Washington University
--| Last Modified: July 1995
------------------------------------------------------------------------
 
  TYPE Switch       IS (On, Off);                   
  TYPE Directions IS (North, East, South, West);
  TYPE ScreenColors IS (Red, Blue, Green, Black);  
 
  MaxHeight: Positive := 20;
  MaxWidth:  Positive := 20;
  SUBTYPE RoomHeight IS Positive RANGE 1..MaxHeight;
  SUBTYPE RoomWidth  IS Positive RANGE 1..MaxWidth;
 
  TYPE Spider IS PRIVATE;
 
  Hit_The_Wall: EXCEPTION;
  
  PROCEDURE DrawRoom;
  -- Pre:  None
  -- Post: Spider's room appears on the screen 
 
  PROCEDURE Start (Which: IN OUT Spider; 
                   Row: IN RoomHeight; Col: IN RoomWidth;
                   WhichColor: IN ScreenColors; 
                   WhichWay: IN Directions);
  -- Pre:  None
  -- Post: Spider appears in the given position.
 
  PROCEDURE Blue  (Which: IN OUT Spider);
  PROCEDURE Green (Which: IN OUT Spider);
  PROCEDURE Red   (Which: IN OUT Spider);
  PROCEDURE Black (Which: IN OUT Spider);
  -- Pre:  None
  -- Post: Change the color of ink with which the spider draws
  --   On black-and-white screen, uses different characters
  --   to represent the colors.
 
  PROCEDURE Face (Which: IN OUT Spider; WhichWay: IN Directions);
  -- Pre:  WhichWay has been assigned a value
  -- Post: Spider turns to face the given direction.
 
  FUNCTION IsFacing (Which: Spider) RETURN Directions;
  -- Pre:  None
  -- Post: Returns the direction the spider is facing.
 
  PROCEDURE Step (Which: IN OUT Spider);
  -- Pre:  None
  -- Post: Spider takes one step forward in the direction it is facing.
  -- Raises: Hit_the_Wall is if spider tries to step into a wall.
 
  PROCEDURE Jump (Which: IN OUT Spider; HowMany: IN Positive);
  -- Pre:  None
  -- Post: Spider jumps forward the given number of steps, without
  --   leaving tracks at each step.
  -- Raises: Hit_the_Wall if spider tries to step into or over a wall.
 
  PROCEDURE Right (Which: IN OUT Spider);
  -- Pre:  None
  -- Post: Spider turns 90 degrees to the right.
 
  FUNCTION  AtWall (Which: Spider) RETURN Boolean;
  -- Pre:  None
  -- Post: Returns True if the spider is standing next to a wall 
  --   (edge of the room) and facing it, and False otherwise.
 
  FUNCTION  NearWall (Which: Spider; HowMany: Positive) RETURN Boolean;
  -- Pre:  None
  -- Post: Returns True if the spider is standing within
  --   HowMany steps of a wall and facing it, and False otherwise.
 
  PROCEDURE Quit;
  -- Pre:  None
  -- Post: End the drawing
 
  PROCEDURE Debug (Setting: Switch);
  -- Pre:  None
  -- Post: Turns on or off single stepping through the program.
 
  FUNCTION  Debugging RETURN Switch; 
  -- Pre:  None
  -- Post: Returns on or Off depending on Debug setting
 
PRIVATE
 
  TYPE Spider IS RECORD
    Ink           : ScreenColors;
    Heading       : Directions;
    CurrentColumn : RoomWidth;               -- spider's position
    CurrentRow    : RoomHeight;              --      in the room.
  END RECORD;
 
END Spiders;
 

To give an idea of how the ADT package operates, Program 10.20 shows several different spiders drawing boxes. We omit the package body; you can write it as a modified version of the original Spider body ( Program 7.11).

Program 10.20
Many Spiders Drawing Many Boxes

WITH Spiders;
USE Spiders;
PROCEDURE Draw_Many_Boxes IS
------------------------------------------------------------------------
--| Draw several boxes with spiders - use loop
--| Author: Michael B. Feldman, The George Washington University 
--| Last Modified: July 1995                                     
------------------------------------------------------------------------
 
  Spider1 : Spider;
  Spider2 : Spider;
  Spider3 : Spider;
 
BEGIN -- Draw_Many_Boxes    
 
  Debug(On);
  DrawRoom;
 
  Start (Which => Spider1, Row => 10, Col => 11,
                 WhichColor => Green, WhichWay => North);
 
  Start (Which => Spider2, Row => 10, Col => 11,
                 WhichColor => Blue, WhichWay => North);
 
  Right (Spider2);
  Jump  (Spider2, 4);
 
  Start (Which => Spider3, Row => 10, Col => 11,
                 WhichColor => Red, WhichWay => North);
 
  Right (Spider3);
  Right (Spider3);
  Right (Spider3);
  Jump  (Spider3, 3);
 
  FOR Side IN 1..4 LOOP
    FOR Count IN 1..3 LOOP
      Step (Spider1);
    END LOOP;
    Right (Spider1);
  END LOOP;
 
  FOR Side IN 1..4 LOOP
    FOR Count IN 1..4 LOOP
      Step (Spider2);
    END LOOP;
    Right (Spider2);
  END LOOP;
 
  FOR Side IN 1..4 LOOP
    FOR Count IN 1..5 LOOP
      Step (Spider3);
    END LOOP;
    Right (Spider3);
  END LOOP;
 
  Quit;
 
END Draw_Many_Boxes; 
 

Sample Run

                    ----------------------------------------- 
                   |. . . . . . . . . . . . . . . . . . . . .|
                   |. . . . . . . . . . . . . . . . . . . . .|
                   |. . . . . . . . . . . . . . . . . . . . .|
                   |. . . . . . . . . . . . . . . . . . . . .|
                   |. . + + + + + + . . . . . . . . . . . . .|
                   |. . + . . . . + . . . . . . . . . . . . .|
                   |. . + . . . . + . . O O O O . . . . . . .|
                   |. . + . . . . + . . O . . O . . . . . . .|
                   |. . + . . . . + . . O . . O . . . . . . .|
                   |. . + + + + + * . . * O O O * X X X X . .|
                   |. . . . . . . . . . . . . . X . . . X . .|
                   |. . . . . . . . . . . . . . X . . . X . .|
                   |. . . . . . . . . . . . . . X . . . X . .|
                   |. . . . . . . . . . . . . . X X X X X . .|
                   |. . . . . . . . . . . . . . . . . . . . .|
                   |. . . . . . . . . . . . . . . . . . . . .|
                   |. . . . . . . . . . . . . . . . . . . . .|
                   |. . . . . . . . . . . . . . . . . . . . .|
                   |. . . . . . . . . . . . . . . . . . . . .|
                   |. . . . . . . . . . . . . . . . . . . . .|
                    ----------------------------------------- 
 


Spider | Previous | Next | Full Table of Contents | Index | Program List | Copyright

Copyright ©1996 by Addison-Wesley Publishing Company, Inc.