This Problem is deceptively difficult to solve. It kind of makes you think you are in for a cakewalk and then BAMB you are slapped in the face with a twist. Here is how I solved it. Keep in mind that we had to solve for different size and styles of maps.
/* * 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.*;
/* * Name: Ken Swain * Section Leader: */
public class CheckerboardKarel extends SuperKarel {
public void run() { /* * The goal of this program is to have Karel build a perfect checker board pattern no matter the map size. * * Pre Condition: Karel is on 1-1 facing east * * Post Condition: Karel has built a perfect checker board. */ checkSingleColumn(); while (frontIsClear()) { buildRow(); } } private void buildRow() { /* * Just build the pattern. * * Pre Condition: Karel must be facing the correct direction on the line he wants to build the row on. * * Post Condition: Karel will have built a row in perfect checker board pattern. * */ while (frontIsClear()) { putBeeper(); move(); if (frontIsClear()) { move(); } } choosePattern(); } private void turn() { /* * Turn the correct direction to turn. * * Pre Condition: Karel hits a wall * * Post condition: Karel is facing the correct direction to move to the next row. * */ if (facingEast()) { turnLeft(); } else { turnRight(); } } private void chooseDirection() { /* * Get oriented on the correct row. * * Pre condition: Karel has moved to the correct row. * * Post Condition: Karel will be facing the correct direction to start building the row. * */ if (rightIsBlocked()) { turnLeft(); } else { turnRight(); } } private void checkSingleColumn() { /* * If it is a single column just create the pattern. * * Pre Condition: Karel is on a single column map * * post condition: Karel will place one row and exit. * */ if (frontIsBlocked()) { turnLeft(); buildRow(); } } private void choosePattern() { /* * Chose the correct pattern based on the presents of beeper on the last corner. * I feel like I could have made this a little more efficient with less checks, * but this actually works beyond the bounds of the problem. * * Pre Conditoon: Karel reaches the end of a row. * * Post Condition: Kerel will chose the correct type of row to build. */ turnAround(); if (frontIsClear()) { move(); } if (beepersPresent()) { patternOne(); } else { patternTwo(); } } private void patternOne() { /* * Build a pattern that will complete the checker board if no beeper is on the last column * * Pre condition: No beeper is on the last column. * * Post condition: Karel will move to the line and then build the correct pattern. * */ turnAround(); if (frontIsClear()) { move(); } turn(); if (frontIsClear()) { move(); chooseDirection(); } } private void patternTwo() { /* * Build checker board pattern based on the last column having a beeper present. * * Pre condition: Beeper present on the last row. * * Post condition: Karel will move to the new row face the correct direction and move a space. */ turnAround(); if (frontIsClear()) { move(); if (noBeepersPresent()) { putBeeper(); turn(); if (frontIsClear()) { move(); chooseDirection(); if (frontIsClear()) { move(); } } } } }}
