문제
WASD키를 이용하여 캐릭터를 상하좌우로 이동하고 주변 맵을 출력하는 문제입니다 이를 해결하는 다음 프로그램의 실행순서를 클릭하세요
옷
WASD와[Enter]를 입력하세요: s
옷
WASD와[Enter]를 입력하세요: q
프로그램 코드
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( "WASD와[Enter]를 입력하세요: " );
direction= scan.nextLine().charAt(0);
8 } while( ( direction == 'W' ) || ( direction == 'A' ) || ( direction == 'S' ) || ( direction == 'D' )
|| ( direction == 'w' ) || ( direction == 'a' ) || ( direction == 's' ) || ( direction == 'd' ) );
scan.close();
9 }
}
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 ) {
switch( direction ) {
case 'W': case 'w':
P21 move( STOP, UP );
break;
case 'A': case 'a':
P22 move( LEFT, STOP );
break;
case 'S': case 's':
P23 move( STOP, DOWN );
break;
case 'D': case 'd':
P24 move( RIGHT, STOP );
break;
}
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 = "";
for( int y = minY; y <= maxY; y++ ) {
for( int x = minX; x <= maxX; x++ ) {
if ( ( this.x == x ) && ( this.y == y ) )
strMap += symbol[ CHARACTER ];
else
strMap += symbol[ map[y][x] ];
}
strMap += "\n";
}
return strMap;
P4e }
}
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 = "";
for( int y = this.y - SCOPE; y <= this.y + SCOPE; y++ ){
for( int x = this.x - SCOPE; 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 }
}