Advertisement
Guest User

Debugging

a guest
Nov 15th, 2012
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.23 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.                 }
  25.        
  26.         private void makeDiagonalRight () {
  27.                 while (frontIsClear()) {
  28.                         putBeeper () ;
  29.                         move () ;
  30.                         turnLeft () ;
  31.                         move () ;
  32.                         turnRight () ;
  33.                 }
  34.         }
  35.        
  36.         private void setUpForNextDiagonal () {
  37.                 turnAround () ;
  38.                 while (frontIsClear()) {
  39.                         move () ;
  40.                 }
  41.                 turnAround () ;
  42.         }
  43.            
  44.  
  45.         private void makeDiagonalLeft () {
  46.                 while (frontIsClear()) {
  47.                         if (noBeepersPresent()) {
  48.                                 putBeeper () ;
  49.                         }
  50.                         move () ;
  51.                         turnRight () ;
  52.                         move () ;
  53.                         turnLeft () ;
  54.                 }
  55.         }
  56.        
  57.         private void checkForLoneBeeper () {
  58.                 while (leftIsClear()) {
  59.                         while (noBeepersPresent() && frontIsClear()) {
  60.                                 move () ;
  61.                         }
  62.                         if (beepersPresent()) {
  63.                                 move () ;
  64.                                 }
  65.                         while (noBeepersPresent() && frontIsClear ()) {
  66.                                 move () ;
  67.                         }
  68.                         checkForSecondBeeper () ;
  69.                         if (frontIsBlocked()) {
  70.                                 placeMidpointBeeper () ;
  71.                         }
  72.                 }
  73.         }
  74.        
  75.         private void checkForSecondBeeper () {
  76.                 if (beepersPresent() && facingNorth ()) {
  77.                         if (frontIsBlocked()) {
  78.                                 turnLeft () ;
  79.                                 move () ;
  80.                                 turnLeft () ;
  81.                                 move () ;
  82.                         }
  83.                         while (frontIsClear() && facingNorth ()) {
  84.                                 move () ;
  85.                                 if (frontIsBlocked()) {
  86.                                         turnLeft () ;
  87.                                         move () ;
  88.                                         turnLeft () ;
  89.                                         move () ;
  90.                                 }
  91.                         }
  92.                 }
  93.                 if (beepersPresent() && facingSouth ()) {
  94.                         if (frontIsBlocked()) {
  95.                                 turnRight () ;
  96.                                 move () ;
  97.                                 turnRight () ;
  98.                                 move () ;
  99.                         }
  100.                         while (frontIsClear() && facingSouth ()) {
  101.                                 move () ;
  102.                                 if (frontIsBlocked()) {
  103.                                         turnRight () ;
  104.                                         move () ;
  105.                                         turnRight () ;
  106.                                         move () ;
  107.                                 }
  108.                         }
  109.                 }
  110.         }
  111.        
  112.         private void placeMidpointBeeper () {
  113.                 turnAround () ;
  114.                 while (noBeepersPresent()) {
  115.                         move () ;
  116.                 }
  117.                 if (beepersPresent()) {
  118.                         pickBeeper () ;
  119.                 }
  120.                 while (frontIsClear()) {
  121.                         move () ;
  122.                 }
  123.                 if (frontIsBlocked()) {
  124.                         putBeeper () ;
  125.                         turnRight () ;
  126.                         move () ;
  127.                         cleanUpBeepers () ;
  128.                 }
  129.         }
  130.        
  131.         private void cleanUpBeepers () {
  132.                 while (noBeepersPresent()) {
  133.                         move () ;
  134.                 }
  135.                 if (beepersPresent()) {
  136.                         pickBeeper () ;
  137.                 }
  138.                 if (frontIsBlocked()) {
  139.                         turnRight () ;
  140.                         move () ;
  141.                         turnRight () ;
  142.                         move () ;
  143.                 }
  144.         }
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement