![]() |
Problem Set 4 Due on Friday 6 October at the start of class |
Unlike previous problem sets, notes, hints, and suggestions for each homework problem are given on a separate page. We want you to have freedom to think about the problem in your own way. Reading the hints page is not required. Its main purpose is to help students to think about the problem if they don't know how to get started. On the other hand, we do also include some notes on what we think are good ways of going about programming. It is not required that you follow those guidelines exactly. However, your programming style should result in a program that is at least as easy to read and understand as the program would be if you followed our suggestions.
There are two problems in this problem set. We have including working
examples of the solutions to the problems in the test
folder that
you download with the ps04_programs
folder. It is a good idea to
run these programs to get a feel for what we are asking you to do. Your
solution should provide the exact same results as our solution (exceptions
noted below).
The code is available in the
ps04_programs
folder in the cs111 download directory on
the cs
server.
FollowWorld.java
file from Task 1;
DeadEndWorld.java
file from Task 2.
Save the modified FollowWorld.java
and
DeadEndWorld.java
files in the ps04_programs
folder. Submit the entire ps04_programs
folder to your
drop folder on the cs111 server.
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 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 or loop. 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 except for the
cell of the very last bagel. The Follower should stop and rest in that
cell. For example, here is the state of the world after Folla has eaten
all the bagels in the previous picture: |
![]() |
![]() |
To begin this problem, open the test
subfolder of the
ps04_programs
folder that you downloaded for this
assignment and run the FollowWorld
applet. When you
start the 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. When you click on the
Run button, a buggle follows the trail of Bagels, picking them
up as it goes. Your goal is to develop a buggle that will correctly
follow all such trails as in the test
example. You will use
Reset and Run to test your program on a variety
of trails. Your solution should behave exactly like the solution we have
given you in the test
folder.
You will need to create a new file for your solution. Start by
entering the following code into a new file called
FollowWorld.java
:
public class FollowWorld extends BagelTrailWorld { public void run () { Follower folla = new Follower(); folla.tick64(); // Would be nicer to stop when find last bagel, // assume predetermined number of steps for now. } }
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 tick()
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.
You must now add a definition for the Follower
class
(of which folla
is a member) to the file. Note that
folla
understands the tick64()
method. This
means that the Follower
class must be a sublass of the
TickBuggle
class. So you WILL NOT have to define
tick64()
. You WILL have to define
a tick()
method.
Your tick()
method should implement your
bagel-following strategy. Remember that the tick()
method will be sent to Folla exactly 64 times for every bagel trail,
regardless of its length and shape. So tick()
must
appropriately do nothing after all the bagels have been consumed.
It is possible to visit all the cells of a 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 and end up where you started. Along the way, you will visit some cells more than once (i.e., backtrack), but that's OK.
Your goal is to program members of the DeadEndBuggle
class (such as
Deanna) to drop bagels in all the dead ends of a particular maze by using
the "right-hand-on-the-wall" strategy to visit cells in the maze until
the buggle has explored the entire maze. Once the buggle has explored the
entire maze and arrives at the start again, the buggle should not go through
the maze again, but should just rest there. The buggle should leave a mark
behind in every visited cell and a bagel in every dead end of the maze.
Here is a snapshot of the above maze after Deanna is done exploring it:
To begin this problem, open the test
subfolder of the
ps04_programs
folder that you downloaded for this
assignment and run the DeadEndWorld
applet. This applet is a working
solution of the maze-navigating, bagel-dropping buggle. When you
start this applet, you should see a maze like the one shown in the
first picture above. Every time you click on the Reset
button, a new maze will be randomly generated. When you click Run,
the buggle uses the right-hand-on-the-wall strategy to explore the maze
and uses conditionals to figure out where to drop bagels. You should test
your solution to make sure that it works properly on at least the first
3 mazes. The first three mazes include all the possible unique starting
configurations. Your solution should behave exactly like the solution provided
except that your buggle may end at the start position in any
orientation (heading) when she finishes exploring the maze.
Your goal is to develop a program that instructs the buggle to correctly explore entire mazes and drop bagels in all the dead ends. You will use Reset and Run to test your program on a variety of mazes.
You will implement your code in the file DeadEndWorld.java
.
The file DeadEndWorld.java
contains two classes, as shown below:
public class DeadEndWorld extends MazeWorld { public void setup() { // setDimensions(19,19); } public void run () { DeadEndBuggle deanna = new DeadEndBuggle(); deanna.tick256(); } } class DeadEndBuggle extends TickBuggle { Point start = new Point(1,1); public void tick() { // Override the default tick method of the TickBuggle class. // Keep "right finger" of buggle on right wall to explore maze. // Drop bagels in dead ends. } // Add auxiliary methods here. }
DeadEndWorld
class is a subclass of the
MazeWorld
class, whose responsibility is to draw a
connected acyclic maze. The run()
method of the
DeadEndWorld
class creates Deanna and tells her to execute
her tick()
method 256 times. (As usual, a predetermined
number of steps is icky; we will see how to get rid of this ickiness
later in the course via recursion and iteration.) You do not
need to modify the DeadEndWorld
class in this problem.
Note that we have defined a Point start
for you which you may
use to figure out if your 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 that 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)
In this problem, you should "teach" members of the DeadEndBuggle
class how to follow the "right-hand-on-the-wall" strategy by filling in
the details of the tick()
method skeleton in
that class.