Advertisement
GARC923

EsperBot

Oct 30th, 2022
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.57 KB | None | 0 0
  1. package Lesson4;
  2.  
  3. import kareltherobot.Robot;
  4. import kareltherobot.World;
  5.  
  6. import java.sql.Wrapper;
  7. import java.util.Scanner;
  8.  
  9.  
  10. public class EsperBot extends Robot {
  11.     public EsperBot(int street, int avenue, Direction direction, int beeperCount) {
  12.         super(street, avenue, direction, beeperCount);
  13.     }
  14.  
  15.  
  16.     public static void main(String[] args) {
  17.         World.setDelay(50);
  18.         World.setVisible();
  19.         World.setSize(10, 10);
  20.         World.showSpeedControl(true);
  21.  
  22.         Lesson3.Activity1 zgod = new Lesson3.Activity1(1, 3, North, -1);
  23.     }
  24.     /**
  25.      * Turns right
  26.      * @return void
  27.      * */
  28.     public void turnRight(){
  29.         int del = World.delay();
  30.         World.setDelay(0);
  31.         turnLeft();
  32.         turnLeft();
  33.         World.setDelay(del);
  34.         turnLeft();
  35.     }
  36.  
  37.     /**
  38.      * Determines whether the left is clear
  39.      * @return boolean
  40.      * */
  41.     boolean retLeft = true;
  42.     public boolean leftIsClear() {
  43.         if (facingNorth()) { //positions the robot to face west
  44.             turnLeft();
  45.         }
  46.         else if (facingSouth()) { //positions the robot to face west
  47.             turnRight();
  48.         }
  49.         else if (facingEast()) { //positions the robot to face west
  50.             turnLeft();
  51.             turnLeft();
  52.         }
  53.         if (frontIsClear()){ // checks if the front is clear. If it is, it returns true
  54.             retLeft = true;
  55.         }else {
  56.             retLeft = false;
  57.         }
  58.         turnRight();
  59.         return retLeft;
  60.     }
  61.  
  62.     /**
  63.      * Method to detect whether there is a stair in front of the robot to go up
  64.      * @return boolean
  65.      * */
  66.     boolean retStair = true;
  67.     public boolean inStair(){
  68.         if (facingNorth()){
  69.             turnRight();
  70.         }
  71.         if (frontIsClear()) {
  72.             retStair = false;
  73.         }
  74.         else{
  75.             retStair = true;
  76.         }
  77.         turnLeft();
  78.         return retStair;
  79.     }
  80.  
  81.  
  82.     // L4A4
  83.     /**
  84.      * Method to check whether karel is standing on a pothole
  85.      * @return boolean
  86.      * */
  87.     boolean retPothole = true;
  88.     public boolean onPothole(){
  89.         int delay= World.delay(); // saves the default delay of the world
  90.         World.setDelay(0); // delay 0 to make the action instantaneous
  91.         turnLeft();
  92.         turnLeft();
  93.         if (frontIsClear()){
  94.             retPothole = true;
  95.         } else {
  96.             retPothole = false;
  97.         }
  98.         turnLeft();
  99.         World.setDelay(delay); // sets delay back to normal
  100.         turnLeft();
  101.         return retPothole;
  102.     }
  103.  
  104.     /**
  105.      * Method for Karel to walk (standing up)
  106.      * @return void
  107.      * */
  108.     public void walk(){
  109.         int delay= World.delay(); // saves the default delay of the world
  110.         World.setDelay(0); // delay 0 to make the action instantaneous
  111.         turnRight();
  112.         move();
  113.         World.setDelay(delay); // sets delay back to normal
  114.         turnLeft();
  115.     }
  116.  
  117.     /**
  118.      * Method to fill potholes
  119.      * @return void
  120.      * */
  121.     public void fillPothole(){
  122.         int delay= World.delay(); // saves the default delay of the world
  123.         World.setDelay(0); // delay 0 to make the action instantaneous
  124.         turnLeft();
  125.         turnLeft();
  126.         World.setDelay(delay); // sets delay back to normal
  127.         move();
  128.         putBeeper(); // Fills the pothole
  129.         World.setDelay(0); // delay 0 to make the action instantaneous
  130.         turnLeft();
  131.         turnLeft();
  132.         World.setDelay(delay); // sets delay back to normal
  133.         move();
  134.     }
  135.  
  136. }
  137.  
  138.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement