랜덤 미로 생성 커서움직임...
누리별
질문 제목 : 랜덤 미로 생성 커서움직임...제가 커서를 움직이면 화면에 생성된 맵이 지워지고
맵크기랑 커서가 움직일수 있느 범위가 다른데 왜그런지 모르겠어서 여쭤봅니다질문 내용 : #include stdio.h
#include time.h
#include string.h
#include stdlib.h
#include ctype.h
#include windows.h
#include conio.h
#define width 20
#define height 20
#define n 1
#define e 4
#define s 2
#define w 8
#define left 75
#define right 77
#define up 72
#define down 80
#define point_x 3 //보드시작좌표x
#define point_y 4 //보드시작좌표y
#define delay 100
#define exit 50
//enum {n=1,e=4,s=2,w=8};
int dx[9];
int dy[9];
int opposite[9];
int directions[4] = {n, e, s, w};
int dx, dy, nx, ny;
int grid[width][height];
int carve_passage_from(int cx, int cy, int *grid[]);
void shuffle_array(int *array, int size);
void setcursor(int x,int y);
coord getcursor(void) ;
void removecursor(void) ;
void showcharacter(void);
void beginmagearrive(void){
int begin;
int arrive;
begin = grid[4][4];
printf(★,begin);
arrive = grid[20][20];
}
int carve_passage(int cx, int cy, int *grid[width][height]) {
int i;
shuffle_array(directions, 4);
for(i = 0; i 4; i++) {
dx = dx[directions[i]];
dy = dy[directions[i]];
// printf(길 찾기1=x:y - %d=%d:%d\n, directions[i], dx, dy);//유효한 셀 검사
nx = cx + dx;
ny = cy + dy;
if ( ((nx width) & (nx = 0)) & ((ny height) & (ny = 0)) ) {
//방문하지 않은 셀
if (grid[nx][ny] == 0) {
removecursor(); //커서 깜박이 지우기
//setcursor((int)grid[nx], (int)grid[ny]); //보드 시작좌표
//printf(유효한 셀 x,y %d:%d\n, nx, ny);
grid[cx][cy] = (int)((int)grid[cx][cy] | (int)directions[i]);
grid[nx][ny] = (int)((int)grid[nx][ny] | (int)opposite[directions[i]]);
if (grid[nx][ny] == 1){
// printf(유효하지 않은 셀 x,y %d:%d\n, nx, ny);
}
carve_passage(nx, ny, grid);
}
}
}
}
void setcursor(int x,int y)
{
coord pos;
pos.x=x;
pos.y=y;
setconsolecursorposition(getstdhandle(std_output_handle),pos);
}
coord getcursor(void)
{
coord curpoint;
console_screen_buffer_info pos;
getconsolescreenbufferinfo(getstdhandle(std_output_handle),&pos);
curpoint.x=pos.dwcursorposition.x;
curpoint.y=pos.dwcursorposition.y;
return curpoint;
}
void removecursor(void)
{
console_cursor_info cur;
getconsolecursorinfo(getstdhandle(std_output_handle),&cur);
cur.bvisible=0;
setconsolecursorinfo(getstdhandle(std_output_handle),&cur);
}
void showcharacter(void)
{
coord cur=getcursor();
printf(9);
setcursor(cur.x, cur.y);
}
int detect(int x,int y)
{
int x1=directions[x];
int y1=directions[y];
// 커서 위치 얻기
coord cur=getcursor();
// 미로내에서의 위치 계산.
x1=cur.x+x;
y1=cur.y+y;
x1=x1/2-2;
y1=y1-2;
// 미로 밖에 있는지 검사
if(!((x1 =0 && x1 width) && (y1 =0 && y1 height)))
{
setcursor(cur.x+x, cur.y+y);
}
//배열을 넘어가지 않는이유?
if(grid[nx][ny]==1)
printf(_);
//미션성공
else if(grid[nx][ny]==0)
printf(|);
else
return 0;
}
void removecharacter_set(int x, int y)
{
int value=detect(x,y);
if(value==0)
{
coord cur=getcursor();
printf( );
setcursor(cur.x+x, cur.y+y);
}
else if(value==1)
{
coord cur=getcursor();
printf(_);
setcursor(cur.x+x, cur.y+y);
}
else if(value==exit)
{
setcursor(10,15);
printf(성공);
system(pause);
exit(1);
}
}
void character_static(void)
{
int kb;
setcursor(2,2); //케릭터시작위치
while(1)
{
while(!_kbhit())
{
showcharacter();
sleep(delay);
}
kb=_getch();
switch(kb)
{
case up:
removecharacter_set(0,-1);
break ;
case down:
removecharacter_set(0,1);
break ;
case right:
removecharacter_set(2,0);
break ;
case left:
removecharacter_set(-2,0);
break ;
}
}
}
void shuffle_array(int *array, int size) {
int i;
for( i=0; i(size - 1); i++) {
int r = i + (rand() % (size - i));
int temp = array[i];
array[i] = array[r];
array[r] = temp;
}
}
int main(void) {
int x,y;
coord cur=getcursor();
opposite[n] = s;
opposite[e] = w;
opposite[s] = n;
opposite[w] = e;
dx[n] = 0;
dx[e] = 1;
dx[s] = 0;
dx[w] = -1;
dy[n] = -1;
dy[e] = 0;
dy[s] = 1;
dy[w] = 0;
/** seed the random generator **/
srand((unsigned int)time((time_t *)null));
memset(&grid[0], 0, sizeof(grid));
carve_passage(0, 0, (int)grid);
////////////////////////////////
/** display the grid **/
printf(☆);//맵의 시작 맨처음
for(x = 0; x (width); x++) {
printf(☆);//맵의 맨윗줄
}
printf(\n);//개행해서 그 다음 줄 부터 출력
for(y = 0; y height; y++) {
printf(|);//맵의 세로 테두리
for(x = 0; x width; x++) {
printf( ((grid[x][y] & s) != 0)? /*맵의통로*/:_/*맵의길*/);
if((grid[x][y] & e) != 0){
printf((( (grid[x][y] | grid[x + 1][y]) & s) != 0) ? /*맵의통로*/:_/*맵의길*/);
} else {
printf(|);//맵의 가로 테두리
}
}
printf(\n);
}
character_static();
}