Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Activity1;
- import kareltherobot.*;
- /**
- * The robot checks if the left intersection is clear to decide if
- * it is going to move or do a back-flip
- */
- public class Activity1Main extends Robot{
- public Activity1Main(int s, int a, Direction d, int b){
- super(s, a, d, b);
- }
- public static void main(String[] args) {
- World.setVisible(); //generates world
- World.setDelay(40);
- World.readWorld("Lesson4World1.kwld");
- Activity1Main mike = new Activity1Main(1,3, North, 0);
- for(int x = 0; x < 4; x++){
- mike.move(); //executes 4 times to get the robot out of the structure
- }
- }
- /**
- * Function checks if the intersection immediately to the left is clear
- * @param - none
- * @return if the left is clear
- */
- public boolean leftIsClear(){
- int delay = World.delay();
- World.setDelay(0);
- turnLeft();
- boolean temp;
- if(!frontIsClear()){
- temp = false;
- }else{
- temp = true;
- }
- World.setDelay(delay);
- turnRight();
- return temp;
- }
- /**
- *Makes the robot check if the left is clear before moving
- * @Overriding
- * @param - none
- * @return - void
- */
- public void move(){
- if(!leftIsClear()){
- super.move(); //if the left is blocked, move forward
- }else{
- for(int i = 0; i < 4; i++){
- turnLeft(); //if the left is clear, do a celebratory back-flip
- }
- }
- }
- /**
- * 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