Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * File: CheckerboardKarel.java
- * ----------------------------
- * When you finish writing it, the CheckerboardKarel class should draw
- * a checkerboard using beepers, as described in Assignment 1. You
- * should make sure that your program works for all of the sample
- * worlds supplied in the starter folder.
- */
- import stanford.karel.*;
- public class CheckerboardKarel extends SuperKarel {
- // You fill in this part
- public void run() {
- // for vertical lines
- if (frontIsBlocked()) {
- putBeeper();
- turnLeft();
- while (frontIsClear()) {
- patternNorth();
- }
- }
- // for squares
- while (frontIsClear()) {
- patternEast();
- checkCeilingFromEast(); // checks ceiling from east AND moves to next row
- patternWest();
- checkCeilingFromWest(); // checks ceiling from west AND moves to next row
- }
- // removes beeper when ending on east side since patterWest()
- // adds a beeper there
- if (rightIsBlocked()) {
- pickBeeper();
- }
- }
- private void patternEast() {
- putBeeper();
- while (facingEast()) {
- skipCornerAndPutBeeper();
- checkEastWall();
- }
- }
- private void skipCornerAndPutBeeper() {
- if (frontIsClear()) {
- move();
- if (frontIsClear()) {
- move();
- putBeeper();
- }
- }
- }
- private void checkEastWall() {
- if (frontIsBlocked()) {
- turnLeft();
- }
- }
- private void checkCeilingFromEast() {
- if (frontIsClear()) {
- toWestRow();
- }
- }
- private void toWestRow() {
- if (beepersPresent()) {
- move();
- turnLeft();
- move();
- } else {
- move();
- turnLeft();
- }
- }
- private void patternWest() {
- putBeeper();
- while (facingWest()) {
- skipCornerAndPutBeeper();
- checkWestWall();
- }
- }
- private void checkWestWall() {
- if (frontIsBlocked()) {
- turnRight();
- }
- }
- private void checkCeilingFromWest() {
- if (frontIsClear()) {
- toEastRow();
- }
- }
- private void toEastRow() {
- if (beepersPresent()) {
- move();
- turnRight();
- move();
- } else {
- move();
- turnRight();
- }
- }
- private void patternNorth() {
- skipCornerAndPutBeeper();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment