Advertisement
Guest User

Untitled

a guest
Jan 18th, 2020
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.50 KB | None | 0 0
  1. package KarelChallenges;
  2. import kareltherobot.*;
  3.  
  4. public class KarelChallenge1 extends Robot {
  5. public KarelChallenge1(int x, int y, Direction d, int b) {
  6. super(x, y, d, b);
  7. }
  8.  
  9. public static void main(String[] args) {
  10. World.readWorld("KarelChallenges1.kwld");
  11.  
  12. World.setDelay(25);
  13. World.setVisible();
  14.  
  15. Robot alphaBot = new KarelChallenge1(1, 6, North, -1);
  16.  
  17. //runs the robot till the end of the map
  18. while(alphaBot.frontIsClear()) {
  19. alphaBot.move();
  20. }
  21. }
  22.  
  23. /**
  24. * @Override
  25. * Moves along the map and then sees if there are beepers in the pits
  26. */
  27. public void move() {
  28. if(checkLeft()) {
  29. beeperPit();
  30. }
  31. }
  32.  
  33. /**
  34. * Goes to the end of the pit to look for a beeper, if there is a beeper, it places one on the other side
  35. * @param
  36. * @reutrn void
  37. */
  38. public void beeperPit() {
  39. int depth = inPit();
  40. if(nextToABeeper()) {
  41. outPit(depth);
  42. matchBeeper(depth);
  43. } else {
  44. outPit(depth);
  45. turnLeft();
  46. super.move();
  47. }
  48. }
  49.  
  50. /**
  51. * Matches beeper on the other side
  52. * @param depth the depth of the pit on the other side
  53. * @return void
  54. */
  55. public void matchBeeper(int depth) {
  56. runAcross(depth);
  57.  
  58. //puts a beeper only if there is no beeper already
  59. if(!nextToABeeper()) {
  60. putBeeper();
  61. }
  62. //turns around an runs back
  63. turnLeft();
  64. turnLeft();
  65. runAcross(depth);
  66. turnRight();
  67. super.move();
  68. }
  69.  
  70. /**
  71. * checks the robot's left to see if there is a wall
  72. * @param
  73. * @return boolean
  74. */
  75. public boolean checkLeft() {
  76. //rotates to be able to check the wall on the left
  77. int delay = World.delay();
  78. World.setDelay(0);
  79. turnLeft();
  80. World.setDelay(delay);
  81.  
  82. //orients itself facing forward again and returns whether there was a wall on the left or not
  83. if(frontIsClear()) {
  84. return true;
  85. } else {
  86. turnRight();
  87. super.move();
  88. return false;
  89. }
  90. }
  91.  
  92. /**
  93. * Gets out of pit
  94. * @param depth the depth of the pit that alphabot has to get out of
  95. * @return int
  96. */
  97. public void outPit(int depth) {
  98. //gets out of the pit
  99. turnRight();
  100. turnRight();
  101. for(int i = 0; i < depth; i++) {
  102. super.move();
  103. }
  104. }
  105.  
  106. /**Goes into the pit returns the depth of it
  107. * @param
  108. * @return int
  109. */
  110. public int inPit() {
  111. int depth = 0;
  112. //moves down and orients itself out
  113. while(frontIsClear()) {
  114. super.move();
  115. depth++;
  116. }
  117. return depth;
  118. }
  119.  
  120. /**
  121. * Runs across to the other side of the map
  122. * @param depth the depth of the pit on the other side
  123. * @return void
  124. */
  125. public void runAcross(int depth) {
  126. for(int i = 0; i < (4+depth); i++) {
  127. super.move();
  128. }
  129. }
  130.  
  131. /**
  132. * Makes the robot appear to simply turn right
  133. * @param
  134. * @return void
  135. */
  136. public void turnRight() {
  137. int delay = World.delay();
  138. World.setDelay(0);
  139.  
  140. //to turn right
  141. turnLeft();
  142. turnLeft();
  143. turnLeft();
  144.  
  145. World.setDelay(delay);
  146. }
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement