Advertisement
GARC923

Challenge 2

Nov 15th, 2022
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.95 KB | None | 0 0
  1. /*
  2. Carlos Garcia
  3. Challenge 2
  4. Maze Runner
  5. */
  6.  
  7. package Challenges;
  8. import kareltherobot.World;
  9.  
  10.  
  11. public class Challenge2 extends EsperBot2 {
  12.     public Challenge2(int street, int avenue, Direction direction, int beeperCount) {
  13.         super(street, avenue, direction, beeperCount);
  14.     }
  15.  
  16.  
  17.     public static void main(String[] args) {
  18.         World.setDelay(1);
  19.         World.setVisible();
  20.         World.setSize(10, 10);
  21.         World.showSpeedControl(true);
  22.         World.readWorld("KarelChallenges2.kwld");
  23.  
  24.         Challenge2 zgod = new Challenge2(1, 1, West, -1);
  25.  
  26.         zgod.mazeRun();
  27.     }
  28.  
  29.     /**
  30.     * Runs the maze
  31.     * @return void
  32.     * */
  33.     public void mazeRun(){
  34.         while (!nextToABeeper()) { // runs if it is not at the end
  35.             if (wallOnRight()) {
  36.                 if (frontIsClear()) {
  37.                     move(); // moves straight if Karel is next to the wall and it is clear ahead
  38.                 }else {
  39.                     if (nextToABeeper()) {
  40.                         turnOff(); // maze solved
  41.                     } else {
  42.                         turnLeft(); // turns left until the front is clear
  43.                         if (frontIsClear()) {
  44.                             move();
  45.                             if (nextToABeeper()) {
  46.                                 turnOff(); // maze solved
  47.                             }
  48.                         }else {
  49.                             turnLeft();
  50.                         }
  51.                     }
  52.                 }
  53.             } else {
  54.                 while (!wallOnRight()){ // turns and moves until it finds the wall on the right.
  55.                     turnRight();
  56.                     move();
  57.                     if (nextToABeeper()) {
  58.                         turnOff(); // maze solved
  59.                     }
  60.                 }
  61.             }
  62.             if (nextToABeeper()) {
  63.                 turnOff(); // maze solved
  64.             }
  65.         }
  66.     }
  67.  
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement