/* FILE NAME: TiredHurdleWorldSolns.java * AUTHOR: Stella * WHEN: Oct 1,2007 * WHAT: For the purpose of Lab5, F07: * Task 2, Exercise 3: TiredHurdler * MODIFICATION HISTORY: Based on older code */ import java.awt.*; public class TiredHurdleWorldSolns extends BuggleWorld { Randomizer rand = new Randomizer(5); public void run () { TiredHurdler happy = new TiredHurdler(2); //can jump up to 2 hurdles happy.tick16(); } //--------------------- // The following main method is needed to run the applet as an application public static void main (String[] args) { runAsApplication(new TiredHurdleWorldSolns(), "TiredHurdleWorld 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 + ")"); // Sohie messing around with varying hurdle heights // draw a hurdle of height 1, then flip a coin to see // if the hurdle should be a height 2 addVerticalWall(x, 0); if (rand.intBetween(1,10) > 5) { addVerticalWall(x,1); } placeHurdles(hurdles - 1, side); } } } } //************************************************************** class TiredHurdler extends Hurdler { private int hurdlesLeft; // This is called an instance variable. It is the first time to see // an instance variable. Such variables are used to keep state. // In this case, it will be used to keep track of how many more // hurdles the buggle can still jump at any point. public TiredHurdler(int maxHurdles) { this.hurdlesLeft = maxHurdles;//initially the buggle can jump maxHurdles } // What the buggle will do in a tick: // 1: If it has arrived at the end, it'll do nothing, "mission accomplishe"! // 2: If it is facing a hurdle, then if it can still jump, // it will jump over it. Otherwise, it will do nothing. // 3: If the road ahead is clear, it will move ahead. public void tick() { // Flesh out this skeleton if (atFinishLine()) return; // you are done, do nothing if (isFacingWall()) { //it must be just a hurdle if ((hurdlesLeft > 0)) { //you still have energy for jumping... jumpHurdle(); // so, jump it hurdlesLeft = hurdlesLeft - 1; //keep track of the jumps } else { //no energy left for jumping... System.out.println("I run out of breath...");//so, do nothing... } }else { //the road ahead is clear forward(); } }//end of tick() } //end of TiredHurdler class