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() {
- while (facingEast()){
- fillRow();
- moveBack();
- moveToNextLine();
- }
- }
- private void fillRow(){
- determinePattern();
- }
- private void determinePattern(){
- if (facingEast()){
- turnRight();
- if (frontIsClear()){
- move();
- if (beepersPresent()){
- turnAround();
- move();
- turnRight();
- putPattern2();
- } else {
- turnAround();
- move();
- turnRight();
- putPattern1();
- }
- } else {
- turnLeft();
- putPattern1();
- }
- }
- }
- private void putPattern1(){
- putBeeper();
- while (frontIsClear()){
- move();
- if (frontIsClear()){
- move();
- putBeeper();
- }
- }
- }
- private void putPattern2(){
- if (frontIsClear()){
- move();
- while (frontIsClear()){
- putBeeper();
- move();
- if (frontIsClear()){
- move();
- }
- }
- checkIfBeeperNeeded();
- } else {
- moveToNextLine();
- determinePattern();
- }
- }
- private void moveBack(){
- turnAround();
- while (frontIsClear()){
- move();
- }
- turnAround();
- }
- private void moveToNextLine(){
- turnLeft();
- if (frontIsClear()){
- move();
- turnRight();
- } else {
- turnLeft();
- }
- }
- private void checkIfBeeperNeeded(){
- if (frontIsBlocked()){
- turnRight();
- move();
- if (beepersPresent()) {
- turnAround();
- move();
- turnRight();
- } else {
- turnAround();
- move();
- putBeeper();
- turnRight();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement