JAVA 프로그래밍

문제

WASD키를 이용하여 캐릭터를 상하좌우로 이동하고 주변 맵을 출력하는 문제입니다 이를 해결하는 다음 프로그램을 해석하세요 
WASD와[Enter]를 입력하세요: s
WASD와[Enter]를 입력하세요: q

WASD와[Enter]를 입력하세요: a
WASD와[Enter]를 입력하세요: a
WASD와[Enter]를 입력하세요: x

WASD와[Enter]를 입력하세요: s
WASD와[Enter]를 입력하세요: s
WASD와[Enter]를 입력하세요: q

알고리즘

 프로그램 시작 
   캐릭터 및 맵 초기화
   캐릭터 이동하기
   캐릭터와 주변 맵을 출력
   캐릭터의 다음 이동 방향 입력 받기
프로그램 종료

맵에 있는 캐릭터 클래스
   캐릭터 이동
      이동 방향 확인
      벽인지 허용 범위인지 확인하고 캐릭터 이동
   캐릭터 및 전체 맵 표현
   캐릭터만 나타나기
   캐릭터만 사라지기
   캐릭터 및 주변 맵 표현

줌 맵에 있는 캐릭터 클래스

프로그램 코드

	// 파일명 : ./Chapter12/CharacterMovementOnZoomMap.java
	import java.util.Scanner;
	import move.ObjectOnZoomMap;
 
	public class CharacterMovementOnZoomMap
	{
		// 프로그램 시작 
1		public static void main( String args[] ) {
			Scanner scan = new Scanner( System.in );
			 
			// 캐릭터 및 맵 초기화 
			int[][] map = {
			               { 1,1,1,1,1,1,1,1,1 },
			               { 1,0,0,0,1,0,0,0,1 },
			               { 1,1,1,0,1,0,1,1,1 },
			               { 1,0,0,0,1,0,0,0,1 },
			               { 1,0,1,1,1,1,1,0,1 },
			               { 1,0,0,0,0,0,0,0,1 },
			               { 1,1,1,1,1,1,1,1,1 }
			              } ;
	 
2			ObjectOnZoomMap character =
3			                            new ObjectOnZoomMap( map, 2, 1 );
					 
			char direction='d';
4			do {
				// 캐릭터 이동하기 
5				character.move( direction );
				// 캐릭터와 주변 맵을 출력 
6				System.out.println(
7				                    character );
				// 캐릭터의 다음 이동 방향 입력 받기 
				System.out.print( "\033[17;1f\033[2KWASD와[Enter]를 입력하세요: " );
				direction= scan.nextLine().charAt(0);
8			} while( ( direction == 'W' ) || ( direction == 'A' ) || ( direction == 'S' ) || ( direction == 'D' )
			      || ( direction == 'w' ) || ( direction == 'a' ) || ( direction == 's' ) || ( direction == 'd' ) );
		// 프로그램 종료 
9		}
	}
	 
	// 파일명 : ./src/move/ObjectOnMap.java
	package move;
	 
	// 맵에 있는 캐릭터 클래스  
	public class ObjectOnMap
	{
		protected int[][] map;
		protected int x, y, minX, minY, maxX, maxY;
		protected final int LEFT = -1, RIGHT = 1, UP = -1, DOWN = 1, STOP = 0;
		protected final int PATH = 0, WALL = 1, CHARACTER = 2;
		protected final String symbol[] = { "  ", "\033[44m   \033[0m", "옷" };
 
P1b		public ObjectOnMap( int[][] map, int x, int y ) {
			this.map = map;
			this.x = x;
			this.y = y;
			this.minX = 0;
			this.minY = 0;
			this.maxX = ( map == null ) ? 0 : map[0].length-1;
			this.maxY = ( map == null ) ? 0 : map.length-1;
P1e		}
	 
		// 캐릭터 이동 
P2b		public void move( char direction ) {
			// 이동 방향 확인  
			int directionX = STOP, directionY = STOP;
			switch( direction ) {
				case 'W': case 'w':
					directionY = UP;
					break;
				case 'A': case 'a':
					directionX = LEFT;
					break;
				case 'S': case 's':
					directionY = DOWN;
					break;
				case 'D': case 'd':
					directionX = RIGHT;
					break;
			}
P21			move( directionX, directionY );
P2e		}
	 
		// 벽인지 허용 범위인지 확인하고 캐릭터 이동 
P3b		public void move( int directionX, int directionY ) {
			if( map[this.y+directionY][this.x+directionX] == WALL )
				return;
				 
			this.x += directionX;
			this.y += directionY;
			this.x = ( this.x <= minX ) ? minX : this.x;
			this.y = ( this.y <= minY ) ? minY : this.y;
			this.x = ( this.x >= maxX ) ? maxX : this.x;
			this.y = ( this.y >= maxY ) ? maxY : this.y;
P3e		}
	 
		// 캐릭터 및 전체 맵 표현 
		@Override
P4b		public String toString() {
			String strMap = "\033[1;1f";
			for( int y = minY; y <= maxY; y++ ) {
				for( int x = minY; x <= maxX; x++ ) {
					strMap += symbol[ map[y][x] ];
				}
				strMap += "\n";
			}
			return strMap + this.appear();
P4e		}
		 
		// 캐릭터만 나타나기 
P5b		public String appear() {
			return "\033[" + (this.y+1) + ";" + (this.x*2+1) + "f"+ symbol[ CHARACTER ];
P5e		}
		 
		// 캐릭터만 사라지기 
P6b		public String disappear() {
			return "\033[" + (this.y+1) + ";" + (this.x*2+1) + "f  ";
P6e		}
	}
 
 
	// 파일명 : ./src/move/ObjectOnZoomMap.java
	package move;
	 
	// 줌 맵에 있는 캐릭터 클래스  
	public class ObjectOnZoomMap extends ObjectOnMap
	{
		public static final int SCOPE = 3;
		 
Z1b		public ObjectOnZoomMap( int[][] map, int x, int y ) {
Z11			super( map, x, y );
Z1e		}
	 
		// 캐릭터 및 주변 맵 표현  
		@Override
Z2b		public String toString() {
			String zoomMap = "\033[1;1f";
			for( int y = this.y - SCOPE, offsetY = y; y <= this.y + SCOPE; y++ ){
				for( int x = this.x - SCOPE, offsetX = x; x <= this.x + SCOPE; x++ ){
					int index = WALL;
					if ( ( x == this.x ) && ( y == this.y ) )
						index = CHARACTER;
					else if ( ( 0 <= x ) && ( x <= maxX ) && ( 0 <= y ) && ( y <= maxY ) )
						index = map[y][x];
					zoomMap += symbol[ index ];
				}
				zoomMap += "\n";
			}
			return zoomMap;
Z2e		}
	}

실행 순서

 
 					※ 실행순서 및 메모리상태는 A키(이전) 및 D키(다음)를 눌러도 확인할 수 있습니다