![]() |
Lab 5
|
Please download the folder lab5_programs
from the
cs111d
account on puma
.
WallHuggerWorld
from lecture (where the Buggle hugs the wall, picking up bagels
on its way).
You can run the applet
WallHuggerWorld here.tick()
method in order to repeat a block of code a ceratin number of times.
Your first task today is to make GohomeWorld
run succesfully. GohomeWorld
is very much like WallHuggerWorld,
with the exception that the Buggle stops
when it is back at point (1,1). Also, there are no bagels on the grid.
To start this task, open GohomeWorld.java
in the lab5_programs
folder
and work through the code to get it to compile, fixing one error at a time.
Think about the best way to make your
buggle stop back at position (1,1).
Remember that you can always use the console window to get more insight into your code.
The purpose of this exercise 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 first row of a BuggleWorld grid. A buggle starting at position (1,1) must run across the first row 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:
Your goal in this problem is to implement three kinds of hurdling buggles:
Open HurdleWorld.java
. It contains two classes:
public class HurdleWorld extends BuggleWorld { // 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 creates a Hurdler
instance
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, which, as you know, does nothing.
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 do nothing.Hurdler
is facing a hurdle, it should jump
the hurdle.Hurdler
has no obstacle 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 cell high. Think about what specific
steps the buggle should take, in order to figure out whether it is facing a wall,
i.e. is at finish line.
Sensor methods, like atFinishLine()
, should leave the position
and heading of the buggle unchanged.jumpHurdle()
method assumes the buggle is
facing a hurdle and causes it to jump it. The buggle should end up
facing EAST in the cell after 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. (As you can imagine, you are at a good start for this problem, if you
just copy the HurdleWorld.java
file into HighHurdleWorld.java
.)
Which methods do you need to change to fit the new specifications?
Note: Your hurdler may not jump 2 cells high if the hurdle ahead 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 start with the HighHurdleWorld.java
file,
in lab5_programs
folder. You should study the given code and the comments there.