Advertisement
Guest User

Debugging

a guest
Nov 18th, 2012
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.69 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.         public void run () {
  17.                 makeDiagonalRight () ;
  18.                 putBeeper () ;
  19.                 setUpForNextDiagonal () ;
  20.                 makeDiagonalLeft () ;
  21.                 putBeeper () ;
  22.                 turnLeft () ;
  23.                 checkForLoneBeeper () ;
  24.                 cleanUpBeepers () ;
  25.                 }
  26.        
  27.         private void makeDiagonalRight () {
  28.                 while (frontIsClear()) {
  29.                         putBeeper () ;
  30.                         move () ;
  31.                         turnLeft () ;
  32.                         move () ;
  33.                         turnRight () ;
  34.                 }
  35.         }
  36.        
  37.         private void setUpForNextDiagonal () {
  38.                 turnAround () ;
  39.                 while (frontIsClear()) {
  40.                         move () ;
  41.                 }
  42.                 turnAround () ;
  43.         }
  44.            
  45.  
  46.         private void makeDiagonalLeft () {
  47.                 while (frontIsClear()) {
  48.                         if (noBeepersPresent()) {
  49.                                 putBeeper () ;
  50.                         }
  51.                         move () ;
  52.                         turnRight () ;
  53.                         move () ;
  54.                         turnLeft () ;
  55.                 }
  56.         }
  57.        
  58.         private void checkForLoneBeeper () {
  59.                 while (leftIsClear()) {
  60.                         while (noBeepersPresent() && frontIsClear()) {
  61.                                 move () ;
  62.                         }
  63.                         if (beepersPresent()) {
  64.                                 move () ;
  65.                                 }
  66.                         while (noBeepersPresent() && frontIsClear ()) {
  67.                                 move () ;
  68.                         }
  69.                         checkForSecondBeeper () ;
  70.                         if (frontIsBlocked()) {
  71.                                 placeMidpointBeeper () ;
  72.                         }
  73.                 }
  74.         }
  75.        
  76.         private void checkForSecondBeeper () {
  77.                 if (beepersPresent() && facingNorth ()) {
  78.                         if (frontIsBlocked()) {
  79.                                 turnLeft () ;
  80.                                 move () ;
  81.                                 turnLeft () ;
  82.                                 move () ;
  83.                         }
  84.                         while (frontIsClear() && facingNorth ()) {
  85.                                 move () ;
  86.                                 if (beepersPresent()) {
  87.                                         turnLeft () ;
  88.                                         move () ;
  89.                                         if (beepersPresent()) {
  90.                                                 turnAround () ;
  91.                                                 move () ;
  92.                                                 turnLeft () ;
  93.                                                 placeMidpointBeeper () ;
  94.                                         }
  95.                                         if (noBeepersPresent()) {
  96.                                                 turnAround () ;
  97.                                                 move () ;
  98.                                                 turnLeft () ;
  99.                                         }
  100.                                         if (frontIsBlocked()) {
  101.                                                 turnLeft () ;
  102.                                                 move () ;
  103.                                                 turnLeft () ;
  104.                                                 move () ;
  105.                                         }
  106.                                 }
  107.                         }
  108.                 }
  109.                 if (beepersPresent() && facingSouth ()) {
  110.                         if (frontIsBlocked()) {
  111.                                 turnRight () ;
  112.                                 move () ;
  113.                                 turnRight () ;
  114.                                 move () ;
  115.                         }
  116.                         while (frontIsClear() && facingSouth ()) {
  117.                                 move () ;
  118.                                 if (beepersPresent()) {
  119.                                         turnRight () ;
  120.                                         move () ;
  121.                                         if (beepersPresent()) {
  122.                                                 turnAround () ;
  123.                                                 move () ;
  124.                                                 turnRight () ;
  125.                                                 placeMidpointBeeper () ;
  126.                                         }
  127.                                         if (noBeepersPresent()) {
  128.                                                 turnAround () ;
  129.                                                 move () ;
  130.                                                 turnRight () ;
  131.                                         }
  132.                                         if (frontIsBlocked()) {
  133.                                                 turnRight () ;
  134.                                                 move () ;
  135.                                                 turnRight () ;
  136.                                                 move () ;
  137.                                         }
  138.                                 }
  139.                         }
  140.                 }
  141.         }
  142.        
  143.         private void placeMidpointBeeper () {
  144.                 turnAround () ;
  145.                 while (noBeepersPresent()) {
  146.                         move () ;
  147.                 }
  148.                 if (beepersPresent()) {
  149.                         pickBeeper () ;
  150.                 }
  151.                 if (facingNorth()) {
  152.                         turnAround () ;
  153.                 }
  154.                 while (frontIsClear()) {
  155.                         move () ;
  156.                 }
  157.                 if (frontIsBlocked()) {
  158.                         putBeeper () ;
  159.                         turnRight () ;
  160.                         move () ;
  161.                 }
  162.         }
  163.        
  164.         private void cleanUpBeepers () {
  165.                 cleanRow () ;
  166.                 while (rightIsClear()) {
  167.                         repositionForRowToEast () ;
  168.                         cleanRow () ;
  169.                         if (leftIsClear()) {
  170.                                 repositionForRowToWest () ;
  171.                                 cleanRow () ;
  172.                         } else {
  173.                                 turnAround () ;
  174.                         }
  175.                 }
  176.                 turnAround () ;
  177.                 cleanRow () ;
  178.                 turnRight () ;
  179.                 cleanRow () ;
  180.                 repositionToBeginning () ;
  181.         }
  182.                        
  183.         private void cleanRow () {             
  184.                 while (frontIsClear()) {
  185.                         move () ;
  186.                         if (beepersPresent()) {
  187.                                 pickBeeper () ;
  188.                         }
  189.                 }
  190.         }
  191.        
  192.         private void repositionForRowToWest () {
  193.                 turnLeft () ;
  194.                 move () ;
  195.                 turnLeft () ;
  196.         }
  197.        
  198.         private void repositionForRowToEast () {
  199.                 turnRight () ;
  200.                 move () ;
  201.                 turnRight () ;
  202.         }
  203.        
  204.         private void repositionToBeginning () {
  205.                 turnRight () ;
  206.                 while (frontIsClear()) {
  207.                         move () ;
  208.                 }
  209.                 turnAround () ;
  210.         }
  211. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement