/* FILE NAME: HighHurdleWorldSolns.java * AUTHOR: Stella * WHEN: Oct 1,2007 * WHAT: For the purpose of Lab5, F07: * Task 2, Exercise 3: HighHurdler * MODIFICATION HISTORY: Skeleton provided by Stella */ import java.awt.*; public class HighHurdleWorldSolns 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 HighHurdleWorldSolns(), "HighHurdleWorld 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 Hurdler extends TickBuggle { //What the buggle will do in a tick: //1: If it has arived at the end, it'll 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()) return; // you are done if (isFacingWall()) { //it must be just a hurdle jumpHurdle(); // so, jump it } else { //the road ahead is clear forward(); } } public boolean atFinishLine() { // Return true if buggle is facing the end wall, and false otherwise. //Since a hurdle can be 1 or 2 levels high now, in order for an obstacle //to be the final wall, it should be 3 levels high. // Should leave buggle in same position and heading as when it starts. // Should not leave any marks behind. // This is just a stub. Replace by a proper definition boolean answer = false; //it may, appropriately, be set to true later. if (isFacingWall()) { //go up (NORTH) *two* levels brushUp(); left(); forward(2); right(); answer = isFacingWall(); //is it facing wall in level 3 ? //go back to starting position and heading left(); backward(2); right(); brushDown(); } return answer; } //Jump over a hurdle, and finish facing east. //A hurdle can be 1 or 2 levels high public void jumpHurdle() { // Flesh out this skeleton //go up (NORTH) one level, and check if you face an obstacle left(); forward(); right(); if (isFacingWall()) { //this is a two-level obstacle //go one more level up (NORTH) left(); forward(); //turn and come down 2 levels right(); forward(); right(); forward(2); left(); //turn for final position } else { //this is just one-level obstacle forward(); //jump over it //go down (SOUTH) one level right(); forward(); left(); //turn for final position } } }