Advertisement
krystation

MidpointFindingKarel

Jul 14th, 2012
472
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.46 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. public class MidpointFindingKarel extends SuperKarel {
  16.  
  17.     public void run() {
  18.         fillRow();
  19.         moveBack();
  20.         pickBeeper();
  21.         turnAround();
  22.         pickUpAllBeepersAtEnds();
  23.     }
  24. /*
  25.  * fillRow:
  26.  * karel will place a beeper along the 1st row, except for the last block.
  27.  *
  28.  * pre-conditions: karel is in the first block of the first row and facing east
  29.  *
  30.  * post-conditions: karel will be in the last block of the first row facing east   
  31.  */
  32.     private void fillRow(){
  33.         while (frontIsClear()) {
  34.             putBeeper();
  35.             move();
  36.         }
  37.     }
  38. /*
  39.  * moveBack:
  40.  * karel will move back to her intital staring position.
  41.  *
  42.  * post-conditions: karel will be facing west  
  43.  */
  44.     private void moveBack() {
  45.         turnAround();
  46.         while (frontIsClear()){
  47.             move();
  48.         }
  49.     }
  50. /*
  51.  * pickUpAllBeepersAtEnds:
  52.  * karel will move to all the outer beepers and remove them till only the
  53.  * center beeper remains.
  54.  *
  55.  * pre-condition: must be in 1st block of 1st row and facing east.
  56.  *
  57.  * post-condition: karel will end up at the end of the row, either on the east
  58.  * or west side, depending on how many blocks are in a row.
  59.  */
  60.     private void pickUpAllBeepersAtEnds() {
  61.         while (frontIsClear()){
  62.             moveToOuterBeepers();
  63.             checkIfOuterBeeper();
  64.         }
  65.     }
  66. /*
  67.  * moveToOuterBeepers:
  68.  * karel will conduct a test to determine which way to move to reach
  69.  *the outer beeper farthest from her
  70.  *
  71.  * pre-conditions: karel must be facing east and standing on a block with no beeper.
  72.  *
  73.  * post-condition: karel will be on the outer beeper furthest from her starting point
  74.  * and be facing east.
  75.  */
  76.     private void moveToOuterBeepers(){
  77.         move();
  78.         if (beepersPresent()){
  79.             turnAround();
  80.             move();
  81.             turnAround();
  82.             moveRightToOuterBeeper();
  83.         } else {
  84.             turnAround();
  85.             move();
  86.             turnAround();
  87.             moveLeftToOuterBeeper();
  88.         }
  89.     }
  90. /*
  91.  * moveRightToOuterBeeper:
  92.  * karel will move right to reach the outer beeper
  93.  *
  94.  * pre-conditions: karel must be facing east and standing on a block with no beeper.
  95.  *
  96.  * post-condition: karel will be on the outer beeper furthest from her starting point
  97.  * and be facing east. 
  98.  */
  99.     private void moveRightToOuterBeeper(){
  100.         move();
  101.         while (beepersPresent()){
  102.             move();
  103.         }
  104.         turnAround();
  105.         move();
  106.         turnAround();
  107.     }
  108.     /*
  109.      * moveLeftToOuterBeeper:
  110.      * karel will move left to reach the outer beeper
  111.      *
  112.      * pre-conditions: karel must be facing east and standing on a block with no beeper.
  113.      *
  114.      * post-condition: karel will be on the outer beeper furthest from her starting point
  115.      * and be facing east. 
  116.      */
  117.         private void moveLeftToOuterBeeper(){
  118.             turnAround();
  119.             move();
  120.             while (beepersPresent()){
  121.                 move();
  122.             }
  123.             turnAround();
  124.             move();
  125.         }  
  126.     /*
  127.      * checkIfOuterBeeper:
  128.      * karel will check to see if the beeper she is standing on is an outer beeper.
  129.      * if so then karel will remove it.
  130.      *
  131.      * pre-conditions: karel must be standing on a beeper and facing east.
  132.      *
  133.      * post-conditions1: karel will be standing in the same position she was in and be facing east.
  134.      * the only difference is that there will be no beeper.
  135.      *  
  136.      * post-condition2: karel will be at either end of the row with her front blocked, if the beeper
  137.      * she was on turns out to be the center beeper.
  138.      */
  139.         private void checkIfOuterBeeper(){
  140.             if (frontIsClear()){
  141.                 turnAround();
  142.                 move();
  143.                 if (beepersPresent()){
  144.                     turnAround();
  145.                     move();
  146.                     pickBeeper();
  147.                 } else {
  148.                     if (frontIsBlocked()){
  149.                         turnAround();
  150.                         move();
  151.                         pickBeeper();
  152.                     } else {
  153.                         checkOtherSide();
  154.                     }
  155.                
  156.                 }
  157.             }
  158.         }
  159.     /*
  160.      * checkOtherSide:
  161.      * another test to double check if beeper is center or outer   
  162.      */
  163.         private void checkOtherSide(){
  164.             turnAround();
  165.             move();
  166.             move();
  167.             if (beepersPresent()){
  168.                 turnAround();
  169.                 move();
  170.                 pickBeeper();
  171.             } else {
  172.                 while (frontIsClear()){
  173.                     move();
  174.                 }
  175.             }
  176.         }
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement