https://pan.baidu.com/s/12oEpv022SiKlJGwZiQNO0g

https://pan.baidu.com/s/1PYRjvvq6fkV_NdTR7FOa2g

• problem1:

``````import stanford.karel.*;

public class CollectNewspaperKarel extends SuperKarel {

// You fill in this part
public void run() {

move2new();
pickBeeper();
return2start();
}
private void move2new() {
move();
turnRight();
move();
turnLeft();
move();
move();
}
private void return2start() {
turnAround();
move();
move();
move();
turnRight();
move();
turnRight();
}

}``````
• problem 2:

``````import stanford.karel.*;

public class StoneMasonKarel extends SuperKarel {

// You fill in this part
public void run() {
put();
while (frontIsClear()) {
move4();
put();
}
}

private void move4() {
for (int i=0;i<4;i++) {
move();
}
}

private void put() {
turnLeft();
while (frontIsClear()) {
put1();
move();
}
put1();
turnAround();
while(frontIsClear()) {
move();
}
turnLeft();
}

private void put1() {
if (noBeepersPresent()) {
putBeeper();
}
}
}``````
• problem 3:

``````import stanford.karel.*;

public class MidpointFindingKarel extends SuperKarel {

// You fill in this part
public void run() {
put();
//在停滞的点放两块物块
putBeeper();
putBeeper();
//移除物块
remove();
turnAround();
//反向移除物块
remove();
turnAround();
while (noBeepersPresent()) {
move();
}
}

//递归定义放置函数，先放左右两边的物块，再在剩余空余位置重复此操作
public void put() {
if (noBeepersPresent()) {
putBeeper();
}
while (flag()==1) {
if (frontIsClear()) {
move();
}
}
if (noBeepersPresent()) {
putBeeper();
}
turnAround();
if (flag()==1) {
put();
}

}

//如果前面有物品或者墙，返回0,否则返回1
private int flag() {
if (frontIsBlocked()) {
return 0;
}else{
move();
if (beepersPresent()) {
turnAround();
move();
turnAround();
return 0;
}else {
return 1;
}

}
}

//如果有物块，则清理
private void remove() {
while (frontIsClear()) {
if (beepersPresent()) {
pickBeeper();
}
move();
}
if (beepersPresent()) {
pickBeeper();
}
}
}``````
• problem 4

这一题是将空盘填为棋盘的样子，要注意奇数偶数两种不同情形。这里的思路是定义put1和put2，put1处理奇数排的情形，put2处理偶数排的情形。每行处理完之后返回最左侧，如果此处有物块，则下一行为偶数排，否则为奇数排。

``````import stanford.karel.*;

public class CheckerboardKarel extends SuperKarel {

// You fill in this part
public void run() {
put1();
//判断是否到达最高层，右手边为墙
while(rightIsClear()) {
//如果此处有物块，下一排采用put2(),否则采用put1()
if (beepersPresent()) {
turnRight();
move();
turnRight();
put2();

}else {
turnRight();
move();
turnRight();
put1();
}
}
}

//在奇数排中每间隔一个距离放物块，结束后返回最左侧,面朝墙
private void put1() {
putBeeper();
while(frontIsClear()) {
move();
if (frontIsClear()) {
move();
putBeeper();
}
}
turnAround();
while (frontIsClear()) {
move();
}
}

//在偶数排中每间隔一个距离放物块，结束后返回最左侧,面朝墙
private void put2() {
if (frontIsClear()) {
move();
}
putBeeper();
while(frontIsClear()) {
move();
if (frontIsClear()) {
move();
putBeeper();
}
}
turnAround();
while (frontIsClear()) {
move();
}
}
}``````