문제
WASD키를 이용하여 맵에서 캐릭터를 상하좌우로 이동하는 문제입니다 이를 해결하는 프로그램의 다음 실행상태에 대해 빈칸을 채우세요
프로그램의 실행순서 및 실행상태
1 public static void main( String args[] ) {
3 new ObjectOnMap( map, 2, 1 );
|
| [0] | [1] | ...(obj01) | | x | | y | | LEFT | | UP | | PATH | | symbol[0] | | (obj02) |
| [0] | 1 | 1 | ... | | | minX | | minY | | RIGHT | | DOWN | | WALL | | [1] | | |
| [1] | 1 | 0 | ... | | | maxX | | maxY | | STOP | | | CHARACTER | | [2] | | |
| | ... | ... | ... | | | map | | ObjectOnMap(), move(), toString(), appear(), disappear() | |
| main() | |
| map | (obj01) |
| character | |
| | | | | | | | | | | | | | | | |
P1b public ObjectOnMap( int[][] map, int x, int y ) {
P1e }
2 ObjectOnMap character =
5 character );
P4b public String toString() {
P4e }
4 System.out.println(
■■■■■■■■■
■ 옷 ■ ■
■■■ ■ ■■■
■ ■ ■
■ ■■■■■ ■
■ ■
■■■■■■■■■
6 do {
8 character.disappear() );
P6b public String disappear() {
P6e }
7 System.out.println(
■■■■■■■■■
■ ■ ■
■■■ ■ ■■■
■ ■ ■
■ ■■■■■ ■
■ ■
■■■■■■■■■
9 character.move( direction );
P2b public void move( char direction ) {
P21 move( directionX, directionY );
P3b public void move( int directionX, int directionY ) {
P3e }
P2e }
11 character.appear() );
P5b public String appear() {
P5e }
10 System.out.println(
■■■■■■■■■
■ 옷■ ■
■■■ ■ ■■■
■ ■ ■
■ ■■■■■ ■
■ ■
■■■■■■■■■
WASD와[Enter]를 입력하세요: s
12 } while( ( direction == 'W' ) || ( direction == 'A' ) || ( direction == 'S' ) || ( direction == 'D' )
6 do {
8 character.disappear() );
P6b public String disappear() {
P6e }
7 System.out.println(
■■■■■■■■■
■ ■ ■
■■■ ■ ■■■
■ ■ ■
■ ■■■■■ ■
■ ■
■■■■■■■■■
9 character.move( direction );
P2b public void move( char direction ) {
P21 move( directionX, directionY );
P3b public void move( int directionX, int directionY ) {
P3e }
P2e }
11 character.appear() );
P5b public String appear() {
P5e }
10 System.out.println(
■■■■■■■■■
■ ■ ■
■■■옷■ ■■■
■ ■ ■
■ ■■■■■ ■
■ ■
■■■■■■■■■
WASD와[Enter]를 입력하세요: q
12 } while( ( direction == 'W' ) || ( direction == 'A' ) || ( direction == 'S' ) || ( direction == 'D' )
13 }
프로그램 코드
import java.util.Scanner;
import move.ObjectOnMap;
public class CharacterMovementOnMap
{
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 ObjectOnMap character =
3 new ObjectOnMap( map, 2, 1 );
4 System.out.println(
5 character );
char direction='d';
6 do {
7 System.out.println(
8 character.disappear() );
9 character.move( direction );
10 System.out.println(
11 character.appear() );
System.out.print( "\033[17;1f\033[2KWASD와[Enter]를 입력하세요: " );
direction= scan.nextLine().charAt(0);
12 } while( ( direction == 'W' ) || ( direction == 'A' ) || ( direction == 'S' ) || ( direction == 'D' )
|| ( direction == 'w' ) || ( direction == 'a' ) || ( direction == 's' ) || ( direction == 'd' ) );
13 }
}
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 }
}