c/c++语言开发共享C语言实现简单的扫雷游戏

本文实例为大家分享了c语言实现简单扫雷游戏的具体代码,供大家参考,具体内容如下前言扫雷游戏规则:1、踩过所有非雷格子即扫雷成功,踩到雷即游戏结束。2、点击方格,如果出现数字,数字表示这个格子周围八个格

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

前言

扫雷游戏规则:

1、踩过所有非雷格子即扫雷成功,踩到雷即游戏结束。
2、点击方格,如果出现数字,数字表示这个格子周围八个格子的雷的个数。

一、如何实现?

1.如何实现雷与雷周围的信息不冲突?

如果采用一个二维数组,1表示雷,0表示非雷,那么某一坐标周围如果雷的个数是1,就会与前面冲突,所以设定两个字符型(char)数组,数组mine用来存储雷的信息,数组show用来存放排查出来的雷的信息(周围八个格子中雷的个数)

2.如何避免使用数组时越界?

如果设置格子的大小为9×9,在查找边界格子(如下图红框内的格子)周围的雷的个数时会越界查找,为避免越界查找,将格子扩大一圈,但是只使用中间9×9部分的格子。

C语言实现简单的扫雷游戏

3.如何实现点击一个格子展开一片的效果?

1、首先,要了解展开一片的条件,当选择的格子不是雷,并且格子周围没有雷时,才会有展开一片的效果。
2、采用递归的方式实现,按照一定的方向依次寻找,直到某个格子周围有雷时跳出,返回上一次递归。

递归要注意递归的截至条件,并且要注意数组的边界。

二、具体代码及实现过程

1.初始化棋盘

两个字符数组大小相同,只是初始化的字符不同,所用采用同一个初始化函数,通过传参来确定初始化的字符。

  void initboard(char arr[rows][cols], int rows, int cols, char set)//set为初始化的字符  {   int i = 0;   int j = 0;   for (i = 0; i < rows; i++)   {   for (j = 0; j < cols; j++)   {   arr[i][j] = set;   }   }  }

2.打印棋盘

代码如下(示例):打印棋盘也是通过传参的方式,为了在选择格子时方便,打印出行号列号

  void displayboard(char arr[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 ",arr[i][j]);   }   printf("n");   }  }

3.放置雷

生成1-10间的随机数,利用随机数确定放置雷的行和列,放置雷之前要判断此位置是否被放置过,如果被放置过就换一个随机数,直至放满指定的雷的个数。

  void setmine(char mine[rows][cols], int row, int col)  {   int count = easy_count;//雷的总数   while (count)   {   int x = rand() % row + 1;//随机产生行号   int y = rand() % col + 1;//随机产生列号   if (mine[x][y] == '0')//没被放置过雷   {   mine[x][y] = '1';   count--;   }   }  }

4.排雷

1、排雷首先要确定输入的坐标是否有效,无效坐标需要重新输入。
2、如果选择的格子是雷,则游戏结束。
3、如果选择的格子不是雷,并且格子周围没有雷,将show数组中对应的格子置为空格,递归按照某一顺序查找其他方向,递归的截止条件为格子周围有雷存在。递归时要注意判断格子没有被查找过,并且要注意坐标的范围不能超过0-9,否则会展开出错。

  void searchmore(char show[rows][cols], char mine[rows][cols], int x, int y,int * win)//不是雷  {   while (mine[x][y] == '0' && show[x][y] == '*' && x >= 1 && x <= row && y >= 1 && y <= col)   {   if (get_mine_count(mine, x, y) == 0)   {   show[x][y] = ' ';   (*win)++;   searchmore(show, mine, x - 1, y, win);   searchmore(show, mine, x - 1, y - 1, win);   searchmore(show, mine, x, y - 1, win);   searchmore(show, mine, x + 1, y - 1, win);   searchmore(show, mine, x + 1, y, win);   searchmore(show, mine, x + 1, y + 1, win);   searchmore(show, mine, x, y + 1, win);   searchmore(show, mine, x - 1, y + 1, win);   }   else   {   (*win)++;   show[x][y] = get_mine_count(mine, x, y) + '0';   break;   }   }  }

4、判断所选格子周围的雷的个数,采用坐标相加的方法,因为数组mine为字符数组,字符在内存中存储的是它的ascii码值,所以相加后减去8个字符‘0’,即减去字符‘0’的ascii码值,就得到周围雷的个数。代码如下:

  int get_mine_count(char mine[rows][cols], int x, int y)  {   return mine[x - 1][y - 1] + mine[x - 1][y] + mine[x - 1][y + 1] +   mine[x][y - 1] + mine[x][y + 1] +   mine[x + 1][y - 1] + mine[x + 1][y] + mine[x + 1][y + 1] - 8 * '0';  }

5、设置一个win,对非雷格子进行计数,如果win与非雷个数相等,则排雷成功。

5.运行结果

C语言实现简单的扫雷游戏

6.源代码

game.h

  #include<stdio.h>  #include<stdlib.h>  #include<time.h>    #define row 9  #define col 9  #define rows row+2  #define cols col+2  #define easy_count 10 //雷的数量      //初始化棋盘  void initboard(char arr[rows][cols], int rows, int cols,char set);    //打印棋盘  void displayboard(char arr[rows][cols], int row, int col);    //布置雷  void setmine(char mine[rows][cols], int row, int col);    //排查雷  void findmine(char show[rows][cols], char mine[rows][cols], int row, int col);

main.c

  #define _crt_secure_no_warnings 1    #include"game.h"    void menu()  {   printf("********************************************n");   printf("*************  1. play  **************n");   printf("*************  0. exit  **************n");   printf("********************************************n");  }    void game()  {   char mine[rows][cols];//存放雷的信息   char show[rows][cols];//存放排查出雷的信息   initboard(mine, rows, cols,'0');//   initboard(show, rows, cols, '*');   setmine(mine, row, col);//布置雷   displayboard(show, row, col);   findmine(show,mine, row, col);  }    void test()  {   int input = 0;   srand((unsigned)time(null));   do   {   menu();   printf("请选择->");   scanf("%d", &input);   switch (input)   {   case 1:   game();   break;   case 0:   printf("退出游戏n");   break;   default:   printf("选择错误,请重新选择!!!n");   break;   }   } while (input);  }    int main()  {   test();   return 0;  }

game.c

  #define _crt_secure_no_warnings 1    #include"game.h"    void initboard(char arr[rows][cols], int rows, int cols, char set)//set为初始化的字符  {   int i = 0;   int j = 0;   for (i = 0; i < rows; i++)   {   for (j = 0; j < cols; j++)   {   arr[i][j] = set;   }   }  }    void displayboard(char arr[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 ",arr[i][j]);   }   printf("n");   }  }    void setmine(char mine[rows][cols], int row, int col)  {   int count = easy_count;//雷的总数   while (count)   {   int x = rand() % row + 1;//随机产生行号   int y = rand() % col + 1;//随机产生列号   if (mine[x][y] == '0')//没被放置过雷   {   mine[x][y] = '1';   count--;   }   }  }    int get_mine_count(char mine[rows][cols], int x, int y)  {   return mine[x - 1][y - 1] + mine[x - 1][y] + mine[x - 1][y + 1] +   mine[x][y - 1] + mine[x][y + 1] +   mine[x + 1][y - 1] + mine[x + 1][y] + mine[x + 1][y + 1] - 8 * '0';  }    void searchmore(char show[rows][cols], char mine[rows][cols], int x, int y,int * win)//不是雷  {   while (mine[x][y] == '0' && show[x][y] == '*' && x >= 1 && x <= row && y >= 1 && y <= col)   {   if (get_mine_count(mine, x, y) == 0)   {   show[x][y] = ' ';   (*win)++;   searchmore(show, mine, x - 1, y, win);   searchmore(show, mine, x - 1, y - 1, win);   searchmore(show, mine, x, y - 1, win);   searchmore(show, mine, x + 1, y - 1, win);   searchmore(show, mine, x + 1, y, win);   searchmore(show, mine, x + 1, y + 1, win);   searchmore(show, mine, x, y + 1, win);   searchmore(show, mine, x - 1, y + 1, win);   }   else   {   (*win)++;   show[x][y] = get_mine_count(mine, x, y) + '0';   break;   }   }  }    void findmine(char show[rows][cols], char mine[rows][cols], int row, int col)  {   int x = 0;   int y = 0;   int win = 0;   while (win < row * col - easy_count)   {   printf("请输入要排查的坐标->");   scanf("%d %d", &x, &y);   if (x >= 1 && x <= row && y >= 1 && y <= col)   {   if (mine[x][y] == '1')   {   printf("很遗憾,你被炸死了......n");   displayboard(mine, row, col);   break;   }   else    {   searchmore(show, mine, x, y, &win);   displayboard(show,row,col);   }   }   else   {   printf("坐标非法,请重新输入->");   }   }   if (win == row * col - easy_count)   {   printf("恭喜你,排雷成功!!!");   displayboard(mine, row, col);   }  }

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

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

ctvol管理联系方式QQ:251552304

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

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

精彩推荐