Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Activity4;
- import kareltherobot.*;
- /**
- * This program loads a world, and then the robot checks and fills any hole it encounters
- */
- public class Activity4Main extends Robot{
- public Activity4Main(int s, int a, Direction d, int b) {
- super(s, a, d, b);
- }
- public static void main(String[] args){
- World.readWorld("Lesson4World3.kwld"); //creates world
- World.setVisible();
- World.setDelay(30);
- Activity4Main will = new Activity4Main(3,1, North, -1); //creates robot
- will.turnRight();
- for(int x = 0; x < 9; x++){ //fills the hole if there is one and moves forward
- will.move();
- will.fillHole();
- }
- }
- /**
- * this function instantly checks whether the robot needs to fill a hole
- * @return if there is a hole or not
- */
- public boolean checkForHole(){
- int delay = World.delay();
- World.setDelay(0);
- boolean y = false;
- turnRight();
- if(frontIsClear()){ //if the front is clear, there is a hole
- y = true;
- }
- World.setDelay(delay);
- turnLeft();
- return y;
- }
- /**
- * If there is a hole, fill it
- * @return void
- */
- public void fillHole(){
- if(checkForHole()){
- turnRight();
- move();
- putBeeper();
- turnLeft();
- turnLeft();
- move();
- turnRight();
- }
- }
- /**
- * This function allow the robot to instantly turn right
- * @param - none
- * @return - void
- */
- public void turnRight(){
- int delay = World.delay();
- World.setDelay(0);
- turnLeft();
- turnLeft();
- World.setDelay(delay);
- turnLeft();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement