문제
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
코드 빈칸 채우기
java.util.Scanner;
move.ObjectOnMap;
CharacterMovementOnMap
{
static void main( String args[] ) {
Scanner scan = 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 }
} ;
ObjectOnMap character =
ObjectOnMap( map, 2, 1 );
System.out.println(
character );
char direction='d';
{
System.out.println(
character.disappear() );
character.move( direction );
System.out.println(
character.appear() );
System.out.print( "\033[17;1f\033[2KWASD와[Enter]를 입력하세요: " );
direction= scan.nextLine().charAt(0);
} ( ( direction 'W' ) ( direction 'A' ) ( direction 'S' ) ( direction 'D' )
( direction 'w' ) ( direction 'a' ) ( direction 's' ) ( direction 'd' ) );
}
}
move;
ObjectOnMap
{
int[][] map;
int x, y, minX, minY, maxX, maxY;
final int LEFT = -1, RIGHT = 1, UP = -1, DOWN = 1, STOP = 0;
final int PATH = 0, WALL = 1, CHARACTER = 2;
final String symbol[] = { " ", "\033[44m \033[0m", "옷" };
ObjectOnMap( int[][] map, int x, int y ) {
.map = map;
.x = x;
.y = y;
.minX = 0;
.minY = 0;
.maxX = ( map null ) ? 0 : map[0].length-1;
.maxY = ( map null ) ? 0 : map.length-1;
}
void move( char direction ) {
int directionX = STOP, directionY = STOP;
( direction ) {
'W': 'w':
directionY = UP;
;
'A': 'a':
directionX = LEFT;
;
'S': 's':
directionY = DOWN;
;
'D': 'd':
directionX = RIGHT;
;
}
move( directionX, directionY );
}
void move( int directionX, int directionY ) {
( map[.y+directionY][.x+directionX] WALL )
;
.x += directionX;
.y += directionY;
.x = ( .x <= minX ) ? minX : .x;
.y = ( .y <= minY ) ? minY : .y;
.x = ( .x >= maxX ) ? maxX : .x;
.y = ( .y >= maxY ) ? maxY : .y;
}
@Override
String () {
String strMap = "\033[1;1f";
( int y = minY; y <= maxY; y++ ) {
( int x = minY; x <= maxX; x++ ) {
strMap += symbol[ map[y][x] ];
}
strMap += "\n";
}
strMap + .appear();
}
String appear() {
"\033[" + (this.y+1) + ";" + (this.x*2+1) + "f"+ symbol[ CHARACTER ];
}
String disappear() {
"\033[" + (this.y+1) + ";" + (this.x*2+1) + "f ";
}
}