Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package KarelChallenges;
- import kareltherobot.*;
- public class KarelChallenge1 extends Robot {
- public KarelChallenge1(int x, int y, Direction d, int b) {
- super(x, y, d, b);
- }
- public static void main(String[] args) {
- World.readWorld("KarelChallenges1.kwld");
- World.setDelay(25);
- World.setVisible();
- Robot alphaBot = new KarelChallenge1(1, 6, North, -1);
- //runs the robot till the end of the map
- while(alphaBot.frontIsClear()) {
- alphaBot.move();
- }
- }
- /**
- * @Override
- * Moves along the map and then sees if there are beepers in the pits
- */
- public void move() {
- if(checkLeft()) {
- beeperPit();
- }
- }
- /**
- * Goes to the end of the pit to look for a beeper, if there is a beeper, it places one on the other side
- * @param
- * @reutrn void
- */
- public void beeperPit() {
- int depth = inPit();
- if(nextToABeeper()) {
- outPit(depth);
- matchBeeper(depth);
- } else {
- outPit(depth);
- turnLeft();
- super.move();
- }
- }
- /**
- * Matches beeper on the other side
- * @param depth the depth of the pit on the other side
- * @return void
- */
- public void matchBeeper(int depth) {
- runAcross(depth);
- //puts a beeper only if there is no beeper already
- if(!nextToABeeper()) {
- putBeeper();
- }
- //turns around an runs back
- turnLeft();
- turnLeft();
- runAcross(depth);
- turnRight();
- super.move();
- }
- /**
- * checks the robot's left to see if there is a wall
- * @param
- * @return boolean
- */
- public boolean checkLeft() {
- //rotates to be able to check the wall on the left
- int delay = World.delay();
- World.setDelay(0);
- turnLeft();
- World.setDelay(delay);
- //orients itself facing forward again and returns whether there was a wall on the left or not
- if(frontIsClear()) {
- return true;
- } else {
- turnRight();
- super.move();
- return false;
- }
- }
- /**
- * Gets out of pit
- * @param depth the depth of the pit that alphabot has to get out of
- * @return int
- */
- public void outPit(int depth) {
- //gets out of the pit
- turnRight();
- turnRight();
- for(int i = 0; i < depth; i++) {
- super.move();
- }
- }
- /**Goes into the pit returns the depth of it
- * @param
- * @return int
- */
- public int inPit() {
- int depth = 0;
- //moves down and orients itself out
- while(frontIsClear()) {
- super.move();
- depth++;
- }
- return depth;
- }
- /**
- * Runs across to the other side of the map
- * @param depth the depth of the pit on the other side
- * @return void
- */
- public void runAcross(int depth) {
- for(int i = 0; i < (4+depth); i++) {
- super.move();
- }
- }
- /**
- * Makes the robot appear to simply turn right
- * @param
- * @return void
- */
- public void turnRight() {
- int delay = World.delay();
- World.setDelay(0);
- //to turn right
- turnLeft();
- turnLeft();
- turnLeft();
- World.setDelay(delay);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement