Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Challenge2;
- import kareltherobot.*;
- /**
- * This program allows a robot to solve any maze
- */
- public class Challenge2Main extends Robot{
- public Challenge2Main (int s, int a, Direction d, int b){ //constructor
- super(s, a, d, b);
- }
- public static void main(String[] args) {
- World.setVisible(); //reads and creates the world
- World.setDelay(20);
- World.readWorld("KarelChallenges2.kwld");
- Challenge2Main roach = new Challenge2Main(1,9, West,0);
- while(!roach.nextToABeeper()){ //the beeper indicates that the maze is solved
- if(roach.rightIsClear()){ //if the right is clear, turn right (right-hand method)
- roach.turnRight();
- roach.move();
- }else if(roach.frontIsClear()){ //if the front is clear, move forward
- roach.move();
- }else{
- roach.turnLeft(); //if neither the front nor the right is clear, turn left
- }
- }
- }
- /**
- * This method allows the robot to instantly turn right
- */
- public void turnRight(){
- int delay = World.delay();
- World.setDelay(0);
- turnLeft();
- turnLeft();
- World.setDelay(delay);
- turnLeft();
- }
- /**
- * This method allows the robot to instantly check if there is a wall to the left
- * @return if the left intersection is clear
- */
- public boolean leftIsClear(){
- int delay = World.delay();
- World.setDelay(0);
- boolean z = false;
- turnLeft();
- if(frontIsClear()){
- z = true;
- }
- World.setDelay(delay);
- turnRight();
- return z;
- }
- /**
- * This method allows the robot to instantly check if there is a wall to the right
- * @return if the right intersection is clear
- */
- public boolean rightIsClear(){
- int delay = World.delay();
- World.setDelay(0);
- boolean z = false;
- turnRight();
- if(frontIsClear()){
- z = true;
- }
- World.setDelay(delay);
- turnLeft();
- return z;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement