Advertisement
Guest User

StoneMasonKarel

a guest
Nov 10th, 2012
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.48 KB | None | 0 0
  1. /*
  2.  * File: StoneMasonKarel.java
  3.  * --------------------------
  4.  * Karel builds and or repairs columns.
  5.  */
  6.  
  7. import stanford.karel.*;
  8.  
  9. public class StoneMasonKarel extends SuperKarel {
  10.         public void run (){
  11.                 while (frontIsClear()) {
  12.                         buildColumn () ;
  13.                         moveToNextColumn () ;
  14.                 }
  15.                 buildColumn () ;
  16.         }
  17.        
  18.  /* The Karel buildColumn function turns left, then depending upon whether Karel's
  19.   * front is clear and a beeper is present Karel places a beeper if there
  20.   * isn't already one. If there is already a beeper present Karel moves
  21.   * forward 1 space. If Karel's front is blocked and no beepers are present
  22.   *  Karel puts a beeper there. Karel then repositions.
  23.   */       
  24.        
  25.         private void buildColumn () {
  26.                 turnLeft () ;
  27.                 while (frontIsClear()) {
  28.                         if (noBeepersPresent()) {
  29.                                 putBeeper () ;
  30.                         } else {
  31.                                 move () ;
  32.                         }
  33.                 }
  34.                 if (frontIsBlocked()) {
  35.                         if (noBeepersPresent()) {
  36.                                 putBeeper () ;
  37.                         }
  38.                         repositionKarel () ;
  39.                 }
  40.         }
  41.  
  42.   /* The repositionKarel function checks if the front is blocked, turns around
  43.    * if it is. Then if the front is clear Karel moves forward but if the front
  44.    * isn't clear Karel turns left.
  45.    *
  46.    */
  47.        
  48.         private void repositionKarel () {
  49.                 turnAround () ;
  50.                 while (frontIsClear()) {
  51.                         move () ;
  52.                 }  
  53.                         turnLeft () ;
  54.  
  55.         }
  56.  
  57.         private void moveToNextColumn () {
  58.                 for (int i=0; i<4;i++) {
  59.                         move () ;
  60.                 }
  61.         }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement