Posted on Leave a comment

CheckerBoardKarel – CS106a,Assignment 1,Problem 3

Eureka! I solved it! Works in all worlds. I didn’t put any comments because the code is self explanatory.

[code lang=”java”]
/*
* 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(){
turnLeft();
for(int i=0;i<32;i++){
putBeeper();
skip();
}
}

private void skip(){
if(frontIsClear()){
move();
//doubleskip();
}

if(frontIsBlocked()){
if(facingNorth()){
if(beepersPresent()){
skipright();
} else {
movedown();
}

} else {
if(facingSouth()){
if(beepersPresent()){
skipleft();
} else {
moveup();
}
}
}

} else {
move();
}
}

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

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

private void doubleskip(){
move();
move();
}

private void skipright(){
moveright();
moveright();
}

private void skipleft(){
moveleft();
moveleft();
}

private void moveright(){
turnRight();
if(frontIsBlocked()){
moveleft();
} else {
move();
}
}

private void moveleft(){
turnLeft();
if(frontIsBlocked()){
moveright();
} else {
move();
}
}

}
[/code]

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.