CS111 Lab 5 - Conditionals

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 two new Buggle environments: HurdleWorld and FollowWorld.

HurdleWorld

This fall, 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. In the Test subfolder of the lab5_programs folder is an applet HurdleWorld.java that you should run with the Applet Viewer. This applet 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:

  1. A Hurdler that jumps all the hurdles in the grid.
  2. A TiredHurdler that jumps at most a presepecified number of hurdles in the grid.

Exercise 1: Hurdler

In the lab5_programs folder,open the lab5.proj 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 you can ignore 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 discussed in lecture. Hurdler has a tick() method that specifies what it does on each clock tick. This method overrides the default tick() message 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: 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.

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.

 

FollowWorld

In the FollowWorld problem, a buggle named Folla at coordinate (1,1) has before her a tantalizing trail of bagels. The exact length and shape and length of the trail is not specified. However, it is known that (1) the bagel trail has at least one bagel; (2) it starts at coordinate (1,1); and (3) it forms a continuous line that may bend in many different directions but may never branch out. For example, here is a sample bagel trail:

Your goal is to program members of the Follower class (such as Folla) to follow the trail of bagels. At every position, a Follower should eat (i.e., pick up) the bagel at that position, and then move in the direction of the next bagel. This process should continue until there are no more bagels. A Follower should also leave behind a colored mark in every grid cell that formerly contained a bagel except for the cell of the very last bagel. The Follower should stop and rest in that cell. For example, here is the state of the world after Folla has eaten all the bagels in the previous picture:

Exercise 3: Follow a Bagel Trail

To begin this problem, use the AppletViewer to run FollowWorld.html in the Test subfolder of the lab5_programs folder. When you start this applet, you should see the bagel trail shown in the first picture above. Every time you click on the Reset button, a new bagel trail will be randomly generated. When you click on the Run button, a buggle follows the trail of Bagels, picking them up as it goes. Your goal is to develop a buggle that will correctly follow all such trails as in the Test example. You will use Reset and Run to test your program on a variety of trails. Your solution should behave exactly like the solution we have given you in the Test folder.

In the lab5_programs folder, open the FollowWorld.java file. This file contains two classes, as shown below:

public class FollowWorld extends BagelTrailWorld {
  public void setup() {
    // setDimensions(11,19);
  }
  public void run () {
    Follower folla = new Follower();
    folla.tick64(); // Would be nicer to stop when find last bagel, 
    // but assume a predetermined number of steps for now.
  }  
}
 
class Follower extends TickBuggle {
  public void tick () {
    // Override default tick method of TickBuggle class. 
    // Follow the trail of bagels until they have all been picked up. 
  }
}
 

The FollowWorld class is a subclass of the BagelTrailWorld class, whose responsibility is to draw bagel trails subject to the constraints mentioned above. The run() method of the FollowWorld class creates Folla and tells her to execute her tick() method 64 times. (We use a predetermined number of steps, but it would be much more elegant to have Folla move until she has eaten all the bagels in the trail. We will show how to accomplish this via recursion in lecture.) You do not need to modify the FollowWorld class in this problem.

In this problem, you should develop a bagel-following strategy for the Follower class and implement it in terms of the tick() method in that class. You should first develop a set of motion rules in the same style as those discussed for the IfBuggle class in lecture. Then you should implement your rules in the tick() method of the Follower class. (Note: you can download the code for IfWorldAnswers.java from the lec8_and_lec9_programs folder inside the CS111 download directory.) Remember that the tick() method will be sent to Folla exactly 64 times for every bagel trail, regardless of its length and shape. So tick() must appropriately "do nothing" after all the bagels have been consumed.

 

Notes, Hints, Suggestions