C언어 미로찾기 질문
카프리콘
질문 제목 : c언어 미로찾기 입니다.미로찾기를 하는도중 길목에 숫자를 생성해 차례대로 먹게끔 하도록 하는 프로그램질문 내용 :
#includestdio.h
#includeconio.h
#includewindows.h
#includetime.h
#define left 75
#define right 77
#define up 72
#define down 80 void gotoxy(int x, int y);
void setmap();
bool move(char ch); char startx = 0;
char starty = 0;
char endx = 0;
char endy = 0;
//1-13char map[20][25] =
{{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,1,1,1,1,0,1,1,1,1,0,1,1,1,1,1,1,1,0,1,1,0,1,1},
{1,0,1,1,1,1,0,1,1,1,1,0,1,1,1,1,1,1,1,0,1,1,0,1,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,0,1,1,0,1,1,1,1,1,1,1,1,1,1,1,1,0,1},
{1,1,1,1,1,1,1,0,1,1,0,1,1,1,1,1,1,1,1,1,1,1,1,0,1},
{1,1,1,1,1,1,1,0,1,1,0,1,1,1,1,1,1,1,1,1,1,1,1,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,1,1,0,1},
{1,0,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,1,1,0,1},
{1,0,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,1,1,0,1},
{1,0,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,1,1,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,0,1,1,1,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,0,1,1,1,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,0,1,1,1,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}
};
void main()
{
setmap();
while(true)
{
if(move(getch()))
{
break;
}
}
while(getch() != 27)
{
gotoxy(0,26);
printf(clear\n종료는 esc\n);
}
}
bool move(char ch)
{
switch(ch)
{
case left:
if(map[starty][startx-1] != 1)
{
printf(□);
startx--;
break;
case right:
if(map[starty][startx+1] != 1)
{
printf(□);
startx++;
}
break;
case up:
if(map[starty-1][startx] != 1)
{
printf(□);
starty--;
}
break;
case down:
if(map[starty+1][startx] != 1)
{
printf(□);
starty++;
}
break;
}
gotoxy(startx*2,starty);
printf(◎);
gotoxy(startx*2,starty);
if(map[startx][starty] == map[endx][endy])
{
return true;
}
else
{
return false;
}
}void setmap()
{
char ch;
int q, p;
int z;
z = rand()%7; // 길을 랜덤으로 하여 숫자를 추가하려고 해논것
q = rand()%19;
p = rand()%24;
for(int i=0; isizeof(map) / sizeof(map[0]); i++)
{
for(int j=0; jsizeof(map[0]) / sizeof(map[0][0]); j++)
{
ch = map[i][j];
switch(ch)
{
case 0:
printf(□);
break;
case 1:
printf(■);
break;
case 2:
printf(◎);
startx = i;
starty = j;
break;
case 3 :
printf(★);
endx = i;
endy = j;
break;
}
} printf(\n);
}
gotoxy(startx*2, starty);
} void gotoxy(int x, int y)
{
coord position = {x, y};
setconsolecursorposition(getstdhandle(std_output_handle), position);
}
함수를 사용하여 화면을 깜박이지 않고 좌표를 구하여 이동하면서 미로를 찾게 하였습니다.
여기서 숫자를 길목에 랜덤으로 부여해서 그것을 먹으면서 전진하려 하는데
예를 들어 while(map[q][p] == 0)
{
{
if( z == 1)
printf(①);
else if(z == 2)
printf(②);/p②);
else if(z == 3)
printf(③);
else if(z == 4)
printf(④);
else if(z == 5)
printf(⑤);
else if(z == 6)
printf(⑥);
else if(z == 7)
printf(⑦);
}
문을 사용하여 넣으려하는데 미로에 신경쓰다보니이항목 추가가 안되더라고요 맵을 나타내주는 함수에 추가 하여 나면 에러가 납니다..조언 부탁드립니다 ㅜㅜ