CS111, Wellesley College

Problem Set 4

Back to Problem Set 4

Task 2 Notes, Hints, Suggestions

Before writing any code, you should think carefully about the kinds of "before and after" rules your buggle should be following. That is, describe the buggle motion in terms of rules like "if the buggle is in configuration A before its move, it should be in configuration B after its move." For example, what should the buggle do when there is a wall to its right? when there is not a wall to its right? when there is a wall in front of it? when there is not a wall in front of it? These conditions are not orthogonal; e.g., the buggle may have a wall both in front of it and to its right.

Try to narrow the number of rules into the smallest number that implement a correct strategy. We strongly encourage you to work with your classmates on this part of the problem.

Once you have figured out a strategy, you should encode that strategy in Java in the body of the tick() method in DeadEndWorld.java. As always, you should feel free to define any auxiliary methods that you find helpful. In this problem, it is particularly helpful to define the following auxiliary method:

public boolean isWallToRight();
Returns true if there is a wall directly to the right of this buggle, and false otherwise. When this method returns, this buggle should have exactly the same position and heading as it did before the method was called.

It is possible to write a clear and correct solution to this problem in remarkably few lines of code. If you find yourself lost in a rats nest of conditionals, you are probably on the wrong track.

Here's one suggestion for how to break the problem down into smaller pieces:

  1. Make the buggle traverse the entire maze.
  2. Make the buggle identify dead ends and drop bagels there.
  3. Make the buggle stop when it reaches the starting position after traversing the entire maze.
The third goal is tricky! Be sure to finish at least the first two goals before spending a lot of time on the third goal. And when trying to solve the third goal:

The DeadEndBuggle class defines a Location instance named start that may be helpful for determining if the buggle is in the starting cell. As an example, the expression

this.getPosition().equals(start)
returns a boolean value of true or false indicating whether or not the buggle is in the starting cell.

You may also want to be able to check if your buggle is headed in a particular direction. The expression to determine if your buggle is facing the Direction defined as EAST follows:

this.getHeading().equals(Direction.EAST)

Back to Problem Set 4