Advertisement
Guest User

Untitled

a guest
Jan 20th, 2020
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.90 KB | None | 0 0
  1. // OCD KAREL V2
  2. package Lesson5;
  3. import kareltherobot.*;
  4.  
  5. public class Lessson5Activity1 extends Robot {
  6. public Lessson5Activity1(int x, int y, Direction d, int b) { super(x, y, d, b); }
  7.  
  8. /**
  9. * robot turns right
  10. * @param
  11. * @return
  12. */
  13. public void turnRight() {
  14. int delay = World.delay();
  15. World.setDelay(0);
  16. turnLeft();
  17. turnLeft();
  18. World.setDelay(delay);
  19. turnLeft();
  20. }
  21. /**
  22. * @param
  23. * @return x
  24. */
  25. public boolean beeper(){
  26. boolean x =frontIsClear();
  27. return x;
  28. }
  29.  
  30. /**
  31. * @param
  32. * @return y
  33. */
  34. public boolean check(){
  35. boolean y = nextToABeeper();
  36. return y;
  37. }
  38.  
  39. /**
  40. * robot returns to the left side of the walls
  41. * @param
  42. * @return
  43. */
  44. public void bobbyReturns() {
  45. for (int index = 0; index < 90; index++) {
  46. if (frontIsClear()) {
  47. super.move();
  48. } else {
  49. turnLeft();
  50. turnLeft();
  51. super.move();
  52. turnLeft();
  53.  
  54. if (frontIsClear()) {
  55. super.move();
  56. turnLeft();
  57. break;
  58. } else {
  59. turnRight();
  60. super.move();
  61. turnLeft();
  62. super.move();
  63. turnLeft();
  64.  
  65. }
  66.  
  67. }
  68. }
  69. }
  70.  
  71. /**
  72. * robot puts beeper to the corresponding side
  73. * @param
  74. * @return
  75. */
  76. public void bobbyCorresponding(){
  77. turnLeft();
  78. turnLeft();
  79.  
  80. while(frontIsClear()) {
  81. super.move();
  82. }
  83.  
  84. if(!(nextToABeeper())){
  85. putBeeper();
  86. }
  87. turnLeft();
  88. turnLeft();
  89. bobbyReturns();
  90. }
  91.  
  92. /**
  93. * robot moves when no wall is in front of it
  94. * @param
  95. * @return
  96. */
  97. public void bobbyFront(){
  98. while(!(frontIsClear())){
  99. turnRight();
  100. super.move();
  101. turnLeft();
  102. }
  103. while ((frontIsClear())){
  104. while(frontIsClear()){
  105. super.move();
  106. }
  107.  
  108. if((nextToABeeper())) {
  109. bobbyCorresponding();
  110. }else{
  111. turnLeft();
  112. turnLeft();
  113. super.move();
  114. turnLeft();
  115. super.move();
  116. turnLeft();
  117. }
  118. }
  119.  
  120. }
  121.  
  122.  
  123. public static void main(String[]args){
  124. World.setDelay(20);
  125. World.setVisible();
  126. World.readWorld("KarelChallenges1.kwld");
  127.  
  128. Lessson5Activity1 bobby =new Lessson5Activity1(1,6,West,100);
  129.  
  130. for(int index=0;index< 90;index++){
  131. bobby.bobbyFront();
  132. }
  133. }
  134.  
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement