![]() |
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();
Returnstrue
if there is a wall directly to the right of this buggle, andfalse
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:
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)