KevinNT03

Extraterrestrial Karel

Jan 23rd, 2020
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.04 KB | None | 0 0
  1. package Challenge3;
  2. import kareltherobot.*;
  3.  
  4. /**
  5.  * This program makes a robot collect piles of beepers
  6.  */
  7. public class Challenge3Main extends Robot{
  8.  
  9.     public Challenge3Main(int s, int a, Direction d, int b){  //constructor
  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("KarelChallenges3.kwld");
  18.  
  19.         Challenge3Main paul = new Challenge3Main(1,1, North, 0);
  20.  
  21.         while(!paul.nextToABeeper()){ //runs until it finds the pile with two beepers
  22.  
  23.             if(paul.checkBeeperFront()){ //if there's a beeper in front, pick it
  24.                 paul.move();
  25.                 paul.pickBeeper();
  26.             }else if(paul.checkBeeperLeft()){ //if there's a beeper to the left, pick it
  27.                 paul.turnLeft();
  28.                 paul.move();
  29.                 paul.pickBeeper();
  30.             }else{ //if it is not in front or to the left, it should be to the right
  31.                 paul.turnRight();
  32.                 paul.move();
  33.                 paul.pickBeeper();
  34.             }
  35.         }
  36.     }
  37.  
  38.     /**
  39.      * This method allows the robot to instantly turn right
  40.      */
  41.     public void turnRight(){
  42.         int delay = World.delay();
  43.         World.setDelay(0);
  44.         turnLeft();
  45.         turnLeft();
  46.         World.setDelay(delay);
  47.         turnLeft();
  48.     }
  49.  
  50.     /**
  51.      * allows the robot to instantly turn around
  52.      */
  53.     public void turnAround(){
  54.         int delay = World.delay();
  55.         World.setDelay(0); //by changing the delay, the robot turns left instantaneously
  56.         turnLeft();
  57.         World.setDelay(delay);
  58.         turnLeft();
  59.     }
  60.  
  61.     /**
  62.      * Immediately checks if there is a beeper in front of the robot
  63.      * @return temp
  64.      */
  65.     public boolean checkBeeperFront(){
  66.         int delay = World.delay();
  67.         World.setDelay(0);
  68.         boolean temp = false;
  69.  
  70.         if(frontIsClear()) { //checks if the front is clear to avoid errors
  71.             move();
  72.  
  73.             if (nextToABeeper()) { //if there's a beeper there, return true
  74.                 temp = true;
  75.             }
  76.  
  77.             turnAround(); //move back
  78.             move();
  79.             turnAround();
  80.         }
  81.  
  82.         World.setDelay(delay);
  83.         return temp;
  84.     }
  85.  
  86.     /**
  87.      * Immediately checks if there is a beeper to the left of the robot
  88.      * @return temp
  89.      */
  90.     public boolean checkBeeperLeft(){
  91.         int delay = World.delay();
  92.         World.setDelay(0);
  93.         boolean temp = false;
  94.  
  95.         turnLeft(); //Because we're checking the left
  96.  
  97.         if(frontIsClear()) { //checks if the front is clear to avoid errors
  98.             move();
  99.  
  100.             if (nextToABeeper()) { //if there is a beeper there, return true
  101.                 temp = true;
  102.             }
  103.  
  104.             turnAround(); //move back
  105.             move();
  106.             turnAround();
  107.         }
  108.  
  109.         turnRight();
  110.         World.setDelay(delay);
  111.         return temp;
  112.     }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment