An Example of Stepwise Refinement(BanishWinter)

来源:互联网 发布:在淘宝上买匕首犯法吗 编辑:程序博客网 时间:2024/06/06 00:58
import stanford.karel.*;


public class BanishWinter extends SuperKarel {


public void run() {
while (beepersInBag()) {
moveToTree();
climbTree();
putDownBeeper();
toBottom();
}
}


private void moveToTree() {
while (frontIsClear())
move();
}


private void climbTree() {
while (frontIsBlocked()) {
turnLeft();
move();
turnRight();
}
}


private void putDownBeeper() {
turnLeft();
putTwoBeeper();
turnRight();
move();
turnRight();
putTwoBeeper();
}


private void putTwoBeeper() {
putBeeper();
move();
putBeeper();
}


private void toBottom() {
while (frontIsClear())
move();
turnLeft();
}


}