Advertisement
Guest User

MidpointFindingKarel - TR

a guest
Aug 13th, 2014
326
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.11 KB | None | 0 0
  1. /*
  2. * File: MidpointFindingKarel.java
  3. * -------------------------------
  4. * When you finish writing it, the MidpointFindingKarel class should
  5. * leave a beeper on the corner closest to the center of 1st Street
  6. * (or either of the two central corners if 1st Street has an even
  7. * number of corners). Karel can put down additional beepers as it
  8. * looks for the midpoint, but must pick them up again before it
  9. * stops. The world may be of any size, but you are allowed to
  10. * assume that it is at least as tall as it is wide.
  11. *
  12. * --
  13. *
  14. * sonHaricBoncukDose(); is used for putting beepers along a
  15. * horizontal line, which can be translated from Turkish to
  16. * English as putBeepersExceptTheLast();
  17. *
  18. * boncukVarkenGitSonBoncuguAl(); is used for picking beepers
  19. * that are at the end of a beeper line, which can be translated
  20. * from Turkish to English as
  21. * asLongAsThereAreBeepersGoPickUpTheLastOne();
  22. *
  23. * the loop is stopped by changing the direction of Karel,
  24. * by making it turn left.
  25. *
  26. * this algorithm is built to be able to work even at a 1x1 world.
  27. */
  28.  
  29. import stanford.karel.*;
  30.  
  31. public class MidpointFindingKarel extends SuperKarel {
  32.  
  33. public void run(){
  34. if(leftIsBlocked()&&rightIsBlocked()&&frontIsBlocked()){
  35. putBeeper();
  36. turnLeft();
  37. }
  38. while(facingEast()||facingWest()){
  39. sonHaricBoncukDose();
  40. boncukVarkenGitSonBoncuguAl();
  41. }
  42. turnLeft();
  43. if(noBeepersPresent()){
  44. move();
  45. }
  46. if(facingWest()){
  47. turnAround();
  48. }
  49. }
  50.  
  51. private void boncukVarkenGitSonBoncuguAl() {
  52. while (beepersPresent()&&frontIsClear()){
  53. move();
  54. }
  55. turnAround();
  56. if (noBeepersPresent()){
  57. if(frontIsClear()){
  58. move();
  59. }
  60. }
  61. if(beepersPresent()){
  62. move();
  63. if(beepersPresent()){
  64. turnAround();
  65. move();
  66. turnAround();
  67. pickBeeper();
  68. }
  69. else{
  70. turnLeft();
  71. }
  72. }
  73. if(facingEast()||facingWest()){
  74. if(frontIsClear()){
  75. move();
  76. }
  77. boncukVarkenGitSonBoncuguAl();
  78. }
  79. }
  80.  
  81. private void sonHaricBoncukDose() {
  82. while(frontIsClear()){
  83. putBeeper();
  84. move();
  85. }
  86. turnAround();
  87. move();
  88. }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement