CheckerboardKarel Solution

Wow, this one is tough. I gotta be honest, this took me days to figure out. There was a lot of trial and error, and I started from scratch a couple times when I started getting lost in the code. Here is the problem:

Here is my solution:


So I guess, if you think about it, there are only four steps Karel the Robot needs to take:

  • Drop alternating beepers while facing East
  • While facing a wall on the East side, move up and turn West
  • Drop alternating beepers while facing West
  • While facing a wall on the West side, move up and turn East
That in itself is not difficult to do. It actually took me about 10 minutes to make it work with the Checkerboard Karel template. The challenge of this problem comes from making sure Karel can replicate the pattern in all the other templates provided (1×8, 8×1, 7×7, etc.).
My initial difficulty with this problem was that I was testing it in only the 8×8 environment. Once I started testing in both the 8×8 and 7×7 environment and making sure that my expectations worked in both at the same time, the problem became a lot more simple. And once those worked, the only one that didn’t work was the 8×1, which wasn’t too difficult to add at the very end.
This is my solution to this problem. You can also find it here.

<pre>/*
 * File: CheckerboardKarel.java
 * ----------------------------
 * When you finish writing it, the CheckerboardKarel class should draw
 * a checkerboard using beepers, as described in Assignment 1.  You
 * should make sure that your program works for all of the sample
 * worlds supplied in the starter folder.
 */

import stanford.karel.*;

public class CheckerboardKarel extends SuperKarel {
	public void run() {
		putBeeper();
		checkWall();
		while (frontIsClear()) {
			beepersEast();
			beepersWest();
		}
	}
	private void beepersEast() {
		while (facingEast()) {
			move();
			if (frontIsClear()) {
				move();
				putBeeper();
			}
			upEast();
		}
	}
	private void upEast() {
		if (frontIsBlocked()) {
			if (noBeepersPresent()) {
				turnLeft();
				if (frontIsClear()) {
					move();
					turnLeft();
					putBeeper();
				}
			}
			else {
				turnLeft();
				if (frontIsClear()) {
					move();
					turnLeft();
					move();
					putBeeper();
				}
			}
		}
	}
	private void beepersWest() {
		while (facingWest()) {
			move();
			if (frontIsClear()) {
				move();
				putBeeper();
			}
			upWest();
		}
	}
	private void upWest() {
		if (frontIsBlocked()) {
			if (noBeepersPresent()) {
				turnRight();
				if (frontIsClear()) {
					move();
					turnRight();
					putBeeper();
				}
			}
			else {
				turnRight();
				if (frontIsClear()) {
					move();
					turnRight();
					move();
					putBeeper();
				}
			}
		}
	}
	private void checkWall() {
		if (frontIsBlocked()) {
			turnLeft();
			while (frontIsClear()) {
				move();
				if (frontIsClear()) {
					move();
					putBeeper();
				}
			}
		}
	}
}</pre>

Enjoy the article? Join over 20,000+ Swift developers and enthusiasts who get my weekly updates.