CS111, Wellesley College

Lab 5

Wedn October 3, 2007


Today:

Please download the folder lab5_programs from the cs111d account on puma.

Part I: Debugging GohomeWorld

Recall WallHuggerWorld from lecture (where the Buggle hugs the wall, picking up bagels on its way). You can run the applet WallHuggerWorld here.
Remember that WallHuggerWorld uses the 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.

Part II: Conditionals

Total checkmarks:

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.


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:

  1. A Hurdler that jumps all the hurdles in the grid. All hurdles are 1 cell high.
  2. A HighHurdler that jumps all the hurdles. This time hurdles can be 1 or 2 cells high.
  3. A TiredHurdler that jumps at most a pre-specified number of hurdles in the grid.

Exercise 1: Hurdler

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:

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. (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.


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 start with the HighHurdleWorld.java file, in lab5_programs folder. You should study the given code and the comments there.