Advertisement
GARC923

C3

Nov 15th, 2022
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.32 KB | None | 0 0
  1. /*
  2. Carlos Garcia
  3. Challenge 3
  4. Beeper Trail
  5. */
  6.  
  7. package Challenges;
  8. import kareltherobot.World;
  9.  
  10.  
  11. public class Challenge3 extends EsperBot3 {
  12.     public Challenge3(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(5);
  19.         World.setVisible();
  20.         World.setSize(10, 10);
  21.         World.showSpeedControl(true);
  22.         World.readWorld("KarelChallenges3b.kwld");
  23.  
  24.         Challenge3 zgod = new Challenge3(1, 1, North, -1);
  25.  
  26.         zgod.followTrail();
  27.     }
  28.  
  29.     /**
  30.      * Function to follow the trail of beepers
  31.      * @return void
  32.      * */
  33.     public void followTrail(){
  34.         while (!nextToABeeper()) { // only works when Karel is not on a beeper
  35.             if (beeperAhead()) { // picks the beeper ahead if it is ahead
  36.                 move();
  37.                 pickBeeper();
  38.             } else if (beeperleft()) { // picks the beeper to the left if it is left
  39.                 turnLeft();
  40.                 move();
  41.                 pickBeeper();
  42.             } else if (beeperRight()) { // picks the beeper to the right if it is right
  43.                 turnRight();
  44.                 move();
  45.                 pickBeeper();
  46.             }
  47.         }
  48.     }
  49.  
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement