扫雷小游戏【C语言】

说起扫雷游戏,大家一定很熟悉了,但是你们知道是怎么实现的呢?今天我就来带大家一起分析分析:

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main ()
{

     int delta[8][2] = {{-1,-1},{-1,0},{-1,1},{0,-1},{0,1},{1,-1},{1,0},{1,1}};//探测输入位置的周围点数
     int row =0,col = 0,num = 0;
     char map[10][10] = {0};
     char show[10][10] = {0};
     srand((unsigned)time(NULL));
	 for(row = 0;row <= 9;row++)
	 {
            for(col = 0;col <= 9;col++)
			{
                map[row][col] = '0';
            }
     }
do
{
     row = rand() % 10;
	 col = rand() % 10;
     if(map[row][col] == '0')
	 {
         map[row][col] = 'x';
         num++;
     }
}while(num < 10);
for (row = 0;row <= 9;row++)
{
     for (col = 0;col <= 9;col++)
	 {
           if(map[row][col] != 'x')
		   {
                 int cnt = 0;
                 for (num = 0;num <= 7;num++)
				 {
                      if(row + delta[num][0] < 0)
					  {
                                continue;
                      }
                      if(row + delta[num][0] > 9)
					  {       
						  continue;
                      }
                      if(col + delta[num][1] < 0)
					  {
                           continue;
                      }
                      if(col + delta[num][1] > 9)
					  {
                           continue;
                       }
					  if(map[row + delta[num][0]][col + delta[num][1]]== 'x')
					  {
                          cnt++;
                       }
                  }
              map[row][col] = '0' + cnt;
           }
      }
}
for (row = 0;row < 10;row++)
{
     for(col = 0;col < 10;col ++)
	 {
           printf("* ");
      }
     printf("\n");
}
num = 0;
do
{
    int x;
    int y;
printf("please enter the coordinate of array:");
scanf("%d%d",&x,&y);
show[x-1][y-1] = 1;
if(map[x-1][y-1] == '0')
{
     for (num = 0;num <= 7;num++)
	 {
          if(x-1 + delta[num][0] < 0)
		  {
			  continue;
           }
          if(x-1 + delta[num][0] > 9)
		  {
               continue;
           }
          if(y -1+ delta[num][1] < 0)
		  {
               continue;
          }
          if(y-1 + delta[num][1] > 9)
		  {
               continue;
          }
          show[x-1+delta[num][0]][y-1+delta[num][1]] = 1;
     }
}
if (map[x-1][y-1]!= 'x'&&map[x-1][y-1] != '0')
{
       for (num = 0;num <= 7;num++)
	   {
             int cnt = 0;
             if(x-1 + delta[num][0] < 0)
			 {
                   continue;
              }
              if(x-1 + delta[num][0] > 9)
			  {
                   continue;
              }
             if(y-1 + delta[num][1] < 0)
			 {
                   continue;
              }
             if(y-1 + delta[num][1] > 9)
			 {
                   continue;
             }
             if( map[x -1 + delta[num][0]][y -1+ delta[num][1]] != 'x')
              {
                       show[x-1 + delta[num][0]][y -1+ delta[num][1]] = 1 ;
               }
       }
}
if(map[x-1][y-1] == 'x')
{
   printf("game over!\n");
   for (row = 0;row < 10;row++)
   {
        for(col = 0;col < 10;col ++)
		{
              printf("%c ",map[row][col]);
        }
        printf("\n");
}
return 0;
}
system("cls");
printf("mine sweeping:\n");
for (row = 0;row < 10;row++)
{
       for(col = 0;col < 10;col ++)
	   {
           if (show[row][col] == 1)
             {
                 printf("%c ", map[row][col]);
              }
           else
               {
                   printf("* ");
               }
        }
printf("\n");
}
num = 0;
for (row = 0;row < 10;row++)
{
     for(col = 0;col < 10;col ++)
	 {
           if (show[row][col] == 1 )
             {
                  num++;
             }
      }
}
printf("num:%d\n",num);
}while(num < 90);
printf("you win!");
return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值