/*
* 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(){
putFirstLine();//put the first line
while(frontIsClear()){
putEvenLine(); //even lines like:2 4 6
if(frontIsClear()){
putOddLine(); //odd lines like:3 5 7
}
}
}
/*turn 180 degrees*/
private void turnBack(){
turnLeft();
turnLeft();
}
private void moveToWall(){
while(frontIsClear()){
move();
}
}
private void putFirstLine(){
putBeeper();
while(frontIsClear()){
move();
if(frontIsClear()){
move();
putBeeper();
}
}
turnBack();
moveToWall();
turnRight();
}
/* before and after:face up */
private void putEvenLine(){
move();
turnRight();
if(frontIsClear()){
move();
putBeeper();
}
while(frontIsClear()){
move();
if(frontIsClear()){
move();
putBeeper();
}
}
turnBack();
moveToWall();
turnRight();
}
/* before and after:face up */
private void putOddLine(){
move();
turnRight();
putBeeper();
while(frontIsClear()){
move();
if(frontIsClear()){
move();
putBeeper();
}
}
turnBack();
moveToWall();
turnRight();
}
}
* 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(){
putFirstLine();//put the first line
while(frontIsClear()){
putEvenLine(); //even lines like:2 4 6
if(frontIsClear()){
putOddLine(); //odd lines like:3 5 7
}
}
}
/*turn 180 degrees*/
private void turnBack(){
turnLeft();
turnLeft();
}
private void moveToWall(){
while(frontIsClear()){
move();
}
}
private void putFirstLine(){
putBeeper();
while(frontIsClear()){
move();
if(frontIsClear()){
move();
putBeeper();
}
}
turnBack();
moveToWall();
turnRight();
}
/* before and after:face up */
private void putEvenLine(){
move();
turnRight();
if(frontIsClear()){
move();
putBeeper();
}
while(frontIsClear()){
move();
if(frontIsClear()){
move();
putBeeper();
}
}
turnBack();
moveToWall();
turnRight();
}
/* before and after:face up */
private void putOddLine(){
move();
turnRight();
putBeeper();
while(frontIsClear()){
move();
if(frontIsClear()){
move();
putBeeper();
}
}
turnBack();
moveToWall();
turnRight();
}
}