Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Moves alphabot forward until there is no wall on the left, then it does a back flip
- * By: Alejandro Antorcha
- */
- package Lesson4;
- import kareltherobot.*;
- public class Activity1Lesson4 extends Robot{
- public Activity1Lesson4(int x, int y, Direction d, int b) {
- super(x, y, d, b);
- }
- public static void main(String[] args) {
- World.readWorld("Lesson4World1.kwld");
- World.setDelay(25);
- World.setVisible();
- Robot alphaBot = new Activity1Lesson4(1, 3, North, 0);
- //runs move() 4 times
- for(int i = 0; i < 4; i++) {
- alphaBot.move();
- }
- }
- /**
- * @Override
- * Moves normally but if it is at the tower, it turns left
- * @param
- * @return void
- */
- public void move() {
- //chooses to either move forward or do a back flip
- if(leftIsClear()) {
- this.backFlip();
- } else {
- super.move();
- }
- }
- /**
- * checks to see if the left is clear
- * @param
- * @return boolean
- */
- public boolean leftIsClear() {
- int delay = World.delay();
- World.setDelay(0);
- //initializes boolean variable clear
- boolean clear;
- //turns left to orient and check
- this.turnLeft();
- //assigns clear to
- clear = this.frontIsClear();
- //orients robot back facing north
- while(!this.facingNorth()) {
- turnLeft();
- }
- //setting the relay to what it was previously set to
- World.setDelay(delay);
- return clear;
- }
- /**
- * makes the robot do a back flip
- * @param
- * @return void
- */
- public void backFlip() {
- for(int i = 0; i < 4; i++) {
- this.turnLeft();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement