Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * File: StoneMasonKarel.java
- * --------------------------
- * Karel builds and or repairs columns.
- */
- import stanford.karel.*;
- public class StoneMasonKarel extends SuperKarel {
- public void run (){
- // 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.
- while (frontIsClear()) {
- buildColumn () ;
- moveToNextColumn () ;
- }
- // build the last column, now that we have no more space to the east ;-)
- buildColumn();
- }
- /* Place a beeper on this spot, if there ISN'T already one there
- * Done in this one spot, to avoid typing the noBeeper check all over the place!
- * */
- private void beeper()
- {
- if (noBeepersPresent()) { putBeeper(); }
- }
- private void buildColumn () {
- // faceing east at start, so turn left to go up the column
- turnLeft () ;
- // put a beeper straight away, so we don't miss the starting spot
- beeper();
- // while we have space in front, move into that space, and place a beeper
- while (frontIsClear()) {
- move();
- beeper();
- }
- /* at this point, we should be at the top, with a beeper/stone in all spots */
- // turn around to face down
- turnAround();
- // while we can move down, do so
- while (frontIsClear()) { move(); }
- // turn left to back in starting orientation
- turnLeft();
- }
- private void moveToNextColumn () {
- for (int i=0; i<4;i++) {
- move () ;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement