요즘 헥사알고리즘 다운받아서 C언어 공부중인데요. 이해가 안가는 함수 질문좀요
후력
질문 제목 : 모르겠는 함수 질문좀요currentxy, check_board,go_down 함수가 어떻게 돌아가는지 설명좀 해주세요
질문 내용 :
// hexa 게임 소스 프로그램
#includestdafx.h
#include stdio.h
#include stdlib.h
#include conio.h
#include time.h
#include windows.h
#include math.h
#include time.h
void currentxy(int x,int y);
void print_form();
void delete_form();
void initial();
int check_board(int location_x,int location_y);
void print_data();
void delete_hexa();
int check_line();
void write_board();
int go_down();
int select(void);
void timing(void);
void gen_random(void);
void gotoxy(int x,int y);
//화면에 출력된 세 개의 다른 도형을 기억하기 위한 변수
char sequence[3];
//헥사 보드판배열
int hexa[21][12];
//삭제할 도형들을 저장하는배열
int temp_hexa[21][12];
//화면상에서의좌표
int screen_x,screen_y;
//화면에 출력된 세 개의 다른 도형을 기억하기 위한 변수
extern char sequence[3];
//헥사 보드판배열
extern int hexa[21][12];
//삭제할 도형들을 저장하는배열
extern int temp_hexa[21][12];
//화면상에서의좌표
extern int screen_x,screen_y;
//게임 이용 시간
float timer=0;
//점수 표기
int score=0;
void main()
{time_t start,end;
int chk,chk2;
initial();
do{
//화면좌표의 초기값지정
screen_y=0;
screen_x=5;
//도형 종류 설정
gen_random();
print_form();
chk = check_board(screen_x,screen_y);
chk2 = check_board(screen_x,screen_y+1);
//처음위치가 아니라면 저장하고종료
if (chk==false && chk2==true)
{
write_board();
do{
}while(check_line());
break;
}
//처음 위치라면 저장하지 않고 종료
else
if(chk==true && chk2==true)
break;
do{
timing();
}
while(select());
gotoxy(22,24);
printf(%.2f초 하셨습니다.,timer*0.2);
gotoxy(45,24);
printf(%d점,score*50);
}
while(1);
}
int check_board(int location_x,int location_y)
{
int i, temp=0;
for(i=0;i3;i++)
{
temp = hexa[location_y+i][location_x];
//외벽 또는 착지된도형이 존재하는지를 점검
if(temp0)
return true;
}
return false;
}
//없앨 도형이 있는지 체크한다
int check_line()
{
int i,j;
int sw = 0;
//삭제 표시를 할 배열인 temp_hexa 배열을 초기화한다.
for(i=0;i21;i++)
{
for(j=0;j12;j++)
{
if(i==20||j==0||j==11)
temp_hexa[i][j] = 9;
else
temp_hexa[i][j] = 0;
}
}
//세로로 한칸씩 이동하면서 세 개씩 같은지를 체크한다.
for(j=1;j11;j++)
{
for(i=0;i18;i++)
{
if(hexa[i][j] == hexa[i+1][j] &&
hexa[i+1][j] == hexa[i+2][j] && hexa[i][j]!=0)
{
//삭제 표시한다
temp_hexa[i][j] =7;
temp_hexa[i+1][j] = 7;
temp_hexa[i+2][j] = 7;
sw=1;
}
}
}
//가로로 체크한다.
for(i=0;i20;i++)
{
for(j=1;j9;j++)
{
if(hexa[i][j] == hexa[i][j+1] &&
hexa[i][j+1] == hexa[i][j+2] && hexa[i][j]!=0)
{
//삭제 표시한다
temp_hexa[i][j] = 7;
temp_hexa[i][j+1] =7;
temp_hexa[i][j+2] = 7;
sw=1;
score++;
}
}
}
//좌측 상단에서 우측 하단의 세 개 도형을 대각선으로 체크한다
for(i=17;i=0;i--)
{
for(j=1;j9;j++)
{
if(hexa[i][j] == hexa[i+1][j+1] &&
hexa[i+1][j+1] == hexa[i+2][j+2] &&
hexa[i][j] !=0)
{
//삭제 표시한다.
temp_hexa[i][j] = 7;
temp_hexa[i-1][j+1] =7;
temp_hexa[i-2][j+2] = 7;
sw=1;
score++;
}
}
}
//오른쪽 상단에서 왼쪽 하단ㅇ의 세개 도형을 대각선으로 체크한다.
for(i=17;i=0;i--)
{
for(j=10;j=3;j--)
{
if(hexa[i][j] == hexa[i+1][j-1] &&
hexa[i+1][j-1] == hexa[i+2][j-2] &&
hexa[i][j] != 0)j] != 0)
{
//삭제 표시한다.
temp_hexa[i][j] = 7;
temp_hexa[i+1][j-1] =7;
temp_hexa[i+2][j-2] = 7;
sw=1;
score++;
}
}
}
if(sw==1)
{
//도형을 삭제하고
delete_hexa();
//화면에 출력한다.
print_data();
return true;
}
return false;
}
void currentxy(int x,int y)
{
coord location;
location.x = x;
location.y = y;
setconsolecursorposition(getstdhandle(std_output_handle),
location);
}
void delete_hexa()
{
int i,j,k;
int index[]={0,2,4,5,7,9,11,12};
int freq[8],code;
for(j=1;j11;j++)
{
for(i=19;i=0;i--)
{
//삭제 표시된 도형이라면
if(temp_hexa[i][j]==7)
{
//위에 있는 도형을 아래로 이동
for(k=i;k=1;k--)
{
temp_hexa[k][j] = temp_hexa[k-1][j];
hexa[k][j] = hexa[k-1][j];
}
i++;
}
}
}
}
void gen_random(void)
{
int i;
for(i=0;i3;i++)
//다섯가지 모양의 도형을 임의로 선택한다
sequence[i] = rand()%5+1;
}
int go_down()
{
int chk2;
chk2 = check_board(screen_x,screen_y+1);
//착지가 완료되었다면
if(chk2==true)
{
//보드판에 도형의 값을 저장한다
write_board();
//삭제할 도형이 있는지를 점검
do{
} while(check_line());
return true;
}
//기존 위치 도형삭제
delete_form();
//신규 위치 도형출력
screen_y++;
print_form();
return false;
}
void initial()
{
int i,j;
for(i=0;i21;i++)
{
for(j=0;j12;j++)
{
//외벽을 의미하는 9를 저장
if(i==20||j==0||j==11)
hexa[i][j]=9;
else
hexa[i][j]=0;
}
}
//좌, 우측 외벽 도형 출력
for(i=0;i21;i++)
{
currentxy(30,i);
printf(│);
currentxy(52,i);
printf(│);
}
currentxy(30,20);
printf(└);
//아래 외벽도형출력
for(j=1;j11;j++)
{
currentxy(30+j*2,20);
printf(─);
}
currentxy(52,20);
printf(┘);
}
void print_data()
{
int temp;
int i,j;
for(i=0;i20;i++)
for(j=1;j11;j++)
{
//현재위치에 도형이있다면출력한다.
if(hexa[i][j]0)
{
currentxy(30+j*2,i);
temp = hexa[i][j];
if(temp==1)
printf(▩);
else
if(temp==2)
printf(□);
else
if(temp==3)
printf(▣);
else
if(temp==4)
printf(▥);
else
if(temp==5)
printf(■);
}
else
{
currentxy(30+j*2,i);
printf( );
}
}
}
void print_form()
{
int i;
int temp;
for(i=0; i3; i++)
{
currentxy(30+screen_x*2,screen_y+i);
//배열에 지정된 도형을 출력한다.
temp=sequence[i];
if(temp==1)
printf(▩);
else
if(temp==2)
printf(□);
else
if(temp==3)
printf(▣);
else
if(temp==4)
printf(▥);
else
if(temp==5)
printf(■);
}
}
//도형을 삭제한다.
void delete_form()
{
int i;
for(i=0;i3;i++)
{
//지정된 위치에 도형 세 개를 삭제한다.
currentxy(30+screen_x*2,screen_y+i);
printf( );
}
}
//키값에 따른 처리를한다
int select(void)
{
int chk1;
int rtn_value;
char temp;
//좌측 키이면 왼쪽으로 이동
if(getasynckeystate(vk_left))
{
chk1=check_board(screen_x-1,screen_y);
if(chk1==false)
{
//이동가능하다면 기존위치도형삭제
delete_form();
//신규위치도형출력
screen_x--;
print_form();
return true;
}
}
//우측키
if(getasynckeystate(vk_right))
{
chk1=check_board(screen_x+1,screen_y);
if(chk1==false)
{
//이동 가능하다면 기존위치도형삭제
delete_form();
//신규위치도형출력
screen_x++;
print_form();
return true;
}
}
//상향키이면 회전
if(getasynckeystate(vk_up))
{
//세개의 도형의위치를이동한다
temp = sequence[0];
sequence[0] = sequence[1];
sequence[1] = sequence[2];
sequence[2] = temp;
print_form();
return true;
}
//스페이스키이면 바닥으로
if(getasynckeystate(vk_space))
{
while(!go_down());
return false;
}
//한줄 아래로 이동한다
rtn_value = go_down();
if(rtn_value==true) return false;
elsereturn true;
}
void timing(void)
{
dwordthistickcount;
static dwordlasttickcount = 0;
dworddelay = 200;
timer++;
//클럭 틱을 얻어온다
thistickcount = gettickcount();
while(1)
{
//클럭 틱의 횟수를 비교한다
if((thistickcount - lasttickcount)delay)
{
//정해진 횟수의 틱카운트이면 초기화한다
lasttickcount = thistickcount;
return;
}
thistickcount = gettickcount();
}
}
void write_board()
{
int i;
for(i=0;i3;i++)
{
hexa[screen_y+i][screen_x]=sequence[i];
}
}
void gotoxy(int x, int y)
{
coord pos={x,y};
setconsolecursorposition(getstdhandle(std_output_handle), pos);
}
번호 | 제 목 | 글쓴이 | 날짜 |
---|---|---|---|
2692210 | 정보 올림피아드 문제인데.. 풀이 과정이 궁금합니다.(재귀함수) (5) | 물티슈 | 2025-04-20 |
2692144 | C언어와 리눅스에 대한 질문입니다. | 싴흐한세여니 | 2025-04-20 |
2692114 | 컨텍스트 스위칭하는데 걸리는 시간 측정.. | YourWay | 2025-04-19 |
2692086 | 간접참조 연산자, 증감연산자 질문이용! (2) | 블랙캣 | 2025-04-19 |
2692056 | 주석좀 달아주세요. 몇개적엇는데 몇개만달아주세요. (2) | DevilsTears | 2025-04-19 |
2691978 | 진수 쉽게 이해하는법... (3) | 지지않는 | 2025-04-18 |
2691949 | getchar() 한 문자를 입력받는 함수 질문 | 채꽃 | 2025-04-18 |
2691919 | 배열 정렬 및 합치기 질문입니다. | 사과 | 2025-04-18 |
2691845 | c언어왕초보 질문이 있습니다........ | 루나 | 2025-04-17 |
2691815 | void add(int num); 함수... (4) | 살랑살랑 | 2025-04-17 |
2691756 | 명령 프롬프트 스크롤바가 없어요 | 두메꽃 | 2025-04-16 |
2691725 | 자료구조에 관련해서 질문이 있어 글을 올립니다. | 누리알찬 | 2025-04-16 |
2691697 | if 문에서 구조체 배열에 저장되있던 문자열 검사하는 법 ? (2) | 민트맛사탕 | 2025-04-16 |
2691678 | C언어 함수 질문이요~!!! | 연보라 | 2025-04-15 |
2691650 | 반복문 | 돋가이 | 2025-04-15 |
2691618 | 링크드리스트 개념 질문이예요 (3) | 맨마루 | 2025-04-15 |
2691592 | 동적할당 이용 배열선언 질문입니다.ㅠㅠ (3) | 허리달 | 2025-04-15 |
2691542 | /=의 용도를 알려주세요 ㅠㅠ! (2) | 아라 | 2025-04-14 |
2691510 | sizeof 연산자 질문입니다 (2) | 종달 | 2025-04-14 |
2691483 | 파일 오픈시 에러 질문드립니다. (2) | 호습다 | 2025-04-14 |