cs106a – Assignment #1 – Task #2

Karel has been hired to repair the damage done to the Quad in the 1989 earthquake. In particular, Karel is to repair a set of arches where some of the stones (represented by beepers, of course) are missing from the columns supporting the arches, as follows:

cs106 – assignment #1 – task #2 – start


Your program should work on the world shown above, but it should be general enough to handle any world that meets certain basic conditions as outlined at the end of this problem. There are several example worlds in the starter folder, and your program should work correctly with all of them.
When Karel is done, the missing stones in the columns should be replaced by beepers, so that the final picture resulting from the world shown above would look like this:
cs106 – assignment #1 – task #2 – end

Karel may count on the following facts about the world, list on the next page:

  • Karel starts at 1st Avenue and 1st Street, facing east, with an infinite number of beepers.
  • The columns are exactly four units apart, on 1st, 5th, 9th Avenue, and so forth.
  • The end of the columns is marked by a wall immediately after the final column. This wall section appears after 13th Avenue in the example, but your program should work for any number of columns.
  • The top of the column is marked by a wall, but Karel cannot assume that columns are always five units high, or even that all columns are the same height.
  • Some of the corners in the column may already contain beepers representing stones that are still in place. Your program should not put a second beeper on these corners.

Start by repairing the first column, climb down. As long as Karel has not reached the other side of the room, move to the next column, repair it, and climb down again:

	public void run() {
		repairColumn();
		climbDown();
		while(frontIsClear()) {
			moveToNextColumn();
			repairColumn();
			climbDown();
		}
	}

To repair a column turn left to face upwards and repair the fist position. As long as Karel has not reached the top of the column move upwards and repair the current position of the column:

	private void repairColumn() {
		turnLeft();
		repairCurrentColumnPosition();
		while (frontIsClear()) {
			move();
			repairCurrentColumnPosition();
		}
	}

Because not every position of a column has to be broken, it has to be fixed only if it is:

	private void repairCurrentColumnPosition() {
		if (noBeepersPresent()) {
			putBeeper();
		}
	}

To climb down, turn around to face down. Move till Karel has reach the bottom and turn left to face in the correct position to continue:

	private void climbDown() {
		turnAround();
		while (frontIsClear()) {
			move();
		}
		turnLeft();
	}

All columns are exactly 4 steps apart, thus Karel has to move 4 times:

	private void moveToNextColumn() {
		for (int i = 0; i < 4; i++) {
			move();
		}
	}

The code for this assignment is available on github.

FacebooktwitterredditpinterestlinkedintumblrmailFacebooktwitterredditpinterestlinkedintumblrmail

2 thoughts on “cs106a – Assignment #1 – Task #2”

  1. Why shouldn’t the Karel move up to fix one column and from there instead of climbing down move to the next column fix that downwards and then move to the column fixing it upwards and so on. Just like moving across consecutive columns in opposite directions rather than traversing the same colum two times.

    public void run() {
    repairColumn();
    moveToNextColumn();
    while(frontIsClear()) {
    repairColumn();
    moveToNextColumn();
    }
    }

    private void repairColumn() {
    if(onBottom)
    turnLeft();
    else
    turnRight();
    repairCurrentColumnPosition();
    while (frontIsClear()) {
    move();
    repairCurrentColumnPosition();
    }
    }

Leave a Reply

Your email address will not be published.