首先,写代码之前要将整体思路写出来:

扫雷游戏:1.需要两个二维数组,一个用来展示,一个用来放雷;

2.整体骨架在代码中都有注释说明;

3.游戏难度比较简单,适合初学者观看,如果有大佬看明白,可以指点一二.


//使用二维数组来表示地图,此处需要2个二维数组,第一个二维数组表示地雷的雷阵,第二个二维数组表示用户看到的地图
//扫雷地图大小9*9;但是二维数组11*11
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#define MINE_COUNT 10
#define ROW 9
#define COL 9
char show_map[ROW + 2][COL + 2];
char mine_map[ROW + 2][COL + 2];
int Menu(){
 int choice = -1;
 printf("************************\n");
 printf("*   欢迎来到扫雷游戏   *\n");
 printf("*       请您选择       *\n");
 printf("*      1.开始游戏      *\n");
 printf("*      2.离开游戏      *\n");
 printf("************************\n");
 while (1){
  scanf("%d", &choice);
  if (choice == 1)
  {
   return 1;
   break;
  }
  else if (choice == 2)
  {
   exit(2);
  }
  else
  {
   printf("输入非法,请重新输入!\n");
   continue;
  }
 }
}
void Init(){     //初始化 布雷       
 srand(time(0));
 memset(mine_map, '0', (ROW + 2)*(COL + 2));
 memset(show_map, '*', (ROW + 2)*(COL + 2));
 int count = MINE_COUNT;
 int row = -1;
 int col = -1;
 while (count>=0){
  row = rand() % ROW + 1;
  col = rand() % COL + 1;
  if (show_map[row][col] == '*')
  {
   mine_map[row][col] = '1';
   count--;
   continue;
  }
 }
}
void Print(){                     //    1 2 3 4 5 6 7 8 9
 printf("    ");                    //    -----------------
 for (int col = 1; col <= COL; col++)              // 01 | | | | | | | | |
 {                       //   ----------------- 
  printf(" %d ", col);
 }
 printf("\n    ---------------------------\n");
 for (int row = 1; row <= ROW; row++)
 {
  printf("%02d ", row);
  printf(" |%c |%c |%c |%c |%c |%c |%c |%c |%c |\n", show_map[row][1], show_map[row][2], show_map[row][3], show_map[row][4], show_map[row][5],
   show_map[row][6], show_map[row][7], show_map[row][8], show_map[row][9]);
  printf("    ---------------------------\n");
 }
}
char MineBoom(){
 printf("    ");
 for (int col = 1; col <= COL; col++)
 {
  printf(" %d ", col);
 }
 printf("\n    ---------------------------\n");
 for (int row = 1; row <= ROW; row++)
 {
  printf("%02d ", row);
  printf(" |%c |%c |%c |%c |%c |%c |%c |%c |%c |\n", mine_map[row][1], mine_map[row][2], mine_map[row][3], mine_map[row][4], mine_map[row][5],
   mine_map[row][6], mine_map[row][7], mine_map[row][8], mine_map[row][9]);
  printf("    ---------------------------\n");
 }
}
char IsFull(){
 int ful = COL*ROW - MINE_COUNT;
 for (int row = 1; row <= ROW; row++){
  for (int col = 1; col <= COL; col++){
   {
    if (show_map[row][col] == '1' || show_map[row][col] == '0')
    {
     ful--;
    }
   }
  }
 }
 if (ful == 0)
 {
  return 'p';
 }
}
char PlayerMove(char mine_map[ROW + 2][COL + 2], char show_map[ROW + 2][COL + 2]){
 int row = -1;
 int col = -1;
 while (1)
 {
  printf("请玩家选的位置(输入格式:坐标 坐标):");
  scanf("%d %d", &row, &col);
  if (row < 1 || row>ROW || col>COL || col < 1)
  {
   printf("输入越界,请重新输入!\n");
   continue;
  }
  if (row > 0 && row<10 && col>0 && col < 10)
  {
   if (show_map[row][col] == '*')
   {
    //1.有雷 显示雷区,结束游戏
    if (mine_map[row][col] == '1')
    {
     MineBoom();
     return 'n';
     break;
    }
    //3.显示此位的周围一圈 是否有雷
    else if (mine_map[row][col] == '0')
    {
     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++;
     }
     {
      show_map[row][col] = count;
      Print();
      return 'k';
     }
     
    }
    else{
     printf("已经选过,请重新输入!\n");
     continue;
    }
   }
   else{
    printf("输入非法,请重新输入!\n");
    continue;
   }
  }
 }
}
void Game(){
 if (Menu() == 1)//1.选择菜单
 {
  Init();//2.初始化,布雷
  Print(); //3.打印棋盘
  while (1){
   if (PlayerMove(mine_map,show_map) == 'n'){
    printf("踩到雷啦,游戏结束!\n");
    break;
   }
   else if (IsFull() == 'p'){
    printf("恭喜玩家胜利!\n");
    break;
   }
   else {
    continue;
   }
  }
 }
 system("pause");
}
int main(){
 Game();
 
 return 0;
}

C语言实现简易扫雷的更多相关文章

  1. C语言之简易了解程序环境

    C语言之简易了解程序环境 大纲: 程序的翻译环境 预编译 编译 汇编 链接 程序的运行环境 在ANSI C的任何一种实现中,存在两个不同的环境. 第1种是翻译环境,在这个环境中源代码被转换为可执行的机 ...

  2. C语言实现简易计算器(可作加减乘除)

    C语言实现简易计算器(加减乘除) 计算器作为课设项目,已完成答辩,先将代码和思路(注释中)上传一篇博客 已增添.修改.整理至无错且可正常运行 虽使用了栈,但初学者可在初步了解栈和结构语法后理解代码 # ...

  3. c语言的简易日历

    用c语言编写的简易日历,代码如下: #include <stdio.h> int main(int argc, const char * argv[]) { // insert code ...

  4. C语言新手写扫雷攻略1

    工欲善其事,必先利其器,首先要准备好开发环境,既然是C语言,那就不是WinAPI的扫雷,就是纯的C语言开发,但是以前的C都是TC开发的,现在用肯定是过时很久了,但是也是有解决办法的,某些大神开发出Ea ...

  5. c语言小游戏-扫雷的完成

    C语言-扫雷游戏 本文将对此游戏做一个大致的概述,此代码适合初学者,编写软件使用了vs2017. 该代码可以实现如下功能: 1.用户可以选择3个难度,分别布置不同个数的雷. 2.随机数设置雷的位置. ...

  6. Java 语言实现简易版扫码登录

    基本介绍 相信大家对二维码都不陌生,生活中到处充斥着扫码登录的场景,如登录网页版微信.支付宝等.最近学习了一下扫码登录的原理,感觉蛮有趣的,于是自己实现了一个简易版扫码登录的 Demo,以此记录一下学 ...

  7. 网络编程:基于C语言的简易代理服务器实现(proxylab)

    本文记录了一个基于c socket的简易代理服务器的实现.(CS:APP lab 10 proxy lab) 本代理服务器支持keep-alive连接,将访问记录保存在log文件. Github: h ...

  8. C语言实现简易2048小游戏

    一直很喜欢玩这个小游戏,简单的游戏中包含运气与思考与策略,喜欢这种简约又不失内涵的游戏风格.于是萌生了用C语言实现一下的想法. 具体代码是模仿这个:https://www.cnblogs.com/ju ...

  9. c语言实现:扫雷

    问题描述:相信大多数人都很熟悉扫雷游戏,在n*n的雷盘上随机埋上一些雷,玩家翻开一个非地雷格时,该格将会出现一个数字——提示周围格子中有多少个是地雷格.游戏的目标是在不翻出任何地雷格的条件下,找出所有 ...

随机推荐

  1. python加解密

    from Crypto.Cipher import AES import base64 secret_key = '1234567890123456' # 密匙 msg_text = 'test so ...

  2. HTTP Strict Transport Security

    HTTP Strict Transport Security (通常简称为HSTS) 是一个安全功能,它告诉浏览器只能通过HTTPS访问当前资源, 禁止HTTP方式. 作用 一个网站接受一个HTTP的 ...

  3. 1.windows下GIT 服务安装

    本章介绍简单在windows 安装git 服务方法.服务器端采用的是Bonobo Git Server,一款用ASP.NET MVC开发的Git源代码管理工具,界面简洁,基于Web方式配置,简单易用. ...

  4. how to create folders in batches

    you need be good at thinking when you see problem. Work experience:when you need to copy web chinese ...

  5. Database 2 Day DBA guide_Chapter3

    Chapter 3: Getting Started with Oracle Enterprise Manager 第三章:开始oracle企业管理器. Purpose(目的) This chapte ...

  6. 使用sqlloader向oracle导入文本数据

    文本文件如下,注意文件名必须有后缀,文本行首也需要|分隔符:[oracle@ycr test]$ more person.txt|aaa|123|m|aaa|123|m|aaa|123|m|aaa|1 ...

  7. jQuery插件编写步骤详解

    如今做web开发,jquery 几乎是必不可少的,就连vs神器在2010版本开始将Jquery 及ui 内置web项目里了.至于使用jquery好处这里就不再赘述了,用过的都知道.今天我们来讨论下jq ...

  8. Locust性能测试2 分布式运行

    locust分布式可以是本机多进程,也可以是本机作为master,其他机器作slave. 试一下本机的多进程运行: 1  控制台输入 locust -f 脚本路径 --master 2  打开另一个控 ...

  9. AndroidStudio 添加 AndroidAnnotations

    1.添加对apt的依赖 buildscript { repositories { mavenCentral() } dependencies { classpath 'com.neenbedankt. ...

  10. 2018.11.24 struts2中的OGNL表达式及两者的结合

    OGNL表达式 OGNL:对象视图导航语言. ${user.addr.name} 这种写法就叫对象视图导航. OGNL不仅仅可以视图导航.支持比EL表达式更加丰富的功能. 理解图示 使用OGNL准备工 ...