되는대로 살자

[C 게임 프로젝트] 지뢰찾기 전체 소스코드 본문

2009~2014/Proejct

[C 게임 프로젝트] 지뢰찾기 전체 소스코드

malu 2010. 11. 11. 16:00

#include <turboc.h>

#define MAP_HEIGHT    10
#define MAP_WEIGHT   10

#define POS_X    0
#define POS_Y    1

#define BOMB_NUM      10

#define PLAG     11
#define BOMB     10
#define EMPTY_TWO  12
#define EMPTY    0
#define EMPTY_AND_NUM 9

void Set();
void MainLoop();
void PrintMap();
void ChgMap();
void showall(int x,int y);
void ChkEnd();

int Map[MAP_HEIGHT][MAP_WEIGHT],disp_map[MAP_HEIGHT][MAP_WEIGHT],Bomb_Pos[BOMB_NUM][2];
int tempmap[MAP_HEIGHT][MAP_WEIGHT];

void showall(int x,int y)
{
 int i,j,count[9],allcount=0;

 for(i=0;i<9;i++) count[i]=0;
 tempmap[y][x]=true;
/* if(Map[y][x+1]==BOMB) return 0;
 if(Map[y][x-1]==BOMB) return 0;
 if(Map[y+1][x]==BOMB) return 0;
 if(Map[y-1][x]==BOMB) return 0;
 if(Map[y+1][x+1]==BOMB) return 0;
 if(Map[y+1][x-1]==BOMB) return 0;
 if(Map[y-1][x+1]==BOMB) return 0;
 if(Map[y-1][x-1]==BOMB) return 0;*/
 if(x+1<MAP_WEIGHT&&Map[y][x+1]<=EMPTY_AND_NUM&&tempmap[y][x+1]<=0)
 {
  allcount++;
  count[0]++;
  disp_map[y][x+1]=Map[y][x];
  tempmap[y][x+1]=true;
 }
 if(x-1>=0&&Map[y][x-1]<=EMPTY_AND_NUM&&tempmap[y][x-1]<=0)
 {
  allcount++;
  count[1]++;
  disp_map[y][x-1]=Map[y][x];
  tempmap[y][x-1]=true;
 }
 if(y+1<MAP_HEIGHT&&Map[y+1][x]<=EMPTY_AND_NUM&&tempmap[y+1][x]<=0) 
 {
  allcount++;
  count[2]++;
  disp_map[y+1][x]=Map[y][x];
  tempmap[y+1][x]=true;
 } 
 if(y-1>=0&&Map[y-1][x]<=EMPTY_AND_NUM&&tempmap[y-1][x]<=0)
 {
  allcount++;
  count[3]++;
  disp_map[y-1][x]=Map[y][x];
  tempmap[y-1][x]=true;
 }
 if(x+1<MAP_WEIGHT&&y+1<MAP_HEIGHT&&Map[y+1][x+1]<=EMPTY_AND_NUM&&tempmap[y+1][x+1]<=0)
 {
  allcount++;
  count[4]++;
  disp_map[y+1][x+1]=Map[y][x];
  tempmap[y+1][x+1]=true;
 } 
 if(x-1>=0&&y-1>=0&&Map[y-1][x-1]<=EMPTY_AND_NUM&&tempmap[y-1][x-1]<=0) 
 {
  allcount++;
  count[5]++;
  disp_map[y-1][x-1]=Map[y][x];
  tempmap[y-1][x-1]=true;
 }
 if(x+1<MAP_WEIGHT&&y-1>=0&&Map[y-1][x+1]<=EMPTY_AND_NUM&&tempmap[y-1][x+1]<=0) 
 {
  allcount++;
  count[6]++;
  disp_map[y-1][x+1]=Map[y][x];
  tempmap[y-1][x+1]=true;
 }
 if(x-1>=0&&y+1<MAP_HEIGHT&&Map[y+1][x-1]<=EMPTY_AND_NUM&&tempmap[y+1][x-1]<=0) 
 {
  allcount++;
  count[7]++;
  disp_map[y+1][x-1]=Map[y][x];
  tempmap[y+1][x-1]=true;
 }
 if(allcount>0)
 {
  if(count[0]>0&&Map[y][x+1]==EMPTY) showall(x+1,y);
  if(count[1]>0&&Map[y][x-1]==EMPTY) showall(x-1,y);
  if(count[2]>0&&Map[y+1][x]==EMPTY) showall(x,y+1);
  if(count[3]>0&&Map[y-1][x+1]==EMPTY) showall(x,y-1);
  if(count[4]>0&&Map[y+1][x+1]==EMPTY) showall(x+1,y+1);
  if(count[5]>0&&Map[y-1][x-1]==EMPTY) showall(x-1,y-1);
  if(count[6]>0&&Map[y-1][x+1]==EMPTY) showall(x+1,y-1);
  if(count[7]>0&&Map[y+1][x-1]==EMPTY) showall(x-1,y+1);
 }
}
void gameover()
{
 int ans,i,j;
 system("cls");
 gotoxy(20,12); printf("Game over");
 gotoxy(20,13); printf("Retry (y/n)");
 scanf("%s",&ans);
 if(ans=='y')
 {
  for(i=0;i<MAP_HEIGHT;i++)
    disp_map[i][j]=EMPTY;
   for(j=0;j<MAP_WEIGHT;j++)
  MainLoop();
 }
 else system("exit");
}
void ChkEnd()
{
 int i,cnt=0;
 for(i=0;i<BOMB_NUM;i++)
  if(disp_map[Bomb_Pos[i][POS_X]][Bomb_Pos[i][POS_Y]]==PLAG) cnt++;
 if(cnt==BOMB_NUM) gameover();
}
void MainLoop()
{
// system("mode con: cols = 12 lines =
 int ans_x,ans_y,i,j;
 int chkmode;
 srand(unsigned(time(NULL)));
 Set();
 PrintMap();
 ///////////////////////////////////////////// 초기 인터페이스
 system("color f0");

 while(1)
 {
  gotoxy(25,21);
  scanf("%d %d %d",&ans_x,&ans_y,&chkmode);
  ///////////////////////////////////////
 //        입력값 분석 & 코딩
   gotoxy(ans_x*2+24-MAP_WEIGHT/2,ans_y+12-MAP_HEIGHT/2);
  if(chkmode==1)
  {
   switch(Map[ans_y][ans_x])
    {
    case EMPTY:
     Map[ans_y][ans_x]=EMPTY_TWO;
     for(i=0;i<MAP_HEIGHT;i++)
     {
      for(j=0;j<MAP_WEIGHT;j++)
       tempmap[i][j]=false;
     }
     showall(ans_x,ans_y);
     for(i=0;i<MAP_HEIGHT;i++)
      for(j=0;j<MAP_WEIGHT;j++)
       if(tempmap[i][j]==1)
       {
        if(Map[i][j]==EMPTY) disp_map[i][j]=EMPTY_TWO;
        else disp_map[i][j]=Map[i][j];
       }
     break;
    case BOMB:
     printf("■");
     delay(1000);
     gameover();
     break;
    case EMPTY_TWO:
     break;
    default:
     disp_map[ans_y][ans_x]=Map[ans_y][ans_x];
    }
  }
  else disp_map[ans_y][ans_x]=PLAG;
  PrintMap();
 /////////////////////////////
 }
}

void PrintMap()
{
 int i,j;
 system("cls");
 gotoxy(24-MAP_WEIGHT/2,12-MAP_HEIGHT/2-1); printf(" 0 1 2 3 4 5 6 7 8 9");
 for(i=0;i<MAP_HEIGHT;i++)
 {
  gotoxy(24-MAP_WEIGHT/2-1,12-MAP_HEIGHT/2+i);
  printf("%d  ",i);
 }
 gotoxy(19,19); printf("지뢰:■ 빈칸:○ 클릭된 빈칸:⊙ 깃발:†");
 gotoxy(19,20); printf("확인:1  깃발:2 ex)x y 1");
 gotoxy(19,21);
 printf("입력>>");
 for(i=0;i<MAP_HEIGHT;i++)
 {
  gotoxy(24-MAP_WEIGHT/2,12-MAP_HEIGHT/2+i);
  for(j=0;j<MAP_WEIGHT;j++)
  {
   switch(disp_map[i][j])
   {
   case EMPTY:
    printf("○");
    break;
   case BOMB:
    printf("■");
    break;
   case PLAG:
    printf("†"); 
    break;
   case EMPTY_TWO:
    printf("⊙");
    break;
   default:
    printf("%d ",disp_map[i][j]);
   }
  }
  ChkEnd();
//  printf("\n");
 }
}
void Set()
{
 int i,j,k,Check_Same_Bomb,Count=0;
    srand(unsigned(time(NULL)));
 for(i=0;i<MAP_HEIGHT;i++)
  for(j=0;j<MAP_WEIGHT;j++)
   Map[i][j]=EMPTY;
 for(i=0;i<BOMB_NUM;i++)
 {
  Bomb_Pos[i][POS_X]=rand()%MAP_WEIGHT;
  Bomb_Pos[i][POS_Y]=rand()%MAP_HEIGHT;
  for(Check_Same_Bomb=0;Check_Same_Bomb<i;Check_Same_Bomb++)
  {
   if(Bomb_Pos[Check_Same_Bomb][POS_X]==Bomb_Pos[i][POS_X]&&Bomb_Pos[Check_Same_Bomb][POS_Y]==Bomb_Pos[i][POS_Y])
   {
    i--;
    break;
   }
   if(Check_Same_Bomb==Count-1)
    Count++;
  }
 }
 for(i=0;i<MAP_HEIGHT;i++)
  for(j=0;j<MAP_WEIGHT;j++)
   disp_map[i][j]=EMPTY;
 for(i=0;i<BOMB_NUM;i++)
  Map[Bomb_Pos[i][POS_Y]][Bomb_Pos[i][POS_X]]=BOMB;
 for(i=0;i<MAP_HEIGHT;i++)
 {
  for(j=0;j<MAP_WEIGHT;j++)
  {
   Count=0;
   if(Map[i][j]!=BOMB)
   {
    if(i+1<MAP_HEIGHT&&Map[i+1][j]==BOMB) Count++;
    if(i-1>=0&&Map[i-1][j]==BOMB) Count++;
    if(j+1<MAP_WEIGHT&&Map[i][j+1]==BOMB) Count++;
    if(j-1>=0&&Map[i][j-1]==BOMB) Count++;
    if(i+1<MAP_HEIGHT&&j+1<MAP_HEIGHT&&Map[i+1][j+1]==BOMB) Count++;
    if(i-1>=0&&j-1>=0&&Map[i-1][j-1]==BOMB) Count++;
    if(i+1<MAP_HEIGHT&&j-1>=0&&Map[i+1][j-1]==BOMB) Count++;
    if(i-1>=0&&j+1<MAP_WEIGHT&&Map[i-1][j+1]==BOMB) Count++;
    Map[i][j]=Count;
   }
  }
 }
}

int main()
{
 MainLoop();
 return 0;
}