Advertisement
KevinNT03

OCD Karel V2

Jan 14th, 2020
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.32 KB | None | 0 0
  1. package Challenge1;
  2. import kareltherobot.*;
  3.  
  4. /**
  5.  * This program puts beepers on the right side for every beeper on the left side
  6.  */
  7. public class Challenge1Main extends Robot{
  8.  
  9.     public Challenge1Main(int s, int a, Direction d, int b){
  10.         super(s, a, d, b);
  11.     }
  12.  
  13.     public static void main(String[] args) {
  14.  
  15.         World.setVisible(); //reads and creates the world
  16.         World.setDelay(25);
  17.         World.readWorld("KarelChallenges1.kwld");
  18.  
  19.         Challenge1Main zoro = new Challenge1Main(1,6, North,-1);
  20.  
  21.         while(zoro.frontIsClear()){ //the program runs until it encounters the final wall
  22.             zoro.move();
  23.  
  24.             if(zoro.leftIsClear()){ //if there is no wall to the left, check for beepers there
  25.                 zoro.checkForBeeper();
  26.                 zoro.turnAround(); //moves back to outside the hole
  27.                 while(!zoro.leftIsClear()||!zoro.rightIsClear()){
  28.                     zoro.move();
  29.                 }
  30.                 zoro.turnLeft();
  31.             }
  32.         }
  33.     }
  34.  
  35.     /**
  36.      * This method allows the robot to instantly turn right
  37.      */
  38.     public void turnRight(){
  39.         int delay = World.delay();
  40.         World.setDelay(0);
  41.         turnLeft();
  42.         turnLeft();
  43.         World.setDelay(delay);
  44.         turnLeft();
  45.     }
  46.  
  47.     /**
  48.      * allows the robot to instantly turn around
  49.      */
  50.     public void turnAround(){
  51.         int delay = World.delay();
  52.         World.setDelay(0); //by changing the delay, the robot turns left instantaneously
  53.         turnLeft();
  54.         World.setDelay(delay);
  55.         turnLeft();
  56.     }
  57.  
  58.     /**
  59.      * This method allows the robot to instantly check if there is a wall to the left
  60.      * @return if the left intersection is clear
  61.      */
  62.     public boolean leftIsClear(){
  63.         int delay = World.delay();
  64.         World.setDelay(0);
  65.         boolean z = false;
  66.         turnLeft();
  67.         if(frontIsClear()){
  68.             z = true;
  69.         }
  70.         World.setDelay(delay);
  71.         turnRight();
  72.         return z;
  73.     }
  74.  
  75.     /**
  76.      * This method allows the robot to instantly check if there is a wall to the right
  77.      * @return if the right intersection is clear
  78.      */
  79.     public boolean rightIsClear(){
  80.         int delay = World.delay();
  81.         World.setDelay(0);
  82.         boolean z = false;
  83.         turnRight();
  84.         if(frontIsClear()){
  85.             z = true;
  86.         }
  87.         World.setDelay(delay);
  88.         turnLeft();
  89.         return z;
  90.     }
  91.  
  92.     /**
  93.      * Checks if there is a beeper in the hole
  94.      */
  95.     public void checkForBeeper(){
  96.         turnLeft();
  97.  
  98.         while(frontIsClear()){ //move forward until it encounters a wall
  99.             move();
  100.         }
  101.  
  102.         if(nextToABeeper()){ //if there is a beeper, put a beeper on the other side and move back
  103.             putBeeperOnOtherSide();
  104.         }
  105.     }
  106.  
  107.     /**
  108.      * Puts a beeper on the other side if there isn't one
  109.      */
  110.     public void putBeeperOnOtherSide(){
  111.         turnAround();
  112.         while(frontIsClear()){ //move forward until it encounters a wall
  113.             move();
  114.         }
  115.  
  116.         if(!nextToABeeper()){ //if there isn't already a beeper there, put one
  117.             putBeeper();
  118.         }
  119.  
  120.         turnAround(); //move back
  121.         while(frontIsClear()){
  122.             move();
  123.         }
  124.     }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement