# Problem 3

The problem 3 is “In this exercise, your job is to get Karel to create a checkerboard pattern of beepers inside an empty rectangular world, as illustrated in the following before-and-after diagram:”

The way that I viewed the problem is that the robot (Karel) had to move along the either in Up / Down or Left / Right and place a beeper every other step, so in basics if he moves up, he will move up 1 place see if he is able to move any more, if not turn right and if the front is not blocked move forward and right again to go back down the aisle next to the previous one we have just been up and whilst moving up and down the aisle to place a beeper every other step ( so need to keep check of the steps) or you could do the same process but going left and right instead, e.g. move right first to the end of that aisle and then go up if able to and then turn around and move left along the aisle above the previous aisle.

Here is my implementation of both ideas, again the source code for the assignment 1 is attached above in the zip file and also the PDF with the full assignment details.

```/* * 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 {   private boolean putBeeper = true;   private void placeBeeperAndMove() { while (true) { if (putBeeper) { putBeeper(); putBeeper = false; } else putBeeper = true;   if (frontIsBlocked()) break; move(); } }   private void goUpAndDown() { boolean turnLeft = true;   while (true) { if (turnLeft) turnLeft(); else turnRight(); placeBeeperAndMove(); turnLeft = (turnLeft == true ? false : true); if (turnLeft) turnLeft(); else turnRight(); if (frontIsBlocked()) break; move(); } }   public void goLeftAndRight() { boolean turnLeft = true;   while (true) { placeBeeperAndMove(); if (turnLeft) turnLeft(); else turnRight(); if (frontIsBlocked()) break; move(); if (turnLeft) turnLeft(); else turnRight(); turnLeft = (turnLeft == true ? false : true); } }   // You fill in this part public void run() { // go up and down //goUpAndDown(); // go left and right goLeftAndRight(); } }```

## 54 thoughts on “Problem 3”

1. Mark says:
```

Kris Bulman says:

April 8, 2012 at 12:36 am

sorry, delete the last post. Here is my solution to problem #3

```/*
* 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() {
if (frontIsBlocked()) {  // If 1x8, turn left and checker the row
turnLeft();
checkerRow();
}
while (frontIsClear()) {
checkerRow();
changeLanes();
}
}

/*
* This method adds spaced beepers to a single line.
* This also takes care of adding a beeper at the end
* of the line if karl is in an uneven grid by adding the
* beeper, changing lanes and completing the following row.
*/
private void checkerRow() {
while (frontIsClear()) {
putBeeper();
move();
if (frontIsClear()) {
move();
if (frontIsBlocked()) {
putBeeper();
changeLanes();
if (frontIsClear()) {
move();
}
}
}
}
}

/*
* Move to the next line and face west if facing east, and vice versa.
*/
private void changeLanes() {
if (frontIsBlocked()){
if (leftIsClear()) {
if (facingEast()) {
turnLeft();
move();
turnLeft();
}
}
}
if (frontIsBlocked()){
if (facingWest()) {
if (rightIsClear()) {
turnRight();
move();
turnRight();
}
}
}
}

}```

```
2. Ujjawal says:

Here is my solution

3. Christian says:

This is my solution for solving this problem. This works for all worlds (including 1xn, nx1 and 1×1 worlds).

```import stanford.karel.*;   public class CheckerboardKarel extends SuperKarel {   public void run() {   /* * There are 2 conditions that end the loop. * Karel is in Top Left corner facing West or Karel is in the Top Right Corner facing East. * So when the front is blocked, AND either the left (east facing) OR right (west facing) is blocked * Karel is on the top row facing either corner. * The program should continue to loop that that condition is NOT met. * * Note: Would be simpler if there was a method called northIsBlocked(). */ while (!(frontIsBlocked() &amp;&amp; // At the end of the row AND one of the following is true ((facingWest() &amp;&amp; rightIsBlocked()) || // In the Left corner (facingEast() &amp;&amp; leftIsBlocked())))) { // Top right corner moveToNextAndPlaceChecker(); }; }   /* * Moves Karel to the next square and places a Beeper in it if the starting square was empty. */ private void moveToNextAndPlaceChecker() { if(beepersPresent()) moveToNextSquare(); else { moveToNextSquare(); putBeeper(); } }   /* * Moves Karel forward OR to the next row if the front is blocked. * Precondition: Karel is facing East or West * Postcondition: Karel is facing East or West */ private void moveToNextSquare() { if (frontIsClear()) move(); else if (facingWest()) { // reached end of line facing west, move to next row and face east turnRight(); move(); turnRight(); } else if (facingEast()) { // reached end of line facing east, move to next row and face west turnLeft(); move(); turnLeft(); } } }```