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 (){
- while (frontIsClear()) {
- buildColumn () ;
- moveToNextColumn () ;
- }
- buildColumn () ;
- }
- /* The Karel buildColumn function turns left, then depending upon whether Karel's
- * front is clear and a beeper is present Karel places a beeper if there
- * isn't already one. If there is already a beeper present Karel moves
- * forward 1 space. If Karel's front is blocked and no beepers are present
- * Karel puts a beeper there. Karel then repositions.
- */
- private void buildColumn () {
- turnLeft () ;
- while (frontIsClear()) {
- if (noBeepersPresent()) {
- putBeeper () ;
- } else {
- move () ;
- }
- }
- if (frontIsBlocked()) {
- if (noBeepersPresent()) {
- putBeeper () ;
- }
- repositionKarel () ;
- }
- }
- /* The repositionKarel function checks if the front is blocked, turns around
- * if it is. Then if the front is clear Karel moves forward but if the front
- * isn't clear Karel turns left.
- *
- */
- private void repositionKarel () {
- turnAround () ;
- while (frontIsClear()) {
- move () ;
- }
- turnLeft () ;
- }
- private void moveToNextColumn () {
- for (int i=0; i<4;i++) {
- move () ;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement