搜索
熱搜: 活動 交友 discuz
查看: 10345|回復: 0
打印 上一主題 下一主題

[求助] 踩地雷問題

[複製鏈接]
跳轉到指定樓層
1#
發表於 2009-12-27 15:46:27 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
想請問要如何輸入設定炸彈量
並在不想玩遊戲時 要如何終止遊戲!!!
如何秀出炸彈數量!!

int bombX[10];            //將這些設成全域變數,方便後面作業
int bombY[10];
int game_continue=1;
char display[9][9];
int bomb_count[9][9];
int truth[9][9]={0};
int near[8][2]={-1,0,-1,-1,0,-1,1,-1,1,0,1,1,0,1,-1,1};
void refresh_display(void);
void mapreal(void);
void maptruth(void);
void whattodo(void);
int sum_near(int x,int y);
void open(int x,int y);
int main(void)        //開頭的操作,剩下的使用自己寫的函數
{
   int i;
   int j;
   int x;
   int y;
   char ch;
   srand(time(NULL));     //用時間選取一組亂數組
   for(i=0;i<9;i++)
   {
      for(j=0;j<9;j++)
          display[j]='*';
   }
   refresh_display();
while(1)
{  
        
   int a,b;
   printf("please enter bomb number\n");
   scanf("bombX[%d] bombY[%d]" ,&a,&b );
   
   printf("Please open a square (x,y) where x=0~8,y=0~8\n");
   printf("x,y=");
   scanf(" %d , %d",&x,&y);
   while(x<0||x>8||y<0||y>8)       //使用者介面
   {
       printf("Please input again!\n");
       scanf(" %d  %d",&x,&y);
   }
   
  
  // for(i=0;i<10;i++)     //亂數選取炸彈
   //{
      
              
     //  bombX=rand()%9;
      // bombY=rand()%9;
  // }
   for(i=0;i<10;i++)      //防止炸彈重覆或和使用者輸入的第一個座標重複
   {
      if(bombX==x&&bombY==y)
      {
         bombX=rand()%9;
         bombY=rand()%9;
         i=0;
         continue;
      }                        
      for(j=i+1;j<10;j++)
      {
         if(bombX==bombX[j]&&bombY==bombY[j])
         {
             bombX[j]=rand()%9;
             bombY[j]=rand()%9;
             j=99; i=0;
         }
      }
   }
   
   for(i=0;i<10;i++)
      truth[bombX][bombY]=1;
   for(i=0;i<9;i++)
   {
      for(j=0;j<9;j++)
         bomb_count[j]=sum_near(i,j);
   }
   open(x,y);
   refresh_display();
   while(game_continue)     //不斷進行詢問並依使用者操作改變值直到勝利或玩輸
   {
      whattodo();
   }
   system("PAUSE");
   return 0;
}
}
void refresh_display(void)     //印出遊戲介面
{
   int i,j;
   if(game_continue==1)
   printf("  0 1 2 3 4 5 6 7 8---X\n");
   for(i=0;i<9;i++)
   {
      printf("%d ",i);
      for(j=0;j<9;j++)
          printf("%c ",display[j]);
      printf("\n");
   }
   printf("|\n|\nY\n");
}
int sum_near(int x,int y)    //將座標(x,y)周圍的8個格子的真值表中的值加起來
{
    int i;
    int t=0;
    if(x>0&&x<8&&y<8&&y>0)
    {
       for(i=0;i<8;i++)
          t=t+truth[x+near[0]][y+near[1]];
    }
    if(x==0)
    {
       if(y==0)
          t=truth[1][0]+truth[1][1]+truth[0][1];
       if(y>0&&y<8)
          t=truth[0][y-1]+truth[0][y+1]+truth[1][y-1]+truth[1][y+1]+truth[1][y];
       if(y==8)
          t=truth[0][7]+truth[1][7]+truth[1][8];
    }
    if(x==8)
    {
       if(y==0)
          t=truth[7][0]+truth[7][1]+truth[8][1];
       if(y>0&&y<8)
          t=truth[8][y-1]+truth[8][y+1]+truth[7][y-1]+truth[7][y+1]+truth[7][y];
       if(y==8)
          t=truth[8][7]+truth[7][8]+truth[7][7];
    }
    if(x>0&&x<8)
    {
       if(y==0)
          t=truth[x-1][0]+truth[x-1][1]+truth[x][1]+truth[x+1][0]+truth[x+1][1];
       if(y==8)
          t=truth[x-1][8]+truth[x-1][7]+truth[x][7]+truth[x+1][7]+truth[x+1][8];
    }
    for(i=0;i<10;i++)
    {
      if(x==bombX&&y==bombY)
          t=9;
    }
    return t;
}
void open(int x,int y)       //開啟座標(x,y)的函數
{
    int i;
    if(bomb_count[x][y]==9&&display[x][y]=='*')
    {
       printf("You lose!!!\n");
       for(i=0;i<10;i++)
          display[bombX][bombY]='X';
       game_continue=0;
    }
    else if(bomb_count[x][y]==0&&display[x][y]=='*')
    {
       display[x][y]=' ';
       for(i=0;i<8;i++)
           open(x+near[0],y+near[1]);
    }
    else if(bomb_count[x][y]>0&&bomb_count[x][y]<9&&display[x][y]=='*')
       display[x][y]='0'+bomb_count[x][y];
    else
       return;
}
void whattodo(void)  //使用者操作函數
{   
    int x;
    int y;
    int p=0;
    int mark_cnt;
    int i;
    int j;
    int selected_num;
    printf("What do you want to do?\n1.open\n2.mark\n3.unmark\n4.double click\n"
                               );
    scanf(" %d",&selected_num);
    while(selected_num<1||selected_num>4)
    {         
       printf("Please choose again!\n");
       scanf(" %d",&selected_num);
    }
    switch(selected_num)
    {
       case 1 :
       {
          printf("Please input(x,y):");
          scanf(" %d , %d",&x,&y);
          while(x<0||x>8||y<0||y>8)
          {
             printf("Please input again!\n");
             scanf(" %d , %d",&x,&y);
          }
          open(x,y);
          break;
       }  
       case 2 :
       {
          printf("Please input(x,y):");
          scanf(" %d , %d",&x,&y);
          while(x<0||x>8||y<0||y>8)
          {
             printf("Please input again!\n");
             scanf(" %d , %d",&x,&y);
          }
          if(display[x][y]=='*')
             display[x][y]='M';
          break;
       }
       case 3 :
       {
          printf("Please input(x,y):");
          scanf(" %d , %d",&x,&y);
          while(x<0||x>8||y<0||y>8)
          {
             printf("Please input again!\n");
             scanf(" %d , %d",&x,&y);
          }
          if(display[x][y]=='M')
              display[x][y]='*';
          break;
       }
       case 4:
       {
          printf("Please input(x,y):");
          scanf(" %d , %d",&x,&y);
          while(x<0||x>8||y<0||y>8)
          {
             printf("Please input again!\n");
             scanf(" %d , %d",&x,&y);
          }
          if(display[x][y]=='0'+bomb_count[x][y])
          {
             mark_cnt=0;
             for(i=0;i<8;i++)
             {
                    if (x+near[0] <0 || x+near[0]>8 || y+near[1]<0 || y+near[1]>8)
                             continue;
                    else if(display[x+near[0]][y+near[1]]=='M')
                             mark_cnt=mark_cnt+1;
             }
             if(mark_cnt==bomb_count[x][y])
             {
                for(i=0;i<8;i++)
                    open(x+near[0],y+near[1]);
             }   
          }   
          break;
       }
    }
    for(i=0;i<9;i++)
    {
       for(j=0;j<9;j++)
       {
          if(bomb_count[j]!=9&&display[j]!='*'&&display[j]!='M')
             p=p+0;
          else if(bomb_count[j]==9&&display[j]!='M')
             p=p+0;
          else if(bomb_count[j]==9&&display[j]!='*')
             p=p+0;
          else
             p=p+1;
       }
    }
    if(p==0)
    {  
       printf("You win!!!\n");
       for(i=0;i<10;i++)
          display[bombX][bombY]='M';
       game_continue=0;
    }
    refresh_display();
}
您需要登錄後才可以回帖 登錄 | 註冊

本版積分規則

本論壇為非營利之網路平台,所有文章內容均為網友自行發表,不代表論壇立場!若涉及侵權、違法等情事,請告知版主處理。


Page Rank Check

廣告刊登  |   交換連結  |   贊助我們  |   服務條款  |   免責聲明  |   客服中心  |   中央分站

手機版|中央論壇

GMT+8, 2024-4-30 13:28 , Processed in 0.025400 second(s), 17 queries .

Powered by Discuz!

© 2005-2015 Copyrights. Set by YIDAS

快速回復 返回頂部 返回列表