/* FILE NAME: HurdleWorldNODANCESolns.java * AUTHOR: Stella * WHEN: Oct 1, 2007 * WHAT: For the purpose of Lab5, F07: Task 2, Exercise 1: Hurdler. * In this version the buggle will NOT "dance" once it reaches the wall. * MODIFICATION HISTORY: This version is based on an older one, by ??? */ import java.awt.*; public class HurdleWorldNODANCESolns extends BuggleWorld { Randomizer rand = new Randomizer(5); public void run () { Hurdler happy = new Hurdler(); happy.tick16(); } //--------------------- // The following main() method is needed to run the applet as an application public static void main (String[] args) { runAsApplication(new HurdleWorldNODANCESolns(), "HurdleWorld NO DANCE Solutions"); } //You don't need to understand the following HurdleWorld methods. public void reset() { super.reset(); int hurdles = rand.intBetween(0,cols-1); placeHurdles(hurdles, cols); } public void setup () { int side = rand.intBetween(5,16); setDimensions(side, side); } public void placeHurdles(int hurdles, int side) { if (hurdles > 0) { int x = rand.intBetween(1, side-1); if (isVerticalWallAt(x,0)) { //System.out.println("lose (" + x + ", " + y + ")"); // We lose; try again! placeHurdles(hurdles, side); } else { // We win; place a bagel //System.out.println("win (" + x + ", " + y + ")"); addVerticalWall(x, 0); placeHurdles(hurdles - 1, side); } } } } //************************************************************** class Hurdler extends TickBuggle { //What the buggle will do in a tick: //1: If it has arived at the end, it will do nothing, "mission accomplishe"! //2: If it is facing a hurdle, it will jump over it. //3: If the road ahead is clear, it will move ahead. public void tick() { // Flesh out this skeleton if (atFinishLine()) { System.out.println("At finish Line"); return; // you are done } if (isFacingWall()) { //it must be just a hurdle, jump over it jumpHurdle(); } else { //the road is clear, go ahead! forward(); } } public boolean atFinishLine() { // Return true if buggle is facing the end wall, and false otherwise. // Should leave buggle in same position and heading as when it starts. // Should not leave any marks behind. // In this version, the buggle will NOT "dance" when it reaches the wall. // Here is how we do that: // When the buggle reaches the end wall, it will drop a bagel at that cell. // When it asks whether it is the end wall ahead, it will right away check // to see if there is a bagel there. If so, it knows she is facing the end // wall. So there is no need to go up and down, etc, // movements that cause the "dancing" behavior. // Notice of course, that the action of dropping a bagel in a cell, // changes the specifications of the problem, // which we may not be allowed to do, in general! boolean answer = false; //it may, appropriately, be set to true later. if (isOverBagel()) { //is the end wall right ahead? return true; } if (isFacingWall()) { //go up (NORTH) one level brushUp(); left(); forward(); right(); answer = isFacingWall(); //is it facing wall in level 2? //go back to starting position and heading left(); backward(); right(); brushDown(); } if (answer == true) { //if the end wall has been reached, // drop a bagel, so next time you don't have to ask again, // effectively avoiding the "dancing" behavior. dropBagel(); } return answer; } //Jump over a hurdle, and finish facing east. public void jumpHurdle() { // Flesh out this skeleton //go up (NORTH) one level left(); forward(); right(); forward(); //go down (SOUTH) one level right(); forward(); left(); } }