랜덤 미로 생성 커서움직임...
은새
질문 제목 : 랜덤 미로 생성 커서움직임...제가 커서를 움직이면 화면에 생성된 맵이 지워지고
맵크기랑 커서가 움직일수 있느 범위가 다른데 왜그런지 모르겠어서 여쭤봅니다질문 내용 : #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();
}
| 번호 | 제 목 | 글쓴이 | 날짜 |
|---|---|---|---|
| 2752085 | 문자열을 랜덤하게 출력해서 대체,삽입,삭제 횟수를 각가 표시하고 그걸 표 형태로 출력 | 슬아s | 2026-10-20 |
| 2752056 | 포인터 && 배열 질문요~ (2) | 해찬나래 | 2026-10-20 |
| 2752031 | x^n 구하는 코드 짜봤는데 값이 틀려요 ㅠㅠ | 세실 | 2026-10-20 |
| 2752000 | 나 정말 미치겠어요..ㅠㅜ (4) | 다이 | 2026-10-20 |
| 2751978 | 문자열 질문이요.. | 초시계 | 2026-10-19 |
| 2751945 | 숫자 맞추는 프로그램이요 ㅠㅠ (1) | 동은 | 2026-10-19 |
| 2751917 | C언어 이중 연결 관련 질문합니다. | 유우 | 2026-10-19 |
| 2751888 | return의 정확한 의미좀 알려주세요 (3) | 올해1살 | 2026-10-19 |
| 2751857 | 포인터 배열 (2) | 터큰 | 2026-10-18 |
| 2751831 | 연산자? 질문입니다 (2) | 진주 | 2026-10-18 |
| 2751800 | C언어 기초질문입니다.. (3) | 핫블랙 | 2026-10-18 |
| 2751739 | 파일 출력을 구조체로 받을 때, 파일의 끝을 판별하는 방법을 알려주세요. (2) | 얼 | 2026-10-17 |
| 2751715 | unresolved external symbol 에러... | 이룩 | 2026-10-17 |
| 2751686 | 변수 설정에 관한 질문입니다. | 꽃달 | 2026-10-17 |
| 2751655 | 자료구조 연습문제 | 한무릎 | 2026-10-17 |
| 2751631 | gets 함수 질문이요. (4) | 해늘 | 2026-10-16 |
| 2751553 | 퀵정렬 소스인데 주석좀 달아주세요 (1) | 싴흐한세여니 | 2026-10-16 |
| 2751528 | 저기 이거하는법좀 질문.. (2) | 가람슬기 | 2026-10-15 |
| 2751445 | 도저히 풀 수가 없네요.... (2) | 신당 | 2026-10-15 |
| 2751412 | 어제부터 계속 해도.........안되는.... (1) | 족장 | 2026-10-14 |
