Advertisement
KevinNT03

Karel - leftIsClear()

Dec 12th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.90 KB | None | 0 0
  1. package Activity1;
  2. import kareltherobot.*;
  3.  
  4. /**
  5.  * The robot checks if the left intersection is clear to decide if
  6.  * it is going to move or do a back-flip
  7.  */
  8. public class Activity1Main extends Robot{
  9.  
  10.     public Activity1Main(int s, int a, Direction d, int b){
  11.         super(s, a, d, b);
  12.     }
  13.  
  14.     public static void main(String[] args) {
  15.         World.setVisible(); //generates world
  16.         World.setDelay(40);
  17.         World.readWorld("Lesson4World1.kwld");
  18.         Activity1Main mike = new Activity1Main(1,3, North, 0);
  19.  
  20.         for(int x = 0; x < 4; x++){
  21.             mike.move(); //executes 4 times to get the robot out of the structure
  22.         }
  23.     }
  24.  
  25.     /**
  26.      * Function checks if the intersection immediately to the left is clear
  27.      * @param - none
  28.      * @return if the left is clear
  29.      */
  30.     public boolean leftIsClear(){
  31.         int delay = World.delay();
  32.         World.setDelay(0);
  33.         turnLeft();
  34.         boolean temp;
  35.  
  36.         if(!frontIsClear()){
  37.             temp = false;
  38.         }else{
  39.             temp = true;
  40.         }
  41.         World.setDelay(delay);
  42.         turnRight();
  43.  
  44.         return temp;
  45.     }
  46.  
  47.     /**
  48.      *Makes the robot check if the left is clear before moving
  49.      * @Overriding
  50.      * @param - none
  51.      * @return - void
  52.      */
  53.     public void move(){
  54.         if(!leftIsClear()){
  55.             super.move(); //if the left is blocked, move forward
  56.         }else{
  57.             for(int i = 0; i < 4; i++){
  58.                 turnLeft();  //if the left is clear, do a celebratory back-flip
  59.             }
  60.         }
  61.     }
  62.  
  63.     /**
  64.      * This function allow the robot to instantly turn right
  65.      * @param - none
  66.      * @return - void
  67.      */
  68.     public void turnRight(){
  69.         int delay = World.delay();
  70.         World.setDelay(0);
  71.         turnLeft();
  72.         turnLeft();
  73.         World.setDelay(delay);
  74.         turnLeft();
  75.     }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement