Advertisement
KevinNT03

Shuttle runs with Karel

Nov 15th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.73 KB | None | 0 0
  1. package Activity1;
  2. import kareltherobot.*;
  3.  
  4. public class Activity1Main extends UrRobot{
  5.  
  6.     public Activity1Main (int s, int a, Direction d, int b){
  7.         super(s, a, d, b);
  8.     }
  9.  
  10.     public static void main(String[] args) {
  11.  
  12.         World.readWorld("Lesson3World1.kwld"); //reading and creating world
  13.         World.setVisible();
  14.         World.setDelay(20);
  15.  
  16.         Activity1Main arthur = new Activity1Main(4,3, North, 0);
  17.  
  18.         for (int out = 0; out < 3; out++){ //there are three columns, so it runs three times
  19.  
  20.             for (int in = 0; in < 5; in++){ //the robot does five shuttle runs
  21.                 arthur.runShuttle();
  22.             }
  23.  
  24.             if (out != 2){ //the robot moves to the next column the first two times
  25.                 arthur.turnAround();
  26.                 arthur.turnLeft();
  27.                 arthur.move();
  28.                 arthur.turnLeft();
  29.             }
  30.         }
  31.     }
  32.  
  33.     /**
  34.      * the robot runs a single shuttle run
  35.      * @param - none
  36.      * @return - void
  37.      */
  38.     public void runShuttle(){
  39.         runToOtherSide();
  40.         pickBeeper();
  41.         turnAround();
  42.         runToOtherSide();
  43.         putBeeper();
  44.         turnAround();
  45.     }
  46.  
  47.     /**
  48.      * goes to to other side
  49.      * @param - none
  50.      * @return - void
  51.      */
  52.     public void runToOtherSide(){
  53.         move();
  54.         move();
  55.         move();
  56.     }
  57.  
  58.     /**
  59.      * allows the robot to instantly turn around
  60.      * @param - none
  61.      * @return - void
  62.      */
  63.     public void turnAround(){
  64.         int delay = World.delay();
  65.         World.setDelay(0); //by changing the delay, the robot turns left instantaneously
  66.         turnLeft();
  67.         World.setDelay(delay);
  68.         turnLeft();
  69.     }
  70.  
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement