Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package KarelChallenges;
- import kareltherobot.*;
- /**
- * Sebastian Stephens
- * This program is essentially Karel strategically going through a maze
- */
- public class Challenge2Main extends Robot {
- /**
- * Constructor for Challenge2Main class
- * @param a
- * @param s
- * @param d
- * @param b
- * @ return - none
- */
- public Challenge2Main (int a, int s, Direction d, int b) {
- super(a, s, d, b);
- }
- public static void main(String[] args){
- World.setDelay(20);
- World.setVisible();
- World.readWorld("KarelChallenges2.kwld");
- //Creates new Challenge2Main object
- Challenge2Main gary = new Challenge2Main(1,1,North, -1);
- //Runs overridden move method
- gary.move();
- }
- /**
- * Overrides move method
- * @ param - none
- * @ return - none
- */
- public void move(){
- //Moves until the robot is next to beeper which signifies the end of the maze
- while(!nextToABeeper()) {
- //If the right is clear it turns right
- if (rightIsClear()) {
- turnRight();
- }
- //It continues to turn left while front is not clear
- else {
- while (!frontIsClear()) {
- turnLeft();
- }
- }
- //Moves
- super.move();
- }
- }
- /**
- * Checks to see if right is clear
- * @ param - none
- * @return boolean
- */
- public boolean rightIsClear(){
- //Turns right and sees if front is clear
- //If it isnt it turns left and returns false
- //IF it is it turns left and returns true
- turnRight();
- if (!frontIsClear()){
- turnLeft();
- return false;
- }
- else {
- turnLeft();
- return true;
- }
- }
- /**
- * Allows gary to turn right
- * @ param - none
- * @ return - void
- */
- public void turnRight(){
- int delay = World.delay();
- World.setDelay(0);
- turnLeft();
- turnLeft();
- World.setDelay(delay);
- turnLeft();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement