RobotProgram (KarelOOP2)

Retired DISLab
Swpark (토론 | 기여) 사용자의 2020년 10월 20일 (화) 17:58 버전
(비교) ← 이전 판 | 현재 판 (비교) | 다음 판 → (비교)
이동: 둘러보기, 찾기
package cp.java.week7;
 
import java.awt.Color;
 
import stanford.karel.Karel;
import stanford.karel.OopKarelProgram;
 
@SuppressWarnings("serial")
public class RobotProgram extends OopKarelProgram {
 
	public void run() {
		Robot[] robots = new Robot[4];
		robots[0] = (Robot)getKarel();
		robots[1] = new PaintKarel();
		robots[2] = new Worker();
		robots[3] = new PaintKarel();
 
		add((Karel)robots[1], 3, 7, SOUTH, 0);
		add((Karel)robots[2], 7, 6, WEST, 0);
		add((Karel)robots[3], 6, 4, NORTH, 0);
 
		startGame(robots);
	}
 
	public static void main(String[] args) {
		OopKarelProgram.main(args, new Worker());
	}
 
	public void startGame(Robot[] robots) {
		for (Robot robot : robots)
			go(robot, 2);
 
		for (Robot robot : robots)
			paintLine(robot, RED);
 
	}
 
	public void paintLine(Robot robot, Color color) {
		robot.walkToWall();
		robot.spin();
		robot.spin();
		paintForward(robot, color);
	}
 
	private void paintForward(Robot robot, Color color) {
		robot.paint(color);
		while (robot.canForward()) {
		    robot.walk();
		    robot.paint(color);
		}		
	}
 
	public int go(Mover mover, int steps) {
		int i = 0;
		for ( ; i < steps; i++) {
			if (!mover.canForward())
				break;
			mover.walk();
		}
		return i;
	}
}
개인 도구
이름공간
변수
행위
둘러보기
구성원
연구
연구실
기타
도구모음
인쇄/내보내기