Advertisement
Guest User

CheckerboardKarel

a guest
Nov 10th, 2012
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.56 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.                                 checkForBehindBeeperWest () ;
  61.                         }
  62.                         if (facingWest ()) {
  63.                                 turnAround () ;
  64.                                 if (frontIsBlocked()) {
  65.                                         turnLeft () ;
  66.                                         makeRowNorth () ;
  67.                                 }
  68.                                 checkForBehindBeeperEast () ;
  69.                                 }
  70.                         }
  71.                 }
  72.         private void makeRowNorth () {
  73.                 while (frontIsClear()) {
  74.                         move () ;
  75.                         putBeeper () ;
  76.                         move () ;
  77.                 }
  78.         }
  79.        
  80.         private void checkForBehindBeeperWest () {
  81.                 move () ;
  82.                 if (beepersPresent()) {
  83.                         turnAround () ;
  84.                         move () ;
  85.                         turnLeft () ;
  86.                         move () ;
  87.                         turnLeft () ;
  88.                         makeRowWest () ;
  89.                 } else {
  90.                         turnAround () ;
  91.                         move () ;
  92.                         putBeeper () ;
  93.                         turnLeft () ;
  94.                         move () ;
  95.                         turnLeft () ;
  96.                         move () ;
  97.                         makeRowWest () ;
  98.                 }
  99.         }
  100.        
  101.         private void checkForBehindBeeperEast () {
  102.                 move () ;
  103.                 if (beepersPresent()) {
  104.                         turnAround () ;
  105.                         move () ;
  106.                         turnRight () ;
  107.                         move () ;
  108.                         turnRight () ;
  109.                         makeRowEast () ;
  110.                 } else {
  111.                         turnAround () ;
  112.                         move () ;
  113.                         putBeeper () ;
  114.                         turnRight () ;
  115.                         move () ;
  116.                         turnRight () ;
  117.                         move  () ;
  118.                         makeRowEast () ;
  119.                 }
  120.         }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement