Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // OCD KAREL V2
- package Lesson5;
- import kareltherobot.*;
- public class Lessson5Activity1 extends Robot {
- public Lessson5Activity1(int x, int y, Direction d, int b) { super(x, y, d, b); }
- /**
- * robot turns right
- * @param
- * @return
- */
- public void turnRight() {
- int delay = World.delay();
- World.setDelay(0);
- turnLeft();
- turnLeft();
- World.setDelay(delay);
- turnLeft();
- }
- /**
- * @param
- * @return x
- */
- public boolean beeper(){
- boolean x =frontIsClear();
- return x;
- }
- /**
- * @param
- * @return y
- */
- public boolean check(){
- boolean y = nextToABeeper();
- return y;
- }
- /**
- * robot returns to the left side of the walls
- * @param
- * @return
- */
- public void bobbyReturns() {
- for (int index = 0; index < 90; index++) {
- if (frontIsClear()) {
- super.move();
- } else {
- turnLeft();
- turnLeft();
- super.move();
- turnLeft();
- if (frontIsClear()) {
- super.move();
- turnLeft();
- break;
- } else {
- turnRight();
- super.move();
- turnLeft();
- super.move();
- turnLeft();
- }
- }
- }
- }
- /**
- * robot puts beeper to the corresponding side
- * @param
- * @return
- */
- public void bobbyCorresponding(){
- turnLeft();
- turnLeft();
- while(frontIsClear()) {
- super.move();
- }
- if(!(nextToABeeper())){
- putBeeper();
- }
- turnLeft();
- turnLeft();
- bobbyReturns();
- }
- /**
- * robot moves when no wall is in front of it
- * @param
- * @return
- */
- public void bobbyFront(){
- while(!(frontIsClear())){
- turnRight();
- super.move();
- turnLeft();
- }
- while ((frontIsClear())){
- while(frontIsClear()){
- super.move();
- }
- if((nextToABeeper())) {
- bobbyCorresponding();
- }else{
- turnLeft();
- turnLeft();
- super.move();
- turnLeft();
- super.move();
- turnLeft();
- }
- }
- }
- public static void main(String[]args){
- World.setDelay(20);
- World.setVisible();
- World.readWorld("KarelChallenges1.kwld");
- Lessson5Activity1 bobby =new Lessson5Activity1(1,6,West,100);
- for(int index=0;index< 90;index++){
- bobby.bobbyFront();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement