Here is my solution to problem 2 from Assignment 1. In this problem Karel must rebuild the archways at Stanford after the earthquake in 1989. The real problem is that you have multiple worlds to solve for. This is my solution.
/* * File: StoneMasonKarel.java * -------------------------- * The StoneMasonKarel subclass as it appears here does nothing. * When you finish writing it, it should solve the "repair the quad" * problem from Assignment 1. In addition to editing the program, * you should be sure to edit this comment so that it no longer * indicates that the program does nothing. */import stanford.karel.*;
/* * Name: Ken Swain * Section Leader: */
public class StoneMasonKarel extends SuperKarel {
public void run() { /* * Repair the damage done to the quad during the earthquake * * Pre Condition: Karel must be on 1-1 facing east * * Post Condition: Karel must have built all columns */ while (frontIsClear()) { moveToWall(); placeColumn(); moveFour(); } moveToWall(); placeColumn(); } private void moveToWall() { /* * Go to the top of the column so Karel can start fixing the column * *Pre Condition: Karal must be standing at the bottom of the column * *Post Condition: Karel will be at the top of the column facing north. */ while (notFacingNorth()) { turnLeft(); } while (frontIsClear()) { move(); } } private void placeColumn() { /* * Place the stones to fix the column. * * Pre Condition: Karel must be at the top of the column facing north. * * Post Condition: Karel will be at the bottom of the column, with it fully built, facing east. */ while (notFacingSouth()) { turnAround(); } while (frontIsClear()) { placeStones(); } if (noBeepersPresent()) { putBeeper(); } turnLeft(); } private void placeStones() { /* * Place the stones where they are missing. * * Pre Condition: Karel must be facing the direction the stones will be placed. * * Post Condition: All stones will be placed. */ while (frontIsClear()) { if (noBeepersPresent()) { putBeeper(); move(); } else { move(); } } } private void moveFour() { /* * Move to the next column. * * Pre Condition: Karel is at the bottom of the map facing east. * * Post condition: Karel will be at the next column facing east. */ for (int i=0; i<4; i++) { if (frontIsClear()) { move(); } } }}
