Advertisement
GARC923

C1 EsperBot

Nov 16th, 2022
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.28 KB | None | 0 0
  1. package Challenges;
  2.  
  3. import kareltherobot.Robot;
  4. import kareltherobot.World;
  5.  
  6.  
  7. public class EsperBot1 extends Robot {
  8.     public EsperBot1(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.      *
  25.      * @return void
  26.      */
  27.     public void turnRight() {
  28.         int del = World.delay();
  29.         World.setDelay(0);
  30.         turnLeft();
  31.         turnLeft();
  32.         World.setDelay(del);
  33.         turnLeft();
  34.     }
  35.  
  36.     /**
  37.      * Checks if there is a wall to the left
  38.      * @return boolean
  39.      */
  40.     public boolean leftIsClear(){
  41.         boolean RETleftIsClear = true;
  42.         int del = World.delay();
  43.         World.setDelay(0);
  44.         if (facingNorth()){
  45.             turnLeft();
  46.             if (frontIsClear()) {
  47.                 RETleftIsClear = true; // left is clear if front is clear when facing left
  48.             } else{
  49.                 RETleftIsClear = false; // left is not clear if front is not clear when facing left
  50.             }
  51.             World.setDelay(del);
  52.             turnRight(); // goes back to starting position
  53.         }
  54.         return RETleftIsClear;
  55.     }
  56.  
  57.  
  58.     /**
  59.      * Goes into the cubby
  60.      * @return int
  61.      */
  62.     public int leftEnd(){
  63.         int distance = 0;
  64.         while (frontIsClear()) {
  65.             move(); // moves forwards until it is against the cubby wall
  66.             distance++; // counts how deep the cubby is
  67.         }
  68.         return distance;
  69.     }
  70.  
  71.     /**
  72.      * Measures the width of the hallway
  73.      * @return int
  74.      */
  75.     public int measureHallway(){
  76.         int hallwayLength = 0;
  77.         turnRight();
  78.         while (frontIsClear()) {
  79.             move();
  80.             hallwayLength++; // counts 1 more distance every time it moves
  81.         }
  82.         turnRight();
  83.         turnRight();
  84.         while (frontIsClear()) {
  85.             move(); //goes back to stating place
  86.         }
  87.         turnRight();
  88.         return hallwayLength;
  89.     }
  90.     }
  91.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement