C语言二维数组实现扫雷游戏

#include<stdio.h>
//使用二维数组实现 扫雷
int main()
{

   char ui[8][8]={
                  '+','+','+','+','+','+','+','+',
                  '+','+','+','+','+','+','+','+',
                  '+','+','+','+','+','+','+','+',
                  '+','+','+','+','+','+','+','+',
                  '+','+','+','+','+','+','+','+',
                  '+','+','+','+','+','+','+','+',
                  '+','+','+','+','+','+','+','+',
                  '+','+','+','+','+','+','+','+'
                 };
   int map[8][8]={
                     0,0,0,0,0,0,0,0,
                     0,0,1,0,0,1,0,0,
                     0,0,0,0,1,0,0,0,
                     0,0,0,0,0,1,0,0,
                     0,0,1,0,0,0,0,0,
                     0,0,1,0,0,0,0,0,
                     0,1,0,1,1,0,0,0,
                     1,0,0,0,0,0,0,0
                     };
   int p[8][2]={{-1,-1} ,{-1,0},{-1,1},{0,-1},{0,1},{1,-1},{1,0},{1,1}};
   int i=0,j=0;
   int h=0,l=0;
   int h1=0,l1=0;
   int n=0;//用来保存 雷的数量 计数
   int win=0;
 while(1)
  {
       printf("    扫雷游戏\n");

          for(i=0;i<8;i++)
            {
              for(j=0;j<8;j++)
                {
                  printf("%c ",ui[i][j]);
                }
               printf("\n");
           }

      printf("请输入你要打开的位置 行 列:");
          scanf("%d%d",&h,&l);
         if(map[h-1][l-1]==1)
          {
              printf("踩到雷 游戏结束\n");
                break;
          }
         h=h-1;
         l=l-1; 

         //没有踩到雷的情况  判断周围有几个雷 并把数字显示在 界面上
         //-1 -1    -1, 0   -1,+1   0 ,-1   0 ,+1   +1 ,-1    +1,0   +1 ,+1 

        //n=map[h-1][l-1]+map[h-1][l]+map[h-1][l+1]+map[h][l-1]+map[h][l+1]+map[h+1][l-1]+map[h+1][l+1]+map[h+1][l];
        i=0;

        while(i<8)
         {
             n=0;
             h1=h;
             l1=l;
             h1= h1+p[i][0];
             l1=l1+p[i][1];
             if(h1>=0&&h1<8&&l1>=0&&l1<8)
               {
                    if(map[h1][l1]==1)
                        {
                           n++;
                        }
               }

             i++;
          }

         //把得到的数字显示到 界面上 ui[h][l];
         //把int数字转换成 字符
         switch(n)
           {
                case 0:
                    ui[h][l]='0';
                       break;
                case 1:
                    ui[h][l]='1';
                       break;
                case 2:
                    ui[h][l]='2';
                       break;
                case 3:
                    ui[h][l]='3';
                       break;
                case 4:
                    ui[h][l]='4';
                       break;
                case 5:
                    ui[h][l]='5';
                       break;
                case 6:
                    ui[h][l]='6';
                       break;
                case 7:
                    ui[h][l]='7';
                       break;
                case 8:
                    ui[h][l]='8';
                       break;
           }
           win++;
           if(win==54)
             {
                printf("获胜 游戏结束\n");
                   break;
             }
   }
  return 0;
}
上一篇:wpf datagrid 如何自定义行的控件实例,(textbox 并选中则全选)


下一篇:Gym - 101350E Competitive Seagulls (博弈)