Advertisement
seby_stephens

Karel Challenge 3

Jan 16th, 2020
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.86 KB | None | 0 0
  1. package KarelChallenges;
  2. import kareltherobot.*;
  3.  
  4. /**
  5.  * Sebastian Stephens
  6.  * This program has Karel move down a path of beepers by checking each side for one.
  7.  */
  8. public class Challenge3Main extends Robot{
  9.  
  10.     /**
  11.      * Constructor for Challenge3Main Class
  12.      * @param a
  13.      * @param s
  14.      * @param d
  15.      * @param b
  16.      * @ return - none
  17.      */
  18.     public Challenge3Main (int a, int s, Direction d, int b) {
  19.         super(a, s, d, b);
  20.     }
  21.  
  22.     public static void main(String[] args){
  23.         World.setDelay(10);
  24.         World.setVisible();
  25.         World.readWorld("KarelChallenges3.kwld");
  26.         //Creates new Challenge3Main object
  27.         Challenge3Main gary = new Challenge3Main(1,1,North, -1);
  28.         //Has gary run masterCheckForBeep method
  29.         gary.masterCheckForBeep();
  30.     }
  31.  
  32.  
  33.     /**
  34.      * Checks to see if there is a beeper on each side
  35.      * @ param - none
  36.      * @ return - none
  37.      */
  38.     public void masterCheckForBeep(){
  39.         //Sets a boolean variable to true and has a while toop that runs while it is true
  40.         boolean x = true;
  41.         while(x = true){
  42.             //If one of these conditions is true this code rune
  43.             if(lookUp() || lookLeft() || lookDown() || lookRight() ){
  44.                 //Picks beeper robot is on so path doesnt mess up
  45.                 pickBeeper();
  46.                 //If there is still a beeper the loop ends and so does the program
  47.                 //This is because there are two beepers on the last space
  48.                 if(nextToABeeper()){
  49.                     pickBeeper();
  50.                     turnOff();
  51.                     x = false;
  52.                 }
  53.             }
  54.  
  55.         }
  56.     }
  57.  
  58.     /**
  59.      * Checks to see if there is a beeper above the space where robot is located
  60.      * @ param - none
  61.      * @return
  62.      */
  63.     public boolean lookUp() {
  64.         //Checks to see if there is wall in front of it
  65.         if(frontIsClear()) {
  66.             //Moves north and then checks for beeper
  67.             move();
  68.             //If it is next to beeper it returns true and robot stays there facing north
  69.             if (nextToABeeper()) {
  70.                 return true;
  71.             }
  72.             //If it is not next to beeper it moves back to the previous space and resets position by facing north
  73.             //Then returns false
  74.             else {
  75.                 turnLeft();
  76.                 turnLeft();
  77.                 move();
  78.                 turnLeft();
  79.                 turnLeft();
  80.                 return false;
  81.             }
  82.         }
  83.         //Nothing runs if there is a wall
  84.         else{
  85.             return false;
  86.         }
  87.  
  88.     }
  89.  
  90.     /**
  91.      * Checks to see if there is a beeper in the space below where the robot is located
  92.      * @ param - none
  93.      * @return
  94.      */
  95.     public boolean lookDown(){
  96.         //Turns around and checks to see if front is clear to make sure there is no wall
  97.         turnLeft();
  98.         turnLeft();
  99.         if(frontIsClear()) {
  100.             //If there is no wall it moves and checks for a beeper
  101.             move();
  102.             //If there is beeper it resets position by facing north
  103.             //Returns true
  104.             if (nextToABeeper()) {
  105.                 turnLeft();
  106.                 turnLeft();
  107.                 return true;
  108.  
  109.             }
  110.             //If there is no beeper then robot turns around and moves up to previous position
  111.             //Returns false
  112.             else {
  113.                 turnLeft();
  114.                 turnLeft();
  115.                 move();
  116.                 return false;
  117.             }
  118.         }
  119.         //Does not run if there is wall
  120.         else{
  121.             return false;
  122.         }
  123.  
  124.     }
  125.  
  126.     /**
  127.      * Checks to see if there is a beeper to the left of the robot
  128.      * @ param - none
  129.      * @return
  130.      */
  131.     public boolean lookLeft(){
  132.         //Faces left and checks if there is a wall
  133.         turnLeft();
  134.         if(frontIsClear()) {
  135.             //Moves forward and checks if there is a beeper
  136.             move();
  137.             //If there it is a beeper robot turns right and method returns true
  138.             if (nextToABeeper()) {
  139.                 turnRight();
  140.                 return true;
  141.             }
  142.             //If there is no beeper it turns around moves and resets position in previous spot
  143.             //Then returns false
  144.             else {
  145.                 turnRight();
  146.                 turnRight();
  147.                 move();
  148.                 turnLeft();
  149.                 return false;
  150.             }
  151.         }
  152.         //Does not run if there is a wall
  153.         else{
  154.             return false;
  155.         }
  156.     }
  157.  
  158.     /**
  159.      * Checks to see if right has beeper
  160.      * @ param - none
  161.      * @return
  162.      */
  163.     public boolean lookRight(){
  164.         //Robot faces right
  165.         turnRight();
  166.         //Checks to see if the front is clear
  167.         if(frontIsClear()) {
  168.             //If front is clear robot moves forward and checks for beeper
  169.             move();
  170.             //If there is a beeper it turns left to reset position north
  171.             //Returns true
  172.             if (nextToABeeper()) {
  173.                 turnLeft();
  174.                 return true;
  175.             }
  176.             //If there is no beeper robot turns around a moves back and faces north
  177.             //Returns false
  178.             else {
  179.                 turnLeft();
  180.                 turnLeft();
  181.                 move();
  182.                 turnRight();
  183.                 return false;
  184.             }
  185.         }
  186.         //If there is wall then nothing runs
  187.         else{
  188.             return false;
  189.         }
  190.     }
  191.    
  192.     /**
  193.      * Allows gary to turn right
  194.      * @ param - none
  195.      * @ return - void
  196.      */
  197.     public void turnRight(){
  198.         int delay = World.delay();
  199.         World.setDelay(0);
  200.         turnLeft();
  201.         turnLeft();
  202.         World.setDelay(delay);
  203.         turnLeft();
  204.     }
  205.  
  206.  
  207. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement