Skip to content

Instantly share code, notes, and snippets.

@NatashaTheRobot
Created October 28, 2011 03:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save NatashaTheRobot/1321586 to your computer and use it in GitHub Desktop.
Save NatashaTheRobot/1321586 to your computer and use it in GitHub Desktop.
This is the solution to the CheckerboardKarel problem in the online Stanford CS 106A class
/*
* 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();
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment