Assignment1_CheckerboardKarel
来源:互联网 发布:童年阴影知乎 编辑:程序博客网 时间:2024/05/01 13:33
/*
* 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:
* Section Leader:
*/
public class CheckerboardKarel extends SuperKarel {
public void run() {
if (frontIsBlocked()) {
putBeeper();
turnLeft();
} else {
putBeeper();
}
while (frontIsClear()) {
while (frontIsClear()) {
move();
if (frontIsClear()) {
move();
putBeeper();
}
}
if (facingEast())
eastToNextRow();
else
westToNextRow();
}
}
private void eastToNextRow() {
turnLeft();
if (frontIsClear()) {
if (noBeepersPresent()) {
move();
putBeeper();
turnLeft();
} else {
move();
turnLeft();
move();
putBeeper();
}
}
}
private void westToNextRow() {
turnRight();
if (frontIsClear()) {
if (noBeepersPresent()) {
move();
putBeeper();
turnRight();
} else {
move();
turnRight();
move();
putBeeper();
}
}
}
}
* 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:
* Section Leader:
*/
public class CheckerboardKarel extends SuperKarel {
public void run() {
if (frontIsBlocked()) {
putBeeper();
turnLeft();
} else {
putBeeper();
}
while (frontIsClear()) {
while (frontIsClear()) {
move();
if (frontIsClear()) {
move();
putBeeper();
}
}
if (facingEast())
eastToNextRow();
else
westToNextRow();
}
}
private void eastToNextRow() {
turnLeft();
if (frontIsClear()) {
if (noBeepersPresent()) {
move();
putBeeper();
turnLeft();
} else {
move();
turnLeft();
move();
putBeeper();
}
}
}
private void westToNextRow() {
turnRight();
if (frontIsClear()) {
if (noBeepersPresent()) {
move();
putBeeper();
turnRight();
} else {
move();
turnRight();
move();
putBeeper();
}
}
}
}