Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Lesson4;
- import kareltherobot.*;
- /**
- * Francesco Di Mise
- * This is a program that uses a robot with sensors, as well as if statements and for loops to get out of a "hole"
- */
- public class Lesson4Activity1 extends Robot{
- /**
- * Constructor
- * @param x
- * @param y
- * @param d
- * @param b
- */
- public Lesson4Activity1 (int x, int y, Direction d, int b) {
- super(x, y, d, b);
- }
- /**
- * Main method. Oly makes the robot and the world, then moves the robot 4 times
- * @param args
- */
- public static void main (String[] args) {
- World.setDelay(50);
- World.setVisible();
- World.readWorld("Lesson4World1.kwld");
- Lesson4Activity1 MrDaddy = new Lesson4Activity1(1,3,North, 0);
- for (int index = 0; index < 4; index++){
- MrDaddy.move();
- }
- }
- /**
- * a boolean method. Checks if the left is clear by instantaneously turning left, checking if the front is clear, then instantly turning back right
- * @return
- */
- public boolean leftIsClear(){
- int delay = World.delay();
- World.setDelay(0);
- turnLeft();
- boolean check;
- if (!frontIsClear()){
- check = false;
- } else {
- check = true;
- }
- World.setDelay(delay);
- turnRight();
- return check;
- }
- /**
- * An override method for move. Checks the boolean method if the left is clear. If is not clear, then it wil move forward. If it is, it wil doa 180 turn
- */
- public void move(){
- if (!leftIsClear()){
- super.move();
- } else {
- for (int index = 0; index < 4; index++){
- turnLeft();
- }
- }
- }
- /**
- * Method to instantly turn right
- */
- 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