Lecture/컴퓨터프로그래밍및실습/CleanupKarel
Retired DISLab
< Lecture | 컴퓨터프로그래밍및실습
방안에 쓰레기가 많이 있다. 청소 로봇을 이용하여 방안의 쓰레기를 모두 줍도록 하려면?
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); } }