Advertisement
krystation

CheckerboardKarel

Jul 14th, 2012
323
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.85 KB | None | 0 0
  1. /*
  2.  * File: CheckerboardKarel.java
  3.  * ----------------------------
  4.  * When you finish writing it, the CheckerboardKarel class should draw
  5.  * a checkerboard using beepers, as described in Assignment 1.  You
  6.  * should make sure that your program works for all of the sample
  7.  * worlds supplied in the starter folder.
  8.  */
  9.  
  10. import stanford.karel.*;
  11.  
  12. public class CheckerboardKarel extends SuperKarel {
  13.  
  14.     public void run() {
  15.         while (facingEast()){
  16.             fillRow();
  17.             moveBack();
  18.             moveToNextLine();
  19.         }
  20.     }
  21.     private void fillRow(){
  22.         determinePattern();
  23.     }
  24.     private void determinePattern(){
  25.         if (facingEast()){
  26.             turnRight();
  27.             if (frontIsClear()){
  28.                 move();
  29.                 if (beepersPresent()){
  30.                     turnAround();
  31.                     move();
  32.                     turnRight();
  33.                     putPattern2();
  34.                 } else {
  35.                     turnAround();
  36.                     move();
  37.                     turnRight();
  38.                     putPattern1();
  39.                 }
  40.             } else {
  41.                 turnLeft();
  42.                 putPattern1();
  43.             }
  44.         }
  45.     }
  46.     private void putPattern1(){
  47.         putBeeper();
  48.         while (frontIsClear()){
  49.             move();
  50.             if (frontIsClear()){
  51.                 move();
  52.                 putBeeper();
  53.             }
  54.         }
  55.     }  
  56.     private void putPattern2(){
  57.         if (frontIsClear()){
  58.             move();
  59.             while (frontIsClear()){
  60.                 putBeeper();
  61.                 move();
  62.                 if (frontIsClear()){
  63.                     move();
  64.                 }  
  65.             }
  66.             checkIfBeeperNeeded();
  67.         } else {
  68.             moveToNextLine();
  69.             determinePattern();
  70.         }
  71.     }  
  72.     private void moveBack(){
  73.         turnAround();
  74.         while (frontIsClear()){
  75.             move();
  76.         }
  77.         turnAround();
  78.     }
  79.     private void moveToNextLine(){
  80.         turnLeft();
  81.         if (frontIsClear()){
  82.             move();
  83.             turnRight();
  84.         } else {
  85.             turnLeft();
  86.         }
  87.     }
  88.     private void checkIfBeeperNeeded(){
  89.         if (frontIsBlocked()){
  90.             turnRight();
  91.             move();
  92.             if (beepersPresent()) {
  93.                 turnAround();
  94.                 move();
  95.                 turnRight();
  96.             } else {
  97.                 turnAround();
  98.                 move();
  99.                 putBeeper();
  100.                 turnRight();
  101.             }
  102.         }
  103.     }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement