CS111, Wellesley College, Fall 2006

Lab 5

Wedn/Thur October 4/5, 2006


Conditionals

Total checkmarks:

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.


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:

  1. A Hurdler that jumps all the hurdles in the grid.
  2. A HighHurdler that jumps all the hurdles (hurdles can be 1 or 2 cells high) in the grid.
  3. A TiredHurdler that jumps at most a prespecified number of hurdles in the grid.

Exercise 1: Hurdler

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:

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:


Exercise 2: HighHurdler

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.


Exercise 3: TiredHurdler

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.