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

3.8 Continuing Saga: Introducing the Spider

This section is the first of several installments in a "continuing saga," an example that begins here and recurs in later chapters. We introduce an imaginary "spider" that steps around an imaginary "room" drawn on the screen. The spider recognizes a number of commands, which we can issue by writing, compiling, and executing spider programs.

Program 3.11 introduces a package called Spider, in which is given the set of commands the spider recognizes and obeys. The body of the spider package appears as Program 7.11. We omit the body here because it is fairly long and the code in it is a bit advanced for this point in the book. For now, we just show two programs that use some of the commands in the spider package. As in the case of the Screen package and Smiley, you can compile and run these after you compile the spider package specification and body. By the time you've completed Chapter 7, you will be able to understand the entire spider package.

Program 3.11
The Spider and Its Commands

PACKAGE Spider IS
------------------------------------------------------------------------
--| This package provides procedures to emulate "Spider" 
--| commands. The spider can move around 
--| the screen drawing simple patterns. 
--| Original Author: John Dalbey, Cal Poly San Luis Obispo, 1992
--| Adapted by: Michael B. Feldman, The George Washington University
--| Last Modified: July 1995
------------------------------------------------------------------------
 
  TYPE Directions IS (North, East, South, West);
 
  Hit_The_Wall: EXCEPTION;
 
  PROCEDURE Start;
  -- Pre:  None
  -- Post: Spider's room appears on the screen with spider in center.
 
  PROCEDURE Quit;
  -- Pre:  None
  -- Post: End the drawing
 
  PROCEDURE Step;
  -- 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 Right;
  -- Pre:  None
  -- Post: Spider turns 90 degrees to the right.
 
  PROCEDURE Face (WhichWay: IN Directions);
  -- Pre:  WhichWay has been assigned a value
  -- Post: Spider turns to face the given direction.
 
  FUNCTION IsFacing RETURN Directions;
  -- Pre:  None
  -- Post: Returns the direction the spider is facing.
 
  PROCEDURE Blue;
  PROCEDURE Green;
  PROCEDURE Red;
  PROCEDURE Black;
  -- 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.
 
  FUNCTION  AtWall 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.
 
  TYPE Switch IS (On, Off);
 
  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
 
END Spider;
 

Returning now to Program 3.11, the package provides an enumeration type

    TYPE Directions IS (North, East, South, West);
 

giving the compass points. On the computer screen, these correspond to up, right, down, and left, respectively.

As in Program 3.8, the specifications of the spider commands are given as pre- and postconditions. The spider can take one step forward, leaving tracks in its current color. It can also turn right, face a given direction, and change the color of its tracks. Also appearing here is Hit_the_Wall, our first example of a user-written exception; this exception is raised if the spider tries to step into a wall.

Let's look at a very simple spider program, Program 3.12. As you can see, it just calls the spider's start and stop commands. The sample run shows that calling Spider.Start draws a "room" on the screen, placing the spider icon (an asterisk) in the center. The spider starts out facing north (up the screen), and leaving green tracks. The box on the left shows the spider's direction, with north, south, east, and west represented by ^, >, v, and < respectively. This version of the package is designed to work on normal monochrome terminals, so the colors are represented by characters, with black, red, blue, and green shown as ., +, X, and 'O'. The "room" is filled with dots representing black, so black spider tracks will seem to be invisible.

Program 3.12
The Simplest Spider Program

WITH Spider;
PROCEDURE Startup IS
------------------------------------------------------------------------
--| Very simple Spider program; just starts and stops
--| Author: Michael B. Feldman, The George Washington University
--| Last Modified: July 1995
------------------------------------------------------------------------
BEGIN -- Startup
  Spider.Start;
  Spider.Quit;
END Startup;
 

Sample Run

                    ----------------------------------------- 
 ---               |. . . . . . . . . . . . . . . . . . . . .|
| ^ |              |. . . . . . . . . . . . . . . . . . . . .|
| O |              |. . . . . . . . . . . . . . . . . . . . .|
 ---               |. . . . . . . . . . . . . . . . . . . . .|
                   |. . . . . . . . . . . . . . . . . . . . .|
                   |. . . . . . . . . . . . . . . . . . . . .|
                   |. . . . . . . . . . . . . . . . . . . . .|
                   |. . . . . . . . . . . . . . . . . . . . .|
                   |. . . . . . . . . . . . . . . . . . . . .|
                   |. . . . . . . . . . . . . . . . . . . . .|
                   |. . . . . . . . . . * . . . . . . . . . .|
                   |. . . . . . . . . . . . . . . . . . . . .|
                   |. . . . . . . . . . . . . . . . . . . . .|
                   |. . . . . . . . . . . . . . . . . . . . .|
                   |. . . . . . . . . . . . . . . . . . . . .|
                   |. . . . . . . . . . . . . . . . . . . . .|
                   |. . . . . . . . . . . . . . . . . . . . .|
                   |. . . . . . . . . . . . . . . . . . . . .|
                   |. . . . . . . . . . . . . . . . . . . . .|
                   |. . . . . . . . . . . . . . . . . . . . .|
                   |. . . . . . . . . . . . . . . . . . . . .|
                    ----------------------------------------- 
 

Now let's look at Program 3.13, which commands the spider to do something, namely, to draw a box by taking three steps and turning right, then three more steps and a turn, and so on. Also, we ask the spider to start out facing west and to change its color. You can see the result in the sample run.

Program 3.13
The Spider Draws a Box

WITH Spider;
PROCEDURE Draw_Box IS
------------------------------------------------------------------------
--| Draw 4 x 4 box with spider
--| Author: Michael B. Feldman, The George Washington University
--| Last Modified: July 1995
------------------------------------------------------------------------
BEGIN -- Draw_Box
 
  Spider.Start;
  Spider.Face (WhichWay => Spider.West);
 
  Spider.Step;
  Spider.Step;
  Spider.Step;
  Spider.Right;
 
  Spider.Step;
  Spider.Step;
  Spider.Step;
  Spider.Right;
 
  Spider.Red;
  Spider.Step;
  Spider.Step;
  Spider.Step;
  Spider.Right;
 
  Spider.Blue;
  Spider.Step;
  Spider.Step;
  Spider.Step;
  Spider.Right;
 
  Spider.Quit;
 
END Draw_Box;
 

Sample Run

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

We'll come back to the spider several more times during the course of the book; our next visit will be in Chapter 5. In the meantime, you might want to experiment with simple spider programs. For example, what happens if the program contains 15 forward steps with no turn? Also try writing

    Spider.Debug(Setting => On);
 

to see what happens.


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

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