CS106A 学习笔记(4):课堂练习

Problem 1:Doubling the number of beepers

doubling-the-number-of-beepers

使 Karel 前面的 Beepers 数量加倍,然后回到起始位置、方向。

动作分解

DoubleBeepers

祥细代码

/*
 * File:    DoubleBeepers.java
 * -------------------------------
 * Karel doubles the number of beepers on the corner directly in front of him in the world.
 * He then returns to his original position/orientation.
 */

import stanford.karel.*;

public class DoubleBeepers extends SuperKarel {

    public void run() {
        move();
        doubleTheBeepers();
        moveBackward();
    }

    /*
     * For every beeper on the current corner, karel puts two beepers on the corner ahead of him.
     */
    private void doubleTheBeepers() {
        while (beepersPresent()) {
            pickBeeper();
            putTwoBeepersNextDoor();
        }
        moveAllBeepersBack();
    }

    /*
     * Put two beepers on the corner in 3rd avenue, and then move back to the starting position/orientation.
     */
    private void putTwoBeepersNextDoor() {
        move();
        for (int i=0; i<2; i++) {
            putBeeper();
        }
        moveBackward();
    }

    /*
     * Move all the beepers on the corner in front of Karel to the corner Karel is currently on.
     */
    private void moveAllBeepersBack() {
        move();
        while (beepersPresent()) {
            moveOneBeeperBack();
        }
        moveBackward();
    }

    /*
     * Move one beeper from the current corner back 1st avenue and return to the original position/orientation.
     */
    private void moveOneBeeperBack() {
        pickBeeper();
        moveBackward();
        putBeeper();
        move();
    }

    /*
     * Karel move back to the 1st avenue, and has the same final orientation. 
     */
    private void moveBackward() {
        turnAround();
        move();
        turnAround();
    }
}

Problem 2:Cleaning up scattered

cleaning-up-scattered-beepers

让 Karel 清除随意放置的方块。

动作分解

  • 每次清理一行,蛇行往上清理。
    • 到达最东边墙时,如果左边没有墙,则进入下一行并面向西。
    • 到达最西边墙时,如果右边没有墙,则进入下一行并面向东。
  • 当偶数行时,进行到最后一行,如果右边有墙,则向后转面向东。

祥细代码

/*
 * File:    CleanupKarel.java
 * ---------------------------------
 * Karel starts at (1,1) and facing east. 
 * Karel cleans up any beepers scattered in the world.
 */

import stanford.karel.*;

public class CleanupKarel extends SuperKarel {

    /*
     * Cleans up a field of beepers, one row at a time.
     */
    public void run () {
        cleanRow();
        while (leftIsClear()) {
            turnToWest();
            cleanRow();
            if (rightIsClear()) {
                turnToEast();
                cleanRow();
            } else {
                /*
                 * In rows with an even number of streets, we want Karel's left to be blocked after
                 * he cleans the last row, so we left him turn around.
                 */
                turnAround();
            }
        }
    }

    /*
     * Let Karel to face west on next row when he at far east wall.
     */
    private void turnToWest() {
        turnLeft();
        move();
        turnLeft();
    }

    /*
     * Let Karel to face east on next row when he at far west wall.
     */
    private void turnToEast() {
        turnRight();
        move();
        turnRight();
    }

    /*
     * Cleans up a row in whichever direction Karel is facing.
     */
    private void cleanRow() {
        if (beepersPresent()) {
            pickBeeper();
        }
        while (frontIsClear()) {
            move();
            if (beepersPresent()) {
                pickBeeper();
            }
        }
    }
}

EOF

文章标签:

发表评论

F7GG