Karel has been hired by the UN to come and help build houses. The day needs saving and Karel is clearly the best robot for the job. Your goal is to have Karel walk along the bottom row and build a house like the following:

Every time Karel finds a beeper. The below images show what Karel should do for two example worlds. You can assume that no houses will overlap and that all houses will fit inside the world. You can't assume the size of the world or the number of houses!

Solution

/**
* Program: UN Karel
* -----------------
* This program makes Karel builds houses, as work for the UN.
*/
public class UNKarel extends SuperKarel {

public void run() {
while(frontIsClear()) {
move();
if(beepersPresent()) {
buildHouse();
}
}
}

/**
* Method: Build House
* -------------------
* Constructs a single house centered around Karel's current position.
* You can assume that there is a beeper present at the start state.
* At the end of the method, Karel will be facing east on the bottom-
* right corner of the house.
*/
private void buildHouse() {
pickBeeper();
turnAround();
move();
turnRight();
placeThree();
turnRight();
move();
turnRight();
placeThree();
turnLeft();
move();
turnLeft();
placeThree();
turnAround();
moveToWall();
turnLeft();
}

/**
* Method: Move To Wall
* --------------------
* Move forward until Karel hits a wall.
*/
private void moveToWall() {
while(frontIsClear()) {
move();
}
}

/**
* Method: Place Three
* -------------------
* Place's three beepers in a row and moves three times!
*/
private void placeThree() {
for(int i = 0; i < 3; i++) {
putBeeper();
move();
}
}

}