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

Retired DISLab
< Lecture | 컴퓨터프로그래밍및실습
Swpark (토론 | 기여) 사용자의 2014년 1월 26일 (일) 00:45 버전
(비교) ← 이전 판 | 현재 판 (비교) | 다음 판 → (비교)
이동: 둘러보기, 찾기

방안에 쓰레기가 많이 있다. 청소 로봇을 이용하여 방안의 쓰레기를 모두 줍도록 하려면?

CleanupKarel1.jpg    CleanupKarel2.jpg

import stanford.karel.SuperKarel;
 
 
public class CleanupKarel extends SuperKarel {
 
    public void run() {
        cleanRow();
        while (leftIsClear()) {
            repositionForRowToWest();
            cleanRow();
            if (rightIsClear()) {
                repositionForRowToEast();
                cleanRow();
            } else {
                turnAround();
            }
        }
    }
 
    private void cleanRow() {
        if (beepersPresent()) {
            pickBeeper();
        }
        while (frontIsClear()) {
            move();
            if (beepersPresent()) {
                pickBeeper();
            }
        }
    }
 
    private void repositionForRowToWest() {
        turnLeft();
        move();
        turnLeft();
    }
 
    private void repositionForRowToEast() {
        turnRight();
        move();
        turnRight();
    }
 
    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);
    }
}

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