Skip to content

Instantly share code, notes, and snippets.

@kenechiokolo
Created March 10, 2015 13:45
Show Gist options
  • Save kenechiokolo/140041c53ee771fbd4f5 to your computer and use it in GitHub Desktop.
Save kenechiokolo/140041c53ee771fbd4f5 to your computer and use it in GitHub Desktop.
Section Assignment 1: Defend Democracy Karel
import stanford.karel.SuperKarel;
public class defendDemocracyKarel extends SuperKarel {
/* This program is designed to clean the ballots in Karel's world. If there is no
* beeper in the centre, this indicates voter intent
* and Karel clears that whole column.
*/
public void run() {
while (frontIsClear()) {
move();
cleanBallot();
move();
}
}
private void cleanBallot() {
/*This method cleans the ballot if the centre corner is empty. It does nothing otherwise.
* Precondition - Karel starts on centre corner facing East
* Postcondition - Karel ends on centre corner facing East
*/
if (noBeepersPresent()) {
turnLeft();
move();
while (beepersPresent()) {
pickBeeper();
}
turnAround();
move();
move();
while (beepersPresent()) {
pickBeeper();
}
turnAround();
move();
turnRight();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment