Advertisement
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 {
- public void run () {
- makeCheckerBoard () ;
- }
- private void makeCheckerBoard () {
- if (frontIsClear()) {
- makeRowEast () ;
- makeRowWest () ;
- } else {
- moveUpAndTurnAround () ;
- }
- }
- private void makeRowEast () {
- while (facingEast()) {
- putBeeper () ;
- move () ;
- if (frontIsClear () && noBeepersPresent()) {
- move () ;
- }
- if (frontIsBlocked()) {
- moveUpAndTurnAround () ;
- }
- }
- }
- private void makeRowWest () {
- while (facingWest ()) {
- putBeeper () ;
- move () ;
- if (frontIsClear () && noBeepersPresent()) {
- move () ;
- }
- if (frontIsBlocked()) {
- moveUpAndTurnAround () ;
- }
- }
- }
- private void moveUpAndTurnAround () {
- if (frontIsBlocked()) {
- if (facingEast ()) {
- turnAround () ;
- if (frontIsBlocked()) {
- turnRight () ;
- makeRowNorth () ;
- }
- checkForBehindBeeperWest () ;
- }
- if (facingWest ()) {
- turnAround () ;
- if (frontIsBlocked()) {
- turnLeft () ;
- makeRowNorth () ;
- }
- checkForBehindBeeperEast () ;
- }
- }
- }
- private void makeRowNorth () {
- while (frontIsClear()) {
- move () ;
- putBeeper () ;
- move () ;
- }
- }
- private void checkForBehindBeeperWest () {
- move () ;
- if (beepersPresent()) {
- turnAround () ;
- move () ;
- turnLeft () ;
- move () ;
- turnLeft () ;
- makeRowWest () ;
- } else {
- turnAround () ;
- move () ;
- putBeeper () ;
- turnLeft () ;
- move () ;
- turnLeft () ;
- move () ;
- makeRowWest () ;
- }
- }
- private void checkForBehindBeeperEast () {
- move () ;
- if (beepersPresent()) {
- turnAround () ;
- move () ;
- turnRight () ;
- move () ;
- turnRight () ;
- makeRowEast () ;
- } else {
- turnAround () ;
- move () ;
- putBeeper () ;
- turnRight () ;
- move () ;
- turnRight () ;
- move () ;
- makeRowEast () ;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement