CS106A学习笔记:Karel

Karel 指令

move

turnLeft

pickBeeper

putBeeper

第一个小程序

MyFirstKarel

import stanford.karel.*;

public class MyFirstKarel extends Karel {
    public void run() {
        move();
        pickBeeper();
        move();
        turnLeft();
        move();
        turnLeft();
        turnLeft();
        turnLeft();
        move();
        putBeeper();
        move();
    }
}

添加右转(turnRight)指令:

import stanford.karel.*;

public class MyFirstKarel extends Karel {
    public void run() {
        move();
        pickBeeper();
        move();
        turnLeft();
        move();
        turnRight();
        move();
        putBeeper();
        move();
    }

    // Define turnRight
    private void turnRight() {
        turnLeft();
        turnLeft();
        turnLeft();
    }

}

For 循环

基本语法:

for (int i = 0; i < N; i++) {
    // statements to be repeated
}

利用 For 循环定义 turnRight 指令:

private void turnRight() {
    for (int i=0; i<3; i++) {
        turnLeft();
    }
}

While 循环

基本语法:

while ( true ) {
    // the body of the while-loop
}

用法举例:

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

If 条件语句

基本语法:

if ( // condition ) {
    // the body
}

用法举例:

if ( beeperPresent() ) {
    pickBeeper();
}

if-else 语句

基本语法:

if ( // condition ) {
    // the body
} else {
    // the body
}

用法举例:

if ( beeperPresent() ) {
    pickBeeper();
} else {
    putBeeper();
}

if 语句嵌套用法举例:

if (beeperPresent()) {
    if (frontIsClear()) {
        move();
    }
} else {
    putBeeper();
}

Karel 条件汇总

Test Opposite
frontIsClear() frontIsBlocked()
leftIsClear() leftIsBlocked()
rightIsClear() rightIsBlocked()
beepersPresent() noBeepersPresent()
beepersInBag() noBeepersInBag()
facingNorth() notFacingNorth()
facingSouth() notFacingSouth()
facingEast() notFacingEast()
facingWest() notFacingWest()

综合练习:Karel 障碍跑

SteepleChase

import stanford.karel.*;
public class SteepleChase extends SuperKarel {
    public void run() {
        for (int i=0; i<8; i++) {
            if (frontIsClear()) {
                move();
            } else {
                jumpHurdle();
            }
        }
    }

    private void jumpHurdle() {
        ascendHurdle();
        move();
        descendHurdle();
    }

    private void ascendHurdle() {
        turnLeft();
        while (rightIsBlocked()) {
            move();
        }
        turnRight();
    }

    private void descendHurdle() {
        turnRight();
        while (frontIsClear()) {
            move();
        }
        turnLeft();
    }

}

EOF

文章标签:

CS106A学习笔记:Karel》有2条评论

  1. 谢谢~障碍跑练习,我用这个试了一下。哈哈~

    import stanford.karel.*;

    public class SteepleChase02 extends SuperKarel {
    public void run()
    {
    for (int i=0;i<7;i++)
    {
    while (frontIsClear())
    {
    move();
    }
    turnLeft();
    while (rightIsBlocked())
    {
    move();
    }
    turnRight();
    move();
    turnRight();
    while (frontIsClear())
    {
    move();
    }
    turnLeft();
    }
    }
    }

发表评论

493E