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

扫雷游戏: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;
}

最新文章

  1. PADS在注册表中的菜单栏数据
  2. 02python算法-二分法简介
  3. 8.Smack类库
  4. Design Patterns----简单的工厂模式
  5. windows7使用Source insight上远程修改ubuntu共享内核源码
  6. Android权限安全(6)四大组件自定义权限示例
  7. EF 5.0 和 EF4.0 语法区别
  8. 【JSONKit】序列化Dictionary崩溃
  9. 【deep learning学习笔记】注释yusugomori的LR代码 --- LogisticRegression.cpp
  10. 解决:Using where; Using join buffer (Block Nested Loop)
  11. HashMap(1.8)理解
  12. kibana Dev tool 查询结果与预期不符
  13. Java 8 新特性:1-函数式接口
  14. asm.js 和 Emscripten 入门教程
  15. SVM Kernel Functions
  16. 001_nginx常用参数查询
  17. 20145330 《网络对抗》PC平台逆向破解:注入shellcode 和 Return-to-libc 攻击实验
  18. 5. 常见C语言字符串库函数的使用及实现
  19. Python __setitem__()、__getitem__()、__delitem__()
  20. 封装动态数组类Array

热门文章

  1. 实现动态代理(Java和spring)
  2. jetbrain rider 逐渐完美了,微软要哭了么?
  3. jsencrypt代码分析——openssl的rsa加密解密在js的实现
  4. Ajax 重构的步骤
  5. python入门3 python变量,id(),is运算符
  6. Jmeter入门15 JSON Assertion 适用于json格式的响应断言
  7. linux命令之添加删除磁盘分区
  8. 【luogu P4231 三步必杀】 题解
  9. oracle 基础语法(二)
  10. ES6笔记01