Lecture/자료구조/2013/Karel을 이용한 프로그래밍 연습

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

목차

프로그래밍 도구

  1. JDK6
  2. Eclipse

Karel 다운로드

이 프로그램을 위해서는 다음 라이브러리를 다운로드하여 프로젝트에 추가해야 한다.

프로그램 작성법

예제 프로그램

  • main 프로그램
import stanford.karel.OopKarelProgram;
 
public class SampleProgram extends OopKarelProgram {
 
	@Override
	public void run() {
		Karel karel = getKarel();
 
		karel.move();
		karel.turnLeft();
	}
 
	public static void main(String[] args) {
		OopKarelProgram.main(args, new Karel());
	}
 
}

메소드 추가한 PowerKarel

이 부분의 본문은 PowerKarel.java입니다.
package hufs.dislab.karel;
 
import stanford.karel.KarelProgram;
import stanford.karel.SuperKarel;
 
public class PowerKarel extends SuperKarel {
 
	public PowerKarel() {
	}
 
	public void turn(int direction) {
		switch(direction) {
		case KarelProgram.EAST :
			turnEast();
			break;
		case KarelProgram.WEST :
			turnWest();
			break;
		case KarelProgram.SOUTH :
			turnSouth();
			break;
		case KarelProgram.NORTH :
			turnNorth();
			break;
		default :
			throw new IllegalArgumentException("wrong direction");
 
		}
	}
	public void turnNorth() {
		if (facingSouth()) {
			turnAround();
		} else if (facingEast()) {
			turnLeft();
		} else if (facingWest()) {
			turnRight();
		}
	}
 
	public void turnSouth() {
		if (facingNorth()) {
			turnAround();
		} else if (facingEast()) {
			turnRight();
		} else if (facingWest()) {
			turnLeft();
		}
	}
 
	public void turnEast() {
		if (facingWest()) {
			turnAround();
		} else if (facingNorth()) {
			turnRight();
		} else if (facingSouth()) {
			turnLeft();
		}
	}
 
	public void turnWest() {
		if (facingEast()) {
			turnAround();
		} else if (facingNorth()) {
			turnLeft();
		} else if (facingSouth()) {
			turnRight();
		}
	}
 
	public void moveNorth() {
		turnNorth();
		move();
	}
 
	public void moveSouth() {
		turnSouth();
		move();
	}
 
	public void moveEast() {
		turnEast();
		move();
	}
 
	public void moveWest() {
		turnWest();
		move();
	}
 
	public boolean northIsBlocked() {
		if (facingNorth()) {
			return frontIsBlocked();
		} else if (facingEast()) {
			return leftIsBlocked();
		} else if (facingWest()) {
			return rightIsBlocked();
		} else {
			turnAround();
			boolean b = frontIsBlocked();
			turnAround();
			return b;
		}
	}
 
	public boolean southIsBlocked() {
		if (facingSouth()) {
			return frontIsBlocked();
		} else if (facingEast()) {
			return rightIsBlocked();
		} else if (facingWest()) {
			return leftIsBlocked();
		} else {
			turnAround();
			boolean b = frontIsBlocked();
			turnAround();
			return b;
		}
	}
 
	public boolean eastIsBlocked() {
		if (facingEast()) {
			return frontIsBlocked();
		} else if (facingNorth()) {
			return rightIsBlocked();
		} else if (facingSouth()) {
			return leftIsBlocked();
		} else {
			turnAround();
			boolean b = frontIsBlocked();
			turnAround();
			return b;
		}
	}
 
	public boolean westIsBlocked() {
		if (facingWest()) {
			return frontIsBlocked();
		} else if (facingNorth()) {
			return rightIsBlocked();
		} else if (facingSouth()) {
			return leftIsBlocked();
		} else {
			turnAround();
			boolean b = frontIsBlocked();
			turnAround();
			return b;
		}
	}
 
	public boolean northIsClear() {
		return !northIsBlocked();
	}
 
	public boolean southIsClear() {
		return !southIsBlocked();
	}
 
	public boolean eastIsClear() {
		return !eastIsBlocked();
	}
 
	public boolean westIsClear() {
		return !westIsBlocked();
	}
}
개인 도구
이름공간
변수
행위
둘러보기
구성원
연구
연구실
기타
도구모음
인쇄/내보내기