Advertisement
Guest User

StoneMason! XD

a guest
Nov 10th, 2012
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.12 KB | None | 0 0
  1. /*
  2.  * File: StoneMasonKarel.java
  3.  * --------------------------
  4.  * Karel builds and or repairs columns.
  5.  */
  6.  
  7.  
  8.  
  9. import stanford.karel.*;
  10.  
  11. public class StoneMasonKarel extends SuperKarel {
  12.         public void run (){
  13.            
  14.                 // if front is clear, then we're NOT on the last column, so we can build a column, and move to the next column starting point.
  15.                 while (frontIsClear()) {
  16.                         buildColumn () ;
  17.                         moveToNextColumn () ;                      
  18.                 }
  19.                
  20.                 // build the last column, now that we have no more space to the east ;-)
  21.                 buildColumn();
  22.                
  23.         }
  24.        
  25.        
  26.         /* Place a beeper on this spot, if there ISN'T already one there
  27.          * Done in this one spot, to avoid typing the noBeeper check all over the place!
  28.          * */
  29.         private void beeper()
  30.         {
  31.             if (noBeepersPresent()) { putBeeper(); }
  32.         }
  33.        
  34.                
  35.         private void buildColumn () {
  36.                 // faceing east at start, so turn left to go up the column
  37.                 turnLeft () ;
  38.                
  39.                 // put a beeper straight away, so we don't miss the starting spot
  40.                 beeper();
  41.                
  42.                 // while we have space in front, move into that space, and place a beeper
  43.                 while (frontIsClear()) {
  44.                     move();
  45.                     beeper();
  46.                 }
  47.                
  48.                 /* at this point, we should be at the top, with a beeper/stone in all spots */
  49.                
  50.                 // turn around to face down
  51.                 turnAround();
  52.                
  53.                 // while we can move down, do so
  54.                 while (frontIsClear()) { move(); }
  55.                
  56.                 // turn left to back in starting orientation
  57.                 turnLeft();
  58.         }
  59.  
  60.  
  61.         private void moveToNextColumn () {
  62.                 for (int i=0; i<4;i++) {
  63.                         move () ;
  64.                 }
  65.         }
  66.        
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement