# CheckerboardKarel Solution

Wow, this one is tough. I gotta be honest, this took me days to figure out. There was a lot of trial and error, and I started from scratch a couple times when I started getting lost in the code. Here is the problem:

Here is my solution:

So I guess, if you think about it, there are only four steps Karel the Robot needs to take:

• Drop alternating beepers while facing East
• While facing a wall on the East side, move up and turn West
• Drop alternating beepers while facing West
• While facing a wall on the West side, move up and turn East
That in itself is not difficult to do. It actually took me about 10 minutes to make it work with the Checkerboard Karel template. The challenge of this problem comes from making sure Karel can replicate the pattern in all the other templates provided (1×8, 8×1, 7×7, etc.).
My initial difficulty with this problem was that I was testing it in only the 8×8 environment. Once I started testing in both the 8×8 and 7×7 environment and making sure that my expectations worked in both at the same time, the problem became a lot more simple. And once those worked, the only one that didn’t work was the 8×1, which wasn’t too difficult to add at the very end.
This is my solution to this problem. You can also find it here.
```
&lt;pre&gt;/*
* 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() {
putBeeper();
checkWall();
while (frontIsClear()) {
beepersEast();
beepersWest();
}
}
private void beepersEast() {
while (facingEast()) {
move();
if (frontIsClear()) {
move();
putBeeper();
}
upEast();
}
}
private void upEast() {
if (frontIsBlocked()) {
if (noBeepersPresent()) {
turnLeft();
if (frontIsClear()) {
move();
turnLeft();
putBeeper();
}
}
else {
turnLeft();
if (frontIsClear()) {
move();
turnLeft();
move();
putBeeper();
}
}
}
}
private void beepersWest() {
while (facingWest()) {
move();
if (frontIsClear()) {
move();
putBeeper();
}
upWest();
}
}
private void upWest() {
if (frontIsBlocked()) {
if (noBeepersPresent()) {
turnRight();
if (frontIsClear()) {
move();
turnRight();
putBeeper();
}
}
else {
turnRight();
if (frontIsClear()) {
move();
turnRight();
move();
putBeeper();
}
}
}
}
private void checkWall() {
if (frontIsBlocked()) {
turnLeft();
while (frontIsClear()) {
move();
if (frontIsClear()) {
move();
putBeeper();
}
}
}
}
}&lt;/pre&gt;
```

Enjoy the article? Join over 20,000+ Swift developers and enthusiasts who get my weekly updates.

• Michael V

Checkerboard code will work on any sized world and will not loop or crash if run again at the end. Any feedback on shortening the code without compromising anything would be appreciated.

```/*
* 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 (frontIsClear()){
while (frontIsClear()) {
patternRow();
if (facingEast()){
if (leftIsClear()){
if (beepersPresent()){
moveToWestOddRow();
}else {
moveToWestEvenRow();
}
}
}else {
if (rightIsClear()){
if (beepersPresent()){
moveToEastOddRow();
}else {
moveToEastEvenRow();
}
}
}
}
}else {
if (leftIsClear()){
if (facingEast()){
turnLeft();
patternRow();
}
}
}
}

private void patternRow(){
putBeeper();
if (frontIsClear()){
move();
if (frontIsClear()){
move();
patternRow();
}
}
}

private void moveToWestEvenRow(){
turnLeft();
move();
turnLeft();
}

private void moveToWestOddRow(){
turnLeft();
move();
turnLeft();
move();
}

private void moveToEastEvenRow(){
turnRight();
move();
turnRight();
}

private void moveToEastOddRow(){
turnRight();
move();
turnRight();
move();
}

}
```
• Moriss

hello, here is mine. works in every situation

```<pre>
/*
* 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(){
// karel starts in lower left corner, facing east; fill the first line and face north
putBeeper();						// works even without this line, but it's better to start with a beeper
gotoWall();
turnLeft();

while(frontIsClear()){				// repeat until ceiling reached (loop precondition: face north)
moveAndPut();					// step up
turnLeft();						// face next line to fill
gotoWall();						// fill it
turnRight();					// face north (postcondition)
if (frontIsClear()){			// ceiling??
moveAndPut();				// step up
turnRight();				// face next line to fill
gotoWall();					// fill it
turnLeft();					// face north (postcondition)
}
}
}

private void gotoWall(){
while (frontIsClear()){
moveAndPut();
}
}

/*
* make a move and put beepers only if no beepers in previous position
*/
private void moveAndPut(){
if (noBeepersPresent()){
move();
putBeeper();
} else {
move();
}
}
}
</pre>
```
• Jon

different solution

```
import stanford.karel.*;

public class CheckerboardKarel extends SuperKarel {

public void run() {
avenueDrop();
while (leftIsClear()) {
westWallReposition();
avenueDrop();
if (rightIsClear()) {
eastWallReposition();
avenueDrop();
}else{
turnAround();
}}}

private void avenueDrop() {
putBeeper();
while (frontIsClear()) {
move();
if (frontIsClear()) {
move();
putBeeper();

}
}
}

private void westWallReposition() {
turnLeft();
move();
turnLeft();
}

private void eastWallReposition() {
turnRight();
move();
turnRight();
}

}

```
• Nice! Thanks.

• Ujjawal

Here is my solution…..

```import stanford.karel.*;
public class CheckerboardKarel extends SuperKarel {
public void run() {
if (frontIsBlocked()) {
turnLeft();
}
putOne();
}
private void putOne(){
putBeeper();
if(frontIsClear()){
move();
} else {
moveToNext();
}
if(frontIsClear()){
move();
putOne();
} else {
moveToNext();
if(frontIsClear()){
//if statement is to end recursion
putOne();
}
}
}

private void moveToNext(){
if(facingEast()){
turnLeft();
if(frontIsClear()){
move();
turnLeft();
}
} else if(facingWest()){
turnRight();
if(frontIsClear()){
move();
turnRight();
}
}
}
}
```
• Here is my solution

```

import stanford.karel.*;

public class CheckerboardKarel extends SuperKarel {

public void run(){
while (frontIsClear()){
goRightAndPutBeepers();
goUp();
goLeftAndPutBeepers();
goUpRight();
}
}

//puting Beepers on the right way
private void goRightAndPutBeepers(){

while(frontIsClear()){
putBeeper();
move();

if(frontIsClear()){
move();
}
}
}

//Going Up
private void goUp(){
turnLeft();
if (frontIsClear()){
move();
turnLeft();
}
}
//puting Beepers on the Left way
private void goLeftAndPutBeepers(){
while (frontIsClear()){
putBeeper();
move();
if(frontIsClear()){
move();
}
}
}
// Going Up from right
private void goUpRight(){
turnRight();
if(frontIsClear()){
move();
turnRight();
}
}
}
```
• Matt

As concise as I can do for now…

```
/*
* File: CheckerboardKarel.java
* ----------------------------
* Karel draws a checkerboard. Must work with any world dimensions.
*/

import stanford.karel.*;

public class CheckerboardKarel extends SuperKarel {

public void run() {
recursiveBeepers();
}

/*
* Pre-condition:  Karel must be facing east on the corner that should
* 				   receive the first beeper for the current street
*
* recursiveBeepers places the beepers in the correct pattern and then
* calls goHome
*/
private void recursiveBeepers() {
putBeeper();
if (frontIsClear()) {
move();
if (frontIsClear()) {
move();
recursiveBeepers();
} else {
goHome();
}
} else {
goHome();
}
}

/*
* Pre-condition:  Karel must be facing east on a completed street
* Post-condition: Karel must be returned to 1st Avenue on the current street
*
* Calls checkHomeCorner to set up for the next street
*/
private void goHome() {
turnAround();
while (frontIsClear()) {
move();
}
turnRight();
checkHomeCorner();
}

/*
* Pre-condition:  Karel must on 1st Avenue on current street, facing east
* Post-condition: Karel must be on the corner to receive first beeper in the next street
*
* Calls recursiveBeepers to run the next street
*/
private void checkHomeCorner() {
if (frontIsClear() &amp;&amp; beepersPresent()) {
move();
turnRight();
if (frontIsClear()) {
move();
recursiveBeepers();
} else {
turnLeft();
if (frontIsClear()) {
move();
turnRight();
recursiveBeepers();
}
}
} else if (frontIsClear() &amp;&amp; noBeepersPresent()) {
move();
turnRight();
recursiveBeepers();
}
}
}
```
• Krys

here’s how I did it: http://pastebin.com/qaajrEQs

• fools

Hi, My Code just keeps repeating itself once it reaches the top. I’m not quite positive that this is a “fencepost” error but I tried to rebuttal it by putting in the “whileEast/West is clear” I dont know why it wont work 🙁

```/*
* 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 {

//CheckerboardKarel class draws a checkerboard using beepers.
public void run() {
while (facingEast()) {
if (leftIsClear()) {
putBeepersRowEast();
}
if (rightIsClear()) {
putBeepersRowWest();
}
}
while (rightIsClear()) {
if (leftIsClear()) {
putBeepersRowEast();
}
if (rightIsClear()) {
putBeepersRowWest();
}
}
}

private void putBeepersRowEast() {
/* Karel places a row of Beepers along a street going east
* Pre-Condition: Karel must be facing East on the Westernmost wall
* Post-Condition: Karel must be facing west on the Easternmost wall one
* row above its original placesetting
*/
putBeeper();
while (frontIsClear()) {
move();
if (frontIsClear()) {
move();
putBeeper();
}
}
if (leftIsClear()) {
turnLeft();
move();
turnLeft();
}
}

private void putBeepersRowWest() {
/* Karel places a row of Beepers along a street going West
/* Pre-Condition: Karel must be facing West on the Westernmost wall
/* Post-Condition: Karel must be facing West on the Westernmost wall
*/
putBeeper();
while (frontIsClear()) {
move();
if (frontIsClear()) {
move();
putBeeper();
}
}
if (rightIsClear()) {
turnRight();
move();
turnRight();
}
}
}
```
• fools

Sorry about the spacing on it I can assure you, he syntax is all there I dont get any syntax errors when it runs.

• asdfasdf
```01
/*
02
* File: CheckerboardKarel.java
03
* ----------------------------
04
* When you finish writing it, the CheckerboardKarel class should draw
05
* a checkerboard using beepers, as described in Assignment 1.  You
06
* should make sure that your program works for all of the sample
07
* worlds supplied in the starter folder.
08
*/
09

10
import stanford.karel.*;
11

12
public class CheckerboardKarel extends SuperKarel {
13

14
//CheckerboardKarel class draws a checkerboard using beepers.
15
public void run() {
16
while (facingEast()) {
17
if (leftIsClear()) {
18
putBeepersRowEast();
19
}
20
if (rightIsClear()) {
21
putBeepersRowWest();
22
}
23
}
24
while (rightIsClear()) {
25
if (leftIsClear()) {
26
putBeepersRowEast();
27
}
28
if (rightIsClear()) {
29
putBeepersRowWest();
30
}
31
}
32
}
33

34
private void putBeepersRowEast() {
35
/* Karel places a row of Beepers along a street going east
36
* Pre-Condition: Karel must be facing East on the Westernmost wall
37
* Post-Condition: Karel must be facing west on the Easternmost wall one
38
* row above its original placesetting
39
*/
40
putBeeper();
41
while (frontIsClear()) {
42
move();
43
if (frontIsClear()) {
44
move();
45
putBeeper();
46
}
47
}
48
if (leftIsClear()) {
49
turnLeft();
50
move();
51
turnLeft();
52
}
53
}
54

55
private void putBeepersRowWest() {
56
/* Karel places a row of Beepers along a street going West
57
/* Pre-Condition: Karel must be facing West on the Westernmost wall
58
/* Post-Condition: Karel must be facing West on the Westernmost wall
59
*/
60
putBeeper();
61
while (frontIsClear()) {
62
move();
63
if (frontIsClear()) {
64
move();
65
putBeeper();
66
}
67
}
68
if (rightIsClear()) {
69
turnRight();
70
move();
71
turnRight();
72
}
73
}
74
}
```
• Roel Stalman
• Sergiu

import stanford.karel.*;

public class CheckerboardKarel extends SuperKarel {

public void run(){

if(noBeepersPresent())

putBeeper();

while(frontIsClear()){

putting();

left();

putting();

right();

}

}

public void putting(){

while(frontIsClear()){

move();

if(frontIsClear()){

move();

putBeeper();

}

}

}

public void left(){

if(noBeepersPresent())

{

turnLeft();

move();

turnLeft();

putBeeper();

}

else{

turnLeft();

move();

turnLeft();

move();

putBeeper();

}

}

public void right(){

if(noBeepersPresent())

{

turnRight();

move();

turnRight();

putBeeper();

}

else{

turnRight();

move();

turnRight();

move();

putBeeper();

}

}

public void turnRight(){

turnLeft();

turnLeft();

turnLeft();

}

}

• Mark

I know I’m late to the game but I am excited I got this! Was challenging for me.

/*
* 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 (frontIsClear()){
fillRow();
comeBack();
}
}
private void fillRow(){
// Spaces the beepers and checks if its safe to move forward
while (frontIsClear()){
if (noBeepersPresent()){
putBeeper();
move();
if (frontIsClear()){
move();
if (frontIsBlocked()){
putBeeper();
}
}
}
else {
move();
putBeeper();
if (frontIsClear()){
move();
}
}
}

}
private void comeBack(){
// Brings you back to the start of the Row
turnAround();
while(frontIsClear()){
move();
}
turnRight();
if (frontIsClear()){ // Stops program when it can’t go up a row
moveRow();
}
}
private void moveRow(){
// Sets up the staggering pattern
if (beepersPresent()){
move();
turnRight();
move();
}
else {
move();
turnRight();
}
}
}

• guest

Coming back to the start of each row is an interesting method… maybe even the easiest. Nice!

• atay

for those interested in some amateur struggling with patchy coding, here’s my solution!

http://pastebin.com/JaqJ6Nnw

• Ali Razavi

Hi everyone,
I’m a begginer and I have a big problem with Karel. I’ve downloaded ECLIPSE MARS 2 and Java jdk from Stanford University’s website. Then I download some file for “assignments” from the same website. I’m pretty sure that I do it exactly as it says on the instructions, but every time I try to run Karel in Eclipse, I only see a blank window. I switched windows from 8.1 to 7, but still have the same problem.