Advertisement
GARC923

Challenge 3 EsperBot

Nov 15th, 2022 (edited)
641
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 3.27 KB | None | 0 0
  1. package Challenges;
  2.  
  3. import kareltherobot.Robot;
  4. import kareltherobot.World;
  5.  
  6.  
  7. public class EsperBot3 extends Robot {
  8.     public EsperBot3(int street, int avenue, Direction direction, int beeperCount) {
  9.         super(street, avenue, direction, beeperCount);
  10.     }
  11.  
  12.  
  13.     public static void main(String[] args) {
  14.         World.setDelay(50);
  15.         World.setVisible();
  16.         World.setSize(10, 10);
  17.         World.showSpeedControl(true);
  18.  
  19.         Lesson3.Activity1 zgod = new Lesson3.Activity1(1, 3, North, -1);
  20.     }
  21.  
  22.     /**
  23.      * Turns right
  24.      * @return void
  25.      */
  26.     public void turnRight() {
  27.         int del = World.delay();
  28.         World.setDelay(0);
  29.         turnLeft();
  30.         turnLeft();
  31.         World.setDelay(del);
  32.         turnLeft();
  33.     }
  34.  
  35.     /**
  36.      * Turns around
  37.      * @return void
  38.      */
  39.     public void turnAround() {
  40.         int del = World.delay();
  41.         World.setDelay(0);
  42.         turnLeft();
  43.         World.setDelay(del);
  44.         turnLeft();
  45.     }
  46.  
  47.     /**
  48.      * Checks if the next beeper is ahead
  49.      * @return boolean
  50.      */
  51.     public boolean beeperAhead() {
  52.         boolean beeperAhead = false; // return value: default to false
  53.         if (frontIsClear()) { // only proceeds if the front is clear
  54.             int del = World.delay();
  55.             World.setDelay(0); //delay 0 to make the action instantaneous
  56.             move();
  57.             if (nextToABeeper()) {
  58.                 beeperAhead = true; // return is true if when it moves it is on a beeper
  59.             }
  60.             turnAround();
  61.             move();
  62.             World.setDelay(del);
  63.             turnAround(); //goes back to starting position
  64.         }
  65.         return beeperAhead; //return statement
  66.     }
  67.  
  68.     /**
  69.      * Checks if the next beeper is to the left
  70.      * @return boolean
  71.      */
  72.     public boolean beeperleft(){
  73.         boolean beeperLeft = false; // return value: default to false
  74.         turnLeft();
  75.         if (frontIsClear()) { // only proceeds if the front is clear
  76.             int del = World.delay();
  77.             World.setDelay(0); //delay 0 to make the action instantaneous
  78.             move();
  79.             if (nextToABeeper()) {
  80.                 beeperLeft = true; // return is true if when it moves it is on a beeper
  81.             }
  82.             turnAround();
  83.             move();
  84.             World.setDelay(del);
  85.             turnLeft(); //goes back to starting position
  86.         } else turnRight();
  87.  
  88.         return beeperLeft; //return statement
  89.     }
  90.  
  91.     /**
  92.      * Checks if the next beeper is right
  93.      * @return boolean
  94.      */
  95.     public boolean beeperRight(){
  96.         boolean beeperRight = false; // return value: default to false
  97.         turnRight();
  98.         if (frontIsClear()) { // only proceeds if the front is clear
  99.             int del = World.delay();
  100.             World.setDelay(0); //delay 0 to make the action instantaneous
  101.             move();
  102.             if (nextToABeeper()) {
  103.                 beeperRight = true; // return is true if when it moves it is on a beeper
  104.             }
  105.             turnAround();
  106.             move();
  107.             World.setDelay(del);
  108.             turnRight(); //goes back to starting position
  109.         }else turnLeft();
  110.  
  111.         return beeperRight; //return statement
  112.     }
  113.  
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement