Wednesday, February 26
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. For example, 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 project in the test
subfolder of the 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 two kinds of hurdling buggles:
In the lab5_programs
folder, open the lab5.mcp
file. Open the
HurdleWorld.java file, which 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 clock 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:
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:
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.
Here is the skeleton for a TiredHurdler class that you can find in HurdleWorld.java:
class TiredHurdler extends Hurdler { 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 () { Hurdler happy = new Hurdler(); happy.tick16(); TiredHurdler harried = new TiredHurdler(3); harried.setColor(Color.green); harried.tick16(); }
This causes TiredHurdler harried to follow Hurdler happy part of the way through a course.