c언어 for문위치좀 부탁드립니다..
베레기
질문 제목 : c언어 for문위치좀 부탁드립니다..질문 내용 :
#includestdio.h
#includestdlib.h
#includewindows.h
#includetime.h
#define x_max 79
#define y_max 24
#includewindows.h
void gotoxy(int x,int y){
coord pos = {x-1,y-1};
setconsolecursorposition(
getstdhandle(std_output_handle),pos);
}
void setcolor( int color,int bgcolor){
color&=0xf;
bgcolor &=0xf;
setconsoletextattribute(
getstdhandle(std_output_handle),
(bgcolor4)|color);
}
int main(){
int data,score=0;
int x=20, y=20;
int m,n,a,b,c,d,e,f,g,h,i,j,k,l; srand(time(null));
m=rand()%79+1;//1~x_max까지
n=rand()%24+1;//1~y_max까지
a=rand()%79+1;//1~x_max까지
b=rand()%24+1;//1~y_max까지
c=rand()%79+1;//1~x_max까지
d=rand()%24+1;//1~y_max까지
e=rand()%79+1;//1~x_max까지
f=rand()%24+1;//1~y_max까지
g=rand()%79+1;//1~x_max까지
h=rand()%24+1;//1~y_max까지
i=rand()%79+1;//1~x_max까지
j=rand()%24+1;//1~y_max까지
k=rand()%79+1;//1~x_max까지
l=rand()%24+1;//1~y_max까지
do{
gotoxy(58,1);
printf(x는 함정,o목표물,☆유저);
setcolor(12,0);
gotoxy(1,1);
printf(score:%d,score);
setcolor(13,0);
gotoxy( m, n ); printf( o );//목표물
setcolor(14,0);
gotoxy( a, b ); printf( x );//함정
gotoxy( c, d ); printf( x );//함정
gotoxy( e, f ); printf( x );//함정
gotoxy( g, h ); printf( x );//함정
gotoxy( i, j ); printf( x );//함정
gotoxy( k, l ); printf( x );//함정
setcolor(15,0);
gotoxy( x, y ); printf( ☆ );
if( m ==x && n==y){
score++;
m=rand()%79+1;
n=rand()%24+1;
}
else if(a==x&&b==y){
score--;
a=rand()%79+1;//1~x_max까지
b=rand()%24+1;//1~y_max까지
}
else if (c==x&&d==y){
score--;
c=rand()%79+1;//1~x_max까지
d=rand()%24+1;//1~y_max까지
}
else if (e==x&&f==y){
score--;
e=rand()%79+1;//1~x_max까지
f=rand()%24+1;//1~y_max까지
}
else if (g==x&&h==y){
score--;
g=rand()%79+1;//1~x_max까지
h=rand()%24+1;//1~y_max까지
}
else if (i==x&&j==y){
score--;
i=rand()%79+1;//1~x_max까지
&n j=rand()%24+1;//1~y_max까지
}
else if (k==x&&l==y){
score--;
k=rand()%79+1;//1~x_max까지
l=rand()%24+1;//1~y_max까지
}
data=getch();
system( cls );
if( data == 0 || data == 0xe0 ) {
data = getch();
switch(data){
case 72://위쪽화살 키
y--; if( y1 ) y = 1; break;
case 75://왼쪽화살표키
x--; if(x1) x=1; break;
case 77://오른쪽화살표키
x++; if(x79) x=79;break;
case 80://아래쪽화살표키
y++; if(y24) y=24;break;
}
}
}while(data!=27);
getch();
return 0;
}
실행시켜보시면 아시겠지만 함정인 x에 ☆가걸리면 +1점 0에별표가걸리면 -1점이 되는 프로그램입니다.(별표는사용자가 키보드를이용하여 조정)
그런대 함정을 랜덤으로 한 30개정도 깔아놓고싶은대 위 코드에서 보신대로 일일이 변수를 지정하여 출력시키는건 너무 코드가 길어지는군요.. for문을 사용하여 간단하게 함정인x를 많이 깔아놓을 수 있을거같은대 어떻게 수정해야할까요.. c언어에 관심이 깊은 고등학생이 질문올립니다.
설명과함께
수정해주셔서 올려주시면 감사하겠습니다.