/*
* 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();
}
}
}
}
本文介绍了一个名为CheckerboardKarel的Karel机器人程序,该程序能够在多种预设世界中绘制出使用哔哔声表示的棋盘图案。通过递进的逻辑判断和循环结构,CheckerboardKarel能够根据不同方向绘制出连续的方格。
1270

被折叠的 条评论
为什么被折叠?



