Computer Science 101 Lab 5: Geography
Due: February 24, 1997

Objectives and Overview: This lab provides more experience with procedures and functions, as well as an opportunity to access information in files.

Suppose you are in the middle of a perfectly square island jungle and are trying to escape to the shore, where there is a boat waiting to take you home. However, between you and the boat lie four life-threatening hazards at unknown (random) locations in the jungle -- an elephant, a lion, a snake, and a cliff -- as pictured below.

Initially, you are located at (0, 0) in the middle of the island, the boat is at (-3, 10) on the North shore, and the four hazards are at other locations unknown to you.

The purpose of this program is to randomly locate both the boat and the hazards in the jungle, and then monitor your progress as you take individual steps out of the jungle toward the boat. The program should initially tell you the location of the boat and fix your initial location at (0,0), the middle of the jungle. But the hidden locations of the hazards should be read from a separate file by the program and not displayed on the screen. Your progress toward the boat will be monitored by the program, which will record and display your new location after each move.

Each of your moves can be one step in either the North, South, East, or West direction. Each integer x or y coordinate value in the picture above represent a single step. For instance, if you take one step North from your starting location, your new location will be (0, 1); taking one step East would have left you at (1, 0); and so on. This program should have two major parts. One part initializes the game by reading the random locations of the four hazards and the boat from the separate file named 'unknowns' (this file is provided in the CS101/Tucker folder). You can assume that the x and y coordinates of the boat are the first two integers in the file, and each of the four hazards is located by a separate pair of integers in the file. Thus, the file will have 10 integers in all.

The second part of the program "plays" the game by asking you to take a series of steps in any of four directions (N, S, E, or W), keeping track of your location after each step, and reporting to you whether you have escaped the jungle or been consumed by one of the hazards along your route.

Thus, it is good to think of designing this program using a separate procedure, say Initialize, that initializes certain variables before the game begins. That procedure should open and read integers from the file 'unknowns' in order to obtain the initial locations of the boat and the hazards.

The second part of your program should include a while loop, in which each repetition will retrieve a single move (N, E, S, or W) that you take, record its effect on your position in the jungle, and display your new position. That display should also report any unusual outcome of that move (i.e., being consumed by a hazard, reaching the shore, or reaching the boat safely). The possibility of being consumed by a hazard should be separately defined by a function, say IsConsumed, whose parameters are your position and the positions of the four hazards. This function should return a Boolean result, true or false, depending on whether or not your current position is identical with that of one of the four hazards.

Part 1 - Initial Design Considerations

Define the variables that will represent the locations of the various objects in the game -- you, the boat, and each of the four hazards.

The Initialize procedure should have these locations as variable (VAR) parameters, and should handle all the file input that is required to fix these locations. It should also display to the user the location of the boat.

The function IsConsumed should also have these locations as parameters, as well as your location.

The second part of the program should be controlled by a while loop. What should occur at each repetition of that loop? That is, what is needed to model a single step in the jungle? Certainly the program needs to read an input move from the user (N, S, E, or W), interpret that move by changing the value of the x- or y-coordinate of their current location, checking whether the new location encounters either a hazard, the boat, or the ocean, and displaying that outcome to the user. Determine the condition under which this while loop will finally terminate.

Part 2 - Write and Run the Program

Complete the program, including the development of the Initialize procedure and IsConsumed function, and then run it several times to make sure that it responds appropriately to each of the possible outcomes suggested above (reaching the boat safely, being consumed, or wandering into the ocean).

(Optional Part) - Allow the Hazards to be Avoided

The program can be refined so that you are warned if any of the hazards becomes too close (say within one unit away from your current position) after each step. This gives you an opportunity to adjust your next step before blindly running into the hazard. This optional refinement can also be captured in the form of a Boolean function, say TooClose, which has as parameters your position along with the positions of the four hazards.

Lab 5 Deliverables:

Answer the following questions about this design experience, and hand in a hard copy of your answers.

  1. What are the locations of the boat and the hazards for the particular file 'unknowns' that is provided?
  2. What is the complete output of a run of this program for these particular locations?
  3. What would be an easy way to test this program against two or three alternative initial locations of the hazards, rather than the particular locations used here?
  4. How did you decide to handle the situation where the user escapes the jungle (reaches the shore) but not at the location of the boat? What are the design alternatives here?
  5. Why are functions and procedures useful for this program? Could you do the program without them? Discuss the tradeoffs in making this choice.
  6. Why is a file useful for this program? Could you solve this problem without one? How?
Also, save a copy of your complete program for this problem as lab5.yourname.p, and drag a copy of it to the Drop Box in the CS101/Tucker folder.