c/c++语言开发共享C语言实现扫雷游戏(可展开)

本文实例为大家分享了c语言实现扫雷游戏的具体代码,供大家参考,具体内容如下# 一、游戏的思路先理清游戏大概需要实现的功能,菜单功能的实现、棋盘初始化、打印棋盘、布置雷等。运用两个数组,一个放入布置雷的

c/c++开发分享C语言实现扫雷游戏(可展开)实例为大家分享了c语言实现扫雷游戏的具体代码,供大家参考,具体内容如下

# 一、游戏的思路

先理清游戏大概需要实现的功能,菜单功能的实现、棋盘初始化、打印棋盘、布置雷等。运用两个数组,一个放入布置雷的信息,另一个放入排查雷的信息。选一个坐标扫雷,坐标有雷则游戏结束,没有就计算选中坐标的周围8个格子中雷的总数放入选中的坐标中,若选中的坐标周围8个格子中都没有雷则自动展开。考虑到棋盘边框的情况,实际数组要比打印出的棋盘多两行两列。下面是宏定义和函数声明:

row、col为打印行、列,rows、cols为实际的数组行列
easy_count为雷的个数,可根据需要调整行列和雷的个数

  #define row 9  #define col 9    #define rows row+2  #define cols col+2    #define easy_count 10    #include<stdio.h>  #include<stdlib.h>  #include<time.h>  #include<windows.h>    void initboard(char board[rows][cols], int rows, int cols, char set);  void displayboard(char board[rows][cols], int row, int col);  void setmine(char board[rows][cols], int row, int col);  void findmine(char mine[rows][cols], char show[rows][cols], int row, int col);  void excludemine(char mine[rows][cols], char show[rows][cols], int x, int y);

二、游戏测试

游戏实现的大致思路体现和菜单的实现,代码如下:

  #define _crt_secure_no_warnings 1    #include"game.h"    void menu()  {   printf("##############################n");   printf("###### 1. play 0.exit ######n");   printf("##############################n");  }    //布置雷 - 字符组存储 - 雷用1表示,非雷用0表示 - 最外层一圈放0  //排查雷 - 为避免歧义,再用一个字符组存储排查出来的雷的信息 - 未排除的用#表示  //最外层加一圈字符,只在中间设置雷,并打印展示棋盘中间位置,因此实际存放数组要比打印的棋盘多两行两列  void game()  {   //雷的信息存储   //1.布置好的雷的信息   char mine[rows][cols] = { 0 };   //2.排查出的雷的信息   char show[rows][cols] = { 0 };   //初始化   initboard(mine, rows, cols, '0');   initboard(show, rows, cols, '#');   //打印棋盘   //displayboard(mine, row, col);//测试使用   displayboard(show, row, col);   //布置雷   setmine(mine, row, col);   //displayboard(mine, row, col);//测试使用   //扫雷   findmine(mine, show, row, col);  }    void test()  {   int input = 0;   srand((unsigned int)time(null));   do   {   menu();   printf("请选择:>");   scanf("%d", &input);   switch(input)   {   case 1:   game();   printf("将返回主菜单n");   sleep(5 * 1000);   break;   case 0:   printf("退出游戏n");   break;   default:   printf("选择错误,请重新选择n");   break;   }   } while (input);  }    int main()  {   test();   return 0;  }

三、游戏流程

存放函数的源文件需要引用头文件

  #define _crt_secure_no_warnings 1  #include "game.h"

1.初始化棋盘

  void initboard(char board[rows][cols], int rows, int cols, char set)  {   int i = 0;   int j = 0;   for (i = 0; i < rows; i++)   {   for (j = 0; j < cols; j++)   {   board[i][j] = set;   }   }  }

2.棋盘打印

  void displayboard(char board[rows][cols], int row, int col)  {   int i = 0;   int j = 0;   //打印列号   for (i = 0; i <= col; i++)   {   printf("%d ", i);   }   printf("n");   for (i = 1; i <= row; i++)   {   printf("%d ", i);   for (j = 1; j <= col; j++)   {   printf("%c ", board[i][j]);   }   printf("n");   }  }

3.布置雷

  void setmine(char board[rows][cols], int row, int col)  {   int count = easy_count;   while (count)   {   int x = rand() % row + 1;//1-9   int y = rand() % col + 1;   if (board[x][y] == '0')   {   board[x][y] = '1';   count--;   }   }  }

4.查找雷和胜负判断

  int checkshow(char show[rows][cols], int row, int col)  {   int win = 0;   int i = 0;   int j = 0;   for (i = 1; i <= row; i++)   {   for (j = 1; j <= col; j++)   {   if (show[i][j] == '#')   win++;   }   }   return win;  }    void findmine(char mine[rows][cols], char show[rows][cols], int row, int col)  {   int x = 0;   int y = 0;   int win = 0;   //9*9 - 10 = 71   while (1)   {   printf("请输入排查雷的坐标:>");   scanf("%d%d", &x, &y);   if (x >= 1 && x <= row && y >= 1 && y <= col)   {   //坐标合法   //1.踩雷   if (mine[x][y] == '1')   {   printf("很遗憾,你被炸死了n");   displayboard(mine, row, col);   break;   }   else//不是雷   {   //计算x,y坐标周围有几个雷   excludemine(mine, show, x, y);   displayboard(show, row, col);   win = checkshow(show, row, col);   if (win == easy_count)    break;   }   }   else   {   printf("坐标非法,请重新输入!n");   }   }   if (win == easy_count)   {   printf("恭喜你,排雷成功n");   displayboard(mine, row, col);   }  }

5.扫雷的展开和提醒

  //'1' - '0' = 1  int get_mine_count(char mine[rows][cols], int x, int y)  {   return mine[x - 1][y] +   mine[x - 1][y - 1] +   mine[x][y - 1] +   mine[x + 1][y - 1] +   mine[x + 1][y] +   mine[x + 1][y + 1] +   mine[x][y + 1] +   mine[x - 1][y + 1] - 8 * '0';  }    void excludemine(char mine[rows][cols], char show[rows][cols], int x, int y)  {   int count = get_mine_count(mine, x, y);   if (count != 0)   {   show[x][y] = count + '0';   }   else   {   show[x][y] = ' ';   if (show[x - 1][y] == '#')   excludemine(mine, show, x - 1, y);   if (show[x - 1][y - 1] == '#')   excludemine(mine, show, x - 1, y - 1);   if (show[x][y - 1] == '#')   excludemine(mine, show, x, y - 1);   if (show[x + 1][y - 1] == '#')   excludemine(mine, show, x + 1, y - 1);   if (show[x + 1][y] == '#')   excludemine(mine, show, x + 1, y);   if (show[x + 1][y + 1] == '#')   excludemine(mine, show, x + 1, y + 1);   if (show[x][y + 1] == '#')   excludemine(mine, show, x, y + 1);   if (show[x - 1][y + 1] == '#')   excludemine(mine, show, x - 1, y + 1);   }  }

以上就是c/c++开发分享C语言实现扫雷游戏(可展开)的全部内容,希望对大家的学习有所帮助,也希望大家多多支持<计算机技术网(www.ctvol.com)!!>。

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/c-cdevelopment/595995.html

(0)
上一篇 2021年5月8日
下一篇 2021年5月8日

精彩推荐