anhnguyen8153

CheckerboardKarel

Jan 5th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.13 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.     // You fill in this part
  15.     public void run() {
  16.         // for vertical lines
  17.         if (frontIsBlocked()) {
  18.             putBeeper();
  19.             turnLeft();
  20.             while (frontIsClear()) {
  21.                 patternNorth();
  22.             }
  23.         }
  24.        
  25.         // for squares
  26.         while (frontIsClear()) {
  27.             patternEast();
  28.             checkCeilingFromEast(); // checks ceiling from east AND moves to next row
  29.             patternWest();
  30.             checkCeilingFromWest(); // checks ceiling from west AND moves to next row
  31.         }
  32.        
  33.         // removes beeper when ending on east side since patterWest()
  34.         // adds a beeper there
  35.         if (rightIsBlocked()) {
  36.             pickBeeper();
  37.         }
  38.        
  39.     }
  40.    
  41.     private void patternEast() {
  42.         putBeeper();
  43.        
  44.         while (facingEast()) {
  45.             skipCornerAndPutBeeper();
  46.             checkEastWall();
  47.         }
  48.     }
  49.    
  50.     private void skipCornerAndPutBeeper() {
  51.         if (frontIsClear()) {
  52.             move();
  53.             if (frontIsClear()) {
  54.                 move();
  55.                 putBeeper();
  56.             }
  57.         }
  58.     }
  59.    
  60.     private void checkEastWall() {
  61.         if (frontIsBlocked()) {
  62.             turnLeft();
  63.         }
  64.     }
  65.    
  66.     private void checkCeilingFromEast() {
  67.         if (frontIsClear()) {
  68.             toWestRow();
  69.         }
  70.     }
  71.    
  72.    
  73.     private void toWestRow() {
  74.         if (beepersPresent()) {
  75.             move();
  76.             turnLeft();
  77.             move();
  78.         } else {
  79.             move();
  80.             turnLeft();
  81.         }
  82.     }
  83.    
  84.     private void patternWest() {
  85.         putBeeper();
  86.        
  87.         while (facingWest()) {
  88.             skipCornerAndPutBeeper();
  89.             checkWestWall();
  90.         }
  91.     }
  92.    
  93.     private void checkWestWall() {
  94.         if (frontIsBlocked()) {
  95.             turnRight();
  96.         }
  97.     }
  98.    
  99.     private void checkCeilingFromWest() {
  100.         if (frontIsClear()) {
  101.             toEastRow();
  102.         }
  103.     }
  104.    
  105.     private void toEastRow() {
  106.         if (beepersPresent()) {
  107.             move();
  108.             turnRight();
  109.             move();
  110.         } else {
  111.             move();
  112.             turnRight();
  113.         }
  114.     }
  115.    
  116.     private void patternNorth() {
  117.         skipCornerAndPutBeeper();
  118.     }
  119.    
  120. }
Advertisement
Add Comment
Please, Sign In to add comment