![]() |
Lab 5
|
The purpose of this lab is to give you experience with conditionals. You will learn how and when to use conditionals to solve problems in a new Buggle environment: HurdleWorld.
Buggles are putting on their sneakers in preparation for one of their favorite activities: hurdle jumping! In this activity, a number of hurdles (walls one grid cell high) are placed at the base of a BuggleWorld grid. A buggle starting at position (1,1) must run across the base of the grid jumping all of the hurdles in its path until it reaches the opposite wall. The buggle does not know the placement of the hurdles in advance and so must detect them when it encounters them. Here is a sample hurdle configuration:
and here is the result of a buggle jumping all of the hurdles:
To begin this problem, download the lab5_programs
folder from the CS111 download folder. The code in the
test
subfolder of lab5_programs
shows the
correct behavior of a hurdling buggle. The applet begins with a random
number and placement of hurdles. Pressing Run sets the hurdler
in motion. Each time Reset is pressed, a new configuration of
grid cells and hurdles is generated.
Your goal in this problem is to implement three kinds of hurdling buggles:
Open HurdleWorld.java
. It contains three classes, two
of which are shown below:
public class HurdleWorld extends BuggleWorld { Randomizer rand = new Randomizer(5); // Lots of code that you can ignore is omitted here. public void run () { Hurdler happy = new Hurdler(); happy.tick16(); } } class Hurdler extends TickBuggle { public void tick() { } public boolean atFinishLine() { // Return true if buggle is facing wall (as opposed to hurdle) and false otherwise. // Should leave buggle in same position and heading as when it starts. // Should not leave any marks behind. } public void jumpHurdle() { // Cause the buggle to jump the hurdle it is facing. } }
The HurdleWorld
class is responsible for changing the
size of the grid and populating it with a random number of
hurdles. You do not have to understand the code that performs these
actions. All that you have to understand about
HurdleWorld
is that it has a run()
method
that makes a Hurdler
instance named happy
and tells this buggle to move for 16 ticks. The Hurdler
class is a subclass of the TickBuggle
class.
Hurdler
has a tick()
method that specifies
what it does on each clock tick. This method overrides the default
tick()
method supplied by the TickBuggle
superclass.
You should flesh out the tick()
method so that it
implements the following behavior:
Hurdler
is at the finish line (facing the
rightmost wall), it should not attempt to move forward.Hurdler
is facing a hurdle, it should jump
the hurdle.Hurdler
has no wall in front of it, it
should step forward one step.As is often the case, the specification of the tick()
method can be simplified with some auxiliary methods. You should
define these methods as part of your solution:
atFinishLine()
method returns a boolean
indicating whether the Hurdler is at the finish line. It can
disinguish the finish line from a hurdle because the finish line is
a wall that is more than one story high. To distinguish these
cases, the Hurdler
must move up one cell and use its
wall detector to see if there is a wall at the second story. Once
it determines the result, it should move back to its initial
position and heading before returning it. In general, new sensor
methods like atFinishLine()
should leave the position
and heading of a buggle invariant; otherwise it would usually be
necessary to undo the movements of the sensor method after calling
it.jumpHurdle()
method assumes the buggle is
facing a hurdle and causes it to jump the hurdle. It should end up
facing EAST in the cell opposite the hurdle.Now, however, the Olympic Officials have suddenly changed the rules! Hurdles can be one or two cells high! And they are randomly placed in the track. So, a hurdler must figure out whether to jump one or two cells high with each hurdle. Here are two sample hurdle runs:
Open up the HighHurdleWorld.java
file to write code for
this problem. Which methods do you need to change to fit the
HighHurdles? You may want to reuse certain parts of your
HurdleWorld.java
code from Exercise 1.
Note: Your hurdler may not jump 2 cells high if the current hurdle is only 1 cell high.
After a long day of hurdling, buggles are often too tired to jump
all of the hurdles in a configuration. The TiredHurdler
class describes buggles who will jump no more than a specified number
of hurdles. The constructor method for TiredHurdler
takes an integer that is the maximum number of hurdles the buggle will
jump. For instance, new TiredHurdler(3)
creates a buggle
that will jump no more than three hurdles. If it encounters a fourth
hurdle, it will stop moving.
For this task, you can edit your code within the
HighHurdleWorld.java
file.
Here is the skeleton for a TiredHurdler
class that you
can find in HighHurdleWorld.java
:
class TiredHurdler extends HighHurdler { private int hurdlesLeft; public TiredHurdler(int hurdlesLeft) { this.hurdlesLeft = hurdlesLeft; } public void tick() { } }
The class has an integer instance variable hurdlesLeft
that keeps track of how many more hurdles the buggle is willing to
jump. The constructor method for TiredHurdler
initialize
this instance variable to the number supplied when the instance is
constructed. When a TiredHurdler
runs the course, it
should decrement this instance variable every time it jumps a hurdle
and refuse to jump a hurdle when this instance variable contains
0
.
Use the following version of run()
to test your
TiredHurdler
:
public void run () { HighHurdler hope = new HighHurdler(); hope.tick16(); TiredHurdler harried = new TiredHurdler(3); harried.setColor(Color.green); harried.tick16(); }
This causes TiredHurdler harried to follow HighHurdler hope part of the way through a course.