Advertisement
Guest User

Debugging

a guest
Nov 9th, 2012
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.50 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.                         move () ;
  29.                         if (frontIsClear () && noBeepersPresent()) {
  30.                                 putBeeper () ;
  31.                                 move () ;
  32.                         }
  33.                         if (frontIsBlocked()) {
  34.                                 moveUpAndTurnAround () ;
  35.                         }
  36.                 }
  37.         }
  38.                
  39.         private void makeRowWest () {
  40.                 while (facingWest ()) {
  41.                                 move () ;
  42.                         if (frontIsClear () && noBeepersPresent()) {
  43.                                 putBeeper () ;
  44.                                 move () ;
  45.                         }
  46.                         if (frontIsBlocked()) {
  47.                                 moveUpAndTurnAround () ;
  48.                         }
  49.                 }
  50.         }
  51.        
  52.         private void moveUpAndTurnAround () {
  53.                 if (frontIsBlocked()) {
  54.                         if (facingEast ()) {
  55.                                 turnLeft () ;
  56.                                 move () ;
  57.                                 turnLeft () ;
  58.                                 makeRowWest () ;
  59.                         }
  60.                         if (facingWest ()) {
  61.                                 turnRight () ;
  62.                                 move () ;
  63.                                 turnRight () ;
  64.                                 makeRowEast () ;
  65.                         }
  66.                 }
  67.         }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement