Advertisement
krystation

KarelDefendsDemocracy

Jul 14th, 2012
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.09 KB | None | 0 0
  1. import stanford.karel.*;
  2. /* karelDefendsDemocracy:
  3.  * karel will go through the punch card and remove any
  4.  * chad.
  5.  */
  6. public class karelDefendsDemocracy extends SuperKarel {
  7.     public void run(){
  8.         move();
  9.         checkForChad();
  10.         workThroughBallot();
  11.     }
  12. /*
  13.  * checkForChad:
  14.  * karel will check to see if there are any chad
  15.  * in the ballot space she is in.
  16.  *
  17.  * pre-conditions: karel must be facing east and standing
  18.  * in the middle slot of the ballot space.
  19.  *
  20.  * post-conditions: karel will be in the the middle slot
  21.  * of the same ballot space and be facing east 
  22.  */
  23.     private void checkForChad(){
  24.         if (noBeepersPresent()){
  25.             checkRight();
  26.             checkLeft();
  27.         }
  28.     }
  29. /*
  30.  * checkRight:
  31.  * checks slot on karels right for chad.
  32.  *
  33.  * pre-conditions: karel must be in the center slot of
  34.  * ballot space and be facing east, and center slot
  35.  * must be clear.
  36.  *
  37.  * post-conditions: same as pre-conditions 
  38.  */
  39.     private void checkRight(){
  40.         turnRight();
  41.         move();
  42.         if (beepersPresent()){
  43.             while (beepersPresent()){
  44.                 pickBeeper();
  45.             }
  46.             turnAround();
  47.             move();
  48.             turnRight();
  49.         } else {
  50.             turnAround();
  51.             move();
  52.             turnRight();
  53.         }
  54.     }
  55. /*
  56.  * checkLeft:
  57.  * checks the slot on karels left for chad.
  58.  *
  59.  * pre-conditions: karel must be in the center slot of
  60.  * ballot space and be facing east, and center slot
  61.  * must be clear.
  62.  *
  63.  * post-conditions: same as pre-conditions
  64.  */
  65.     private void checkLeft(){
  66.         turnLeft();
  67.         move();
  68.         if (beepersPresent()){
  69.             while (beepersPresent()){
  70.                 pickBeeper();
  71.             }
  72.             turnAround();
  73.             move();
  74.             turnLeft();
  75.         } else {
  76.             turnAround();
  77.             move();
  78.             turnLeft();
  79.         }
  80.     }
  81. /*
  82.  * workThroughBallot:
  83.  * karel will make her way through the rest of the ballot
  84.  * check for chad as she goes along.
  85.  *
  86.  * pre-conditions: karel must be facing east and font must
  87.  * be clear.
  88.  *
  89.  * post-conditions: karel will be facing east and front
  90.  * will be blocked 
  91.  */
  92.     private void workThroughBallot(){
  93.         while (frontIsClear()){
  94.             move();
  95.             if (frontIsClear()){
  96.                 move();
  97.             }
  98.             checkForChad();
  99.         }
  100.     }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement