Advertisement
Guest User

Untitled

a guest
Dec 6th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.04 KB | None | 0 0
  1. package Lesson4;
  2. import kareltherobot.*;
  3.  
  4. /**
  5.  * Francesco Di Mise
  6.  * This is a program that uses a robot with sensors, as well as if statements and for loops to get out of a "hole"
  7.  */
  8. public class Lesson4Activity1 extends Robot{
  9.  
  10.     /**
  11.      * Constructor
  12.      * @param x
  13.      * @param y
  14.      * @param d
  15.      * @param b
  16.      */
  17.     public Lesson4Activity1 (int x, int y, Direction d, int b) {
  18.  
  19.         super(x, y, d, b);
  20.     }
  21.  
  22.     /**
  23.      * Main method. Oly makes the robot and the world, then moves the robot 4 times
  24.      * @param args
  25.      */
  26.     public static void main (String[] args) {
  27.         World.setDelay(50);
  28.         World.setVisible();
  29.         World.readWorld("Lesson4World1.kwld");
  30.  
  31.         Lesson4Activity1 MrDaddy = new Lesson4Activity1(1,3,North, 0);
  32.  
  33.         for (int index = 0; index < 4; index++){
  34.             MrDaddy.move();
  35.         }
  36.     }
  37.  
  38.     /**
  39.      * a boolean method. Checks if the left is clear by instantaneously turning left, checking if the front is clear, then instantly turning back right
  40.      * @return
  41.      */
  42.     public boolean leftIsClear(){
  43.         int delay = World.delay();
  44.         World.setDelay(0);
  45.         turnLeft();
  46.         boolean check;
  47.  
  48.         if (!frontIsClear()){
  49.             check = false;
  50.         } else {
  51.             check  = true;
  52.         }
  53.         World.setDelay(delay);
  54.         turnRight();
  55.         return check;
  56.  
  57.     }
  58.  
  59.     /**
  60.      * An override method for move. Checks the boolean method if the left is clear. If is not clear, then it wil move forward. If it is, it wil doa 180 turn
  61.      */
  62.     public void move(){
  63.  
  64.         if (!leftIsClear()){
  65.             super.move();
  66.         } else {
  67.             for (int index = 0; index < 4; index++){
  68.                 turnLeft();
  69.             }
  70.         }
  71.     }
  72.  
  73.     /**
  74.      * Method to instantly turn right
  75.      */
  76.     public void turnRight(){
  77.         int delay = World.delay();
  78.         World.setDelay(0);
  79.         turnLeft();
  80.         turnLeft();
  81.         World.setDelay(delay);
  82.         turnLeft();
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement