//함수의 선언부입니다 앞의 함수가 뒤의 함수를 호출하거나 선언부가 없으면 이리저리 꼬여서 잘 동작안해요~
void gotoxy(int x, int y);
void FileToArray(char * filename, int map[][45]);
void ArrayToScreen(int map[][45]);
void input(int map[][45]);
POINT getheroxy(int map[][45]);
int heroUp(int map[][45]);
int heroDown(int map[][45]);
int heroRight(int map[][45]);
int heroLeft(int map[][45]);
void move(int map[][45],POINT src,int direction);
int clear(int map[][45]);
//콘솔 커서의 좌표를 옮기는 함수입니다. 그냥 그렇구나 하고 외워서 씁니당
void gotoxy(int x, int y)
{
COORD Cur;
Cur.X=x;
Cur.Y=y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),Cur);
}
//파일읽어서 배열에 넣기
//선행학습 : 2차원 배열을 인수로 전달
// 파일 입출력 함수
// 무한루프와 무한루프 푸는 방법
// 문자와 문자열의 차이
void FileToArray(char * filename, int map[][45])
{
FILE * fp;
int ch,x,y;
fp = fopen(filename,"rt");
x=0,y=0;
while(1){
if((ch = fgetc(fp))!=EOF)
{
if(ch != '\n')
{
map[y][x]=ch;
x++;
}else if(ch =='\n')
{
map[y][x]=EndOfLine;
y++;
x=0;
}
}else if(ch == EOF)
{
map[y][x]=EndOfArray;
break;
}
}
}
//배열읽어서 화면에 출력
//선행학습 : 2차원 배열 인수로 받기
// 문자열과 문자의 차이알기
// switch문 알기
void ArrayToScreen(int map[][45])
{
int x,y;
x=0,y=0;
gotoxy(0,0);
while(1)
{
if(map[y][x]==EndOfLine)
{
y++;
x=0;
putc('\n',stdout);
}else if(map[y][x]==EndOfArray)
{
break;
}
else
{
switch(map[y][x])
{
case hero_over_air: printf("★"); break;
case stone_over_air: printf("●"); break;
case wall_over_air: printf("■"); break;
case stone_over_holl: printf("●"); break;
case hero_over_holl: printf("★"); break;
case air: printf(" "); break;
case holl: printf("◎"); break;
}
x++;
}
}
}
//입력받아서 적절한 함수 실행
//선행학습 : GetAsyncKeyState사용법
// static 변수와 printf는 디버깅용으로 써놨음 몰라도됨;
void input(int map[][45])
{
static int a=0;
printf("\n %d \r",a++);
if(GetAsyncKeyState(VK_LEFT))
heroLeft(map);
if(GetAsyncKeyState(VK_RIGHT))
heroRight(map);
if(GetAsyncKeyState(VK_UP))
heroUp(map);
if(GetAsyncKeyState(VK_DOWN))
heroDown(map);
}
//주인공 찾아서 해당 좌표 리턴
//선행학습 : POINT구조체 사용법
// 2차원배열 인수로 받기
// 무한루프
// 무한루프 나가기
POINT getheroxy(int map [][45])
{
int x,y;
POINT p;
x=0;y=0;
while(1)
{
if(map[y][x]==EndOfLine)
{
x=0;
y++;
}
if(map[y][x]==hero_over_air || map[y][x]==hero_over_holl)
{
p.x=x;
p.y=y;
return p;
}
if(map[y][x]==EndOfArray)
{
break;
}
x++;
}
printf("error: found no hero");
p.x=0;
p.y=0;
return p;
}
//주인공을 위로 아래로 왼쪽으로 오른쪽으로 움직이기 전 움직일수있나 보고 움직이는함수부르기
int heroUp(int map[][45])
{
POINT xy = getheroxy(map);
if(map[xy.y-1][xy.x]==air || map[xy.y-1][xy.x]==holl)
{
move(map,xy,direction_up);
return 1;//성공
}
if(map[xy.y-1][xy.x]==stone_over_air || map[xy.y-1][xy.x]==stone_over_holl)
{
if(map[xy.y-2][xy.x]==air || map[xy.y-2][xy.x]==holl)
{
xy.y--;
move(map,xy,direction_up);
xy.y++;
move(map,xy,direction_up);
return 1;//성공
}
}
return 0;//실패
}
int heroDown(int map[][45])
{
POINT xy = getheroxy(map);
if(map[xy.y+1][xy.x]==air || map[xy.y+1][xy.x]==holl)
{
move(map,xy,direction_down);
return 1;//성공
}
if(map[xy.y+1][xy.x]==stone_over_air || map[xy.y+1][xy.x]==stone_over_holl)
{
if(map[xy.y+2][xy.x]==air || map[xy.y+2][xy.x]==holl)
{
xy.y++;
move(map,xy,direction_down);
xy.y--;
move(map,xy,direction_down);
return 1;//성공
}
}
return 0;//실패
}
int heroLeft(int map[][45])
{
POINT xy = getheroxy(map);
if(map[xy.y][xy.x-1]==air || map[xy.y][xy.x-1]==holl)
{
move(map,xy,direction_left);
return 1;//성공
}
if(map[xy.y][xy.x-1]==stone_over_air || map[xy.y][xy.x-1]==stone_over_holl)
{
if(map[xy.y][xy.x-2]==air || map[xy.y][xy.x-2]==holl)
{
xy.x--;
move(map,xy,direction_left);
xy.x++;
move(map,xy,direction_left);
return 1;//성공
}
}
return 0;//실패
}
int heroRight(int map[][45])
{
POINT xy = getheroxy(map);
if(map[xy.y][xy.x+1]==air || map[xy.y][xy.x+1]==holl)
{
move(map,xy,direction_right);
return 1;//성공
}
if(map[xy.y][xy.x+1]==stone_over_air || map[xy.y][xy.x+1]==stone_over_holl)
{
if(map[xy.y][xy.x+2]==air || map[xy.y][xy.x+2]==holl)
{
xy.x++;
move(map,xy,direction_right);
xy.x--;
move(map,xy,direction_right);
return 1;//성공
}
}
return 0;//실패
}
//돌이나 주인공 움직이기
//선행학습 : switch문
// 매크로(#define)
//
void move(int map[][45],POINT tomove,int direction)//움직일 목표칸이 비어있을때에만 호출
{
switch(direction)
{
case direction_up:
if(map[tomove.y][tomove.x]== stone_over_air)
{
map[tomove.y][tomove.x]=air;
if(map[tomove.y-1][tomove.x]==air)
map[tomove.y-1][tomove.x]=stone_over_air;
if(map[tomove.y-1][tomove.x]==holl)
map[tomove.y-1][tomove.x]=stone_over_holl;
}else if(map[tomove.y][tomove.x]== hero_over_air)
{
map[tomove.y][tomove.x]=air;
if(map[tomove.y-1][tomove.x]==air)
map[tomove.y-1][tomove.x]=hero_over_air;
if(map[tomove.y-1][tomove.x]==holl)
map[tomove.y-1][tomove.x]=hero_over_holl;
}
//
if(map[tomove.y][tomove.x]== stone_over_holl)
{
map[tomove.y][tomove.x]=holl;
if(map[tomove.y-1][tomove.x]==air)
map[tomove.y-1][tomove.x]=stone_over_air;
if(map[tomove.y-1][tomove.x]==holl)
map[tomove.y-1][tomove.x]=stone_over_holl;
}else if(map[tomove.y][tomove.x]== hero_over_holl)
{
map[tomove.y][tomove.x]=holl;
if(map[tomove.y-1][tomove.x]==air)
map[tomove.y-1][tomove.x]=hero_over_air;
if(map[tomove.y-1][tomove.x]==holl)
map[tomove.y-1][tomove.x]=hero_over_holl;
}
//
break;
case direction_down:
if(map[tomove.y][tomove.x]== stone_over_air)
{
map[tomove.y][tomove.x]=air;
if(map[tomove.y+1][tomove.x]==air)
map[tomove.y+1][tomove.x]=stone_over_air;
if(map[tomove.y+1][tomove.x]==holl)
map[tomove.y+1][tomove.x]=stone_over_holl;
}else if(map[tomove.y][tomove.x]== hero_over_air)
{
map[tomove.y][tomove.x]=air;
if(map[tomove.y+1][tomove.x]==air)
map[tomove.y+1][tomove.x]=hero_over_air;
if(map[tomove.y+1][tomove.x]==holl)
map[tomove.y+1][tomove.x]=hero_over_holl;
}
//
if(map[tomove.y][tomove.x]== stone_over_holl)
{
map[tomove.y][tomove.x]=holl;
if(map[tomove.y+1][tomove.x]==air)
map[tomove.y+1][tomove.x]=stone_over_air;
if(map[tomove.y+1][tomove.x]==holl)
map[tomove.y+1][tomove.x]=stone_over_holl;
}else if(map[tomove.y][tomove.x]== hero_over_holl)
{
map[tomove.y][tomove.x]=holl;
if(map[tomove.y+1][tomove.x]==air)
map[tomove.y+1][tomove.x]=hero_over_air;
if(map[tomove.y+1][tomove.x]==holl)
map[tomove.y+1][tomove.x]=hero_over_holl;
}
//
break;
case direction_left:
if(map[tomove.y][tomove.x]== stone_over_air)
{
map[tomove.y][tomove.x]=air;
if(map[tomove.y][tomove.x-1]==air)
map[tomove.y][tomove.x-1]=stone_over_air;
if(map[tomove.y][tomove.x-1]==holl)
map[tomove.y][tomove.x-1]=stone_over_holl;
}else if(map[tomove.y][tomove.x]== hero_over_air)
{
map[tomove.y][tomove.x]=air;
if(map[tomove.y][tomove.x-1]==air)
map[tomove.y][tomove.x-1]=hero_over_air;
if(map[tomove.y][tomove.x-1]==holl)
map[tomove.y][tomove.x-1]=hero_over_holl;
}
//
if(map[tomove.y][tomove.x]== stone_over_holl)
{
map[tomove.y][tomove.x]=holl;
if(map[tomove.y][tomove.x-1]==air)
map[tomove.y][tomove.x-1]=stone_over_air;
if(map[tomove.y][tomove.x-1]==holl)
map[tomove.y][tomove.x-1]=stone_over_holl;
}else if(map[tomove.y][tomove.x]== hero_over_holl)
{
map[tomove.y][tomove.x]=holl;
if(map[tomove.y][tomove.x-1]==air)
map[tomove.y][tomove.x-1]=hero_over_air;
if(map[tomove.y][tomove.x-1]==holl)
map[tomove.y][tomove.x-1]=hero_over_holl;
}
//
break;
case direction_right:
if(map[tomove.y][tomove.x]== stone_over_air)
{
map[tomove.y][tomove.x]=air;
if(map[tomove.y][tomove.x+1]==air)
map[tomove.y][tomove.x+1]=stone_over_air;
if(map[tomove.y][tomove.x+1]==holl)
map[tomove.y][tomove.x+1]=stone_over_holl;
}else if(map[tomove.y][tomove.x]== hero_over_air)
{
map[tomove.y][tomove.x]=air;
if(map[tomove.y][tomove.x+1]==air)
map[tomove.y][tomove.x+1]=hero_over_air;
if(map[tomove.y][tomove.x+1]==holl)
map[tomove.y][tomove.x+1]=hero_over_holl;
}
//
if(map[tomove.y][tomove.x]== stone_over_holl)
{
map[tomove.y][tomove.x]=holl;
if(map[tomove.y][tomove.x+1]==air)
map[tomove.y][tomove.x+1]=stone_over_air;
if(map[tomove.y][tomove.x+1]==holl)
map[tomove.y][tomove.x+1]=stone_over_holl;
}else if(map[tomove.y][tomove.x]== hero_over_holl)
{
map[tomove.y][tomove.x]=holl;
if(map[tomove.y][tomove.x+1]==air)
map[tomove.y][tomove.x+1]=hero_over_air;
if(map[tomove.y][tomove.x+1]==holl)
map[tomove.y][tomove.x+1]=hero_over_holl;
}
//
break;
}
}
int clear(int map[][45])
{
int x,y;
x=0;y=0;
while(1)
{
if(map[y][x]==EndOfLine)
{
y++;
x=0;
}
if(map[y][x]==EndOfArray)
{
return 1;//clear
}
if(map[y][x]==hero_over_holl || map[y][x]==holl)
{
return 0;//clear false
}
x++;
}
return -1;//can't reach
}