Advertisement
Guest User

Debugging

a guest
Nov 12th, 2012
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.64 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.                 searchingForMidpoint () ;
  23.                 }
  24.        
  25.         private void makeDiagonalRight () {
  26.                 while (frontIsClear()) {
  27.                         putBeeper () ;
  28.                         move () ;
  29.                         turnLeft () ;
  30.                         move () ;
  31.                         turnRight () ;
  32.                 }
  33.         }
  34.        
  35.         private void setUpForNextDiagonal () {
  36.                 turnAround () ;
  37.                 while (frontIsClear()) {
  38.                         move () ;
  39.                 }
  40.                 turnAround () ;
  41.         }
  42.            
  43.  
  44.         private void makeDiagonalLeft () {
  45.                 while (frontIsClear()) {
  46.                         if (noBeepersPresent()) {
  47.                                 putBeeper () ;
  48.                         }
  49.                         move () ;
  50.                         turnRight () ;
  51.                         move () ;
  52.                         turnLeft () ;
  53.                 }
  54.         }
  55.        
  56.         private void searchingForMidpoint () {
  57.                 turnLeft () ;
  58.                 while (frontIsClear()) {
  59.                         if (beepersPresent()) {
  60.                                 move () ;
  61.                         }
  62.                         move () ;
  63.                         if (beepersPresent()) {
  64.                                 turnLeft () ;
  65.                                 move () ;
  66.                                 turnLeft () ;
  67.                         }      
  68.                 }
  69.                
  70.         }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement