Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Challenge1;
- import kareltherobot.*;
- /**
- * This program puts beepers on the right side for every beeper on the left side
- */
- public class Challenge1Main extends Robot{
- public Challenge1Main(int s, int a, Direction d, int b){
- super(s, a, d, b);
- }
- public static void main(String[] args) {
- World.setVisible(); //reads and creates the world
- World.setDelay(25);
- World.readWorld("KarelChallenges1.kwld");
- Challenge1Main zoro = new Challenge1Main(1,6, North,-1);
- while(zoro.frontIsClear()){ //the program runs until it encounters the final wall
- zoro.move();
- if(zoro.leftIsClear()){ //if there is no wall to the left, check for beepers there
- zoro.checkForBeeper();
- zoro.turnAround(); //moves back to outside the hole
- while(!zoro.leftIsClear()||!zoro.rightIsClear()){
- zoro.move();
- }
- zoro.turnLeft();
- }
- }
- }
- /**
- * This method allows the robot to instantly turn right
- */
- public void turnRight(){
- int delay = World.delay();
- World.setDelay(0);
- turnLeft();
- turnLeft();
- World.setDelay(delay);
- turnLeft();
- }
- /**
- * allows the robot to instantly turn around
- */
- public void turnAround(){
- int delay = World.delay();
- World.setDelay(0); //by changing the delay, the robot turns left instantaneously
- turnLeft();
- World.setDelay(delay);
- turnLeft();
- }
- /**
- * This method allows the robot to instantly check if there is a wall to the left
- * @return if the left intersection is clear
- */
- public boolean leftIsClear(){
- int delay = World.delay();
- World.setDelay(0);
- boolean z = false;
- turnLeft();
- if(frontIsClear()){
- z = true;
- }
- World.setDelay(delay);
- turnRight();
- return z;
- }
- /**
- * This method allows the robot to instantly check if there is a wall to the right
- * @return if the right intersection is clear
- */
- public boolean rightIsClear(){
- int delay = World.delay();
- World.setDelay(0);
- boolean z = false;
- turnRight();
- if(frontIsClear()){
- z = true;
- }
- World.setDelay(delay);
- turnLeft();
- return z;
- }
- /**
- * Checks if there is a beeper in the hole
- */
- public void checkForBeeper(){
- turnLeft();
- while(frontIsClear()){ //move forward until it encounters a wall
- move();
- }
- if(nextToABeeper()){ //if there is a beeper, put a beeper on the other side and move back
- putBeeperOnOtherSide();
- }
- }
- /**
- * Puts a beeper on the other side if there isn't one
- */
- public void putBeeperOnOtherSide(){
- turnAround();
- while(frontIsClear()){ //move forward until it encounters a wall
- move();
- }
- if(!nextToABeeper()){ //if there isn't already a beeper there, put one
- putBeeper();
- }
- turnAround(); //move back
- while(frontIsClear()){
- move();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement