Advertisement
seby_stephens

Karel Challenge 1

Jan 15th, 2020
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.51 KB | None | 0 0
  1. package KarelChallenges;
  2. import kareltherobot.*;
  3.  
  4. /**
  5.  * Sebastian Stephens
  6.  * This program is essentially Karel placing a beeper on the opposite end of where there is
  7.    already a beeper.
  8.  **/
  9.  
  10. public class Challenge1Main extends Robot{
  11.  
  12.  
  13.     /**
  14.      * Constructor for Challenge1Main class
  15.      * @param a
  16.      * @param s
  17.      * @param d
  18.      * @param b
  19.      * @ return - none
  20.      */
  21.     public Challenge1Main (int a, int s, Direction d, int b) {
  22.         super(a, s, d, b);
  23.     }
  24.  
  25.  
  26.     public static void main (String[] args) {
  27.         World.setDelay(20);
  28.         World.setVisible();
  29.         World.readWorld("KarelChallenges1.kwld");
  30.         //Creates robot object named Gary
  31.         Challenge1Main gary = new Challenge1Main(1,6,North, -1);
  32.  
  33.         //While front is clear it performs overridden move method
  34.         while (gary.frontIsClear()){
  35.             gary.move();
  36.         }
  37.     }
  38.  
  39.     /**
  40.      * Overrides move method to move forward only if the left isnt clear, and to run checkLeftHole if it is not
  41.      * @ param - none
  42.      * @ return - void
  43.      */
  44.     public void move(){
  45.         if (!leftIsClear()){
  46.             super.move();
  47.         }
  48.         else {
  49.             checkLeftHole();
  50.         }
  51.     }
  52.  
  53.  
  54.     /**
  55.      * Checks to see if left hole has beeper or not
  56.      * @ param - none
  57.      * @ return - void
  58.      */
  59.     public void checkLeftHole(){
  60.         //Turns left and moves until the front is clear
  61.         turnLeft();
  62.         while (frontIsClear()){
  63.             super.move();
  64.         }
  65.         //Once it gets to a stop, it checks if a beeper is there, if it is it fills the opposite side
  66.         if (nextToABeeper()) {
  67.             fillOppositeSide();
  68.             //Once fill opposite side method ends it ends at the end of the left side hole
  69.             //exit method has to run to get out of hole to reset position
  70.             exit();
  71.         } else {
  72.             //If beeper is not there it exits the hole and resets position
  73.             exit();
  74.         }
  75.     }
  76.  
  77.     /**
  78.      * Places a beeper on the opposite side of a row with a beeper
  79.      * @ param - none
  80.      * @ return - void
  81.      */
  82.     public void fillOppositeSide(){
  83.  
  84.         //Turns around and moves until it hits a wall
  85.         turnRight();
  86.         turnRight();
  87.         while (frontIsClear()){
  88.             super.move();
  89.         }
  90.         //Once it hits a wall it checks to see if a beeper is there
  91.         //Only if a beeper is not there it places a beeper
  92.         if (!frontIsClear() && !nextToABeeper()){
  93.             putBeeper();
  94.         }
  95.         //Turns around and moves all the way to the opposite side until it hits a wall
  96.         turnRight();
  97.         turnRight();
  98.         while (frontIsClear()){
  99.             super.move();
  100.         }
  101.     }
  102.  
  103.     /**
  104.      * Allows one to exit the left hole
  105.      * @ param - none
  106.      * @ return - void
  107.      */
  108.     public void exit(){
  109.         //Turns around
  110.         turnLeft();
  111.         turnLeft();
  112.         //Checks to see if left or right is clear
  113.         //Since there are some holes where one side is clear
  114.         //If one of the sides is not clear it moves and it stops when both sides are clear
  115.         while(!leftIsClear() || !rightIsClear()){
  116.             super.move();
  117.         }
  118.         //Turns left and moves forward a space
  119.         turnLeft();
  120.         super.move();
  121.     }
  122.  
  123.     /**
  124.      * Checks to see if left is clear
  125.      * @ param - none
  126.      * @return boolean
  127.      */
  128.     public boolean leftIsClear(){
  129.         //Turns left and sees if front is clear
  130.         //If it isnt it turns right and returns false
  131.         //IF it is it turns right and returns true
  132.         turnLeft();
  133.         if (!frontIsClear()){
  134.             turnRight();
  135.             return false;
  136.         }
  137.         else {
  138.             turnRight();
  139.             return true;
  140.         }
  141.     }
  142.  
  143.     /**
  144.      * Checks to see if right is clear
  145.      * @ param - none
  146.      * @return boolean
  147.      */
  148.     public boolean rightIsClear(){
  149.         //Same concept as leftIsClear but for right side
  150.         turnRight();
  151.         if (!frontIsClear()){
  152.             turnLeft();
  153.             return false;
  154.         }
  155.         else {
  156.             turnLeft();
  157.             return true;
  158.         }
  159.  
  160.     }
  161.  
  162.     /**
  163.      * Allows gary to turn right
  164.      * @ param - none
  165.      * @ return - void
  166.      */
  167.     public void turnRight(){
  168.         int delay = World.delay();
  169.         World.setDelay(0);
  170.         turnLeft();
  171.         turnLeft();
  172.         World.setDelay(delay);
  173.         turnLeft();
  174.     }
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement