CS106A 学习笔记:Assignment (1)

Problem 1

CollectNewspaperKarel

  1. Move to the newspaper,
  2. Pick it up, and
  3. Return to its starting point.
  4. include a private method for each of the steps shown in the outline.

思路

问题1比较简单,练习各种指令的使用。代码如下:

import stanford.karel.*;

public class CollectNewspaperKarel extends SuperKarel {

    public void run() {
        moveToWall();
        turnLeft();//facing east
        move();
        pickBeeper();
        turnAround();//facing west
        moveToWall();
        turnRight();// facing east
    }

    /*
     * Pre-condition:   none
     * Postcondition:   move to first wall in whichever direction
     * then turn right and move one step
     */
    private void moveToWall() {
        while (frontIsClear()) {
            move();
        }
        turnRight();
        move();
    }

}

Problem 2:

StoneMasonKarel

  • Karel starts at 1st Avenue and 1st Street, facing east, with an infinite number of beepers.
  • The columns are exactly four units apart, on 1st, 5th, 9th Avenue, and so forth.
  • The end of the columns is marked by a wall immediately after the final column. This wall section appears after 13th Avenue in the example, but your program should work for any number of columns.
  • The top of the column is marked by a wall, but Karel cannot assume that columns are always five units high, or even that all columns are the same height.
  • Some of the corners in the column may already contain beepers representing stones that are still in place. Your program should not put a second beeper on these corners.

思路

类似于障碍跑那个例子,修复一行完成行原路返回,如此循环即可。代码如下:

import stanford.karel.*;

public class StoneMasonKarel extends SuperKarel {

    /*
     * we need to repair the 1st Avenue
     * then move to the next Avenue and repair it if it exists
     */
    public void run() {
        repair();
        while (frontIsClear()) {
            moveToNext();
            repair();
        }

    }

    /*
     * repair the Avenue and then back to the original position
     */
    private void repair() {
        uphill();
        downhill();
    }

    /*
     * Pre-condition:   Facing East at bottom
     * Post-condition:  Facing East at top
     */
    private void uphill() {
        turnLeft();

        while (frontIsClear()) {
            checkBeeper();
        }
        if (noBeepersPresent()) {
            putBeeper();
        }
        turnRight();
    }

    /*
     * Pre-condition:   Facing East at top
     * Post-condition:  Facing East at bottom
     */
    private void downhill() {
        turnRight();
        while (frontIsClear()) {
            move();
        }
        turnLeft();
    }

    /*
     * Check if present has a beeper, if not put one till the end
     */
    private void checkBeeper() {
        if (noBeepersPresent()) {
            putBeeper();
            move();
        } else {
            move();
        }
    }

    /*
     * Move to the next avenue if it exists
     */
    private void moveToNext() {
        for (int i=0; i<4; i++) {
            move();
        }
    }

}

Problem 3

CheckerboardKarel

创建一个跳棋棋盘,主要难度为区分奇偶数行/列的不同,同时需要为单行或单列做特殊处理。

原理很简单,蛇行往上逐行进行。

奇数列是最后一点没有方块,偶数列时最后一点有方块。

代码如下:

import stanford.karel.*;

public class CheckerboardKarel extends SuperKarel {

    public void run() {

        putBeeper(); // First, put a beeper at <1,1>

        /*
         * Special the world is only one column wide or one row high.
         */
        if (frontIsBlocked()) {
            turnLeft();
            while (frontIsClear()) {
                move();
                if (frontIsClear()) {
                    move();
                    putBeeper();
                }
            }
        }

        /*
         * If there has a beeper, move
         * If front is clear, move and put a beeper
         * If front is blocked, turn around and go to the next street
         */
        while (frontIsClear()) {
            if (beepersPresent()) {
                move();
            }
            if (frontIsClear()) {
                move();
                putBeeper();
            }
            goToNext();
        }
    }

    /*
     * Karel turn around and go to the next street
     */
    private void goToNext() {
        evenAvenuesBlocked();
        oddAvenuesBlocked();
    }

    /*
     * Karel turn around when it has Even-Avenues
     */
    private void evenAvenuesBlocked() {
        if (frontIsBlocked()) {
            if (noBeepersPresent()) {
                if (facingEast()) {
                    evenEast();
                } else {
                    evenWest();
                }
            }
        }
    }

    /*
     * Karel turn around when it has Odd-Avenues
     */
    private void oddAvenuesBlocked() {
        if (frontIsBlocked()) {
            if (beepersPresent()) {
                if (facingEast()) {
                    oddEast();
                } else {
                    oddWest();
                }
            }
        }
    }

    /*
     * Pre-condition:   Facing east at the Even-Avenue
     * Post-condition:  Facing west
     */
    private void evenEast() {
        turnLeft();
        if (frontIsClear()) {
            move();
            putBeeper();
            turnLeft();
        }
    }

    /*
     * Pre-condition:   Facing west at the Even-Avenue
     * Post-condition:  Facing east
     */
    private void evenWest() {
        turnRight();
        if (frontIsClear()) {
            move();
            putBeeper();
            turnRight();
        }
    }

    /*
     * Pre-condition:   Facing east at the Odd-Avenue
     * Post-condition:  Facing west
     */
    private void oddEast() {
        turnLeft();
        if (frontIsClear()) {
            move();
            turnLeft();
        }
    }

    /*
     * Pre-condition:   Facing west at the Odd-Avenue
     * post-condition:  Facing east
     */
    private void oddWest() {
        turnRight();
        if (frontIsClear()) {
            move();
            turnRight();
        }
    }
}

Problem 4

MidpointFindingKarel

  • Karel starts at 1st Avenue and 1st Street, facing east, with an infinite number of beepers in its bag.
  • The initial state of the world includes no interior walls or beepers.
  • The world need not be square, but you may assume that it is at least as tall as it is wide. Your program, moreover, can assume the following simplifications:
  • If the width of the world is odd, Karel must put the beeper in the center square. If the width is even, Karel may drop the beeper on either of the two center squares.
  • It does not matter which direction Karel is facing at the end of the run.

暂未想到好的解决方法,想到后再补上。

EOF

文章标签:

CS106A 学习笔记:Assignment (1)》有1条评论

  1. 学了第3课后,再看这几个程序写的真是一塌糊涂,,,哈哈!~自己都看不下去了。

发表评论

9MZZ