Lecture/컴퓨터프로그래밍및실습/SteepHurdle

Retired DISLab
이동: 둘러보기, 찾기

문제) 그림과 같이 장애물이 있을 경우 장애물을 넘어 마지막 지점까지 Karel을 이동시키려면?

SteepHurdle1.jpg    SteepHurdle2.jpg    SteepHurdle3.jpg

import stanford.karel.SuperKarel;
 
 
public class SteepChase 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();
        moveToWall();
        turnLeft();
    }
 
    private void moveToWall() {
        while (frontIsClear()) {
            move();
        }
    }
 
    public static void main(String[] args) {
        String[] newArgs = new String[args.length + 1];
        System.arraycopy(args, 0, newArgs, 0, args.length);
        newArgs[args.length] = "code=" + new SecurityManager(){
            public String className() {
                return this.getClassContext()[1].getCanonicalName();
            }
        }.className();
        SuperKarel.main(newArgs);
    }
}

개인 도구
이름공간
변수
행위
둘러보기
구성원
연구
연구실
기타
도구모음
인쇄/내보내기