Created
March 10, 2015 13:45
-
-
Save kenechiokolo/140041c53ee771fbd4f5 to your computer and use it in GitHub Desktop.
Section Assignment 1: Defend Democracy Karel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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