扫雷(c语言)

#define  _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
#define ROW 9
#define COL 9
#define MINE_COUNT 10
void Init(char show_map[ROW + 2][COL + 2], char mine_map[ROW + 2][COL + 2])
{
   //1.把我们的show_map 全部初始化位' '
   for (int row =0; row < ROW+2; ++row)
   {
      for (int col =0; col < COL+2; ++col)
      {
         show_map[row][col]= ' ';
      }
   }
   //2.把mine_map全初始化为'0'
   for (int row =0; row < ROW+2; ++row)
   {
      for (int col =0; col < COL+2; ++col)
      {
         mine_map[row][col]= '0';
      }
   }
   //3.把mine_map哪些位置是地雷排布好
   int  mine_count = MINE_COUNT;
   while(mine_count >0)
   {
      //尝试随即设置地雷
      //0-9
      int row =rand() % ROW + 1;
      int col =rand() % COL + 1;
      if (mine_map[row][col]== '1')
         //当前坐标已经有地雷了
      {
         continue;
      }
      mine_map[row][col]= '1';
      --mine_count;
   }
   }
void DisplayMap(char map[ROW + 2][COL + 2])
{
           //不光要打印出地图的详细内容,也要打印出地图的坐标
   printf("    ");
   //先打印出左上角的空格
//打印列坐标
   for (int i = 1;i <= ROW; ++i)
   {
      printf("%d",i);
   }
   printf("\n");
   for (int i = 1;i <= ROW; ++i)
   {
      printf("---");
   }
   printf("\n");
   //按行打印地图内容
   for (int row =1; row < ROW; ++row)
   {
      //先打印行号
      printf("%d| ",row);
      //在打印改行的每一列
      for (int col =1; col < COL; ++col)
      {
         printf("%c",map[row][col]);
      }
      printf("\n");
   }
}
void UpdateShowMap(char show_map[ROW + 2][COL + 2], char mine_map[ROW + 2][COL + 2], int row, int col)
{
   //统计当前周围8个格子有几个地雷,把数字更新到show_map上
   int count =0;
   /*if
(mine_map[row - 1][col - 1] == '1')

   {

      ++count;

   }

   if (mine_map[row
- 1][col] == '1')

   {

      ++count;

   }

   if (mine_map[row
- 1][col+ 1] == '1')

   {

      ++count;

   }

   if
(mine_map[row][col - 1] == '1')

   {

      ++count;

   }

   if
(mine_map[row][col + 1] == '1')

   {

      ++count;

   }

   if (mine_map[row
+ 1][col - 1] == '1')

   {

      ++count;

   }

   if (mine_map[row
+ 1][col] == '1')

   {

      ++count;

   }

   if (mine_map[row
+1][col + 1] == '1')

   {

      ++count;

   }*/
   count = (mine_map[row - 1][col - 1] - '0')
     + (mine_map[row - 1][col] - '0')
      + (mine_map[row - 1][col + 1] - '0')
      + (mine_map[row][col - 1] - '0')
      + (mine_map[row][col + 1] - '0')
      + (mine_map[row + 1][col - 1] - '0')
      + (mine_map[row + 1][col] - '0')
      + (mine_map[row + 1][col + 1] - '0');
   show_map[row][col] = '0' +
count;
}
int Menu()
{
   printf("===================\n");
   printf("     1.开始游戏    \n");
   printf("     0.结束游戏    \n");
   printf("===================\n");
   printf("请您输入您的选择!\n");
   int choice= 0;
   scanf("%d",&choice);
   return choice;
}
void Game()
{
   char show_map[ROW + 2][COL + 2];
   char mine_map[ROW + 2][COL + 2];
   //由于数组作为参数会隐式转换为指针,拿着数组名传到函数里面已经成为了指针
   //在函数内部对参数进行修改会影响外部的数组的
   Init(show_map,mine_map);
   int  safe_blank_count = 0;
   while (1)
   {
      DisplayMap(show_map);
      int  row = 0;
      int  col = 0;
      printf("请输入坐标:");
      scanf("%d%d", &row, &col);
      if(row<1 || row>ROW || col < 1 || col >COL)
      {
         printf("您输入的坐标不合法,请重新输入!\n");
         continue;
      }
      //验证是否踩到地雷
      if(mine_map[row][col] == '1')
      {
         printf("游戏结束!扫雷失败!\n");
         return;
      }
      //验证是否扫雷成功
      ++safe_blank_count;
      if(safe_blank_count == ROW*COL - MINE_COUNT)
      {
         printf("游戏结束!扫雷成功!\n");
         return;
      }
      //更新地图的状态
      UpdateShowMap(show_map,mine_map,row,col);
      system("cls");
   }
   DisplayMap(mine_map);
}
int main()
{
   srand((unsigned int)time(0));
   while (1)
   {
      int choice= Menu();
      if (choice== 0)
      {
         printf("Goodbye!\n");
         break;
      }
      if (choice== 1)
      {
         Game();
      }
   }
   system("pause");
   return 0;
}
//使用二维数组表示地图

``

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值