Your goal is to fill the first row of Karel's world with beepers. You can assume that Karel starts in the bottom left corner, facing east. For example, if you ran your program on the world on the left, it should produce the world on the right.

Your program should also work for worlds of different sizes. For example if we run the EXACT same program on this slightly larger world, Karel should still be able to place a full line of beepers without crashing. This means we can't use a for loop. We don't know before hand how big the world is going to be!

Solution

/**
 * Program: BeeperLine
 * -------------------
 * Fill the first row with beepers. Assumes that Karel starts out in
 * the bottom left corner of the screen, facing east.
 */
public class BeeperLine extends SuperKarel {
	
	public void run() {
		while(frontIsClear()) {
			putBeeper();
			move();
		}
		// This line is necessary to place the final beeper. The number
		// of times Karel moves is one less than the number of times karel
		// places a beeper (if the world is five squares wide, we place
		// 5 beepers but only move 4 times
		putBeeper();
	}
}