Advertisement
saul96

MidpointFindingKarel

Apr 4th, 2015
655
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 4.36 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. import stanford.karel.*;
  14.  
  15. /*
  16.  * PRE-CONDITIONS:  Karel starts in lower-left corner facing east
  17.  *                  Square world
  18.  * POST-CONDITIONS: Karel stands on a beeper in the center (or left/right
  19.  *                  center) of First Street
  20.  *                  
  21.  */
  22.  
  23. public class MidpointFindingKarel extends SuperKarel {
  24.     public void run () {
  25.         spiralAreaWithBeepers();
  26.         orientAndHeadSouth();
  27.         // double the beeper count on center, to find later
  28.         putBeeper();
  29.         // go to starting position
  30.         goHome();
  31.         unSpiralBeepers();
  32.         goHome();
  33.         goToBeeper();
  34.     }
  35.     private void spiralAreaWithBeepers() {
  36.         while (noBeepersPresent()) {
  37.                 dropBeepersEast();
  38.                 dropBeepersNorth();
  39.                 dropBeepersWest();
  40.                 dropBeepersSouth();
  41.         }
  42.     }
  43.     private void orientAndHeadSouth() {
  44.         while (notFacingSouth()) {
  45.             turnLeft();
  46.         }
  47.         while (frontIsClear()) {
  48.             move();
  49.         }
  50.     }
  51.     private void goHome() {
  52.         while (notFacingWest()) {
  53.             turnLeft();
  54.         }
  55.         while (frontIsClear()) {
  56.             move();
  57.         }
  58.         if (leftIsBlocked()) {
  59.             turnAround();
  60.         } else {
  61.             orientAndHeadSouth();
  62.             turnLeft();
  63.         }
  64.     }
  65.     private void unSpiralBeepers() {
  66.         while (beepersPresent()) {
  67.             pickBeepersEast();
  68.             pickBeepersNorth();
  69.             pickBeepersWest();
  70.             pickBeepersSouth();    
  71.         }
  72.     }
  73.     private void goToBeeper() {
  74.         while (noBeepersPresent()) {
  75.             move();
  76.         }
  77.     }
  78.     private void dropBeepersEast() {
  79.         while (facingEast()) {
  80.             if (noBeepersPresent()) {
  81.                 putBeeper();
  82.             }
  83.             if (frontIsClear()) {
  84.                 move();
  85.                 if (beepersPresent()) {
  86.                     turnAround();
  87.                     move();
  88.                     turnRight();
  89.                     move();
  90.                 }
  91.             } else {
  92.                 turnLeft();
  93.                 move();
  94.             }
  95.         }
  96.     }
  97.     private void dropBeepersNorth() {
  98.         while (facingNorth()) {
  99.             if (noBeepersPresent()) {
  100.                 putBeeper();
  101.             }
  102.             if (frontIsClear()) {
  103.                 move();
  104.                 if (beepersPresent()) {
  105.                     turnAround();
  106.                     move();
  107.                     turnRight();
  108.                     move();
  109.                 }
  110.             } else {
  111.                 turnLeft();
  112.                 move();
  113.             }
  114.         }      
  115.     }
  116.     private void dropBeepersWest() {
  117.         while (facingWest()) {
  118.             if (noBeepersPresent()) {
  119.                 putBeeper();
  120.             }
  121.             if (frontIsClear()) {
  122.                 move();
  123.                 if (beepersPresent()) {
  124.                     turnAround();
  125.                     move();
  126.                     turnRight();
  127.                     move();
  128.                 }
  129.             } else {
  130.                 turnLeft();
  131.             }
  132.         }
  133.     }
  134.     private void dropBeepersSouth() {
  135.         while (facingSouth()) {
  136.             if (noBeepersPresent()) {
  137.                 putBeeper();
  138.             }
  139.             if (frontIsClear()) {
  140.                 move();
  141.                 if (beepersPresent()) {
  142.                     turnAround();
  143.                     move();
  144.                     turnRight();
  145.                     move();
  146.                 }
  147.             } else {
  148.                 turnLeft();
  149.             }
  150.         }
  151.     }
  152.     private void pickBeepersEast() {
  153.         while (facingEast()) {
  154.             if (beepersPresent())   {
  155.                 pickBeeper();
  156.             }
  157.             if (frontIsClear()) {
  158.                 move();
  159.                 if (noBeepersPresent()) {
  160.                     turnAround();
  161.                     move();
  162.                     turnRight();
  163.                     move();
  164.                 }
  165.             } else {
  166.                 turnLeft();
  167.             }
  168.         }
  169.     }
  170.     private void pickBeepersNorth() {
  171.         while (facingNorth()) {
  172.             if (beepersPresent())   {
  173.                 pickBeeper();
  174.             }
  175.             if (frontIsClear()) {
  176.                 move();
  177.                 if (noBeepersPresent()) {
  178.                     turnAround();
  179.                     move();
  180.                     turnRight();
  181.                     move();
  182.                 }
  183.             } else {
  184.                 turnLeft();
  185.             }
  186.         }
  187.     }
  188.     private void pickBeepersWest() {
  189.         while (facingWest()) {
  190.             if (beepersPresent())   {
  191.                 pickBeeper();
  192.             }
  193.             if (frontIsClear()) {
  194.                 move();
  195.                 if (noBeepersPresent()) {
  196.                     turnAround();
  197.                     move();
  198.                     turnRight();
  199.                     move();
  200.                 }
  201.             } else {
  202.                 turnLeft();
  203.             }
  204.         }
  205.     }
  206.     private void pickBeepersSouth() {
  207.         while (facingSouth()) {
  208.             if (beepersPresent())   {
  209.                 pickBeeper();
  210.             }
  211.             if (frontIsClear()) {
  212.                 move();
  213.                 if (noBeepersPresent()) {
  214.                     turnAround();
  215.                     move();
  216.                     turnRight();
  217.                     move();
  218.                 }
  219.             } else {
  220.                 turnLeft();
  221.             }
  222.         }
  223.     }  
  224. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement