Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import stanford.karel.*;
- /* karelDefendsDemocracy:
- * karel will go through the punch card and remove any
- * chad.
- */
- public class karelDefendsDemocracy extends SuperKarel {
- public void run(){
- move();
- checkForChad();
- workThroughBallot();
- }
- /*
- * checkForChad:
- * karel will check to see if there are any chad
- * in the ballot space she is in.
- *
- * pre-conditions: karel must be facing east and standing
- * in the middle slot of the ballot space.
- *
- * post-conditions: karel will be in the the middle slot
- * of the same ballot space and be facing east
- */
- private void checkForChad(){
- if (noBeepersPresent()){
- checkRight();
- checkLeft();
- }
- }
- /*
- * checkRight:
- * checks slot on karels right for chad.
- *
- * pre-conditions: karel must be in the center slot of
- * ballot space and be facing east, and center slot
- * must be clear.
- *
- * post-conditions: same as pre-conditions
- */
- private void checkRight(){
- turnRight();
- move();
- if (beepersPresent()){
- while (beepersPresent()){
- pickBeeper();
- }
- turnAround();
- move();
- turnRight();
- } else {
- turnAround();
- move();
- turnRight();
- }
- }
- /*
- * checkLeft:
- * checks the slot on karels left for chad.
- *
- * pre-conditions: karel must be in the center slot of
- * ballot space and be facing east, and center slot
- * must be clear.
- *
- * post-conditions: same as pre-conditions
- */
- private void checkLeft(){
- turnLeft();
- move();
- if (beepersPresent()){
- while (beepersPresent()){
- pickBeeper();
- }
- turnAround();
- move();
- turnLeft();
- } else {
- turnAround();
- move();
- turnLeft();
- }
- }
- /*
- * workThroughBallot:
- * karel will make her way through the rest of the ballot
- * check for chad as she goes along.
- *
- * pre-conditions: karel must be facing east and font must
- * be clear.
- *
- * post-conditions: karel will be facing east and front
- * will be blocked
- */
- private void workThroughBallot(){
- while (frontIsClear()){
- move();
- if (frontIsClear()){
- move();
- }
- checkForChad();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement