Advertisement
KevinNT03

Road Worker Karel

Jan 2nd, 2020
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.81 KB | None | 0 0
  1. package Activity4;
  2. import kareltherobot.*;
  3.  
  4. /**
  5.  * This program loads a world, and then the robot checks and fills any hole it encounters
  6.  */
  7. public class Activity4Main extends Robot{
  8.     public Activity4Main(int s, int a, Direction d, int b) {
  9.         super(s, a, d, b);
  10.     }
  11.  
  12.     public static void main(String[] args){
  13.         World.readWorld("Lesson4World3.kwld"); //creates world
  14.         World.setVisible();
  15.         World.setDelay(30);
  16.  
  17.         Activity4Main will = new Activity4Main(3,1, North, -1); //creates robot
  18.  
  19.         will.turnRight();
  20.  
  21.         for(int x = 0; x < 9; x++){ //fills the hole if there is one and moves forward
  22.             will.move();
  23.             will.fillHole();
  24.         }
  25.     }
  26.  
  27.     /**
  28.      * this function instantly checks whether the robot needs to fill a hole
  29.      * @return if there is a hole or not
  30.      */
  31.     public boolean checkForHole(){
  32.         int delay = World.delay();
  33.         World.setDelay(0);
  34.         boolean y = false;
  35.         turnRight();
  36.  
  37.         if(frontIsClear()){ //if the front is clear, there is a hole
  38.            y = true;
  39.         }
  40.  
  41.         World.setDelay(delay);
  42.         turnLeft();
  43.         return y;
  44.     }
  45.  
  46.     /**
  47.      * If there is a hole, fill it
  48.      * @return void
  49.      */
  50.     public void fillHole(){
  51.         if(checkForHole()){
  52.             turnRight();
  53.             move();
  54.             putBeeper();
  55.             turnLeft();
  56.             turnLeft();
  57.             move();
  58.             turnRight();
  59.         }
  60.     }
  61.  
  62.     /**
  63.      * This function allow the robot to instantly turn right
  64.      * @param - none
  65.      * @return - void
  66.      */
  67.     public void turnRight(){
  68.         int delay = World.delay();
  69.         World.setDelay(0);
  70.         turnLeft();
  71.         turnLeft();
  72.         World.setDelay(delay);
  73.         turnLeft();
  74.     }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement