Graphic by Keith Ohlfs

CS111, Wellesley College, Fall 1997

Problem Set 4:
Bagging Bagels

Due: Sunday, November 2, 11:59pm

[CS111 Home Page] [Syllabus] [Students] [Lecture Notes] [Assignments] [Programs] [Documentation] [Software Installation] [FAQ] [CS Dept.] [CWIS]

We all know that buggles like to eat bagels. In this problem set, you will explore two problems that involve a hungry buggle looking for bagels. Both problems require you to exercise your knowledge of conditionals in the context of implementing strategies for buggle motion.


Problem 1: FollowWorld

In the FollowWorld problem, a buggle named Folla at coordinate (1,1) has before her a tantalizing trail of bagels. The exact length and shape and length of the trail is not specified. However, it is known that (1) the bagel trail has at least one bagel; (2) it starts at coordinate (1,1); and (3) it forms a continuous line that may bend in many different directions but may never branch out. For example, here is a sample bagel trail:

Your goal is to program members of the Follower class (such as Folla) to follow the trail of bagels. At every position, a Follower should eat (i.e., pick up) the bagel at that position, and then move in the direction of the next bagel. This process should continue until there are no more bagels. A Follower should also leave behind a colored mark in every grid cell that formerly contained a bagel so that the original path of the bagels is clear from the marks. For example, here is the state of the world after Folla has eaten all the bagels in the above picture:

To begin this problem, download the FollowWorld folder via Fetch from the ~cs111/download directory on Nike. You should rename this folder to username_FollowWorld, where username is your Nike user name. To save you time, we do not ask you to rename any of the files within this folder.

Before you look at any of the files, use the AppletViewer to run FollowWorld.html. When you start this applet, you should see the bagel trail shown in the first picture above. Every time you click on the Reset button, a new bagel trail will be randomly generated. Your goal is to develop a buggle that will correctly follow all such trails. You will use Reset and Run to test your program on a variety of trails.

Open the project file FollowWorld.proj. This contains many files, but the only one you need to look at is FollowWorld.java. This file contains two classes, as shown below:

public class FollowWorld extends BagelTrailWorld {
		
  public void run () {
    Follower folla = new Follower();
    folla.move64(); // Would be nicer to stop when find last bagel, 
		         // but assume a predetermined number of steps for now.
    }  
  }
 
class Follower extends Mover {
	
  public void move () {
    // Override default move method of Mover class. 
    // Follow the trail of bagels until they have all be picked up. 
  }
}

The FollowWorld class is a subclass of the BagelTrailWorld class, whose responsibility is to draw bagel trails subject to the constraints mentioned above. The run() method of the FollowWorld class creates Folla and tells her to execute her move() method 64 times. (We use a predetermined number of steps, but it would be much more elegant to have Folla move until she has eaten all the bagels in the trail. We will show how to accomplish this via recursion in lecture.) You do not need to modify the FollowWorld class in this problem.

In this problem, you should develop a bagel-following strategy for the Follower class and implement it in terms of the move() method in that class. You should first develop a set of motion rules in the same style as those discussed for the Snaker class in lecture on October 23, 1997. Then you should implement your rules in the move() method of the Follower class. (Note: you can download the code for Snaker.java from the Buggles folder inside the CS111 download directory.) Remember that the move() method will be sent to Folla exactly 64 times for every bagel trail, regardless of its length and shape. So move() must appropriately "do nothing" after all the bagels have been consumed.

Some notes/hints/suggestions:

	public void setup() {
	  setDimensions(columns,rows);
	}
 

To submit this problem, turn in your entire modified username_FollowWorld folder into your PS4 drop folder on Nike.


Problem 2: FindWorld

In the FindWorld problem, a hungry buggle named Fiona starting at coordinates (1,1) is attempting to find a single bagel hidden in an connected acyclic maze. An connected acyclic maze is a maze in which there is a unique non-backtracking path from any point in the maze to any other. This means that it is always possible for Fiona to find her bagel! Here is an example of a connected acyclic maze:

It is possible to visit all the cells of an connected acyclic maze by using the "right-hand-on-the-wall" strategy. That is, if you always keep your right hand on a wall as you walk through such a maze, you will eventually visit all the cells. Along the way, you will visit some cells more than once (backtrack), but that's OK.

Your goal is to program members of the Finder class (such as Fiona) to find the bagel in the maze by using the "right-hand-on-the-wall" strategy to visit cells in the maze until the bagel is found. Upon finding the bagel, the buggle should not eat it. but just sit on top of it. The buggle should leave a mark behind in every visited cell except for the cell containing the bagel. Here is a snapshot of the above grid when Fiona finds the bagel:

To begin this problem, download the FindWorld folder via Fetch from the ~cs111/download directory on Nike. You should rename this folder to username_FindWorld, where username is your Nike user name. To save you time, we do not ask you to rename any of the files within this folder.

Before you look at any of the files, use the AppletViewer to run FindWorld.html. When you start this applet, you should see the maze and bagel shown in the first picture above. Every time you click on the Reset button, a new maze and bagel will be randomly generated. Your goal is to develop a buggle that will correctly find the bagel in any such maze. You will use Reset and Run to test your program on a variety of trails.

Open the project file FindWorld.proj. This contains many files, but the only one you need to look at is FindWorld.java. This file contains two classes, as shown below:

public class FindWorld extends MazeWorld {
	
	public void run () {
		Finder fiona = new Finder();
		fiona.move256(); // Would be nicer to stop when find bagel, 
		                 // but assume a predetermined number of steps for now.
	}
	
}
 
class Finder extends Mover {
	
	public void move() {
		// Override the default move method of the Mover class. 
		// Keep "right finger" of buggle on right wall until find bagel.
	}
	
}
 

The FindWorld class is a subclass of the MazeWorld class, whose responsibility is to draw a connected acyclic maze with a bagel in it. The run() method of the FindWorld class creates Fiona and tells her to execute her move() method 256 times. (Again, a predetermined number of steps is icky; we will see how to get rid of this ickiness via recursion.) You do not need to modify the FindWorld class in this problem.

In this problem, you should "teach" members of the Finder class how to follow the "right-hand-on-the-wall" strategy by filling in the details of the move() method in that class. As in the FollowWorld problem, you should first develop a set of motion rules (draw lots of pictures!), and then implement these rules in terms of the move() method. All of the notes/hints/suggestions from the FollowWorld problem also apply in this problem. In particular, you are encouraged to (1) discuss the strategy with your classmates and (2) define any auxiliarly methods that you find helpful.

To submit this problem, turn in your entire modified username_FindWorld folder into your PS4 drop folder on Nike.