Advertisement
Guest User

CheckerboardKarel

a guest
Nov 10th, 2012
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.46 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.         public void run () {
  14.                 makeCheckerBoard () ;
  15.         }
  16.        
  17.         private void makeCheckerBoard () {
  18.                 if (frontIsClear()) {
  19.                         makeRowEast () ;
  20.                         makeRowWest () ;
  21.                 } else {   
  22.                         moveUpAndTurnAround () ;
  23.                 }
  24.         }
  25.        
  26.         private void makeRowEast () {
  27.                 while (facingEast()) {
  28.                         putBeeper () ;
  29.                         move () ;
  30.                         if (frontIsClear () && noBeepersPresent()) {
  31.                                 move () ;
  32.                         }
  33.                         if (frontIsBlocked()) {
  34.                                 moveUpAndTurnAround () ;
  35.                         }
  36.                 }
  37.         }
  38.                
  39.         private void makeRowWest () {
  40.                 while (facingWest ()) {
  41.                         putBeeper () ;
  42.                         move () ;
  43.                         if (frontIsClear () && noBeepersPresent()) {
  44.                                 move () ;
  45.                         }
  46.                         if (frontIsBlocked()) {
  47.                                 moveUpAndTurnAround () ;
  48.                         }
  49.                 }
  50.         }
  51.        
  52.         private void moveUpAndTurnAround () {
  53.                 if (frontIsBlocked()) {
  54.                         if (facingEast ()) {
  55.                                 turnAround () ;
  56.                                 if (frontIsBlocked()) {
  57.                                         turnRight () ;
  58.                                         makeRowNorth () ;
  59.                                 }
  60.                                 move () ;
  61.                                 if (beepersPresent()) {
  62.                                         turnAround () ;
  63.                                         move () ;
  64.                                         turnLeft () ;
  65.                                         move () ;
  66.                                         turnLeft () ;
  67.                                         makeRowWest () ;
  68.                                 } else {
  69.                                      turnAround () ;
  70.                                      move () ;
  71.                                      putBeeper () ;
  72.                                      turnLeft () ;
  73.                                      move () ;
  74.                                      turnLeft () ;
  75.                                      makeRowWest () ;
  76.                                 }
  77.                         }
  78.                         if (facingWest ()) {
  79.                             turnAround () ;
  80.                             if (frontIsBlocked()) {
  81.                                     turnLeft () ;
  82.                                     makeRowNorth () ;
  83.                             }
  84.                             move () ;
  85.                             if (beepersPresent()) {
  86.                                     turnAround () ;
  87.                                     move () ;
  88.                                     turnRight () ;
  89.                                     move () ;
  90.                                     turnRight () ;
  91.                                     makeRowEast () ;
  92.                                     }
  93.                             } else {
  94.                                  turnAround () ;
  95.                                  move () ;
  96.                                  putBeeper () ;
  97.                                  turnRight () ;
  98.                                  move () ;
  99.                                  turnRight () ;
  100.                                  makeRowEast () ;
  101.                                  }
  102.                         }
  103.                 }
  104.         private void makeRowNorth () {
  105.                 if (frontIsClear()) {
  106.                         move () ;
  107.                         putBeeper () ;
  108.                         move () ;
  109.                 }
  110.         }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement