#include <stdio.h>
#include <conio.h>
#include <Windows.h>
#define ESC (27) // 종료키
#define LEFT (75) // 좌측 방향키
#define RIGHT (77) // 우측 방향키
#define UP (72) // 위쪽 방향키
#define DOWN (80) // 아래 방향키
/***
*
* map 속성 정의
*
***/
#define _ (0) // 공백
#define C (1) // 캐릭터
/***
*
* map 속성 정의
*
***/
#define MAP_RANGE (10) // map의 넓이
int map[] = { // 초기 Map 정의
C, _, _, _, _, _, _, _, _, _
, _, _, _, _, _, _, _, _, _, _
, _, _, _, _, _, _, _, _, _, _
, _, _, _, _, _, _, _, _, _, _
, _, _, _, _, _, _, _, _, _, _
, _, _, _, _, _, _, _, _, _, _
, _, _, _, _, _, _, _, _, _, _
, _, _, _, _, _, _, _, _, _, _
, _, _, _, _, _, _, _, _, _, _
, _, _, _, _, _, _, _, _, _, _
}
, i = 0; // 반복문에 사용되는 변수
/***
* character current position
***/
int ccp = 0;
/***
* map의 전체 크기를 알려주는 함수
***/
int getMapSize(){
return sizeof( map ) / sizeof( int );
}
/***
* map의 넓이를 기준으로, 끝부분을 체크하여 new line 처리를 해주는 함수
***/
void printEndOfMap(){
if( (MAP_RANGE - 1) == (i % MAP_RANGE) ){
printf("\n");
}
}
// map data를 기준으로 화면에 표시
void printMap(){
system("cls");
printf("[%d]\n", ccp );
for( i = 0; getMapSize() > i; ++i ){
switch( map[i] ){
case _:
printf( "□" );
break;
case C:
printf( "@" );
break;
}
printEndOfMap();
}
// Sleep(500);
}
void changPos(int target ){
int t = map[ccp];
map[ccp] = map[target];
map[target] = t;
ccp = target;
}
int main(){
int action = ' ';
for( ; ESC != action; ){
action = getch();
switch( action ){
case LEFT:
changPos(ccp - 1);
break;
case RIGHT:
changPos(ccp + 1);
break;
case UP:
break;
case DOWN:
break;
}
printMap();
}
return 0;
}
Language/algorithm