'2009/03/31'에 해당되는 글 2건

  1. 2009.03.31 C언어 미뢰찾기 (미완성)
  2. 2009.03.31 Time, 동적 메모리
C/C++2009. 3. 31. 20:25

#include <turboc.h>
#define MAX 10


struct BOOM {
 int map;
 int block;
 int flag;
};


int Clear_Check(struct BOOM boom[MAX][MAX],int Boom_Check){
 int Count = 0;
 int i,j;
 for(i=0;i<MAX;i++){
  for(j=0;j<MAX;j++){

   if(boom[i][j].block == 1)
    Count++;   
  }
 }
 gotoxy(0,MAX+1);
 printf("Block_Count = %d \n",Count);

 if( ( Count == ((MAX-2)*(MAX-2) - MAX) ) && Boom_Check == MAX){
  return 1; // 참
 }else{
  return 0; // 거짓


 }
}

 

int  Check_Count(struct BOOM boom[MAX][MAX],int i,int j){
 int Count =0;
 int x,y;

 


 for(x=-1;x<=1;x++){
  for(y=-1;y<=1;y++){   
   if(boom[i+x][j+y].map == 10){
    Count++;


   }
  }
 }


 return Count;
}
void Print_MAP(struct BOOM boom[MAX][MAX]){
 int i,j;
 gotoxy(0,0);
 for(i=0;i<MAX;i++){
  for(j=0;j<MAX;j++){
   if(boom[i][j].block == 0){
    switch(boom[i][j].map){
    case 9:
     textcolor(15);
     printf("▧");
     break;
    default:
     textcolor(2);
     printf("□");
     break;


    }
   }else{
     switch(boom[i][j].map){
    case 0:
     printf("  ");
     break;
    case 1:
     textcolor(11);
     printf("①");
     break;
    case 2:
     textcolor(14);
     printf("②");
     break;
    case 3:
     textcolor(13);
     printf("③");
     break;
    case 4:
     textcolor(8);
     printf("④");
     break;
    case 5:
     textcolor(9);
     printf("⑤");
     break;
    case 9:
     textcolor(15);
     printf("▧");
     break;
    case 10:
     textcolor(RED);
     printf("※");
     break;


     }
    }
    if(boom[i][j].block == 0 && boom[i][j].flag == 1 ){
     textcolor(BLUE);
     printf("\b\b▶");
    }

 


   }


  


   printf("\n");
  }


}

 

 


int main(){


 struct BOOM boom[MAX][MAX];


 int i,j;
 int x,y;
 int X,Y;
 char ch;
 int Boom_Check=0;


 for(i=0;i<MAX;i++){
  for(j=0;j<MAX;j++){
   if(i==0||j==0|| i == MAX-1 || j == MAX-1){
    boom[i][j].map = 9;
   }else{
    boom[i][j].map = 0;
    boom[i][j].block = 0;
    boom[i][j].flag = 0;
   }
  }
 }

 


 //지뢰 심기
 for(i=0;i<MAX;i++){
  x = baserand(1,MAX-2);
  y = baserand(1,MAX-2);
  if(boom[x][y].map != 10){
   boom[x][y].map = 10;
  }else{
   i--;
   continue;
  }


 }
 //Count 세기

 


 for(i=1;i<MAX-1;i++){
  for(j=1;j<MAX-1;j++){
   if(boom[i][j].map != 10){
    boom[i][j].map = Check_Count(boom,i,j);


   }


  }
 }


 X = Y = 2;


 Print_MAP(boom);


 while(1){
  gotoxy(0,MAX+3);
  printf("  X = %2d , Y = %2d \n",X,Y);
  printf("  Count = %2d \n",boom[Y][X].map);
  printf("  Boom Check = %2d\n",Boom_Check);


  gotoxy(X*2,Y);
  ch = getch();


  switch(ch){
   case UP:
    if(Y!=1)Y--;
    break;
   case DOWN:
    if(Y!=MAX-2)Y++;
    break;
   case LEFT:
    if(X!=1)X--;
    break;
   case RIGHT:
    if(X!=MAX-2)X++;
    break;
   case 'f':


    if(boom[Y][X].block == 0){


     if(boom[Y][X].flag == 1){
     
      boom[Y][X].flag = 0;


      if(boom[Y][X].map == 10){
       Boom_Check--;
      }


     }else{
    
      boom[Y][X].flag = 1;


      if(boom[Y][X].map == 10){
       Boom_Check++;
      }


     }
    }
   

 

 


    break;


   case ' ':


    boom[Y][X].block = 1;
    if(boom[Y][X].map == 10){
     gotoxy(0,MAX+6);
     textcolor(WHITE);


     for(i=0;i<MAX;i++){
      for(j=0;j<MAX;j++){
       if(boom[i][j].map==10){
        boom[i][j].block = 1;
       }


      }


     }


     Print_MAP(boom);


     printf("지뢰를 밟았습니다 \n");     
     printf("게임을 끝내겠습니다...\n");
     return 0;
    }
    break;


  }

 


  Print_MAP(boom);


  if(Clear_Check(boom,Boom_Check/*클리어 조건*/) == 1 ){

 

 

   gotoxy(0,MAX+7);
   printf("게임을 클리어 하셨습니다 \n");
   printf("수고하셨습니다 ^^ \n");
   break;
  }


 }
  return 0;
 }


아직 구현할게 남았고...  

쌤 ver.이다  

어렵다....

Posted by 샤키
C/C++2009. 3. 31. 20:23
/*
#include <stdio.h>
#include <time.h>
int main(){
 time_t t;
 time(&t);
 printf("숫 자 : %d \n",t);
 printf("문 자 : %s \n",ctime(&t));
 
 return 0;
}
*/
/*
#include <stdio.h>
#include <time.h>
int main(){
 char date[10];
 char time[10];
 _strdate(date);
 _strtime(time);
 printf("날 짜 : %s \n",date);
 printf("시 간 : %s \n\n",time);
 printf("시: %c%c \n",time[0],time[1]);
 printf("분: %c%c \n",time[3],time[4]);
 printf("초: %c%c \n\n",time[6],time[7]);
 printf("월: %c%c \n",date[0],date[1]);
 printf("일: %c%c \n",date[3],date[4]);
 printf("년: %c%c \n",date[6],date[7]);
 
 return 0;
}
*/
/*
#include <turboc.h>  //디지털 시계구현
int main(){
 time_t t;
 struct tm *pt;
 while(1){
  time(&t);
  pt=localtime(&t);    //현재시간
  printf("%d년 %d월 %d일 %d시 %d분 %d초 \r",
   1900+pt->tm_year,pt->tm_mon+1,pt->tm_mday,pt->tm_hour,pt->tm_min,pt->tm_sec);
  Sleep(1000);
  }
 return 0;
}
*/
/*
#include <turboc.h>  //세부적인 시간 구하기
#include <sys/timeb.h>
int main(){
 struct tm t;
 struct _timeb tb;
 while(1){  
  if(kbhit()){
  _ftime(&tb);
  t=*localtime(&tb.time);    //현재시간  
  printf("%d년 %d월 %d일 %d시 %d분 %d.%d \r",
   1900+t.tm_year,t.tm_mon+1,t.tm_mday,t.tm_hour,t.tm_min,t.tm_sec,tb.millitm);  
  Sleep(10);    
  getch();
   printf("\n");
 }
 } 
 
 return 0;
}
*/
/*
#include <turboc.h>   //D-day 프로그램
#define DAYSEC (24*60*60)
 void input(struct tm * in_time){
 printf("년도 : ");
 scanf("%d",&(in_time->tm_year));
 in_time->tm_year-=1900;
 printf("월 : ");
 scanf("%d",&(in_time->tm_mon));
 in_time->tm_mon-=1;
 printf("일 : ");
 scanf("%d",&(in_time->tm_mday));
}
int main(){
 time_t n1,n2;
 struct tm t1,t2 = {0,0,0,1,0,100}; //2000년 1월 1일     {1,0,100} 일 월 1900년
 int tmp;
 input(&t2);

 n1 = time(NULL);
 t1 = *localtime(&n1);
 n1 = mktime(&t1);
 n2 = mktime(&t2);
 n1 = n1 - n2;
 tmp = (int)(n1/DAYSEC);
 printf("%d년 %d월 %d일부터 지금까지\n",
  t2.tm_year+1900,t2.tm_mon+1,t2.tm_mday);
 if(mktime(&t1) > mktime(&t2)){
  printf("%d일 지났습니다.\n",tmp);
 }else{
  printf("%d일 남았습니다.\n",abs(tmp));
 }
 return 0;
}
*/


/*
#include <stdio.h>   //동적 할당예제
#include <stdlib.h>  //malloc함수 및 free 함수
int main(){
 int* int_p;        //↓sizeof(int) 해야
 int_p = (int *)malloc(4);  //heap영역에 4byte를 할당
 if(int_p==NULL){ //메모리 할당 실패 시 종료
  printf("메모리 할당 실패 \n");
  return 1;
 }
 *int_p = 100;
 printf("*int_p의 값은 : %d \n", *int_p);
 free(int_p);   //메모리 해제
 printf("*int_p의 값은(메모리 해제후) : %d \n", *int_p);
 
 return 0;
}
*/
/*
#include <stdio.h>
#include <stdlib.h>   //malloc함수 및 free 함수
int main(){
 int* int_ar;
 int i;
 int sel;
 printf("메모리를 할당할 공간 : ");
 scanf("%d",&sel);
 int_ar = (int*)malloc(sel*sizeof(int));
 for(i=0;i<sel;i++){
  int_ar[i] = (i+1)*2;
  printf("ar[%d] = %d \n", i, int_ar[i]);
 }
 free(int_ar);  //메모리 해제
 return 0;
}
*/
#include <stdio.h>
struct node {
 int Data;
 char Name[10];
 int Kor;
 int Eng;
 int Math;
};
int main(){
 struct node * tmp;
 int i;
 tmp = (struct node *)malloc( 5 * sizeof(struct node));
 for(i=0;i<5;i++){
  tmp[i].Data = 1001 + i;
  printf("ID : %d \n",tmp[i].Data);
  printf("이름 : ");
  fflush(stdin);
  scanf("%s",&tmp[i].Name);
  printf("국어 : ");  
  scanf("%s",&tmp[i].Kor);
 }
 

 for(i=0;i<5;i++){
  printf("%5d | %10s | 5d \n",tmp[i].Data,tmp[i].Name,tmp[i].Kor);
 }
 return 0;
}
Posted by 샤키