Advertisement
seby_stephens

Karel Challenge 2

Jan 15th, 2020
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.19 KB | None | 0 0
  1. package KarelChallenges;
  2. import kareltherobot.*;
  3.  
  4. /**
  5.  * Sebastian Stephens
  6.  * This program is essentially Karel strategically going through a maze
  7.  */
  8.  
  9. public class Challenge2Main extends Robot {
  10.     /**
  11.      * Constructor for Challenge2Main class
  12.      * @param a
  13.      * @param s
  14.      * @param d
  15.      * @param b
  16.      * @ return - none
  17.      */
  18.     public Challenge2Main (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(20);
  24.         World.setVisible();
  25.         World.readWorld("KarelChallenges2.kwld");
  26.         //Creates new Challenge2Main object
  27.         Challenge2Main gary = new Challenge2Main(1,1,North, -1);
  28.         //Runs overridden move method
  29.         gary.move();
  30.  
  31.     }
  32.  
  33.     /**
  34.      * Overrides move method
  35.      * @ param - none
  36.      * @ return - none
  37.      */
  38.     public void move(){
  39.         //Moves until the robot is next to beeper which signifies the end of the maze
  40.         while(!nextToABeeper()) {
  41.             //If the right is clear it turns right
  42.             if (rightIsClear()) {
  43.                 turnRight();
  44.             }
  45.             //It continues to turn left while front is not clear
  46.             else {
  47.                 while (!frontIsClear()) {
  48.                     turnLeft();
  49.                 }
  50.             }
  51.             //Moves
  52.             super.move();
  53.         }
  54.     }
  55.  
  56.  
  57.     /**
  58.      * Checks to see if right is clear
  59.      * @ param - none
  60.      * @return boolean
  61.      */
  62.     public boolean rightIsClear(){
  63.         //Turns right and sees if front is clear
  64.         //If it isnt it turns left and returns false
  65.         //IF it is it turns left and returns true
  66.         turnRight();
  67.         if (!frontIsClear()){
  68.             turnLeft();
  69.             return false;
  70.         }
  71.         else {
  72.             turnLeft();
  73.             return true;
  74.         }
  75.  
  76.     }
  77.     /**
  78.      * Allows gary to turn right
  79.      * @ param - none
  80.      * @ return - void
  81.      */
  82.     public void turnRight(){
  83.         int delay = World.delay();
  84.         World.setDelay(0);
  85.         turnLeft();
  86.         turnLeft();
  87.         World.setDelay(delay);
  88.         turnLeft();
  89.     }
  90.  
  91.  
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement